0

I need to make a program that has (at most) 50 linked lists. Basically, my program generates some messages and based on a indicator that comes in the front of my string, I need to put the message in the right linked list.

I don't know if it is clear enough but I will try to show part of my code (the important part). The function I made to add a new element (on the top) of the linked list is the following:

void InsertLL (News p, char M[]) {
    char * text = malloc(strlen(M)+1);
    strcpy(text, M);
    News s,t;
    t = malloc(sizeof(struct List));
    t-> Text = text;
    s = p;
    p = t;
    p-> next = s;
 }

My struct List (the type of the elements of my lists) contains a char pointer (called text) and a pointer to the next element of the list.

Simulating my program, suppose that I received a message that needs to be put in the linked list where the begin is pointed by the pointer p[0]. So I create a new element (forget the case that the list is empty, I already made this one) and add in the top of my list using the function I've shown. Now, suppose that I received another message that needs to be put in the next pointer p[1]. If I print p[0] -> Text, I get the text of p[1]->Text.

I mean, if I add a new element in the list pointed by p[i], all the previous texts p[i] -> Texts gets the new text of this new element. I have no idea what am I doing wrong.

I don't know if it is enough to help me, if more information is needed, just tell me.

2
  • Actually I didn't understand that is the real problem. Commented Oct 12, 2013 at 20:31
  • Is your intent a "stack-like" list (where new elements always appear at the front) or a "queue-like" list (where new elements are always appended to the end, and the front is only set on first-insertion)? It makes a big difference in the algorithm. What you have here at least appears to be stack-like, but I don't think that is what you want. Commented Oct 12, 2013 at 20:50

1 Answer 1

1

Problem with your code is you are not maintaining the list of nodes. you are overwriting the same node again and again.

Algo:

  1. If no node then create one.

  2. If nodes are present then navigate to end node.(You can use tail pointer for quicker access).

  3. Append the node at the end.

Creating Node:

  • Declare a node type pointer.

  • Allocate memory to it.

  • Update the content of the node.

  • Set the next pointer of the node to null.

  • Add this node to end of the list.

ex. inserting node 3 in the list of node 1 and node 2.

Link list demo

This is the general approach that you can use

typedef struct node{
    int val;   //you can use your text here
    struct node* next;
    }NODE;

struct node* head=0;

int addNode(int v){
    if(head==0){                              //checking for empty node.
        struct node* n=malloc(sizeof(NODE));
        n->val=v;
        head=n;
    }
    else{
        struct node* temp=head;
        while(temp->next != 0)    //Navigating till end
        {
            temp=temp->next;
        }
        struct node* n=malloc(sizeof(NODE)); //allocating memory
        n->val=v;            //you need to use strcpy for string here.                         
        temp->next=n;       //adjusting pointers
        n->next=0;
    }
}

You can check this demo created by me for Double linked List http://ideone.com/s6TtUX

Sign up to request clarification or add additional context in comments.

9 Comments

@user2768645 Show me your struct.
struct List { char * Text; News next; } ; News is struct List *)
why if I defined News as struct List *? I made typedef struct List * News
Hmm. Then you need to maintain the list of nodes only. Check my prog. it's easy.
By the way, the problem persists even using News * next
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.