单向链表操作(产生,删除,插入以及查询)
2012-12-12 23:29
585 查看
// // main.m // leap // // Created by Mamong on 12-11-26. // Copyright (c) 2012年 Mamong. All rights reserved. // #include<stdio.h> //#include<malloc.h> //#define NULL 0 #define LEN sizeof(DAT) typedef struct student { long num; char name[100]; int age; char sex; int score; struct student *next; }DAT; int static n=0; DAT* create() { DAT *head,*e; DAT *u,*R; head=(DAT *)malloc(LEN); e=(DAT *)malloc(LEN); R=head; printf("type in the first student's info:"); scanf("%ld %s %d %c %d",&e->num,e->name,&e->age,&e->sex,&e->score); if (0!=e->num) n=n+1; while(0!=e->num) { u=(DAT *)malloc(LEN); u->num=e->num;strcpy(u->name,e->name);u->age=e->age;u->sex=e->sex;u->score=e->score; R->next=u; R=u; if(getchar()=='\n') printf("type in the %d(st/nd/rd/th) student's info:",++n); scanf("%ld %s %d %c %d",&e->num,e->name,&e->age,&e->sex,&e->score); } R->next=NULL; return head; } void print(DAT *head) { DAT * p; if(NULL==head->next) printf("error:null\n"); else {p=head->next; do { printf("%ld,%s,%d,%c,%d\n",p->num,p->name,p->age,p->sex,p->score); p=p->next; }while(NULL!=p); } } DAT *del(DAT *head,long num) { DAT *p1,*p2; if(NULL==head->next) {printf("table is NULL\n");goto end;} p1=head; while(num!=p1->num&&NULL!=p1->next) { p2=p1; p1=p1->next; } if(num==p1->num) { if(head==p1) head=p1->next; else p2->next=p1->next; printf("the number to delete is %ld\n",num); n--; } else printf("error:no such student\n"); end: return(head); } DAT *insert(DAT *head,DAT *stud) { DAT *p0,*p1,*p2; p1=head->next; p0=stud; if(NULL==head->next) {head->next=p0;p0->next=NULL;} else { while((p0->num>p1->num)&&(NULL!=p1->next)) { p2=p1; p1=p1->next; } if(p0->num<=p1->num) { if(p1==head->next) head->next=p0; else p2->next=p0; p0->next=p1; } else {p1->next=p0;p0->next=NULL;} } n++; return(head); } void search(DAT *head,long num) { DAT *p1; long p0; p1=head->next; if(p1==NULL) { printf("error:table is NULL\n"); } while(p1!=NULL) { p0=p1->num; if(p0==num){ printf("number %ld student's info as follow:%ld,%s,%d,%c,%d",num,num,p1->name,p1->age,p1->sex,p1->score);break; } else p1=p1->next; } if(p1==NULL) printf("reach the tail,no such student"); } int main(int argc,char * argv[]) { long search_num; DAT *head,*stud; head=(DAT *)malloc(sizeof(DAT)); long num; head=create(); print(head); printf("type in the number to delete:"); scanf("%ld",&num); while(0!=num) { head=del(head,num); print(head); printf("type in the next number to delete:"); scanf("%ld",&num); } printf("new student info:"); stud=(DAT *)malloc(LEN); scanf("%ld %s %d %c %d",&stud->num,stud->name,&stud->age,&stud->sex,&stud->score); if(NULL!=stud) while(0!=stud->num) { head=insert(head,stud); print(head); printf("next new student's info:"); stud=(DAT *)malloc(LEN); scanf("%ld %s %d %c %d",&stud->num,stud->name,&stud->age,&stud->sex,&stud->score); } printf("type in the number you want to search:"); scanf("%ld",&search_num); search(head,search_num); return 1; }
相关文章推荐
- Oracle数据库的概念(权限、角色)以及各种操作(创建表空间、创建表、查询、更新、删除、插入)和常用函数
- C语言单向动态链表程序,实现链表的建立,合并,重新排序,链表元素的插入与删除,以及根据元素成员的值进行元素删除。
- (C语言版)链表(一)——实现单向链表创建、插入、删除等简单操作(包含个人理解说明及注释,新手跟着写代码)
- (C++版)链表(一)——实现单向链表创建、插入、删除等相关操作
- 2016年12月21日学习总结----单向循环链表操作程序(头插,尾插,中间插入,删除,逆序)
- c++之链表篇1:单向链表的创建,打印,删除,插入,销毁等基本操作
- 双循环链表的创建以及插入删除等操作
- (C语言版)链表(二)——实现单向循环链表创建、插入、删除、释放内存等简单操作
- 数据结构之单向链表操作1-(插入,删除,交换,反转,排序等操作)
- (C语言版)链表(一)——实现单向链表创建、插入、删除等简单操作(包含个人理解说明及注释,新手跟着写代码)
- 双链表的实现以及插入删除等操作
- 顺序表,链表,静态链表的建立以及插入和删除操作
- 单链表的基本操作_创建、查询、插入、删除
- 单向链表操作:新建,输出,删除,插入
- (C语言版)链表(二)——实现单向循环链表创建、插入、删除、释放内存等简单操作
- 单向链表的基本操作-创建、插入、删除
- 关于动态存储分配函数的调用,在已经过排序的数组中查找及删除内容的操作,余数的分析,删除字符数组中的空格,对链表的逆置,在源字符串中查找子字符串的个数,函数指针以及函数的调用,循环赋值带来的问题以及插入
- 单向链表的删除及插入操作(以头插入法建立单向链表)
- 设计一个整型链表类list,能够实现链表节点的插入、删除、以及链表数据的输出操作。
- 对线性表理解以及C语言实现链表的插入删除等操作。