链表的基本操作
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://qq164587043.blog.51cto.com/261469/78005 |
最近遭遇成都的地震,课也不上了,被余震弄得人心惶惶,每晚和朋友轮班睡觉,恼火的很。随手拿了本数据结构的书,反正闲着无聊,写了几个链表的基本程序玩玩,再熟悉熟悉链表。 #include <stdio.h> #include <stdlib.h> #define LEN sizeof(struct student) #define FORMANT() \ printf("==================================\n");\ printf("num eng math avg(auto)\n");\ printf("==================================\n") struct student { int num; float english; float math; float avg; struct student *next; }; int n = 0; struct student *creat(); void prt(struct student *head); struct student *insert(struct student *head,int index); struct student *del(struct student *head, int index); int main(int argc, char *argv[]) { struct student stu, *head; int pos,position; printf("创建链表:\n"); head = creat(); prt(head); printf("\n插入操作:\n"); printf("请输入要插入的位置:"); scanf("%d",&pos); head = insert(head,pos); prt(head); printf("\n删除操作:\n"); printf("请输入要删除的位置:"); scanf("%d",&position); head = del(head,position); prt(head); system("PAUSE"); return 0; } struct student *creat() { struct student *p1, *p2, *head = NULL; p1 = p2 = (struct student *)malloc(LEN); printf("请输入学生的信息:\n"); FORMANT(); scanf("%d%f%f", &p1->num,&p1->english,&p1->math); p1->avg = (p1->english + p1->math)/2; while(p1->num != 0) { n++; if(n == 1) { head = p1; } else { p2->next = p1; } p2 = p1; p1 = (struct student *)malloc(LEN); scanf("%d%f%f", &p1->num,&p1->english,&p1->math); p1->avg = (p1->english + p1->math)/2; } p1->next = NULL; return head; } void prt(struct student *head) { struct student *p1; p1 = head; printf("\n显示链表:\nnow there are %d datas.\n",n); FORMANT(); while(p1 != NULL) { printf("%-8d%-8.2f%-9.2f%-8.2f\n", p1->num, p1->english, p1->math, p1->avg); p1=p1->next; } } struct student *insert(struct student *head,int index) { struct student *p1, *p2 = head, *p3 = head; if(index < 1 || index > n+1) { printf("the position is overflow...\n"); exit(1); } printf("请输入待插入的数据:\n"); FORMANT(); p1 = (struct student *)malloc(LEN); scanf("%d%f%f", &p1->num,&p1->english,&p1->math); p1->avg = (p1->english + p1->math)/2; if(index == 1) { p1->next = head; head = p1; } else if(index == n+1) { while(p2->next != NULL) { p2 = p2->next; } p2->next = p1; p1->next = NULL; } else { int i = 1, j = 1; while(i < index) { p2 = p2->next; i++; } while(j < index-1) { p3 = p3->next; j++; } p3->next = p1; p1->next = p2; } n++; return head; } struct student *del(struct student *head, int index) { struct student *p1 = head, *p2 = head, *p3 = head; if(n == 0) { printf("the list is empty!\n"); exit(1); } if(index < 1 || index > n) { printf("the position is overflow....\n"); exit(1); } if(index == 1) { head = p1->next; free(p1); } else if(index == n) { while(p1->next->next != NULL) { p1 = p1->next; } free(p1->next); p1->next = NULL; } else { int i = 1, j = 1; while(i < index+1) { p2 = p2->next; i++; } while(j < index-1) { p3 = p3->next; j++; } p1 = p3->next; p3->next = p2; free(p1); } n--; return head; } 本文出自 “国产0与1” 博客,请务必保留此出处http://qq164587043.blog.51cto.com/261469/78005 本文出自 51CTO.COM技术博客 |


hello_world
博客统计信息
热门文章
最新评论
友情链接
