|
源代码网推荐
在这里列举了一个应用单链表基本算法的综合程序,双向链表和循环链表的综合程序大家可以自己去试一试。 源代码网推荐#include <stdio.h> 源代码网推荐#include <malloc.h> 源代码网推荐#include <string.h> 源代码网推荐#define n 10源代码网整理以下typedef struct node 源代码网推荐{ 源代码网推荐char name[20]; 源代码网推荐struct node *link; 源代码网推荐}stud; 源代码网整理以下stud * creat(int n) 源代码网推荐{ 源代码网推荐stud *p,*h,*s; 源代码网推荐int i; 源代码网推荐if((h=(stud *)malloc(sizeof(stud)))==null) 源代码网推荐{ 源代码网推荐printf("不能分配内存空间!"); 源代码网推荐exit(0); 源代码网推荐} 源代码网推荐h->name[0]="\0"; 源代码网推荐h->link=null; 源代码网推荐p=h; 源代码网推荐for(i=0;i<n;i++) 源代码网推荐{ 源代码网推荐if((s= (stud *) malloc(sizeof(stud)))==null) 源代码网推荐{ 源代码网推荐printf("不能分配内存空间!"); 源代码网推荐exit(0); 源代码网推荐} 源代码网推荐p->link=s; 源代码网推荐printf("请输入第%d个人的姓名",i+1); 源代码网推荐scanf("%s",s->name); 源代码网推荐s->link=null; 源代码网推荐p=s; 源代码网推荐} 源代码网推荐return(h); 源代码网推荐} 源代码网整理以下stud * search(stud *h,char *x) 源代码网推荐{ 源代码网推荐stud *p; 源代码网推荐char *y; 源代码网推荐p=h->link; 源代码网推荐while(p!=null) 源代码网推荐{ 源代码网推荐y=p->name; 源代码网推荐if(strcmp(y,x)==0) 源代码网推荐return(p); 源代码网推荐else p=p->link; 源代码网推荐} 源代码网推荐if(p==null) 源代码网推荐printf("没有查找到该数据!"); 源代码网推荐} 源代码网整理以下stud * search2(stud *h,char *x) 源代码网推荐{ 源代码网推荐stud *p,*s; 源代码网推荐char *y; 源代码网推荐p=h->link; 源代码网推荐s=h; 源代码网推荐while(p!=null) 源代码网推荐{ 源代码网推荐y=p->name; 源代码网推荐if(strcmp(y,x)==0) 源代码网推荐return(s); 源代码网推荐else 源代码网推荐{ 源代码网推荐p=p->link; 源代码网推荐s=s->link; 源代码网推荐} 源代码网推荐} 源代码网推荐if(p==null) 源代码网推荐printf("没有查找到该数据!"); 源代码网推荐} 源代码网整理以下void insert(stud *p) 源代码网推荐{ 源代码网推荐char stuname[20]; 源代码网推荐stud *s; 源代码网推荐if((s= (stud *) malloc(sizeof(stud)))==null) 源代码网推荐{ 源代码网推荐printf("不能分配内存空间!"); 源代码网推荐exit(0); 源代码网推荐} 源代码网推荐printf("\n请输入你要插入的人的姓名:"); 源代码网推荐scanf("%s",stuname); 源代码网推荐strcpy(s->name,stuname); 源代码网推荐s->link=p->link; 源代码网推荐p->link=s; 源代码网推荐} 源代码网整理以下void del(stud *x,stud *y) 源代码网推荐{ 源代码网推荐stud *s; 源代码网推荐s=y; 源代码网推荐x->link=y->link; 源代码网推荐free(s); 源代码网推荐} 源代码网整理以下void print(stud *h) 源代码网推荐{ 源代码网推荐stud *p; 源代码网推荐p=h->link; 源代码网推荐printf("数据信息为:\n"); 源代码网推荐while(p!=null) 源代码网推荐{ 源代码网推荐printf("%s ",&*(p->name)); 源代码网推荐p=p->link; 源代码网推荐} 源代码网推荐} 源代码网整理以下void quit() 源代码网推荐{ 源代码网推荐exit(0); 源代码网推荐} 源代码网整理以下void menu(void) 源代码网推荐{ 源代码网推荐clrscr(); 源代码网推荐printf("\t\t\t单链表c语言实现实例\n"); 源代码网推荐printf("\t\t|————————————————|\n"); 源代码网推荐printf("\t\t| |\n"); 源代码网推荐printf("\t\t| [1] 建 立 新 表 |\n"); 源代码网推荐printf("\t\t| [2] 查 找 数 据 |\n"); 源代码网推荐printf("\t\t| [3] 插 入 数 据 |\n"); 源代码网推荐printf("\t\t| [4] 删 除 数 据 |\n"); 源代码网推荐printf("\t\t| [5] 打 印 数 据 |\n"); 源代码网推荐printf("\t\t| [6] 退 出 |\n"); 源代码网推荐printf("\t\t| |\n"); 源代码网推荐printf("\t\t| 如未建立新表,请先建立! |\n"); 源代码网推荐printf("\t\t| |\n"); 源代码网推荐printf("\t\t|————————————————|\n"); 源代码网推荐printf("\t\t 请输入你的选项(1-6):"); 源代码网推荐} 源代码网整理以下main() 源代码网推荐{ 源代码网推荐int choose; 源代码网推荐stud *head,*searchpoint,*forepoint; 源代码网推荐char fullname[20]; 源代码网整理以下 源代码网推荐while(1) 源代码网推荐{ 源代码网推荐menu(); 源代码网推荐scanf("%d",&choose); 源代码网推荐switch(choose) 源代码网推荐{ 源代码网推荐case 1:head=creat(n); 源代码网推荐break; 源代码网推荐case 2:printf("输入你所要查找的人的姓名:"); 源代码网推荐scanf("%s",fullname); 源代码网推荐searchpoint=search(head,fullname); 源代码网推荐printf("你所查找的人的姓名为:%s",*&searchpoint->name); 源代码网推荐printf("\n按回车键回到主菜单。"); 源代码网推荐getchar();getchar(); 源代码网推荐break; 源代码网推荐case 3: printf("输入你要在哪个人后面插入:"); 源代码网推荐scanf("%s",fullname); 源代码网推荐searchpoint=search(head,fullname); 源代码网推荐printf("你所查找的人的姓名为:%s",*&searchpoint->name); 源代码网推荐insert(searchpoint); 源代码网推荐print(head); 源代码网推荐printf("\n按回车键回到主菜单。"); 源代码网推荐getchar();getchar(); 源代码网推荐break; 源代码网推荐case 4:print(head); 源代码网推荐printf("\n输入你所要删除的人的姓名:"); 源代码网推荐scanf("%s",fullname); 源代码网推荐searchpoint=search(head,fullname); 源代码网推荐forepoint=search2(head,fullname); 源代码网推荐del(forepoint,searchpoint); 源代码网推荐break; 源代码网推荐case 5:print(head); 源代码网推荐printf("\n按回车键回到主菜单。"); 源代码网推荐getchar();getchar(); 源代码网推荐break; 源代码网推荐case 6:quit(); 源代码网推荐break; 源代码网推荐default:printf("你输入了非法字符!按回车键回到主菜单。"); 源代码网推荐clrscr(); 源代码网推荐menu(); 源代码网推荐getchar(); 源代码网推荐} 源代码网推荐} 源代码网推荐} 源代码网推荐(完) 源代码网推荐 源代码网供稿. |