附录部分章节算法的C语言代码_第1页
附录部分章节算法的C语言代码_第2页
附录部分章节算法的C语言代码_第3页
附录部分章节算法的C语言代码_第4页
附录部分章节算法的C语言代码_第5页
已阅读5页,还剩40页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、附录:部分章节算法的C语言代码第一章 绪论#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2#define NULL 0typedef struct char kind;int num;ElemType;typedef ElemType *Truck;typedef int Status;Status InitTruck(Truck *T, ElemType v1, ElemType v2, ElemType v3)*T=(ElemType *) mal

2、loc (3*sizeof(ElemType);if (!*T) exit(OVERFLOW);(*T)0 =v1; (*T)1 =v2; (*T)2 =v3;return OK;Status DestroyTruck(Truck *T)free (* T ) ; *T=NULL ;return OK;Status GoodsGet(Truck T, int i, ElemType *e)if (i<1|i>3) return ERROR;*e=Ti-1;return OK;Status GoodsChange(Truck *T, int i, ElemType e, ElemTy

3、pe *f)if(i<1|i>3) return ERROR;*f=(*T)i-1;(*T)i-1=e;return OK;Status GoodsSort(Truck *T)ElemType x;if(*T)0.kind>(*T)1.kind)x=(*T)0;(*T)0=(*T)1;(*T)1=x;if(*T)0.kind>(*T)2.kind)x=(*T)0;(*T)0=(*T)2;(*T)2=x;if(*T)1.kind>(*T)2.kind)x=(*T)1;(*T)1=(*T)2;(*T)2=x;return OK;Status Max(Truck T,

4、ElemType *e)*e=(T0.num>=T1.num)?( (T0.num >=T2 .num) ? T0 : T2) : ( (T1 .num >=T2 .num) ? T1 : T2);return OK;Status Min(Truck T, ElemType *e)*e=(T0.num<=T1 .num)?( (T0 .num<=T2 .num) ? T0 : T2) : ( (T1 .num <=T2 .num) ? T1 : T2);return OK;Status ShowGoods(Truck T)printf("nThe

5、Goods is (%c: %d,%c: %d,%c: %d)",T0.kind,T0.num,T1.kind,T1.num,T2.kind,T2.num);void System()printf("nnnn*");printf("* Welcome To Use Truck Operator! *");printf("*n");void Warn()printf("nPlease creat a Truck first!");main()ElemType v1,v2,v3,e,f;int a,Exit,

6、i;int test; Truck T; test=0; do System(); printf("1:Creat a Truckn"); printf("2:Get Goodsn"); printf("3:Change the Goodsn"); printf("4:Sort The Truckn"); printf("5:Get the Maxn"); printf("6:Get the Minn"); printf("7:Release the Truckn&

7、quot;); printf("8:Exitn"); printf("nnChoose a Number(1-8):"); scanf("%d",&a); switch(a) case 1: scanf("%*n%*c"); printf("nPlease input the kinds of goods1 (kind: "); scanf("%c%*n%*c",&v1.kind); printf("nPlease input the num of

8、goods1 (num: "); scanf("%d%*n%*c",&v1.num); printf("nPlease input the kinds of goods2 (kind: "); scanf("%c%*n%*c",&v2.kind); printf("nPlease input the num of goods2 (num: "); scanf("%d%*n%*c",&v2.num); printf("nPlease input the

9、kinds of goods3 (kind: "); scanf("%c%*n%*c",&v3.kind); printf("nPlease input the num of goods3 (num: "); scanf("%d%*n%*c",&v3.num); InitTruck(&T,v1,v2,v3); test=1; ShowGoods(T); printf("nnnPress Any Key to Continue."); getch(); break; case 2:

10、if (test=0) Warn();break; printf("nPlease choose the number you want(1-3):"); scanf("%d",&i); if (i<=0)|(i>3) printf("nWrong number!");break; GoodsGet(T,i,&e); printf("The goods is:%c: %d",e.kind, e.num); ShowGoods(T); printf("nnnPress Any K

11、ey to Continue."); getch(); break; case 3: if (test=0) Warn();break; printf("nPlease choose the number you want change(1-3):"); scanf("%d%*n%*c",&i); if (i<=0)|(i>3) printf("nWrong number!");break; printf("nPlease input the kinds of goods you want ch

12、ange it into(kind: "); scanf("%c%*n%*c",&e.kind); printf("nPlease input the num of goods you want change it into(num: "); scanf("%d%*n%*c",&e.num); GoodsChange(&T,i,e,&f); ShowGoods(T); printf("nnnPress Any Key to Continue."); getch(); bre

13、ak; case 4: if (test=0) Warn();break; GoodsSort(&T); ShowGoods(T); printf("nnnPress Any Key to Continue."); getch(); break; case 5: if (test=0) Warn();break; Max(T,&e); printf("The Max is %c: %d.",e.kind,e.num); printf("nnnPress Any Key to Continue."); getch();

14、break; case 6: if (test=0) Warn();break; Min(T,&e); printf("The Min is %c: %d.",e.kind,e.num); printf("nnnPress Any Key to Continue."); getch(); break; case 7: if (test=0) Warn();break; DestroyTruck(&T); test=0; printf("nThe Truck has been released!"); break; ca

15、se 8:Exit=TRUE;break; default:printf("nWrong Number!"); while(Exit!=TRUE);第二章 线性表一、顺序表#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2#define NULL 0typedef int Status;typedef int ElemType;#define MAXLEN 100#define LIST_INCREASE 10typedef stru

16、ctElemType *list; int length; int maxsize;list_Sq;Status CreatList_S(list_Sq *L)L->list=(ElemType *) malloc(MAXLEN*sizeof(ElemType); if (!L->list) exit(OVERFLOW); L->length=0; L->maxsize=MAXLEN; return OK;Status DestoryList_S(list_Sq *L)free(L->list); return OK;Status ClearList_S(list

17、_Sq *L)L->length=0; return OK;Status ListEmpty_S(list_Sq *L) if (L->length=0) return TRUE; else return FALSE;Status ListLength_S(list_Sq L)return L.length;Status GetElem_S(list_Sq L,int i,ElemType *e)if(i<1|i>L.length+1) return ERROR; *e=*(L.list+i-1); return OK;Status LocateElem_S(list_

18、Sq L,ElemType e)int *p; int i=1; p=L.list; while (i<=L.length)&&(*p+!=e) +i; if (i<=L.length) return i; else return ERROR;Status PriorElem_S(list_Sq L,int cur_e,int *pre_e)int *p; int i=1; p=L.list; while(i<=L.length)&&(*p+!=cur_e) +i; if (i<=L.length&&i!=1) *pre_

19、e=*(L.list+i-2);return OK; else return ERROR;Status NextElem_S(list_Sq L,int cur_e,int *next_e)int *p; int i=1; p=L.list; while(i<=L.length)&&(*p+!=cur_e) +i; if (i<L.length) *next_e=*(L.list+i);return OK; else return ERROR;Status ListInsert_S(list_Sq *L, int i,ElemType e)int *newbase,

20、*q,*p; if(i<1|i>L->length+1) return ERROR; if(L->length>=L->maxsize)newbase=(ElemType *) realloc(L->list, (L->maxsize+LIST_INCREASE)*sizeof(ElemType); if (!newbase) exit(OVERFLOW); L->list=newbase; L->maxsize+=LIST_INCREASE; q=L->list+i-1; for(p=&(L->listL->

21、;length-1);p>=q;-p) *(p+1) = *p; *q=e; +L->length; return OK;Status ListDelete_S(list_Sq *L,int i,ElemType *e)int *p,*q; if (i<1|i>L->length) return ERROR; p=L->list+i-1; *e=*p; q=L->list+L->length -1; for(+p;p<=q;+p) *(p-1) = *p; -L->length; return OK;Status Show(list_

22、Sq L)int i; for(i=1;i<=L.length;i+) printf("n%d.tt%dn",i,*(L.list+i-1);void System()printf("nnnn*"); printf("* Welcome To Use Linear List Operator! *"); printf("*n");void Warn()printf("nPlease creat Linear List first!");main()int a,Exit,i,e; int t

23、est=0; list_Sq L; do System(); printf("1:Creat Linear Listn"); printf("2:Test if the Linear List is Emptyn"); printf("3:Get the length the Linear Listn"); printf("4:Insert an Elementn"); printf("5:Delete an Elementn"); printf("6:Get an Elementn&

24、quot;); printf("7:Search an Elementn"); printf("8:Get an Element prior to your Inputn"); printf("9:Get an Element next to your Inputn"); printf("10:Clear the Linear Listn"); printf("11:Release the Linear Listn"); printf("12:Exitn"); printf(

25、"nnChoose a Number(1-12):"); scanf("%d",&a); switch(a) case 1: if (CreatList_S(&L)=OK) printf("nSucceed to Creat Linear List!n");test=1; else printf("nFail to Creat Linear List!n");test=0; printf("nnnPress Any Key to Continue."); getch(); bre

26、ak; case 2: if (test=0) Warn();break; if (ListEmpty_S(&L) printf("nThe Linear List is Empty."); else printf("nThe Linear List is not Empty."); Show(L); printf("nnnPress Any Key to Continue."); getch(); break; case 3: if (test=0) Warn();break; a=ListLength_S(L); prin

27、tf("nThe length of the Linear List is %d.",a); Show(L); printf("nnnPress Any Key to Continue."); getch(); break; case 4: if (test=0) Warn();break; printf("nPlease choose the number you want insert before:"); scanf("%d",&i); printf("nPlease input a num

28、ber you want to insert:"); scanf("%d",&e); a=ListInsert_S(&L,i,e); if (a=ERROR) printf("nWrong number!");break; Show(L); printf("nnnPress Any Key to Continue."); getch(); break; case 5: if (test=0) Warn();break; printf("nPlease choose the number you wa

29、nt to Delete:"); scanf("%d",&i); a=ListDelete_S(&L,i,&e); if (a=ERROR) printf("nWrong number!");break; printf("nThe number you have deleted is %d",e); Show(L); printf("nnnPress Any Key to Continue."); getch(); break; case 6: if (test=0) Warn()

30、;break; printf("nPlease choose the number you want:"); scanf("%d",&i); if (GetElem_S(L,i,&e)=ERROR) printf("nWrong number!");break; printf("The number you want is:%d",e); Show(L); printf("nnnPress Any Key to Continue."); getch(); break; case

31、7: if (test=0) Warn();break; printf("nPlease choose the number you want to Search:"); scanf("%d",&e); i=LocateElem_S(L,e); if (!i) printf("nIt does not exist!");break; printf("The Position of the number is %d",i); Show(L); printf("nnnPress Any Key to

32、Continue."); getch(); break; case 8: if (test=0) Warn();break; printf("nPlease input the number of which you want get its prior:"); scanf("%d",&i); a=PriorElem_S(L,i,&e); if (a=ERROR) printf("nIt does not exist!");break; printf("The number's prior

33、is:%d",e); Show(L); printf("nnnPress Any Key to Continue."); getch(); break; case 9: if (test=0) Warn();break; printf("nPlease input the number of which you want get its next:"); scanf("%d",&i); a=NextElem_S(L,i,&e); if (a=ERROR) printf("nIt does not e

34、xist!");break; printf("The number's next is:%d",e); Show(L); printf("nnnPress Any Key to Continue."); getch(); break; case 10: if (test=0) Warn();break; ClearList_S(&L); printf("nThe Linear List has been cleared!"); break; case 11: if (test=0) Warn();break;

35、 DestoryList_S(&L); test=0; printf("nThe Linear List has been released!"); break; case 12: Exit=TRUE;break; default:printf("nWrong Number!"); while(Exit!=TRUE);二、单链表#define ERROR -1#define OK 1#include "stdio.h"#include "stdlib.h"typedef int Status;typedef

36、 char ElemType;typedef struct LNode ElemType data; struct LNode *next;LNode, *List_Link;/*This function creates a List_Link with n nodes.*/Status CreatList_L(List_Link *L,int n) int i; List_Link p; printf("Please input %d characters:",n); *L= (List_Link) malloc(sizeof(LNode); (*L)->next

37、=NULL; for(i=n;i>0;i-) p= (List_Link) malloc(sizeof(LNode); scanf("%c",&p->data); p->next=(*L)->next; (*L)->next=p; scanf("%*n%*c");/*This function inserts a node whose value is e before the node i */Status ListInsert_L(List_Link *L,int i,ElemType e) List_Link

38、p, s; int j; p=*L; j=0; while(p&&j<i-1) p=p->next; +j; if(!p|j>i-1)return ERROR; s= (List_Link) malloc(sizeof(LNode); s->data=e; s->next=p->next; p->next=s; return OK;/*The function deletes the node i */Status ListDelete_L(List_Link *L,int i,ElemType *e) List_Link p, q;

39、int j; p=*L; j=0; while(p->next&&j<i-1) p=p->next; +j; if(!(p->next)|j>i-1)return ERROR; q=p->next; p->next=q->next; *e=q->data; free(q); return OK;Status GetElem_L(List_Link L,int i,ElemType *e) List_Link p; int j; p=L->next; j=1; while(p&&j<i) p=p-&

40、gt;next; +j; if(!p|j>i)return ERROR; *e=p->data; return OK;int LocateElem_L(List_Link L, ElemType e) List_Link p; int j; p=L->next; j=1; while(p&&p->data!=e) p=p->next; +j; if(!p) return 0; return j;Status Print_L(List_Link L) List_Link p; p=L->next; printf("nThe Eleme

41、nts in this list are:"); while(p) printf("%c ",p->data); p=p->next; printf("n"); return OK;main() ElemType ch; int i,j,k; List_Link head; do for(i=1;i<=48;i+) printf("n"); printf("1. Create a Linear List.n"); printf("2. Insert an element.n&q

42、uot;); printf("3. Delete an element.n"); printf("4. Get an element.n"); printf("5. Search an element.n"); printf("6. Output the Linear List.n"); printf("7. Exitn"); printf("Please Choose:"); scanf("%d%*n%*c",&k); switch(k) cas

43、e 1: printf("nEnter the length of list:"); scanf("%d%*n%*c",&i); CreatList_L(&head, i); Print_L(head); getch(); break; case 2: do printf("nEnter the inster-position and data(i,e):"); j=scanf("%d,%c%*n%*c",&i,&ch); if(j!=2) scanf("%*n%*c&qu

44、ot;); while(j!=2); printf("The original list:"); Print_L(head); j=ListInsert_L(&head, i, ch); if(j=ERROR)printf("Parameter Errorn"); else printf("The new list:"); Print_L(head); getch(); break; case 3: do printf("nEnter the delete-position:"); j=scanf(&quo

45、t;%d%*n%*c",&i); if(j!=1) scanf("%*n%*c"); while(j!=1); printf("The original list:"); Print_L(head); j=ListDelete_L(&head, i, &ch); if(j=ERROR)printf("Parameter Errorn"); else printf("The deleted element is %c; the new list:",ch); Print_L(head

46、); getch(); break; case 4: do printf("nEnter the search-position:"); j=scanf("%d%*n%*c",&i); if(j!=1) scanf("%*n%*c"); while(j!=1); j=GetElem_L(head, i, &ch); if(j=ERROR)printf("Parameter Errorn"); else printf("The %dth element is %cn",i, ch)

47、; Print_L(head); getch(); break; case 5: printf("nEnter the search-element:"); scanf("%c%*n%*c",&ch); j=LocateElem_L(head, ch); if(j=0)printf("Not Foundn"); else printf("Found the %dth element which value is %c.n",j, ch); Print_L(head); getch(); break; cas

48、e 6: Print_L(head); getch(); break; while(k!=7);三、例题#define ERROR -1#define OK 1#include "stdio.h"#include "stdlib.h"typedef int Status;typedef struct char kind;int num;ElemType;typedef struct LNode ElemType data; struct LNode *next;LNode, *List_Link;typedef List_Link Whouse;type

49、def List_Link Truck;/*This function creates a List_Link with n nodes.*/Status CreatList_L(List_Link *L) *L= (List_Link) malloc(sizeof(LNode); (*L)->next=NULL;/*This function inserts a node whose value is e before the node i */Status ListInsert_L(List_Link *L,int i,ElemType e) List_Link p, s; int

50、j; p=*L; j=0; while(p&&j<i-1) p=p->next; +j; if(!p|j>i-1)return ERROR; s= (List_Link) malloc(sizeof(LNode); s->data=e; s->next=p->next; p->next=s; return OK;/*The function deletes the node i */Status ListDelete_L(List_Link *L,int i,ElemType *e) List_Link p, q; int j; p=*

51、L; j=0; while(p->next&&j<i-1) p=p->next; +j; if(!(p->next)|j>i-1)return ERROR; q=p->next; p->next=q->next; *e=q->data; free(q); return OK;Status GetElem_L(List_Link L,int i,ElemType *e) List_Link p; int j; p=L->next; j=1; while(p&&j<i) p=p->next; +

52、j; if(!p|j>i)return ERROR; *e=p->data; return OK;int LocateElem_L(List_Link L, ElemType e) List_Link p; int j; p=L->next; j=1; while(p&&p->data.kind!=e.kind) p=p->next; +j; if(!p) return 0; return j;Status List_Sorted_Insert_L (List_Link *L, ElemType e)/*非递减排列的有序线性表的插入*/List_L

53、ink p,s;p=*L;while(p->next &&e.kind>=p->next->data.kind) p=p->next;s=(List_Link)malloc(sizeof (LNode);s->data=e;s->next=p->next;p->next = s;return OK;void CreatWhouse(Whouse *W, int n) ElemType e; int i,number; char kinds; CreatList_L(W); for ( i=1; i<=n; ) printf("nPlease input the kinds of the goods(az):"); scanf("%c%*n%*c",&kinds); printf("nPlease input the number of the goods:"); scanf("%d%*n%*c",&number); e.kind=kinds;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论