当前位置:文档之家› 软基第三次上机实验报告

软基第三次上机实验报告

软基第三次上机实验报告EX3.1一、程序流程说明1)链栈结点类型定义为:typedef struct node{int data;struct node *next;}node_type;2)编写进栈函数push3)编写出栈函数pop4)编写main函数,首先建立一空链栈;调用进栈函数,将从键盘输入的数据元素逐个进栈,输入0结束;显示进栈后的数据元素;调用两次出栈函数,显示出栈后的数据元素。

二、程序代码#include<stdio.h>#include<stdlib.h>#define true 1#define false 0typedef struct node{int data;struct node*next;}node_type;typedef struct{node_type*top;int length;}lstack_type;int push(lstack_type*lp,int x){node_type*p;p=(node_type*)malloc(sizeof(node_type));if(p!=NULL){p->data=x;p->next=lp->top;lp->top=p;}elsereturn 0;}void pop(lstack_type *lp){node_type*p;if(p==NULL){return;}else{p=lp->top;lp->top=lp->top->next;free(p);}}node_type*print(node_type*head){node_type*temp;temp=head;while(temp!=NULL){printf("%d\t",temp->data);temp=temp->next;}return 0;}void main(){int i,j;i=0;j=0;lstack_type M,*lp;lp=&M;lp->top=NULL;printf("Please input the data(end by '0'):\n");scanf("%d",&i);while(i!=0){push(lp,i);scanf("%d",&i);}printf("All data is:\n");print(lp->top);printf("\n The first pop:\n");pop(lp);print(lp->top);printf("\n The second pop:\n");pop(lp);print(lp->top);}三、测试数据输入:1 2 3 4 5 6 7 8 9 0输出:All data is:9 8 7 6 5 4 3 2 1The first pop is:8 7 6 5 4 3 2 1The second pop is:7 6 5 4 3 2 1四、上机时遇到的问题无五、实际运行结果六、心得体会要熟悉栈的使用EX3.2一、程序流程1)顺序循环队列类型定义为:#define N 20typedef struct{ int data[N];int front, rear;}queue_type;2)编写循环队列出队函数dequeue3)编写循环队列入队函数enqueue4)编写函数:void aa(queue_type *q);调用出对函数把队列q中的元素一一出对列,如果是负数直接抛弃;如果是正数,则调用入队函数,插入到q的队尾。

5)编写main函数,首先建立一个队列,其中的数据元素为:{2, 3, -4, 6, -5, 8, -9, 7, -10, 20};然后调用aa函数,并将aa函数调用前后队列的数据元素分别输出到屏幕上。

二、程序代码#include "stdio.h"#define MAXNUM 20#define true 1#define false 0typedef struct{ int data[MAXNUM];int front, rear;}queue_type;int enqueue(queue_type *q, int x){ if( (q->rear+1)%MAXNUM == q->front)return(false);else{q->data[q->rear]=x; /*新元素入队尾*/q->rear=(q->rear+1)%MAXNUM; /*移rear*/return(true);}}int dequeue(queue_type *q){ if(q->rear==q->front) /*判队列是否为空*/{ printf("queue is empty");return(0); /*返回空值*/}else{q->front=(q->front+1)%MAXNUM; return(q->data[q->front-1]);}}void aa(queue_type *q){int i,j=0,k; i=dequeue(q);while(j<10){if(i>0){ enqueue(q, i);j++;i=dequeue(q);}else{j++;i=dequeue(q);}}if(q->front>q->rear){for(i=q->front;i<MAXNUM-1;i++){printf("%d ",q->data[i]);}for(i=0;i<q->rear;i++){printf("%d ",q->data[i]);}}else{for(i=q->front-1;i<q->rear;i++){printf("%d ",q->data[i]); }}}void main(){queue_type M,*lp;int i,j,a[]={2, 3, -4, 6, -5, 8, -9, 7, -10, 20};lp=&M;lp->front=0;lp->rear=0;for(i=0;i<10;i++){ enqueue(lp,a[i]);}printf("调用函数前队列元素为:\n");for(i=0;i<10;i++){printf("%d ",lp->data[i]);}printf("\n调用函数后队列元素为:\n");aa(lp);}三、测试数据调用前:2 3 -4 6 -5 8 -9 7 -10 20调用后:2 3 6 8 7 20四、上机遇到的问题始终有点小错误没有消除五、实际运行结果六、心得体会要熟悉队列的知识EX3.3:一、程序流程说明书上第12题:1、创建两个栈公用一个以为数组空间S[m],他们的栈底分别设在一维数组的两端。

