插入到c中的链表中

时间:2019-03-31 08:23:44

标签: 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;
    }

}

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

1 个答案:

答案 0 :(得分:0)

这是链接列表中插入节点的完整代码。它可以在节点的开始,结束,之后和之前插入节点。

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