1、数据结构实验指导书 数据结构实验指导书注意事项:在磁盘上创建一个目录,专门用于存储数据结构实验的程序。实验一 顺序表操作一、 实验目的1、 掌握使用Turbo C2.0上机调试线性表的基本方法;2、 掌握线性表的基本操作:插入、删除、查找以及线性表合并等运算在顺序存储结构和链接存储结构上的运算。二、 实验要求1、 认真阅读和掌握本实验的程序。2、 上机运行本程序。3、 打印出程序的运行结果,并结合程序进行分析。4、 按照你对线性表的操作需要,重新改写主程序并运行,打印出文件清单和运行结果三、 实验内容程序:线性表基本操作的实现这个程序中演示了顺序表的创建、插入、删除和查找和合并。 程序如下:#
2、include#include#include#define ERROR 0#define OK 1#define EQUAL 1struct STU char name20; char stuno10; int age; int score;stu50;typedef struct STU ElemType;struct LIST ElemType elem50; int length;typedef struct LIST List;int init(List *L) *L=(List *)malloc(sizeof(List); (*L)-length=0;int ListLength(
3、List *L) return L-length;void GetElem(List L,int i,ElemType *e)*e=L.elemi;int EqualList(ElemType *e1,ElemType *e2)if (strcmp(e1-name,e2-name) return 0; if (strcmp(e1-stuno,e2-stuno) return 0; if (e1-age!=e2-age) return 0; if (e1-score!=e2-score) return 0; return 1;int LocateElem(List *La,ElemType e,
4、int type)int i; switch (type) case EQUAL: for(i=0;ilength;i+) if(EqualList(&La-elemi,&e) return 1; break; default: break; return 0;void UnionList(List *La, List *Lb) int La_len,Lb_len; int i; ElemType e; La_len=ListLength(La); Lb_len=ListLength(Lb); for(i=0;iLb_len;i+) GetElem(*Lb,i,&e); if(!LocateE
5、lem(La,e,EQUAL) ListInsert(La,+La_len,e); int printlist(List L)int i; printf(name stuno age scoren); for(i=0;iL.length;i+) printf(%-10s %st%dt%dn, L.elemi.name, L.elemi.stuno, L.elemi.age, L.elemi.score); printf(n);int ListInsert(List *L,int i,struct STU e) struct STU *p,*q; if (iL-length+1) return
6、ERROR; q=&(L-elemi-1); for(p=&L-elemL-length-1;p=q;-p) *(p+1)=*p; *q=e; +L-length; return OK;/*ListInsert Before i */main()struct STU e; List *La,*Lb; clrscr(); printf(nn-List Demo is running.-nn); printf(First is InsertList function.n); init(&La); strcpy(e.name,stu1); strcpy(e.stuno,100001); e.age=
7、80; e.score=1000; ListInsert(La,1,e); strcpy(e.name,stu2); strcpy(e.stuno,100002); e.age=80; e.score=1000; ListInsert(La,2,e); printlist(*La); printf(List A length now is %d.nn,La-length); getch(); strcpy(e.name,stu3); strcpy(e.stuno,100003); e.age=80; e.score=1000; ListInsert(La,3,e); printlist(*La
8、); printf(List A length now is %d.nn,La-length); getch(); init(&Lb); strcpy(e.name,zmofun); strcpy(e.stuno,100001); e.age=80; e.score=1000; ListInsert(Lb,1,e); strcpy(e.name,bobjin); strcpy(e.stuno,100002); e.age=80; e.score=1000; ListInsert(Lb,2,e); strcpy(e.name,stu1); strcpy(e.stuno,100001); e.ag
9、e=80; e.score=1000; ListInsert(Lb,3,e); printlist(*Lb); printf(List B length now is %d.nn,Lb-length); getch(); printf(Second is UnionList function.n); printf(Now union List A and List B.n); UnionList(La,Lb); printlist(*La); printf(List A length now is %d.nn,La-length); getch(); clrscr();实验二 单链表操作一、
10、实验目的 掌握握单链表的基本操作:插入、删除、查找等运算。二、 实验要求1 认真阅读和掌握本实验的程序。2 上机运行本程序。3 打印出程序的运行结果,并结合程序进行分析。4 按照你对单链表的操作需要,重新改写主程序并运行,打印出文件清单和运行结果三、 实验内容 程序:线性单链表基本操作的实现这个程序中演示了单链表的创建、插入、删除和查找。参考程序如下:#include #include #include #define ERROR 0#define OK 1#define EQUAL 1#define OVERFLOW -1#define LIST_INIT_SIZE 100#define L
11、ISTINCREMENT 10struct STU char name20; char stuno10; int age; int score;stu50;typedef struct STU ElemType;struct LNODE ElemType data; struct LNODE *next;typedef struct LNODE LNode;typedef struct LNODE *LinkList;int init(LinkList *L) *L=(LNode *)malloc(sizeof(LNode); if(!L) exit(ERROR); (*L)-next=NUL
12、L; return OK;/*init */int ListLength(LinkList L) int j=0; while (L-next) L=L-next; j+; return j;int GetElem(LinkList L,int i,ElemType *e) LinkList p; int j; p=L-next;j=1; while(p&jnext;+j; if(!p|j1) return ERROR; *e=p-data; return OK;int EqualList(ElemType *e1,ElemType *e2) if (strcmp(e1-name,e2-nam
13、e)=0) return 1; else return 0;int Less_EqualList(ElemType *e1,ElemType *e2) if (strcmp(e1-name,e2-name)next) p=p-next; if(EqualList(&p-data,&e) return 1; return 0; break; default: break; return 0;void MergeList(LinkList La,LinkList Lb,LinkList *Lc) LinkList pa,pb,pc; pa=La-next;pb=Lb-next; *Lc=pc=La
14、; while(pa & pb) if(Less_EqualList(&pa-data,&pb-data) pc-next=pa;pc=pa;pa=pa-next; else pc-next=pb;pc=pb;pb=pb-next; pc-next=pa?pa:pb; free(Lb);int printlist(LinkList L) int i; LinkList p; p=L; printf(name stuno age scoren); while(p-next) p=p-next; printf(%-10s %st%dt%dn, p-data.name, p-data.stuno,
15、p-data.age, p-data.score); printf(n);int ListInsert(LinkList L,int i,ElemType e) LinkList p,s; int j; p=L;j=0; while(p&jnext; +j; if(!p|ji-1) return ERROR; s=(LinkList)malloc(sizeof(LNode); s-data=e; s-next=p-next; p-next=s; return OK;/*ListInsert Before i */main() struct STU e; LinkList La,Lb,Lc; c
16、lrscr(); printf(nn-List Demo is running.-nn); printf(First is InsertList function.n); init(&La); strcpy(e.name,stu1); strcpy(e.stuno,100001); e.age=80; e.score=1000; ListInsert(La,1,e); strcpy(e.name,stu3); strcpy(e.stuno,100002); e.age=80; e.score=1000; ListInsert(La,2,e); printlist(La); getch(); s
17、trcpy(e.name,stu5); strcpy(e.stuno,100003); e.age=80; e.score=1000; ListInsert(La,3,e); printlist(La); getch(); init(&Lb); strcpy(e.name,stu2); strcpy(e.stuno,100001); e.age=80; e.score=1000; ListInsert(Lb,1,e); strcpy(e.name,stu4); strcpy(e.stuno,100002); e.age=80; e.score=1000; ListInsert(Lb,2,e);
18、 strcpy(e.name,stu6); strcpy(e.stuno,100001); e.age=80; e.score=1000; ListInsert(Lb,3,e); printlist(Lb); getch(); MergeList(La,Lb,&Lc); printlist(Lc); getch(); 实验三 栈的基本操作和应用一、 实验目的 掌握栈的基本操作:初始化栈、判栈为空、出栈、入栈等运算。二、实验要求1 认真阅读和掌握本实验的算法。2 上机将本算法实现。 3 打印出程序的运行结果,并结合程序进行分析。三、实验内容程序1:栈的顺序存储表示与实现以及栈的基本操作的算法In
19、itStack(&S) 构造一个空栈SDestroyStack(&S) 栈S存在则栈S被销毁ClearStack(&S) 栈S存在则清为空栈StackEmpty(S) 栈S存在则返回TRUE,否则FALSEStackLength(S) 栈S存在则返回S的元素个数,即栈的长度GetTop(S,&e) 栈S存在且非空则返回S的栈顶元素Push(&S,e) 栈S存在则插入元素e为新的栈顶元素Pop(&S,&e) 栈S存在且非空则删除S的栈顶元素并用e返回其值StackTraverse(S,visit()栈S存在且非空则从栈底到栈顶依次对S的每个数据元素调用函数visit()一旦visit()失败,则
20、操作失败参考程序如下:#include#include#include#define ERROR 0#define TRUE 1#define FALSE 0#define OK 1#define EQUAL 1#define OVERFLOW -1#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int Status ;struct STU char name20; char stuno10; int age; int score;typedef struct STU SElemType;struct STACK SEle
21、mType *base; SElemType *top; int stacksize;typedef struct STACK SqStack;typedef struct STACK *pSqstack;Status InitStack(SqStack *S);Status DestroyStack(SqStack *S);Status ClearStack(SqStack *S);Status StackEmpty(SqStack S);int StackLength(SqStack S);Status GetTop(SqStack S,SElemType *e);Status Push(
22、SqStack *S,SElemType e);Status Pop(SqStack *S,SElemType *e);Status StackTraverse(SqStack S,Status (*visit)();Status InitStack(SqStack *S) (*S)=(SqStack *)malloc(sizeof(SqStack); (*S)-base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType); if(!(*S)-base)exit(OVERFLOW); (*S)-top=(*S)-base; (*S)-s
23、tacksize=STACK_INIT_SIZE; return OK;Status DestroyStack(SqStack *S) free(S-base); free(S);Status ClearStack(SqStack *S) S-top=S-base;Status StackEmpty(SqStack S) if(S.top=S.base) return TRUE; else return FALSE;int StackLength(SqStack S) int i; SElemType *p; i=0; p=S.top; while(p!=S.base) p+; i+; Sta
24、tus GetTop(SqStack S,SElemType *e) if(S.top=S.base) return ERROR; *e=*(S.top-1); return OK;Status Push(SqStack *S,SElemType e) /* if(S-top - S-base=S-stacksize) S-base=(SElemType *) realloc(S-base, (S-stacksize + STACKINCREMENT) * sizeof(SElemType); if(!S-base)exit(OVERFLOW); S-top=S-base+S-stacksiz
25、e; S-stacksize += STACKINCREMENT; */ *(S-top+)=e; return OK;Status Pop(SqStack *S,SElemType *e) if(S-top=S-base) return ERROR; *e=*-S-top; return OK;Status StackPrintElem(SElemType * e) printf(%s %s %d %dn,e-name,e-stuno,e-age,e-score);Status StackTraverse(SqStack S,Status (*visit)() while(S.top!=S.
26、base) visit(-S.top);main() SElemType e; SqStack *Sa; clrscr(); printf(nn-SqStack Demo is running.-nn); printf(First is Push function.n); InitStack(&Sa); strcpy(e.name,stu1); strcpy(e.stuno,100001); e.age=80; e.score=1000; printf( Now Stack is Empty.n); StackTraverse(*Sa,StackPrintElem); Push(Sa,e);
27、printf( Now Stack has one element.n); StackTraverse(*Sa,StackPrintElem); strcpy(e.name,stu3); strcpy(e.stuno,100002); e.age=80; e.score=1000; Push(Sa,e); printf( Now Stack has another element.n); StackTraverse(*Sa,StackPrintElem); printf( Now Pop Stack,the top elem put into variable e.n); Pop(Sa,&e); printf(%sn%sn%dn%dn,e.name,e.stuno,e.age,e.score); printf( Lets see the left of Stacks
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1