C中的链接列表,无法插入和显示节点 [英] Linked list in C , Can't insert and display node

查看:105
本文介绍了C中的链接列表,无法插入和显示节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现链接列表,但无法找出未显示预期结果的实际错误所在?我试图通过在可疑的地方随机放置printfs来跟踪程序的控制流...

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...

我试图跟踪控件,并意识到在插入第一个节点之后,所做的更改不会反映在原始链表中;回到main()后,链接列表再次为空!

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.

推荐答案

这些功能

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

按值将指针传递到节点.因此,函数中指针的任何更改都不会影响原始指针.

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

您应该声明类似的功能

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

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

并称呼为

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.

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

这篇关于C中的链接列表,无法插入和显示节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