c - c中插入链表

标签 c linked-list

我想遍历链表并打印链表的所有元素直到最后。 我执行以下操作

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int main()
{
    struct linkedList   //making user defined linked list
    {
        int num;
        struct linkedList *ptr;
    };
    int choice=1,last=0;
    typedef struct linkedList node;   
    node *temp,*lasst,*head;      //initialization of pointers.
    while(choice==1)
    {
        temp=(node *)malloc(sizeof(node)); //allocation of memory to temp
        printf("enter num");
        scanf("%d",&temp->num);
        if(last==0)
        {
            lasst=head=temp;
        }
        else
        {
            lasst->ptr=temp;
            lasst=temp;
        }
        printf("do u want to enter more data? type 1");
        scanf("%d",&choice);
    }
    lasst->ptr=0;
    temp=head;
    while(temp!=0)
    {
        printf("%d =>",temp->num);
        temp=temp->ptr;
    }

}

我想打印链表中存在的所有元素,但我的代码只打印链表的最后一个元素,我该怎么办?

最佳答案

这是在链表中插入节点的完整代码。可以在节点的begin、end、after、before插入节点。

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

    struct node
    {
        int info;
        struct node *link;
    }*new1,*first=NULL,*save,*pred;

    struct node *insert_begin(int x,struct node *first);
    struct node *insert_end(int x,struct node *first);
    struct node *insert_after(int x,struct node *first);
    struct node *insert_before(int x,struct node *first);
    void display(struct node *first);

    void main()
    {
        int x,ch,c=0;
        //clrscr();
        do
        {
        printf("\n_____________________________________________\n");
        printf("\n1 - Insert Node At the Begining of the Node");
        printf("\n2 - Insert Node At the Ending of the Node");
        printf("\n3 - Insert Node At the After Particular Node");
        printf("\n4 - Insert Node At the Before Particular Node");
        printf("\n_____________________________________________\n");

        printf("\n\tEnter Your Choice Here -->");
        scanf("%d",&ch);

        printf("\nEnter Value To Insert In The Node -->");
        scanf("%d",&x);

        switch(ch)
        {
            case 1:
                first=insert_begin(x,first);
                break;
            case 2:
                first=insert_end(x,first);
                break;
            case 3:
                first=insert_after(x,first);
                break;
            case 4:
                first=insert_before(x,first);
                break;
            default:
                printf("\nPlease Reenter Choice....");
                break;
        }

        display(first);

        printf("\nDo You Want To Continue.. Press 1 For Continue -->");
        scanf("%d",&c);
        }while(c==1);
        //getch();
    }

    void display(struct node *first)
    {
        printf("\n**********************************************\n");
        printf("\n-----NODES ARE BELOW-----\n");

        while(first!=NULL)
        {
            printf(" [ %d ]  ",first->info);
            first=first->link;
        }
        printf("\n**********************************************\n");
    }

    struct node *insert_begin(int x,struct node *first)
    {
        new1=(struct node*)malloc(sizeof(struct node));
        new1->info=x;
        new1->link=NULL;

        if(first==NULL)
        {
            first=new1;
        }
        else
        {
            new1->link=first;
            first=new1;
        }
        return first;
    }

    struct node *insert_end(int x,struct node *first)
    {
        new1=(struct node*)malloc(sizeof(struct node));
        new1->info=x;
        new1->link=NULL;
        save=first;

        if(first==NULL)
        {
            first=new1;
              //    save=first;
        }
        else
        {
            while(save->link!=NULL)
            {
                save=save->link;
            }
            save->link=new1;
        }
       //   save=first;
        return first;
    }
    struct node *insert_after(int x,struct node *first)
    {
        int v;
        new1=(struct node*)malloc(sizeof(struct node));
        new1->info=x;
        new1->link=NULL;
        save=first;

        printf("\nEnter Node Value Which Insert After Above Node =");
        scanf("%d",&v);

        if(first==NULL)
        {
            first=new1;
            save=first;
        }
        else
        {
            if(first->link==NULL)
            {
                new1->link=first;
                first=new1;
            }
            else
            {
                while(save->link!=NULL && save->info!=v)
                {
                    save=save->link;
                }
                new1->link=save->link;
                save->link=new1;
             }
        }
        save=first;
        return first;
    }
    struct node *insert_before(int x,struct node *first)
    {
        int v;
        new1=(struct node*)malloc(sizeof(struct node));
        new1->info=x;
        new1->link=NULL;
        save=first;

        printf("\nEnter Value of node Which Insert Before Above Node =");
        scanf("%d",&v);

        if(first==NULL)
        {
            first=new1;
        }
        else
        {
            if(first->link==NULL)
            {
                new1->link=first;
                first=new1;
            }
            else
            {
                while(save->link!=NULL && save->info!=v)
                {
                    pred=save;
                    save=save->link;
                }
                pred->link=new1;
                new1->link=save;
            }
        }
        return first;
    }

关于c - c中插入链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55439049/

相关文章:

无法让简单的 GTK3 应用程序运行

iphone - 如何在 iPhone 上使用 fopen()?

java - 给定链表的成对交换元素(Java解决方案)

c - C 中的简单链表实现

使用链表创建多项式并将它们相加

java - 不兼容的类型 : required int found object error in generic linkedlist

c - 如何在C中通过udp套接字发送整数数组?

c - epoll_wait 接收未知事件

php - 将 C 转换为 PHP

c - 输入空格