程序在单个链表中插入元素

时间:2022-01-28 07:16:47
#include<stdio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *next;
};
struct node* insert_beg(struct node *h,int x)
{
    struct node *t;
    printf("\n address = %u --- ",*h);
    t=(struct node *)malloc(sizeof(struct node));
    if(h==NULL)
    {
        t->data=x;
        t->next=NULL;
        h=t;
    }
    else
    {


        t->data=x;
        t->next=h;
        h=t;
    }
    return h;

}
void display(struct node *h1)
{
    struct node *t=h1;
    while(t->next!=NULL)
    {
        printf("%d->",t->data);
        t=t->next;
    }
}
int main()
{
    struct node *p=NULL;
    int a,ch=5;
    while(ch--)
    {

    printf("\n Enter data");
    scanf("%d",&a);
    p=insert_beg(p,a);
    display(p);
    }display(p);

}

The above is a code for inserting element in the begining of the single linked link list in c.

以上是用于在c中的单个链接链接列表的开头插入元素的代码。

The code compiles successfuly but when i am trying to insert an element the system hangs up... Not to locate the error. Can anyone suggest the correction i need to done.

代码编译成功,但当我试图插入一个元素时系统挂起...不找到错误。任何人都可以建议我需要做的修正。

Is there any error in the expression mentioned below... Need help.

下面提到的表达式中是否有任何错误...需要帮助。

p=insert_beg(p,a);

3 个解决方案

#1


0  

    while(t->next!=NULL)

should be:

应该:

    while(t!=NULL)

also, your insert function can be simplified to:

此外,您的插入功能可以简化为:

struct node* insert_beg(struct node *h,int x)
{
    struct node *t;
    t=(struct node *)malloc(sizeof(struct node));
    t->data=x;
    t->next=h;
    return t;
}

#2


1  

#include<stdio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *next;
};
struct node* insert_beg(struct node *h,int x)
{
    struct node *t;
    t=(struct node *)malloc(sizeof(struct node));
    if(h==NULL)
    {
        t->data=x;
        t->next=NULL;
        h=t;
    }
    else
    {
        t->data=x;
        t->next=h;
        h=t;
    }
    return h;
}
void display(struct node *h1)
{
     struct node *t=h1;
     while(t->next!=NULL)
     {
         printf("%d->",t->data);
         t=t->next;
     }
    printf("%d",t->data);
}
int main()
{
    struct node *p=NULL;
    int a,ch=5;
    while(ch>=0)
    {
        printf("\n Enter data:-");
        scanf("%d",&a);
        p=insert_beg(p,a);
        display(p);
        ch--;
    }
    display(p);

}

}

#3


0  

You have an error in:

你有一个错误:

printf("\n address = %u --- ",*h);

Trying to print an entire structure.

试图打印整个结构。

In general use a debbuger for this as explained here

一般情况下,如此处所述使用debbuger

You can compile your program with debug information as follows:

您可以使用以下调试信息编译程序:

gcc -o main -g main.c

And run it in gdb:

并在gdb中运行它:

gdb main

Type 'run' command inside gdb, if it fails you can use a 'backtrace' to get loads of information why. Get comfortable with gdb tutorials as the manual might scare you off at first.

在gdb中输入'run'命令,如果失败,你可以使用'backtrace'来获取大量信息。熟悉gdb教程,因为手册最初可能会吓到你。

#1


0  

    while(t->next!=NULL)

should be:

应该:

    while(t!=NULL)

also, your insert function can be simplified to:

此外,您的插入功能可以简化为:

struct node* insert_beg(struct node *h,int x)
{
    struct node *t;
    t=(struct node *)malloc(sizeof(struct node));
    t->data=x;
    t->next=h;
    return t;
}

#2


1  

#include<stdio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *next;
};
struct node* insert_beg(struct node *h,int x)
{
    struct node *t;
    t=(struct node *)malloc(sizeof(struct node));
    if(h==NULL)
    {
        t->data=x;
        t->next=NULL;
        h=t;
    }
    else
    {
        t->data=x;
        t->next=h;
        h=t;
    }
    return h;
}
void display(struct node *h1)
{
     struct node *t=h1;
     while(t->next!=NULL)
     {
         printf("%d->",t->data);
         t=t->next;
     }
    printf("%d",t->data);
}
int main()
{
    struct node *p=NULL;
    int a,ch=5;
    while(ch>=0)
    {
        printf("\n Enter data:-");
        scanf("%d",&a);
        p=insert_beg(p,a);
        display(p);
        ch--;
    }
    display(p);

}

}

#3


0  

You have an error in:

你有一个错误:

printf("\n address = %u --- ",*h);

Trying to print an entire structure.

试图打印整个结构。

In general use a debbuger for this as explained here

一般情况下,如此处所述使用debbuger

You can compile your program with debug information as follows:

您可以使用以下调试信息编译程序:

gcc -o main -g main.c

And run it in gdb:

并在gdb中运行它:

gdb main

Type 'run' command inside gdb, if it fails you can use a 'backtrace' to get loads of information why. Get comfortable with gdb tutorials as the manual might scare you off at first.

在gdb中输入'run'命令,如果失败,你可以使用'backtrace'来获取大量信息。熟悉gdb教程,因为手册最初可能会吓到你。