创建一个链表、删除一个节点、插入一个节点--实验
2013-03-19 00:30
423 查看
创建一个链表、删除一个节点、插入一个节点:
(说明:在VC6.0能够正常运行,仅供参考)
(说明:在VC6.0能够正常运行,仅供参考)
#include <stdio.h> #include <malloc.h> int n=0; void print_list(struct student *head); struct student *creat(); struct student *del(struct student *head,int num); struct student *insert(struct student *head,struct student *p); struct student { int num; float score; struct student *next; }; int main() { int number; struct student *head,stu; head = NULL; head = creat(); print_list(head); printf("please input the del number:\n"); scanf("%d",&number); head = del(head,number); print_list(head); printf("please input the insert number:\n"); scanf("%d,%f",&stu.num,&stu.score); head = insert(head,&stu); print_list(head); return 0; } /*************创建一个节点********************/ struct student *creat(void) { struct student *head,*tail,*p; head = tail = p = NULL; tail = p = (struct student *)malloc(sizeof(struct student)); printf("please input the number:\n"); scanf("%d,%f",&p->num,&p->score); while (p->num != 0) { n = n+1; if (n == 1) { head = p; } else { tail->next = p; tail = p; } p = malloc(sizeof(struct student)); scanf("%d,%f",&p->num,&p->score); } tail->next = NULL; return (head); } /***************删除一个节点*****************/ struct student *del(struct student *head,int num) { struct student *p1,*p2; p1 = p2 = NULL; p1 = head; while (p1->num != num && p1->next != NULL) { p2 = p1; p1 = p1->next; } if (num == p1->num) { if (p1 == head) { head = p1->next; } else { p2->next = p1->next; } printf("del the number:%d\n",num); } return (head); } /************插入一个节点**************/ struct student *insert(struct student *head,struct student *p) { struct student *p0,*p1,*p2; p1 = head; p0 = p; if (head == NULL) { head = p0; p0->next = NULL; } else { while ((p0->num > p1->num)&&(p1->next != NULL)) { p2 = p1; p1 = p1->next; } if (p0->num <= p1->num) { if (head == p1) { head = p0; } else { p2->next = p0; p0->next = p1; } } } return (head); } void print_list(struct student *head) { struct student *p; printf("now,these %d records are:\n",n); p = head; if (head != NULL) { do { printf("%d,%f\n",p->num,p->score); p = p->next; } while (p != NULL); } }
相关文章推荐
- 数据结构和算法设计专题之---单链表中在指定的节点前面插入以及删除一个节点
- 每天一个小程序(2)——带头结点的链表的创建以及插入和删除
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- 创建一个链表,可以插入学生信息、删除学生信息
- 单链表创建、测长度、打印、删除节点、插入节点、排序、逆置
- 单向链表小练习--》创建链表头,添加删除链表节点,释放链表内存,选择插入数据
- 【算法之链表(三)】单链表中,在仅允许使用一个指针的情况下,在指定的节点前面插入以及删除一个节点
- 建立一个带附加头结点的单链表.实现测长/打印/删除结点/插入结点/逆置/查找中间节点/查找倒数第k个节点/判断是否有环
- 数据结构和算法设计专题之---单链表中在指定的节点前面插入以及删除一个节点
- 设计一个整型链表类list,能够实现链表节点的插入、删除、以及链表数据的输出操作。
- 【链表面试题】删除无头单链表的非尾节点,插入一个元素到无头链表指定位置
- 用头插、尾插、按顺序插入创建一个不带头节点的链表,栈的基本操作
- 创建一个链表,插入删除,指定元素
- 【链表面试题】删除无头单链表的非尾节点,插入一个元素到无头链表指定位置
- malloc的链表,创建链表,遍历链表,插入节点,删除节点
- java实现创建链表以及插入节点,查找结点,删除节点等操作
- 实现双向链表删除一个节点P,在节点P后插入一个节点
- 链表的创建、遍历、节点的插入、节点的删除
- 数据结构和算法设计专题之---单链表中在指定的节点前面插入以及删除一个节点
- 数据结构和算法设计专题之---单链表中在指定的节点前面插入以及删除一个节点