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$