《数据结构》 实验报告2

  • 格式:doc
  • 大小:470.50 KB
  • 文档页数:9

下载文档原格式

  / 9
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

xxx 实验报告

课程名称数据结构实验

名称

实验二栈和队列的应用

系部班级姓名学号

实验

时间2012 年10月15日21时30分~时分地点机位

评语

指导教师:

成绩

实验二栈和队列的应用

一、实验目的

1. 掌握栈的顺序表示和实现

2. 掌握队列的链式表示和实现

二、实验内容

1. 编写一个程序实现顺序栈的各种基本运算。

2. 实现队列的链式表示和实现。

三、实验步骤及结果

编写一个程序实现顺序栈的各种基本运算:

1.初始化顺序栈

2.插入元素

3.删除栈顶元素

4.取栈顶元素

5.置空顺序栈

实现队列的链式表示和实现:

1.初始化并建立链队列

2. 入链队列

3. 出链队列

4. 遍历链队列

四、程序主要语句及作用

1. 编写一个程序实现顺序栈的各种基本运算。

#include

#include

#define ElemType int

typedef struct Qnode

{ ElemType data;

struct Qnode *next;

}Qnodetype;

typedef struct

{ Qnodetype *front;

Qnodetype *rear;

}Lqueue;

void Lappend(Lqueue *q,int x)

{ Qnodetype *s;

s=(Qnodetype*)malloc(sizeof(Qnodetype));

s->data=x;

s->next=NULL;

q->rear->next=s;

q->rear=s;

}

void creat(Lqueue *q)

{ Qnodetype *h;

int i,n,x;

printf("shurujiangjianlilianduilieyuansudegeshu:n= ");

scanf("%d",&n);

h=(Qnodetype*)malloc(sizeof(Qnodetype));

h->next=NULL;

q->front=h;

q->rear=h;

for(i=1;i<=n;i++)

{ printf("lianduiliedi%dgeyuansudezhiwei:",i);

scanf("%d",&x);

Lappend(q,x);

}

}

ElemType Ldelete(Lqueue *q)

{ Qnodetype *p;

ElemType x;

if(q->front==q->rear)

{ printf("duilieweikong!\n");

x=0;

}

else

{ p=q->front->next;

q->front->next=p->next;

if(p->next==NULL)

q->rear=q->front;

x=p->data;

free(p);

}

return(x);

}

void display(Lqueue *q)

{ Qnodetype *p;

p=q->front->next;

printf("\nlianduilieyuansuyiciwei:");

while(p!=NULL)

{ printf("%d-->",p->data);

p=p->next;

}

printf("\n\nbianlilianduiliejieshu!\n");

}

main()

{ Lqueue *p;

int x,cord;

printf("\n*****diyicicaozuoqingxunzechushihuabingjianlilianduilie!*****\n ");

do

{ printf("\n lianduiliedejibencaozuo\n ");

printf("=========================================\n");

printf(" zhucaidan \n");

printf("=========================================\n");

printf(" 1 chushihua \n");

printf(" 2 rulianduilie \n");

printf(" 3 chulianduilie \n");

printf(" 4 bianlilianduilie \n");

printf(" 5 jieshuyunxing \n");

printf("==========================================\n");

scanf("%d",&cord);

switch(cord)

{ case 1:

{ p=(Lqueue *)malloc(sizeof(Lqueue));

creat(p);

display(p);

}break;

case 2:

{ printf("shurudulieyuansudezhi:x=");

scanf("%d",&x);

Lappend(p,x);

display(p);

}break;

case 3:

{ printf("chulianduilieyuansu:x=%d\n",Ldelete(p));

display(p);

}break;

case 4:

{display(p);}break;

case 5:

{exit (0);}

}

}while (cord<=5);

}