Question

I am trying to create a linked list in order to enhance my concepts of pointers and address. I have to create linked list in following way:

(1) Read all the nodes together at once at terminal.

(2) Then show the final linked list so formed at last.

How i try to do so ? I am reading first the size of linked list (total number of nodes to be entered). Then i read all the nodes 1 by one in do-while loop. After reading all the nodes i try to create linked list. I differentiate the case when the node is first node to be created by a count variable which will have count=0 when the node is first node after that it will be in another loop.

The output i get is as follows:

 enter the size of node
4
start entering the number of elements until your size
2
3
4
5
Printing linked list
2-> //It don't print the other nodes, Just first one
hp@ubuntu:~/Desktop/pointer$ 

My full code to do so is :

#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;

void main() 
{
    int size, data;
    int count = 0; //this count flag is to check is it's first node or not inside the do-while loop.
    tree = NULL;
    printf("enter the size of node\n");
    scanf("%d", & size);
    printf("start entering the number of elements until your size\n");
    node * temp3 = tree;
    node * prev;
    //Problem creating area is below
    do
    {
        scanf("%d", & data);
        if (count == 0)
        {
            node * temp;
            temp = (node * ) malloc(sizeof(node));
            temp-> freq = data;
            temp-> next = NULL;
            prev = temp;
        } 
        else if (count != 0) 
        {
            node * temp;
            temp = (node * ) malloc(sizeof(node));
            temp-> freq = data;
            temp-> next = NULL;
            prev-> next = temp;
        }
        size--;
        ++count;
    }
    while (size > 0);

    printf("Printing linked list\n");
    node * temp1;
    temp1 = prev;
    //there may be problem here
    while (temp1-> next != NULL) 
    {
        printf("%d-> ", temp1-> freq);
        temp1 = temp1-> next;
    }
    printf("\n");
}

Couldanyone please help me in printing the full linked list by pointing me the error with it's solution ?

Was it helpful?

Solution

Okay there is some unnecessary pointers and a few pointer mistakes being made, for ease of answering I've rewritten your code, I'll try to explain what I did here:

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

struct node
{
    int freq;
    struct node * next;
};
typedef struct node node;
//only need two pointers when building a linked list, one for the top and one for the
//current node
node *tree = NULL, *curr = NULL; //init both pointers to NULL initially

int main()
{
    int size, data; //dont need count, you'll see in a minute why
    printf("enter the size of node\n");
    scanf("%d", & size);
    printf("start entering the number of elements until your size\n");

    //Problem creating area is below
    do
    {
        scanf("%d", &data);
        if (tree == NULL) //just test for top node being NULL instead of using count
        {
            node *temp;
            temp = malloc(sizeof(node));
            temp->freq = data;
            temp->next = NULL;
            //stylistically i like using curr rather than prev, just a style choice
            tree = temp; //set tree to first node
            curr = tree; //make the top node the current node
        }
        else //don't need else if, there are only two conditions
        {
            node *temp = malloc(sizeof(node));
            temp->freq = data;
            temp->next = NULL;
            curr->next = temp; //set the next node in list to the new one
            curr = curr->next; //here's where you had pointer issues, move the current
                               //to the newly created node
        }
        size--;
    }
    while (size > 0);

    printf("Printing linked list\n");
    curr = tree; //reuse curr, no need to make a new pointer

    //test for the current node being NULL, takes care of special case of empty list
    //causing a segfault when you attempt to access a member of an invalid pointer
    while (curr != NULL)
    {
        printf("%d->", curr->freq);
        curr = curr->next; //move to next item in list
    }
    printf("\n");
    return 0;
}

I ran a sample run with size of 3 and inputs of 1, 2, and 3, and I get as output: 1->2->3->

OTHER TIPS

You got two problems.

else if (count != 0) 
    {
        node * temp = prev;
        temp = (node * ) malloc(sizeof(node));
        temp-> freq = data;
        temp-> next = NULL;
        prev-> next = temp;
    }

You aren't changing prev to point to your new node. It still points to '2' in your scenario, and you'll never have more than two nodes in the list.

Try something like

else if (count != 0) 
    {
        /* node * temp = prev; */ //This code is not doing anything useful
        temp = (node * ) malloc(sizeof(node));
        temp-> freq = data;
        temp-> next = NULL;
        prev-> next = temp;
        prev = temp;
    }

Next, your printing loop should probably be

node* temp1 = start; //You need a variable that points to the first node in the list
do 
{
    printf("%d-> ", temp1-> freq);
    temp1 = temp1-> next;
} 
//The last item will always have next == NULL, and must be included
while (temp1-> next != NULL); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top