数据结构实验指导书
- 格式:doc
- 大小:87.50 KB
- 文档页数:27
《数据结构》实验指导书注意事项:在磁盘上创建一个目录,专门用于存储数据结构实验的程序。
实验一顺序表操作一、实验目的1、掌握使用Turbo C2.0上机调试线性表的基本方法;2、掌握线性表的基本操作:插入、删除、查找以及线性表合并等运算在顺序存储结构和链接存储结构上的运算。
二、实验要求1、认真阅读和掌握本实验的程序。
2、上机运行本程序。
3、打印出程序的运行结果,并结合程序进行分析。
4、按照你对线性表的操作需要,重新改写主程序并运行,打印出文件清单和运行结果三、实验内容程序:线性表基本操作的实现这个程序中演示了顺序表的创建、插入、删除和查找和合并。
程序如下:#include<stdio.h>#include<malloc.h>#include<conio.h>#define ERROR 0#define OK 1#define EQUAL 1struct STU{char name[20];char stuno[10];int age;int score;}stu[50];typedef struct STU ElemType;struct LIST{ElemType elem[50];int length;};typedef struct LIST List;int init(List **L){*L=(List *)malloc(sizeof(List));(*L)->length=0;}int ListLength(List *L){return L->length;}void GetElem(List L,int i,ElemType *e) {*e=L.elem[i];}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,int type) {int i;switch (type){case EQUAL:for(i=0;i<La->length;i++)if(EqualList(&La->elem[i],&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;i<Lb_len;i++){GetElem(*Lb,i,&e);if(!LocateElem(La,e,EQUAL))ListInsert(La,++La_len,e);}}int printlist(List L){int i;printf("name stuno age score\n");for(i=0;i<L.length;i++)printf("%-10s %s\t%d\t%d\n", L.elem[i].name, L.elem[i].stuno, L.elem[i].age, L.elem[i].score);printf("\n");}int ListInsert(List *L,int i,struct STU e){ struct STU *p,*q;if (i<1||i>L->length+1) return ERROR;q=&(L->elem[i-1]);for(p=&L->elem[L->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("\n\n-------------------List Demo is running...----------------\n\n");printf("First is InsertList function.\n");init(&La);strcpy(,"stu1");strcpy(e.stuno,"100001");e.age=80;e.score=1000;ListInsert(La,1,e);strcpy(,"stu2");strcpy(e.stuno,"100002");e.age=80;e.score=1000;ListInsert(La,2,e);printlist(*La);printf("List A length now is %d.\n\n",La->length);getch();strcpy(,"stu3");strcpy(e.stuno,"100003");e.age=80;e.score=1000;ListInsert(La,3,e);printlist(*La);printf("List A length now is %d.\n\n",La->length); getch();init(&Lb);strcpy(,"zmofun");strcpy(e.stuno,"100001");e.age=80;e.score=1000;ListInsert(Lb,1,e);strcpy(,"bobjin");strcpy(e.stuno,"100002");e.age=80;e.score=1000;ListInsert(Lb,2,e);strcpy(,"stu1");strcpy(e.stuno,"100001");e.age=80;e.score=1000;ListInsert(Lb,3,e);printlist(*Lb);printf("List B length now is %d.\n\n",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.\n\n",La->length);getch();clrscr();}实验二单链表操作一、实验目的掌握握单链表的基本操作:插入、删除、查找等运算。
二、实验要求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 LISTINCREMENT 10struct STU{char name[20];char stuno[10];int age;int score;}stu[50];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=NULL;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||j>1) return ERROR;*e=p->data;return OK;}int EqualList(ElemType *e1,ElemType *e2){if (strcmp(e1->name,e2->name)==0)return 1;elsereturn 0;}int Less_EqualList(ElemType *e1,ElemType *e2){if (strcmp(e1->name,e2->name)<=0)return 1;elsereturn 0;}int LocateElem(LinkList La,ElemType e,int type){int i;LinkList p;p=La;switch (type){case EQUAL:while(p->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;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 score\n");while(p->next){p=p->next;printf("%-10s %s\t%d\t%d\n", p->, p->data.stuno, 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||j>i-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;clrscr();printf("\n\n-------------------List Demo is running...----------------\n\n");printf("First is InsertList function.\n"); init(&La);strcpy(,"stu1");strcpy(e.stuno,"100001");e.age=80;e.score=1000;ListInsert(La,1,e);strcpy(,"stu3");strcpy(e.stuno,"100002");e.age=80;e.score=1000;ListInsert(La,2,e);printlist(La);getch();strcpy(,"stu5");strcpy(e.stuno,"100003");e.age=80;e.score=1000;ListInsert(La,3,e);printlist(La);getch();init(&Lb);strcpy(,"stu2");strcpy(e.stuno,"100001");e.age=80;e.score=1000;ListInsert(Lb,1,e);strcpy(,"stu4");strcpy(e.stuno,"100002");e.age=80;e.score=1000;ListInsert(Lb,2,e);strcpy(,"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();printf("\n\n\n\n\n\nWelcome to visit !\n\n\n\n\n\n\n");}实验三栈的基本操作和应用一、实验目的掌握栈的基本操作:初始化栈、判栈为空、出栈、入栈等运算。