数据结构3:单链表学生管理系统

#include<myhead.h>

typedef struct Data
{
    int age;
    int score;
    char name[20];
}asd;

typedef struct stu
{
    union
    {
        int len;
        struct Data data;
    };
    struct stu *next;
}link1,*plink1;

plink1 get_head()
{
    plink1 p=malloc(sizeof(link1));
    if (NULL==p)
    {
      printf("头结点创建失败");
      return NULL;
    }
    
    p->len=0;
    p->next=NULL;
    return p;
}
 
int taile_insert(plink1 L,asd e)
{
    if (L==NULL)
    {
        printf("单链表为空\n");
        return -1;
    }
  
    plink1 t=L;
    for (int i = 0; i < L->len; i++)
    {
        t=t->next;
    }
    plink1 p=malloc(sizeof(link1));
    p->data=e;
    t->next=p;
    p->next=NULL;
    L->len++;
    return 0;
}

int get_any(plink1 L,asd e,int a)
{
    plink1 t=L;
    for (int i = 0; i < a-1; i++)
    {
        t=t->next;
    }
    plink1 p=malloc(sizeof(link1));
    p->data=e;
    p->next=t->next;
    t->next=p;
    L->len++;
}

int dele_any(plink1 L,int a)
{
    plink1 t=L;
    for (int i = 0; i < a-1; i++)
    {
        t=t->next;
    }
    plink1 p=t->next;
    t->next=t->next->next;
    free(p);
    p=NULL;
    L->len--;
}

int out_put(plink1 L)
{
    plink1 t=L;
    for (int i = 0; i < L->len; i++)
    {
        t=t->next;
        printf("姓名:%s   年龄:%d   成绩:%d\n",t->data.name,t->data.age,t->data.score);
    }
    
}

int stu_reve(plink1 L)
{
    plink1 t=L->next;
    plink1 Q=L->next->next;
    while (Q!=NULL)
    {
        t->next=Q->next;
        Q->next=L->next;
        L->next=Q;
        Q=t->next;
    }
    
}
int main()
{
    plink1 L=get_head();
    asd students[4]={{15,95,"zhang"},{16,80,"li"},{15,75,"wang"},{15,63,"ling"}};
    
    for (int i = 0; i < 4; i++)
    {
        taile_insert(L,students[i]);
    }


    out_put(L);
    asd student;
    printf("请输入你要插入的学生信息,及其位置\n");
    printf("学生姓名:\n");
    scanf("%s",student.name);
    printf("学生年龄:\n");
    scanf("%d",&student.age);
    printf("学生成绩:\n");
    scanf("%d",&student.score);
    printf("要插入的位置:\n");
    int a=0;
    scanf("%d",&a);
    get_any(L,student,a);
    out_put(L);
    printf("请输入要删除的学生位置\n");
    scanf("%d",&a);
    dele_any(L,a);
    out_put(L);
    printf("逆置后:\n");
    stu_reve(L);
    out_put(L);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值