链表的基本操作(建立,删除,插入)

插入数据:判断头节点的值是否大于和当下一个节点为空或者是下一个节点的值大于带插入的值(当然啦,这里是有顺序的插入啦)。

删除数据:头节点删除,中间删除,尾部删除。需要有两个指针,一个在前,一个在后。

具体解释代码上应有emmm。

代码如下:

/*链表的建立*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;//存储数据 
    struct node *next;//指向下一个节点 
};
int main()
{
    struct node *head,*p,*q,*t;
    int i,n,a;
    scanf("%d",&n);
    head = NULL;
    for(i = 1;i <= n;i++)
    {
        scanf("%d",&a);
        p = (struct node*)malloc(sizeof(struct node));//申请空间
        p -> data = a;
        p -> next = NULL;
        if(head==NULL)
        {
            head = p;
         } 
         else
         {
             q -> next = p;
         }
         q = p;
     } 
     
     t = head;//得到头节点
     while(t!=NULL) 
     {
         printf("%d ",t->data);
         t = t->next;
     }
     free(head);
    return 0;
 } 

/*链表的插入*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    struct node *head,*p,*q,*t;
    int i,n,a;
    scanf("%d",&n);
    head = NULL;
    for(i = 1;i <= n;i++)
    {
        scanf("%d",&a);
        p = (struct node*)malloc(sizeof(struct node));//申请空间
        p -> data = a;
        p -> next = NULL;
        if(head==NULL)
        {
            head = p;
         } 
         else
         {
             q -> next = p;
         }
         q = p;
     } 
     printf("请输入要插入的数:\n");
     int b;
     scanf("%d",&b);
     t = head;//得到头节点
     p = (struct node*)malloc(sizeof(struct node));
    p->data = b;        
     while(t!=NULL)//当前节点是最后一个节点或者下一个节点的值大于带插入的值时插入数据 
     {
         if(head->data>b)//头节点 
         {
             p->next = head;
            head = p; 
            break;
        }
         if(t->next==NULL||t->next->data>b)
         {
             
            p->next = t->next;//连接关键 为了不让数据丢失,应该将后面的数据先连上 
            t->next = p; 
             
             break;
         }
         t = t->next;       
     }
     
     t = head;
     while(t!=NULL)
     {
         printf("%d ",t->data);
         t = t->next;
     }
     
    return 0;
}

/*链表的删除*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;//存储数据 
    struct node *next;//指向下一个节点 
};
int main()
{
    struct node *head,*p,*q,*t;
    int i,n,a;
    scanf("%d",&n);
    head = NULL;
    for(i = 1;i <= n;i++)
    {
        scanf("%d",&a);
        p = (struct node*)malloc(sizeof(struct node));//申请空间
        p -> data = a;
        p -> next = NULL;
        if(head==NULL)
        {
            head = p;
         } 
         else
         {
             q -> next = p;
         }
         q = p;
     } 
     printf("请输入要删除的数:\n");//在头部,在中间,在尾部 
     int b;
     scanf("%d",&b); 
     while(1)
     {
        t = head;//得到头节点
         while(t->next!=NULL&&t->data!=b) 
         {
             p = t;
             t = t->next;
         }
         if(t->data==b)
         {
             if(t==head)//删除头节点 
             {
                 head = head ->next; 
                 free(t);//释放被删节点
                 break; 
            }
            else//删除中间元素 
            {
                p -> next = t -> next;
                free(t);
                break;
            }
         }
         else
         {
             printf("没有找到该元素。\n");
             break;
         }
    }
     t = head;
     while(t!=NULL) 
     {
         printf("%d ",t->data);
         t = t->next;
     }
     return 0;
}

发布了45 篇原创文章 · 获赞 6 · 访问量 4962

猜你喜欢

转载自blog.csdn.net/qq_40761693/article/details/86709736
今日推荐