2、编写函数,取栈顶元素get(i),其中i为0或1,表示堆栈号。

二、程序代码#include<stdio.h>#include<string.h>#define m 100//创建两个栈公用一个以为数组空间S[m],他们的栈底分别设在一维数组的两端。

typedef struct stack_type{int stack[m];int top1;int top2;} stacktype;//出栈int get(int i,stacktype *s){int out;if(i==0){out=s->stack[s->top1];s->top1--;return out;}else{out=s->stack[s->top2];s->top2++;return out;}}int main(){stack_type s;int data;s.top1=0;s.top2=m-1;printf("Please enter the fitst stack:\n");while(scanf("%d",&data)&&data!=0)//输入元素,以0为结束{s.stack[s.top1]=data;s.top1++;}s.top1--;printf("Please enter the second stack:\n");while(scanf("%d",&data)&&data!=0)//输入元素,以0为结束{s.stack[s.top2]=data;s.top2--;}s.top2++;printf("The top of stack1 is:%d\n",get(0,&s));printf("The top of stack2 is:%d\n",get(1,&s));return 0;}三、输入与输出Please enter the fitst stack:1 2 3 0Please enter the second stack:-4 -3 -2 -1 0The top of stack1 is:3The top of stack2 is:-1请按任意键继续. . .四、上机遇到的问题1)问题:[Error] C:\Users\Administrator\Desktop\大二学习\软基上机\未命名3.cpp:33: error: base operand of `->' has non-pointer type `stack_type'原因:主函数中不能用->代替.解决办法:将该行更改为s.stack[count]=data;2)问题:输出The top of stack1 is:5177344The top of stack2 is:-346788601原因:top在输入完成以后指向了栈顶元素的下一个解决办法:利用top1--;和top2++;来调整其指向五、实际运行结果六、心得体会无EX3.4:一、程序流程说明书上第13题:1、创建一维数组Sq[m],存储循环队列的元素2、设立标志tag,区分头尾指针相同时队列是空是满2、编写此队列相对应的入队列和出队列函数二、程序代码#include<stdio.h>#include<stdlib.h>#include<string.h>#define N 20int tag = 0;//定义循环队列typedef struct{int data[N];int front, rear;} queue_type;//出队函数int dequeue(queue_type *q){int out;if(q->rear ==q ->front&&tag==0) printf("the queue is NULL!");else{out = q->data[q->front];q->front=(q->front+1)%N;}return(out);}//入队函数void enqueue(queue_type *q,int newnum){if(q->rear%N==q->front&&tag==1) printf("the queue is FULL!");else{q->data[q->rear]=newnum;q->rear=(q->rear+1)%N;if(q->rear<q->front)tag=0;}}//输出void showqueue(queue_type *q){int i;for(i=q->front; i!=q->rear; i=(i+1)%N)printf("%d ",q->data[i]);printf("\n");}int main(){queue_type q;int tag=0,data;q.front=0;q.rear=0;printf("enter the elements of the queue:\n");while(scanf("%d",&data)&&(data!=0))enqueue(&q,data);printf("the queue is:\n");showqueue(&q);printf("the queue is:\n");showqueue(&q);printf("deque:\n%d\n",dequeue(&q));return 0;}三、输入与输出enter the elements of the queue:1 2 3 4 5 0the queue is:1 2 3 4 5the queue is:1 2 3 4 5deque:1请按任意键继续. . .四、上机遇到的问题1)问题:[Error] C:\Users\Administrator\Desktop\大二学习\软基上机\未命名3.cpp:27: error: too few arguments to function `void enqueue(queue_type*, int)'原因:主函数调用enqueue时没有给予足够的参数,写成了enqueue(&q);解决办法:将该行更改为enqueue(&q,data);五、实际运行结果六、心得总结有了上一次编写链表题的经验以后,这次上机要顺利许多。

相关主题