链表的创建、插入、删除、输出

#include <bits/stdc++.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct list1
{
    int num;
    struct list1 *next;
};

struct list1 *creat()
{
    struct list1 *head,*p,*q;
    head = (struct list1 *)malloc(sizeof(struct list1));
    p = head;
    while(1)
    {
        q = (struct list1 *)malloc(sizeof(struct list1));
        scanf("%d",&q -> num);
        if(q->num == 0)
            break;
        else
        {
            p -> next = q;
            p = q;
        }
    }
    p -> next = NULL;
    return head;
}

void insert1(struct list1 *head, int m)
{
    struct list1 *p, *q, *r;
    p = head;
    q = p -> next;
    r = (struct list1 *)malloc(sizeof(struct list1));
    r -> num = m;
    while(q != NULL)
    {
        if(m > q -> num)
        {
            p = q;
            q = q ->next;
        }
        else
            break;
    }
    p -> next = r;
    r -> next = q;
}

void delete1(struct list1 *head, int n)
{
    struct list1 *p, *q;
    p = head;
    q = p ->next;
    while(q != NULL)
    {
        if(q -> num == n)
        {
            p -> next = q -> next;
            free(q);
            break;
        }
        else
        {
            p = q;
            q = q -> next ;
        }
        if(q == NULL)
            printf("未找到元素");
    }
}


void print(struct list1 *head)
{
    struct list1 *p;
    p = head -> next;
    while(p != NULL)
    {
        printf("%d",p -> num);
        p = p ->next;
    }
}

int main()
{
    int m, n;
    struct list1 *a, *b;
    a = creat();
    print(a);
    scanf("%d",&m);
    insert1(a, m);
    print(a);
    scanf("%d",&n);
    delete1(a, n);
    print(a);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值