首页 > 学院 > 开发设计 > 正文

链表初识

2019-11-08 02:16:07
字体:
来源:转载
供稿:网友
//链表#include<stdio.h>#include<string.h>#include<malloc.h>typedef struct Student{int id;char name[20];int score;struct Student *next;}Stud,*Stu;//head是一个带头节点的链表void insert(Stu head);void del(Stu head,int id);void PRint(Stu head);int main(){Stu s1=(Stu)malloc(sizeof(Stud));//头指针s1->next=NULL;s1->id=0;//借用来表示链表的节点个数for(int i=0;i<3;i++) insert(s1);print(s1);printf("请输入要删除的学生学号:");int id;scanf("%d",&id);del(s1,id);print(s1);return 0;}void insert(Stu head){printf("请输入学号 姓名 成绩:");Stu q=(Stu)malloc(sizeof(Stud));scanf("%d %s %d",&q->id,q->name,&q->score);q->next=head->next;head->next=q;head->id++;}//head是一个带头节点的链表,id是要删除节点的学号void del(Stu head,int id){Stu p=head;while(p->next!=NULL){if (p->id==id){Stu q=head->next;head->next=p->next;free(q);break;}else if(p->next->id==id){Stu q=p->next;p->next=p->next->next;free(q);head->id--;break;}p=p->next;}}//输出链表数据void print(Stu head){Stu p=head;printf("链表共有%d个节点/n",head->id);while(p->next!=NULL){printf("学号%d,姓名%s,成绩%d/n",p->next->id,p->next->name,p->next->score);p=p->next;}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表