C语言实现链表插入,删除相关操作

#include <stdio.h>
#include<malloc.h>
#define SIZE 15
#define LEN sizeof(struct Node)

struct Node
{
    int data;
    struct Node *next;
};

int n;
struct Node * Creat_LinkList(void)
{
    n=0;
    struct Node *head;
    struct Node *cur,*pre;
    cur=pre=(struct Node *)malloc(LEN);//开辟一个新的结构体单元,cur、pre指向同一个地址
    printf("please input any num,end with '0' : ");
    scanf("%d",&cur->data);//输入第一个数
    head=NULL;
    while (cur->data!=0)//当输入0的时候结束链表的创建
    {
        n+=1;
        if(n==1) head=cur;//如果是第一个节点,把cur赋给head和pre
        else pre->next=cur;//如果不是第一个节点,把cur是pre指向的下一个节点
        pre=cur;
        cur=(struct Node *)malloc(LEN);//cur是新开辟的节点
        scanf("%d",&cur->data);
    }
    pre->next=NULL;
    return head;
}

/*插入节点*/
struct Node *Insert(struct Node *head,int pos,int num)
{
    struct Node *cur,*pre,*nex;
    cur=head;

    if(pos<1 && pos>n) return head;//插入位置不在范围内

    else if(pos==1)//插入位置在第一个
    {
        pre=(struct Node *)malloc(LEN);
        pre->data=num;
        pre->next=cur;
        head=pre;
    }
    else if(pos==n)//插入的位置在最后一个
    {
        while(cur->next!=NULL) cur=cur->next;
        nex=(struct Node *)malloc(LEN);
        cur->next=nex;
        nex->data=num;
        nex->next=NULL;
    }
    else//在中间任意位置
    {
        int t=0;
        while(t==pos)//此时cur指向目标
        {
            cur=cur->next;
            t++;
        }
        nex=(struct Node *)malloc(LEN);
        nex->next=cur->next;
        nex->data=num;
        cur->next=nex;
    }
    n+=1;
    return head;
}


/*删除比num大的数*/
struct Node * Del_Biggers(struct Node *head,int num)
{
    struct Node *cur,*pre;
    if(head==NULL)//空表
    {
        return head;
    }
    cur=head;
    while(cur)//当cur不为NULL时
    {
        if(cur->data>num)
        {
            if(cur==head) head=cur->next;//当第一个节点满足要求时,head指向下一个节点
            else pre->next=cur->next;//不是第一个节点,把前一个节点的next指针指向下一个节点
            n-=1;
            free(cur);//释放当前节点
            cur=head;//此时应把head再赋给cur
        }
        else
        {
            pre=cur;
            cur=cur->next;
        }
    }
    return head;
}

//打印链表
void Print_List(struct Node *head)
{
    printf("there are %d num in the linkedlist : ",n);
    struct Node *cur;
    cur=head;
    while(cur!=NULL)
    {
        printf("%d ",cur->data);
        cur=cur->next;
    }
    printf("\n");
}

int main(void)
{
    int d=0;
    struct Node *head;
    head=Creat_LinkList();
    int m=n;
    Print_List(head);
    printf("please input the num to compare with : ");
    scanf("%d",&d);
    head=Del_Biggers(head,d);
    m-=n;
    printf("there are %d num be deleted.\n",m);
    Print_List(head);

    int pos,num;
    printf("please input the position (not more than %d ) you want to insert: ",n);
    scanf("%d",&pos);
    printf("please input any number you want to insert: ");
    scanf("%d",&num);
    Insert(head,pos,num);
    Print_List(head);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值