数据结构实验指导手册_第1页
数据结构实验指导手册_第2页
数据结构实验指导手册_第3页
数据结构实验指导手册_第4页
数据结构实验指导手册_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

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

文档简介

1、实验一 线性表的顺序存储实验一、实验目的1、掌握用Visual C+6.0上机调试顺序表的基本方法2、掌握顺序表的基本操作,插入、删除、查找、以及有序顺序表的合并等算法的实现二、实验内容1、顺序表基本操作的实现问题描述 当我们要在顺序表的第i个位置上插入一个元素时,必须先将顺序表中第i个元素之后的所有元素依次后移一个位置,以便腾空一个位置,再把新元素插入到该位置。若是欲删除第i个元素时,也必须把第i个元素之后的所有元素前移一个位置。基本要求 要求生成顺序表时,可以键盘上读取元素,用顺序存储结构实现存储。实现提示 要实现基本操作,可用实现的基本操作,也可设计简单的算法实现。程序实现#includ

2、e <stdio.h>#include <conio.h>typedef int DataType ;# define maxnum 20typedef structint datamaxnum;int length;SeqList;/*插入函数*/int insert(SeqList *L , int i , DataType x)/* 将新结点x插入到顺序表L第i个位置 */ int j ;if( i<0 | i>(*L).length +1) printf(" n i 值不合法 ! "); return 0;if(* L).leng

3、th >=maxnum-1) printf(" n 表满不能插入!"); return 0; for(j=(*L).length;j>=i;j-) (*L).dataj+1=(*L).dataj;(*L).datai = x;(*L).length+;return 1;/*删除函数*/int delete( SeqList *L ,int i) /*从顺序L中删除第i个结点*/ int j ;if( i<0| i>(*L).length ) printf(" n 删除位置错误 ! ") ;return 0;for(j=i+1;j&

4、lt;=(*L).length;j+)(*L).dataj-1 =(*L).dataj;(*L).length-;return 1;/*生成顺序表*/void creatlist(SeqList * L) int n , i , j ;printf("请输入顺序表 L 的数据个数:n") ;scanf("%d" , &n) ;for(i=0 ; i<n ; i+) printf("data%d =" , i) ; scanf("%d",&(*L).datai);(*L).length=n-1;

