C语言单链表的修改、插入(前中后三个位置)、删除(删单个元素与整条链表删除)

还不太了解单链表如何创建的同学请点击链接
https://blog.csdn.net/tongjingqi_/article/details/105831323
想了解单链表的排序和查找的同学请点击
https://blog.csdn.net/tongjingqi_/article/details/105927156
本篇实现单链表的插入删除修改

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node* add_head(struct node* head,int x)//实现在开始结点前增加一个新结点
{
   struct node *p=(struct node *)malloc(sizeof(struct node));
   p->next=head;
   p->data=x;
   head=p;
   return head;
}
void add_tail(struct node* head,int x)//实现在最后一个结点增加新结点
{
    struct node *p=(struct node *)malloc(sizeof(struct node));
    struct node *q=(struct node *)malloc(sizeof(struct node));
    q->next=NULL;
    q->data=x;
    for(p=head;p->next!=NULL;p=p->next);//找到最后一个结点
    p->next=q;//把最后一个结点指向新增的结点
};
struct node* insert(struct node* head,int x)//把x插在第一个大于x的数之前
{
    struct node *p=(struct node *)malloc(sizeof(struct node));
    struct node *q=(struct node *)malloc(sizeof(struct node));
    struct node *r=(struct node *)malloc(sizeof(struct node));
    for(p=head,q=head;p->data<x&&p->next!=NULL;p=p->next)q=p;//找到插入点的前后两个指针q和p,注意赋值顺序
    if(p==head)head=add_head(head,x);//头部插
    else if(p->next==NULL)add_tail(head,x);//尾部插
    else{中间插
     r->data=x;
     r->next=p;
     q->next=r;
    }
    return head;
}
struct node* Delete(struct node* head,int x)//删除值为x的结点
{
    struct node *p=(struct node *)malloc(sizeof(struct node));
    struct node *q=(struct node *)malloc(sizeof(struct node));
     for(p=head,q=head;p->data!=x&&p->next!=NULL;q=p,p=p->next);//先找到x的位置
     if(p==head)
        {
            head=p->next;//x是第一个数据的情况
            free(p);
        }
     else {
            q->next=p->next;//直接把被删结点的前一个结点的next指向被删结点后一个结点
            free(p);
     }
     return head;
};
void Delete_alvl(struct node* head)//整条链表的删除
{
    struct node *p=(struct node *)malloc(sizeof(struct node));
    struct node *q=(struct node *)malloc(sizeof(struct node));
    for(p=head,q=head;q!=NULL;q=q->next,free(p),p=q);
}
struct node* change(struct node* head,int x,int y)
{
    struct node *p=(struct node *)malloc(sizeof(struct node));
    for(p=head;p!=NULL;p=p->next)
    {
        if(p->data==x){
            p->data=y;
            return p;
        }
    }
}
int main()
{
    int i,n;
    scanf("%d",&n);
    struct node *p,*q,*head=NULL;
    for(i=0;i<n;i++)//创建链表
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&(p->data));
        if(head==NULL)head=p;
        else q->next=p;
        q=p;
    }
        p->next=NULL;
    for(p=head;p!=NULL;p=p->next)//遍历链表并输出
    {
        printf("%d ",p->data);
    }
    printf("\n");
    head=add_head(head,100);//
    add_tail(head,77);
    head=insert(head,-1);
    head=insert(head,101);
    head=insert(head,4);
     for(p=head;p!=NULL;p=p->next)
    {
        printf("%d ",p->data);
    }
    printf("\n");
    head=Delete(head,4);
    for(p=head;p!=NULL;p=p->next)
    {
        printf("%d ",p->data);
    }
    printf("#");
    p=change(head,1,111111);
    if(p!=NULL)printf("%d",*p);
    Delete_all(head);

    return 0;
}

在这里插入图片描述

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值