Linked list in C , Can't insert and display node

倖福魔咒の 提交于 2020-01-25 04:30:29

问题


I tried to implement the linked list but couldn't make out what is actually going wrong that it isn't showing the expected result? I tried to trace the control flow of the program by putting in random printfs at suspicious places...

I tried to trace the control and realized that after inserting the first node the changes are not getting reflected in the original linked list; after getting back to main() the linked list is empty again!

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

struct node
{
    int data;
    struct node *link;
};

int count(struct node *q);
void append(struct node *q, int item);
void display(struct node *q);
void add_after(struct node *q, int item, int pos);
void add_beg(struct node *q, int item);
int delete(struct node *q);

int main()
{
    struct node *p = NULL;
    int item,count,i;

    printf("Enter the element to insert\n");
    scanf("%d",&item);

    append(p,item);

    printf("Control\n");
    printf("%d",p);

    display(p);


    //printf("No. of elements in Linked list = %d",count(p));

    // printf("Enter number of elements: ");
    // scanf("%d", &count);

    // for (i = 0; i < count; i++)
    // {
    //     printf("Enter %dth element: ", i);
    //     scanf("%d", &item);
    //     append(p,item);
    // }

    //printf("No. of elements in Linked List are : %d",count(p));

    return 0;
}

void append(struct node *q, int item)
{
    struct node *temp=NULL  , *new=NULL;

    temp = (struct node *)malloc(sizeof(struct node));

    if(q==NULL)  //Condition for empty linked list
    {
       // procedure to insert first node
        temp->data = item;
        temp->link = NULL;
        q = temp;
        //printf("iz here");
    }
    else 
    {
        //printf("ABCD\n");
        temp = q;
        while(temp->link!=NULL)
        temp = temp->link;

        new = (struct node *)malloc(sizeof(struct node));
        new->data = item;
        new->link = NULL;
        temp->link = new;
    }
}

void display(struct node *q)
{
    // printf("Hitesh");
    //printf("%d",q);

    while(q->link!=NULL)
    {
        printf("%d->",q->data);
        q = q->link;
    }
}

int count(struct node *q)
{
    int c=0;
    while(q->link!=NULL)
    {
        q=q->link;
        c++;
    }
    return c;   
}

void add_after(struct node *q, int item, int pos)
{
    int i;
    struct node *temp , *new ;
    temp=q;

    for(i=0;i<pos;i++)
    temp = temp->link;

    new=(struct node*)malloc(sizeof(struct node));
    new->data = item;
    new->link = temp;
    temp = new;
    q = temp;
}

void add_beg(struct node *q, int item)
{
    struct node *temp;
    temp=q;
    temp=(struct node*)malloc(sizeof(struct node));
    temp->data = item;
    temp->link = q;
    q=temp;
}


Note: I have not cleaned comments from the code in order that you can see what things I have done to check what's happening.


回答1:


These functions

void append(struct node *q, int item);
void add_after(struct node *q, int item, int pos);
void add_beg(struct node *q, int item);

passes the pointer to a node by value. So any changes of the pointer within the functions do not influence on the original pointer.

You should declare the functions like

void append(struct node **q, int item);
void add_after(struct node **q, int item, int pos);
void add_beg(struct node **q, int item);

For example the function append could be defined the following way

int append( struct node **head, int item )
{
    struct node *new_node = malloc( sizeof( struct node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = item;
        new_node->link = NULL;

        while( *head != NULL ) head = &( *head )->link;

        *head = new_node'
    }

    return success;
}

and called like

struct node *head = NULL;
//...
append( &head,item );

These functions

void display(struct node *q)
{
    // printf("Hitesh");
    //printf("%d",q);

    while(q->link!=NULL)
    {
        printf("%d->",q->data);
        q = q->link;
    }
}

int count(struct node *q)
{
    int c=0;
    while(q->link!=NULL)
    {
        q=q->link;
        c++;
    }
    return c;   
}

are also invalid because there is no check whether the pointer q is equal to NULL.

They can be defined the following way

void display( struct node *head )
{
    for ( ; head != NULL; head = head->link )
    {
        printf( "%d->", head->data );
    }
}

size_t count( struct node *head )
{
    size_t n = 0;

    for ( ; head != NULL; head = head->link )
    {
        ++n;
    }

    return n;   
}


来源:https://stackoverflow.com/questions/58117464/linked-list-in-c-cant-insert-and-display-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!