简体   繁体   English

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

[英]Program inserting an element in single linked list

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

} }

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. 在gdb中键入'run'命令,如果失败,则可以使用'backtrace'来获取大量信息。 Get comfortable with gdb tutorials as the manual might scare you off at first. 熟悉gdb教程,因为手册可能一开始会让您感到恐惧。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM