单链表的创建,查找,插入,删除操作

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node{
    int num;
    struct node *next;
};
struct node *Create()
{
    struct node *head,*p,*q;
    int x,i=2;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    q=head;
    printf("请输入第1个学生的成绩:"); 
    scanf("%d",&x);
    while(x!=-1)
    {
        p=(struct node *)malloc(sizeof(struct node));
        p->num=x;
        q->next=p;
        q=p;
        printf("请输入第%d个学生的成绩:",i);
        scanf("%d",&x);
        i++; 
    }
    q->next=NULL;
    return head;
}
struct node *Getk(struct node *head,int k)
{
    struct node *p=head->next;
    int s=1,q;
    while(p&&s<k)
{
        p=p->next; s++;
}
    if(s==k) return p;
    else return NULL;
}
    
struct node *Insert(struct node *head,int Index,int i)
{
    struct node *p,*s;
    p=Getk(head,Index-1);
    s=(struct node *)malloc(sizeof(struct node));
    s->num=i;
    s->next=p->next;
    p->next=s;
    return head;
}
struct node *Delete(struct node *head,int Loc)
{
    struct node *w,*c;
    w=Getk(head,Loc-1);
    c=w->next;
    w->next=c->next;
    free(c);
    return head;
     
}
void Print(struct node *head)
{
    struct node *L;
    L=head->next;
    int Index=1;
    while(L!=NULL)
    {
        printf("第%d个学生的成绩是%d\n",Index,L->num);
        L=L->next;
        Index++;
    }
}
void main()
{
    struct node *phead,*x1;
    int l,s;
    phead=Create();
    Print(phead);
    printf("想要查找哪个结点:");
    scanf("%d",&l); 
    x1=Getk(phead,l);
    printf("第%d个同学的成绩是%d\n",l,x1->num);
    int y,loc;
    printf("请输入要增加的结点位置:");
    scanf("%d",&y);
    printf("请输入成绩大小:");
    scanf("%d",&loc);
    Insert(phead,y,loc);
    Print(phead);
    int o;
    printf("请输入要删除的结点位置:");
    scanf("%d",&o);
    Delete(phead,o);
    Print(phead);
    
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值