C中的链表,无法插入和显示节点

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

我试图实现链表,但无法弄清楚到底出了什么问题,它没有显示预期的结果?我试图通过在可疑的地方随机 printfs 来跟踪程序的控制流...

我试图跟踪控件,发现在插入第一个节点后,更改并没有反映在原始链表中;返回 main() 后,链表再次为空!

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


注意:我没有清除代码中的注释,以便您可以看到我做了什么来检查发生了什么。

这些函数

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

按值传递指向节点的指针。所以函数内指针的任何变化都不会影响原来的指针。

你应该像这样声明函数

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

例如,函数 append 可以按以下方式定义

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

并称赞

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

这些函数

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

也是无效的,因为没有检查指针q是否等于NULL

可以这样定义

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