0

I am beginner to linked list. I have a situation that to take the size of link is taken at terminal and then read all the data to be kept in freq (In my code it is "freq" but generally called data/info), and create a linked list using them.

What I have done so far is shown below in a code, which just read the size of LL to be created and creates the node for each data inputed. Now how I have to link those nodes such that the element at first will point to other where last will have NULL. Right now i have NULL in the next of each created node.

Here is my code :

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include <string.h>

struct node
{
 int freq;
 struct node *next;
};
typedef struct node node;
node *tree=NULL;

main()
{
int size,data;

printf("enter the size of node\n");
scanf("%d", &size);
printf("start entering the number of elements until your size\n");
node *prev;
 node *temp;
prev = NULL;
do
{
 scanf("%d\n", &data);

 temp = (node*)malloc(sizeof(node));
 temp->freq=data;
 temp->next=NULL;
 if (prev)
    prev->next = temp;
 else
    tree = temp;
 prev = temp;
 size--;
}
while(size>0);

node *temp1;
temp1=temp;
while(temp1->next!=NULL)
{
  printf("%d-> ",temp->freq);
  temp1=temp1->next;
}
}

Que(1): I have tried to link these nodes taken at terminal but it still don't print the traversed the linked list.where is the problem?

The output is:
hp@ubuntu:~/Desktop/Internship_Xav/Huf_pointer$ ./ll 
enter the size of node
4
start entering the number of elements until your size
22
11
4
5
6//It don't print the linked list here
hp@ubuntu:~/Desktop/Internship_Xav/Huf_pointer$ 
Sss
  • 1,519
  • 8
  • 37
  • 67

2 Answers2

1

You will have to keep track of the node added in the previous iteration, so that you can make the previous node next field point to the new node. Something like this:

printf("start entering the number of elements until your size\n");
node *prev;
prev = NULL;
do
{
 scanf("%d\n", &data);
 node *temp;
 temp = (node*)malloc(sizeof(node));
 temp->freq=data;
 temp->next=NULL;
 if (prev)
    prev->next = temp;
 else
    tree = temp;
 prev = temp;
 size--;
}
while(size>0);

Note that in the first iteration, this will set tree to the newly allocated node. This is necessary if you want to traverse the list after you create it. By the end of the loop, head points to the first element, and the last element's next points to NULL.

And yes, your approach to traverse the list is correct.

UPDATE

The approach you described to traverse the list is correct, but you didn't implement it correctly.

You want to start from the head of the list, not from temp, because temp is the last node you allocated. And the condition is not while (temp1->next != NULL), the loop will never execute because temp1 is the last node, and the last node's next field always points to NULL.

Instead, this is what you want:

node *temp1;
temp1 = tree;
while(temp1 != NULL)
{
  printf("%d-> ", temp1->freq);
  temp1 = temp1->next;
}

Note that the argument to printf() also changed, you were passing temp->freq, the correct variable would be temp1->freq.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • Thanks,Let me apply it. Then i will come back to you. :) – Sss Feb 28 '14 at 15:43
  • i couldn't understand these lines could you explain these lines again "if (prev) prev->next = temp; else tree = temp; prev = temp;" and also why you needed to use "prev" node ? – Sss Feb 28 '14 at 15:47
  • @user234839 It provides initialization for `tree`. `if (prev)` means "if this is not the first time we're here" - in that case, we update the previous node `next` (that is, `prev->next`) to the new node (which is `temp`). Otherwise (i.e., `prev` is NULL, first time through), we initialize `tree` to `temp` because this will be the head of the list. And then, after all that, in either case, we always execute `prev = temp;` because `temp` is now the previous node for the next iteration. `prev = temp;` prepares the code for the next iteration. – Filipe Gonçalves Feb 28 '14 at 15:51
  • I made the changes you asked. (please see the edited code) but still it don't print the linked list.Any help please ? It just don't execute the while loop to print the the tree(and to traverse)even once. – Sss Feb 28 '14 at 16:17
0

Just keep track of the previous node and link it to the next.

node *temp,*temp2=NULL,*head;
do
{
 scanf("%d", &data);

 temp = (node*)malloc(sizeof(node));
 if (temp2==NULL)
   head=temp;
 else
   temp2->next=temp;

 temp->freq=data;
 temp->next=NULL;
 temp2=temp;

 size--;
}while(size>0);

head will give the starting node of the linked list.

Also, you probably wanted scanf("%d", &data); instead of scanf("%d\n", &data);. See this answer.

Community
  • 1
  • 1
HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
  • Not me, but maybe because your first answer didn't work. And you're missing the `while (size > 0);` – Filipe Gonçalves Feb 28 '14 at 15:43
  • @RikayanBandyopadhyay , I have edited the code. It still don't traverse the linked list. If you see the output then it just read the data and breaks out of the code. Any help please ? – Sss Feb 28 '14 at 16:27