5、printf("n") ;/*creatlist */*输出顺序表 L*/printout(SeqList * L) int i ;for (i=0 ; i<=(* L).length ; i+) printf(" data%d=", i) ; printf("%d", (*L).datai);/*printout */printf("n");main() SeqList *L ;char cmd ;int i , t , x;clrscr() ;creatlist(L);doprintf("ni

6、, I - 插入n") ;printf("d , D - 删除n") ;printf("q , Q - 退出n") ;docmd=getchar() ;while(cmd!='i')&&(cmd!='I')&&(cmd!='d')&&(cmd!='D')&&(cmd!='q')&&(cmd!='Q');switch(cmd) case 'i': case

7、'I':printf("nPlease input the DATA: ");scanf("%d",&x) ;printf("nWhere? ");scanf("%d",&i) ;insert(L,i,x) ;printout(L);break ;case 'd':case 'D' :printf("nWhere to Delete? ");scanf("%d",&i);delete(L,i);print

8、out(L);break ;while(cmd!='q')&&(cmd!='Q');2、有序顺序表的合并问题描述 已知顺序表la和lb中的数据元素按非递减有序排列,将la和lb表中的数据元素,合并成为一个新的顺序表lc基本要求 lc中的数据元素仍按非递减有序排列,并且不破坏la和lb表程序实现# include <stdio.h># define maxnum 20typedef int DataType ;typedef struct DataType datamaxnum ; int length ;SeqList ;int Me

9、rgeQL(SeqList la , SeqList lb , SeqList *lc) int i , j , k ;if (la.length+1 + lb.length+1>maxnum) printf("narray overflow!") ;return 0;i=j=k=0;while(i<=la.length && j<=lb.length) if (la.datai<=lb.dataj)lc->datak+=la.datai+ ;else lc->datak+=lb.dataj+;/* 处理剩余部分 */wh

10、ile (i<=la.length) lc->datak+=la.datai+;while (j<=lb.length) lc->datak+=lb.dataj+;lc->length=k-1;return 1;main() SeqList la=3,4,7,12,15,4 ;SeqList lb=2,5,7,15,18,19,5 ;SeqList lc ;int i ;if (MergeQL(la,lb,&lc) printf("n") ;for(i=0;i<=lc.length ; i+)printf("%4d&qu

11、ot;,lc.datai); 实验二 单链表实验一、实验目的1、掌握用Visual C+6.0上机调试单链表的基本方法2、掌握单链表的插入、删除、查找、求表长以及有序单链表的合并算法的实现二、实现内容1、单链表基本操作的实现问题描述要在带头结点的单链表h中第i个数据元素之前插入一个数据元素x ,首先需要在单链表中寻找到第i-1个结点并用指针p指示,然后申请一个由指针s 指示的结点空间,并置x为其数据域值,最后修改第i-1个结点,并使x结点的指针指向第i个结点,要在带头结点的单链表h中删除第i个结点,首先要计数寻找到第i个结点并使指针p指向其前驱第i-1个结点,然后删除第i个结点并释放被删除结点

12、空间。基本要求用链式存储结构实现存储实现提示链式存储结构不是随机存储结构,即不能直接取到单链表中某个结点,而要从单链表的头结点开始一个一个地计数寻找。程序实现# include <stdio.h ># include <malloc.h >typedef char DataType ;typedef struct node DataType data; /*结点的数据域*/ struct node *next; /*结点的指针域*/ListNode;void Init_List(ListNode *L)(*L)=(ListNode *)malloc(sizeof(Lis

13、tNode);/*产生头结点*/(*L)->next=NULL;int List_Length(ListNode *L )int n=0;ListNode *p=L->next;while(p!=NULL)n+;p=p->next;return n;ListNode* GetNode(ListNode *L,int i) int j; ListNode *p; p=L;j=0; /*从头结点开始扫描*/ while(p->next&&j!=i) /*顺指针向后扫描,直到p->next为NULL或i=j为止*/ p=p->next; j+; i

14、f(i=j) return p; /*找到了第i个结点*/ else return NULL; /*当i<0或i>0时,找不到第i个结点*/ void InsertList(ListNode *L,DataType x,int i) ListNode *p,*s; p=GetNode(L,i-1); /*寻找第i-1个结点*/ if (p=NULL) /*i<1或i>n+1时插入位置i有错*/ printf("position error"); return ; s=(ListNode *)malloc(sizeof(ListNode); s->

15、;data=x;s->next=p->next;p->next=s; void DeleteList(ListNode *L ,int i)ListNode *p,*r;p=GetNode(L,i-1); /*找到第i-1个结点*/if (p=NULL|p->next=NULL) /*i<1或i>n时,删除位置错*/ printf("position error"); return ; r=p->next; /*使r指向被删除的结点a*/p->next=r->next; /*将ai从链上删除*/free(r); /*使用

16、头插法建立带头结点链表算法*/ListNode * CreatListF(void)char ch;ListNode *head=(ListNode *)malloc(sizeof(ListNode); /*生成头结点*/ListNode *s; /*工作指针*/head->next=NULL; ch=getchar(); /*读入第1个字符*/while(ch!='n') s=(ListNode *)malloc(sizeof(ListNode); /*生成新结点*/ s->data=ch; /*将读入的数据放入新结点的数据域中*/ s->next=head

17、->next; head->next=s; ch=getchar(); /*读入下一字符*/return head; /*使用尾插法建立带头结点链表算法*/ListNode * CreatListR1(void) char ch; ListNode *head=(ListNode *)malloc(sizeof(ListNode); /*生成头结点*/ ListNode *s,*r; /*工作指针*/ r=head; /*尾指针初值也指向头结点*/ while(ch=getchar()!='n') s=(ListNode *)malloc(sizeof(ListNo

18、de); s->data=ch; r->next=s; r=s; r->next=NULL; /*终端结点的指针域置空,或空表的头结点指针域置空*/ return head; /*复制链表A中的内容到表B中*/ void copy(ListNode *a, ListNode *b) ListNode *pa=a->next; ListNode *u; ListNode *rb=b; while(pa!=NULL) u=( ListNode *)malloc(sizeof(ListNode); u->data=pa->data; rb->next=u;

19、rb=u; pa=pa->next;rb->next=NULL;/*输出带头结点的单链表*/void DisplaySL(ListNode *la, char *comment) ListNode *p ; p=la->next ; if(p) printf("n%sn" , comment) ; while(p)printf("%4c",p->data);p=p->next; printf("n") ;/*主函数*/main( ) ListNode *la ,*lb,*lc, *p ;int n,x,i

20、;printf("n用头插法建立链表la,请输入节点内容:");la=CreatListF();DisplaySL(la,"新生成链la节点内容:"); printf("n链表la的长度: %2d",List_Length(la);printf("n请输入要插入的元素: ");scanf("%c",&x) ;printf("n请输入要插入的位置:");scanf("%d",&i) ;InsertList(la,x,i);DisplaySL(

21、la,"插入后链la节点内容");printf("n请输入要删除元素的位置:");scanf("%d",&i);DeleteList(la,i);DisplaySL(la, "删除后链la节点内容");printf("n用尾插法建立链表lb,请输入节点内容:");fflush(stdin);lb=CreatListR1();DisplaySL(lb,"新生成链lb节点内容:"); Init_List(&lc);copy(la,lc);DisplaySL(lc,

22、"复制生成的链lc节点内容:"); 2、有序单链表的合并问题描述 已知单链表la和lb中的数据元素按非递减有序排列,将la和lb中的数据元素,合并为一个新的单链表lc,lc中的数据元素仍按非递减有序排列。基本要求 不破坏la表和lb表的结构。程序实现# include <stdio.h>#include<alloc.h>#define NULL 0typedef int DataType;typedef struct SLNode DataType data; struct SLNode * next; slnodetype;int MergeSL(

23、slnodetype *la,slnodetype *lb,slnodetype *lc);int CreateSL(slnodetype *la,int n);void DisplaySL(slnodetype *la , char * comment);main( ) slnodetype *la, *lb, *lc ,*p;int n,m;la=(slnodetype *)malloc(sizeof(slnodetype);la->next=NULL;lb=(slnodetype *)malloc(sizeof(slnodetype);lb->next=NULL;lc=(sl

24、nodetype *)malloc(sizeof(slnodetype);lc->next=NULL;printf("n 输入链la节点数:");scanf("%d",&n);printf("n 输入链la 节点内容:");CreateSL(la,n);DisplaySL(la,"链la 节点内容:");printf("n 输入链lb节点数:");scanf("%d",&m);printf("n 输入链lb节点内容:");Create

25、SL(lb,m);DisplaySL(lb,"链lb 节点内容:");if(MergeSL(la,lb,&lc) DisplaySL(lc,"合成后的链lc:");getchar();int MergeSL(slnodetype * la , slnodetype *lb,slnodetype * *lc) slnodetype * pa, * pb, * pc; lc=(slnodetype *)malloc(sizeof(slnodetype); pa=la->next; pb=lb->next; pc= *lc; while(p

26、a&&pb) pc->next=(slnodetype*)malloc(sizeof(slnodetype);pc=pc->next; if(pa->data<=pb->data) pc->data=pa->data; pa=pa->next; else pc->data=pb->data; pb=pb->next; while (pa) /*插入la链的剩余段 */ pc->next=(slnodetype*)malloc(sizeof(slnodetype); pc=pc->next;pc->

27、;data=pa->data; pa=pa->next; /*插入lb链的剩余段*/ while(pb) pc->next=(slnodetype*)malloc(sizeof(slnodetype); pc=pc->next;pc->data=pb->data; pb=pb->next; /*生成单链表*/int CreateSL(slnodetype *la ,int n) int i ;slnodetype *p , *q ;q=la ;for (i=1 ; i<=n ; i+) p=(slnodetype *)malloc(sizeof(

28、slnodetype);scanf("%d" , &p->data) ;q->next=p;q=p;q->next=NULL ;return 1 ;/*输出单链表*/void DisplaySL(slnodetype *la, char *comment) slnodetype *p ;p=la->next ;if(p) printf("n%sn" , comment) ;while(p) printf("n%3d" , p->data);p=p->next ;printf("n&

29、quot;) ;实验三 循环链表实验一、实验目的1、掌握用Visual C+6.0上机调试循环链表的基本方法2、进一步掌握循环单链表的插入、删除、查找算法的实现二、实现内容1. 约瑟夫环问题问题描述设有N个人围坐一圈,现从某个人开始报数,数到M的人出列,接着从出列的下一个人开始重新报数,数到M的人以出列,如此下去,直到所有人都出列为此。试设计确定他们的出列次序序列的程序。基本要求 选择单向循环链表作为存储结构模拟整个过程,并依次输出列的各人的编号。实现提示 程序运行之后,首先要求用户指定初始报数的下限值,可以n<=30,此题循环链表可不设头节点,而且必须注意空表和非空表的界限。如 n=8

30、, m=4 时,若从第一个人, 设每个人的编号依次为 1,2,3,开始报数,则得到的出列次序为4,8,5,2,1,3,7,6,如下图所示,内层数字表示人的编号 ,每个编号外层的数字代表人出列的序号。程序实现#include <stdio.h>#include <malloc.h>typedef struct node int num; struct node *next; linklist;linklist *creat(head,n) /*使n个人围成一圈,并给每个人标识号数*/ linklist *head; int n ; linklist *s,*p; int

31、i; s=(linklist * )malloc(sizeof(linklist); head=s; s->num=1; p=s; for(i=2;i<=n; i+) s=(linklist *) malloc(sizeof(linklist); s->num=i; p->next=s; p=s; p->next=head; return(head); /* creat */linklist * select(linklist *head, int m) linklist *p, *q; int i, t; p=head; t=1; q=p; /* q为p的前趋指

32、针*/ p=p->next; do t=t+1 ; /*报一次数*/ if(t%m=0) printf("%4d", p->num); q->next=p->next; free(p); p=q->next; else q=p; p=p->next; while(q!=p); head=p; printf("%4d",p->num); return (head); /* select */main( ) int n,m; linklist *head; printf("ninput the total

33、number:n="); scanf("%d", &n); printf("ninput the number to call:m="); scanf("%d", &m); head=creat(head,n); head=select(head,m); printf("nthe last one is :%d", head->num); /* main */思考题:编程实现两个循环单链表的合并。实验四 栈、队列的实现及应用一、实验目的1、掌握栈和队列的顺序存储结构和链式存储结构,以

34、便在实际背景下灵活运用。2、掌握栈和队列的特点,即先进后出与先进先出的原则。3、掌握栈和队列的基本操作实现方法。二、实验内容1、实现栈的顺序存储# define MAXSIZE 100 typedef int ElemType;typedef struct ElemType dataMAXSIZE; int top;SeqStack; void InitStack(SeqStack *s) s->top=0; return 1;int StackEmpty(SeqStack *s) if(s->top=0) return 1; else return 0;int StackFull

35、(SeqStack *s) if(s->top=MAXSIZE-1) return 1; else return 0; void Push(SeqStack *s,int x) if (StackFull(s) printf("the stack is overflow!n"); return 0; else s->datas->top=x; s->top+; void Display(SeqStack *s) if(s->top=0) printf("the stack is empty!n"); else while(s

36、->top!=0) printf("%d->",s->datas->top); s->top=s->top-1; ElemType Pop(SeqStack *s) if(StackEmpty(s) return 0; else return s->data-s->top; ElemType StackTop(SeqStack *s) int i;if(StackEmpty(s) return 0; else i=s->top-1;return s->datai; /*返回栈顶元素的值,但不改变栈顶指针*/ mai

37、n(SeqStack *p) int n,i,k,h,x1,x2,select; printf("create a empty stack!n"); InitStack(p); printf("input a stack length:n"); scanf("%d",&n); for(i=0;i<n;i+) printf("input a stack value:n"); scanf("%d",&k); Push(p,k); printf("select 1:Di

38、splay()n"); printf("select 2:Push()n"); printf("select 3:Pop()n"); printf("select 4:StackTop()n"); printf("input a your select(1-4):n"); scanf("%d",&select); switch(select) case 1:display(p); break; case 2:printf("input a push a value:n

39、"); scanf("%d",&h); Push(p,h); display(p); break; case 3:x1=Pop(p); printf("x1->%dn",x1); display(p); break; case 4:x2=StackTop(p);printf("x2->%d",x2);break; 2、利用栈实现数制转换 # define MAXSIZE 100typedef int ElemType; /*将顺序栈的元素定义为整型*/typedef struct ElemType dat

40、aMAXSIZE; int top;SeqStack; void InitStack(SeqStack *s) s->top=0; return 1;int StackEmpty(SeqStack *s) if(s->top=0) return 1; else return 0;int StackFull(SeqStack *s) if(s->top=m-1) return 1; else return 0; void Push(SeqStack *s,int x) if (StackFull(s) printf("the stack is overflow!n&q

41、uot;); return 0; else s->datas->top=x; s->top+; ElemType Pop(SeqStack *s) ElemType y; if(StackEmpty(s) printf("the stack is empty!n"); return 0; else y=s->datas->top; s->top=s->top-1; return y; ElemType StackTop(SeqStack *s) if(StackEmpty(s) return 0; else return s->

42、;datas->top;void Dec_to_Ocx (int N) /* n是非负的十进制整数,输出等值的八进制数*/SeqStack *S; /*定义一个顺序栈*/ElemType x; Init_SeqStack(S); /*初始化栈*/if(N<0) printf("nError,The number must be over 0。"); return; if(!N) Push(S,0);while(N) /*自右向左产生八进制的各位数字,并将其进栈*/ Push(S,N%8); /*余数入栈 */ N=N/8; /*商作为被除数*/ printf(&

43、quot;Number %d converted to OCT:",N);while(StackEmpty(S) /*栈非空时退栈输出*/ x=Pop(S); printf(“%d”,x); printf("n"); main( ) int n; printf("Input a number to convert to OCT:n");scanf("%d",&n);Dec_to_Ocx (n);3、实现循环队列的顺序存储#define maxsize 100typedef structint datamaxsize;

44、 int front; int rear;seqqueue;int sqinit(seqqueue *p)p->front=0;p->rear=0;return 1;int enqueue(seqqueue *q, int e) if(q->rear+1)%maxsize=q->front) return 0; else q->dataq->rear=e; q->rear=(q->rear+1)%maxsize; return 1; int dequeue(seqqueue *q) int e;if (q->front=q->rear

45、)return 0;e=q->dataq->front;q->front=(q->front+1)%maxsize;return e;int empty(seqqueue *q) int v; if (q->front=q->rear) v=1; else v=0; return v; int gethead(seqqueue *q) int e; if (q->front=q->rear) e=-1; else e=q->dataq->front; return e; void display(seqqueue *q) int s;

46、 s=q->front; printf("the sequeue is display:n"); if (q->front=q->rear) printf("the sequeue is empty!"); else while(s<q->rear) printf("->%d", q->datas);s=(s+1)%maxsize;printf("n"); main(seqqueue *head) int n,i,m,x,y,select,xq; printf("

47、create a empty sequeuen"); sqinit(head); printf("please input the sequeue length:n"); scanf("%d",&n); for (i=0;i<n;i+) printf("please input a sequeue value:n"); scanf("%d",&m); enqueue(head,m); printf("head->rear:%dn",head->rear

48、); printf("head->front:%dn",head->front); display(head); printf("select 1 * enqueue() n"); printf("select 2 * dequeue() n"); printf("select 3 * empty () n"); printf("select 4 * gethead() n"); printf("select 5 * display() n"); printf(&

49、quot;please select (1-5):"); scanf("%d",&select); switch(select) case 1: printf("please input a value :n "); scanf("%d",&x); enqueue(head,x); display(head); break; case 2: dequeue(head); display(head); break; case 3: if(empty(head) printf("the sequeue

50、is empty"); else printf("the sequeue is full"); case 4: y=gethead(head); printf("output head value:%dn",y); break; case 5: display(head); break;实验五 串及数组的实验一、实验目的1、了解串及数组的两种存储方法,掌握数组在作为存储结构中的地址计算方法。2、了解稀疏矩阵的两种压缩存储方法的特点和适用范围,领会稀疏矩阵运算采用的处理方法。二、实验内容1、顺序串的基本操作 #define MaxSize 100

51、 typedef struct char strMaxSize; int len; strtype; void assign(strtype *s,char t) int i=0; while (ti!='0') s->stri=ti;i+; s->stri='0' s->len=i; void strcopy(strtype *s,strtype t) int i; for (i=0;i<=t.len;i+) s->stri=t.stri; int length(strtype s) return(s.len); int equa

52、l(strtype s,strtype t) int i=0; if (s.len!=t.len) return(0); else for (i=0;i<s.len;i+) if (s.stri!=t.stri) return(0);return(1); strtype concat(strtype s,strtype t) strtype r; int i,j; for (i=0;i<s.len;i+) r.stri=s.stri; for (j=0;j<=t.len;j+) r.strs.len+j=t.strj; r.len=i+j; return(r); int index(strtype s,strtype t) int i,j,k; for (i=0;s.stri;i+) for (j=i,k=0;s.strj=t.strk;j+,k+) if (!t.strk+1) return(i); return(-1); strtype substr(strtype s,int i,int k) strtype t; int j; for (j=i;j<i+k;j+) t.strj-i=s.strj; t.len=k; t.strt.len='0' return(t); voi

温馨提示

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

最新文档

评论

0/150

提交评论