软件技术基础--上机实验三
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
姓名:吴振国学号:2011019190006
上机实验三
题目一:
一、程序流程说明
(1)创建一个链栈,(2)编写输出,进栈出站函数,(3)编写主函数
二、程序代码
#include
#include
#define LEN sizeof(node_type)
typedef struct node
{
int data;
struct node *next;
}node_type;
typedef struct {
node_type *top;
int length;
}lstack_type;
lstack_type *create_list(void)
{
lstack_type *p;
p=(lstack_type *)malloc(sizeof(lstack_type));
p->top=NULL;
p->length=0;
return(p);
}
void print(node_type *head)
{
node_type *p;
printf("Now ,those records are:\n");
p=head;
if(head!=NULL)
{
do{
printf("%4d",p->data);
p=p->next;
}
while(p!=NULL);
}
}
int push(lstack_type *lp, int x)
{ node_type *p;
p=(node_type *)malloc(LEN);
if(p!=NULL)
{ p->data = x;
p->next = lp->top;
lp-> top = p;
lp->length++;
return(1);
}
else return(0);
}
int pop(lstack_type *lp)
{
node_type *p;
int x;
if(lp->top==NULL){
printf("stack is underflow");
return(0);
}
else{
x = lp->top->data;
p = lp->top;
lp->top = lp->top->next;
lp->length--;
free(p);
return(true);
}
}
void main()
{
lstack_type *p1;
int a=0,b=0,m=0;
p1=create_list();
printf("\n input the numbers:\n");
scanf("%d",&m);
while(m!=0)
{ push(p1,m);
scanf("%d",&m);
}
print(p1->top);
pop(p1);
pop(p1);
print(p1->top);
}
三:典型测试数据(输入):2 4 6 9 43
应输出(上机前自己分析的结果):43 9 6 4 2出栈后6 4 2
四:上机时遇到的问题:①不能输出解决办法:更改函数返回类型
②编译出错解决办法:输入数据时“&”不要忘五:实际运行结果
六、小结&体会
链栈的使用很方便,比顺序栈更好操作。
题目二:
一、程序流程说明
(1)创建一个循环队列(2)编写出队列函数,入队列函数,和负数删除函数
(3)编写主函数
二、程序代码
#include
#define N 20
typedef struct
{ int data[N];
int front, rear;
}queue_type;
void create_list(queue_type *lp)
{
int i, elem;
lp->front=0;lp->rear=0;
printf("\n please input datas of the list\n"); lp->data[0]=NULL ;
for(i=1; i< N; i++)
{
scanf(" %d", &elem);
if(elem== -1) break;
lp->data[i]=elem;
lp->rear++;
}
}
void printlist(queue_type *lp)
{
int i,j=0;
printf("\nThese records are:\n");
if(lp->rear<=0)
{
printf("No data!\n");
return;
}
else