数据结构——栈与队列

ps:文中题目来源为uestc数据结构上机实验,代码均为原创

ex3_1:链栈——基本题

1)链栈结点类型定义为:
typedef struct node
{
int data;
struct node *next;
}node_type;
2)编写进栈函数push
3)编写出栈函数pop
4)编写main函数,首先建立一空链栈;
调用进栈函数,将从键盘输入的数据元素逐个进栈,输入0结束;显示进栈后的数据元素;
调用两次出栈函数,显示出栈后的数据元素。

ex3_2:循环队列——基本题

1)顺序循环队列类型定义为:
#define N 20
typedef struct
{ int data[N];
int front, rear;
}queue_type;
2)编写循环队列出队函数dequeue
3)编写循环队列入队函数enqueue
4)编写函数:void aa(queue_type *q);
其功能为,调用出队函数把队列q中的元素一一出队,如果是负数直接抛弃;如果是正数,则调用入队函数,插入到q的队尾。
5)编写main函数,首先建立一个队列,其中的数据元素为:{2, 3, -4, 6, -5, 8, -9, 7, -10, 20};然后调用aa函数,并将aa函数调用前后队列的数据元素分别输出到屏幕上。

ex3_3:教材第一章习题12题——扩展题

1)两个栈共用一个数组空间,它们的栈底分别在数组两端,栈顶相向而行。编写入栈和出栈函数,实现两个栈元素分别的(但共用)入栈和出栈。
2)main中函数完成以下测试:
a、能否在共用空间上实现两个独立的栈:即能否向两个栈分别输入元素;能否分别从两个栈取出元素,每个栈取出的元素的顺序符合各自栈的特点
b、能否在共用空间用满时,及时制止新的入栈行为。
例如:
假设数组大小为6,main函数实现以下动作,向栈1接连入栈4个元素后,向栈2入栈2个元素致栈满,再向栈2输入一个元素,将报错。接着从栈1出栈1个元素,再向栈2入栈,就会成功。最后,两个栈分别出空,观察输出顺序是否满足栈的特点。

ex3_4:教材第一章习题13题——扩展题

1)实现一种扩展的循环队列,使得全部的数组空间都能使用,基本思路是当传统循环队列放满时:即
(rear+1)%MAXNUM=front 为真
时,可以再入队一个元素,接着rear = (rear+1)%MAXNUM后就会与front相等,此时将另外一个变量flag设置为1,表示此时的rear=front不是为空,而是满。否则flag为0时,如果出现rear==front,则表示队列为空。
2)main()函数实现以下测试:
a、能否实现全部“装满”,即装入元素个数为MAXNUM
b、能否按照循环队列那样绕着存放空间循环存放。
c、能否在装满后,拒绝再装。
d、能否在装满后,不会变成“空”的——即可以还可正常出队。
e、能否在全部出空后,不会变成“满”的——即可还可正常入队。
例如:
假设循环队列最大空间为5,main()函数实现以下动作,接连成功入队5个元素,入队第6个元素时,报错。接着出队3个元素,入队3个元素,均成功。再入队1个,报错。继续连续成功出队6个元素,出队第7个时报错。最后,再成功入队2个元素

ex3-5——教材(第5版例5)扩展题(选做)

计算并输出二项展开式(a+b)n的各项系数,即求一个杨辉三角形的最下面一层所有元素。
本题算法难点在利用杨辉三角形计算原理,不断利用队列在上一层元素的基础上,求出下一层元素。

#include<stdio.h>
#include<malloc.h>
#define MAX 20
typedef struct node{		
	int  data;		
	struct node *next;    
}node,*stacklink; 
typedef struct {	
	int data[5];	
	int fullflag;	
	int front,rear;
}nbqueue; 
typedef struct dhStack{
    int top1;   
    int top2;    
    int data[MAX];	
}dhstack; 
typedef struct{
     int data[MAX];	  
     int front, rear;
}queue;
void dhstack_init(dhstack &ds){	
	ds.top1=0;	
	ds.top2=MAX-1;	
	for(int i=0;i<MAX;i++){		
		ds.data[i]=0;	
		}
} 
void dhstack_push(dhstack &ds,int n,int e){
	if(ds.top1>ds.top2) {	
		printf("full");		
		return;
		}	
	if(n==1){ 		
		ds.data[ds.top1]=e;		
		ds.top1++;	
	}else if(n==2){		
		ds.data[ds.top2]=e;		
		ds.top2--;	
	}else{		
		printf("error");		
		return;	
	}
} 
void dhstack_pop(dhstack &ds,int n,int e){
	if(n==1){	
		if(ds.top1==0)	{		
			printf("empty");			
			return;		
			}		
		e=ds.data[ds.top1];		
		ds.top1--;	
	}else if(n==2)	{	
		if(ds.top2==MAX-1){		
			printf("empty");			
			return;		
		}		
		e=ds.data[ds.top2];		
		ds.top1++; 	
	}else{		
		printf("error");		
		return;	
	}
}
void dhstack_show(dhstack ds){
	int i;	
	printf("stack1 top to base:");	
	for(i=ds.top1-1;i>=0;i--)		
		printf("%d ",ds.data[i]);	
	printf("\nstack2 top to base;");	
	for(i=ds.top2+1;i<MAX;i++)	
		printf("% d",ds.data[i]);	
		printf("\n");
} 
void nbqueue_init(nbqueue &q){	
	for(int i=0;i<MAX;i++){	
		q.data[i]=0;	
	}	
	q.front=0;	
	q.rear=0;	
	q.fullflag=0;
}
 void nbqueue_inqueue(nbqueue &q,int e){
 	if((q.front==q.rear)&&q.fullflag==1) {	
 		printf("Full");		
 		return;	
 	}	
 	q.data[q.rear%5]=e;	
 	q.rear=(q.rear+1)%5;	
 	if(q.front==q.rear)		
 		q.fullflag=1; 
 } 
 void nbqueue_dequeue(nbqueue &nq,int &e){
 	if(nq.front==nq.rear&&nq.fullflag==0){	
 		printf("empty");		
 		return;	
 	}	
 	e=nq.data[nq.front];	
 	nq.front=(nq.front+1)%5;	
 	if(nq.fullflag==1)	
 		nq.fullflag=0; 
 } 
void nbqueue_show(nbqueue &q){	
	int i;	
	if(q.rear>q.front){	
		for(i=q.front;i<q.rear;i++)		
		printf("%d ",q.data[i]);	
		printf("\n");	
	}
	if(q.rear<=q.front)		
		for(i=q.front;i<q.rear+5;i++)			
			printf("%d ",q.data[i%5]);		
	printf("\n");
} 
void queue_init(queue &q){
	for(int i=0;i<MAX;i++)	{		
		q.data[i]=0;	
	}	
	q.front=0;	
	q.rear=0;
}  
void inqueue(queue &q,int e){
	if((q.rear+1)%MAX==q.front)
		return;
	q.data[q.rear%MAX]=e;	
	q.rear=(q.rear+1)%MAX; 
}
void dequeue(queue &q,int &e){
	if(q.rear==q.front) return;	
	e=q.data[q.front];	
	q.front=(q.front+1)%MAX;
} 
void queue_show(queue q){
	int i;	
	if(q.rear>q.front){	
		for(i=q.front;i<q.rear;i++)		
			printf("%d ",q.data[i]);		
		printf("\n");	
	}	
	if(q.rear<q.front)	
		for(i=q.front;i<q.rear+MAX;i++)		
			printf("%d ",q.data[i%MAX]);		
		printf("\n");
	} 
int init(stacklink &S){
	S=(stacklink)malloc(sizeof(node));	
	if(!S) return 0;	
	S->next=NULL;	
	return 1;	
}
void push(stacklink &s,int e){
	stacklink newnode;
	newnode=(stacklink)malloc(sizeof(stacklink));	
	newnode->next=s;	
	newnode->data=e;	
	s=newnode;
}  
int pop(stacklink &S,int &e){	
	stacklink p;		
	if(S->next==NULL) return 0;	
	p=S;		
	S=S->next;		
	e=p->data;		
	return 1;
}
void show(stacklink s){
	printf("top to base:");	
	while(s->next!=NULL){	
		printf("%d ",s->data);		
		s=s->next;	
	}	
	printf("\n");
} 
void aa(queue &q){
	int e;	
	int length;	
	if(q.rear>=q.front)	
		length=q.rear-q.front;	
	else	
		length=q.rear-q.front+MAX;	
	for(int i=0;i<length;i++){	
		dequeue(q,e);	
		if(e>0)		
			inqueue(q,e);	
	}
} 
void yanghui(int n){
	int i=0;	
	int it=1,next=1;	
	queue q;	
	queue_init(q);	
	inqueue(q,0);	
	inqueue(q,1);	
	inqueue(q,0);	
	for(i=0;i<n;i++){	
		next=1;		
		while(next!=0)	{		
			dequeue(q,it);			
			next=q.data[q.front];
			inqueue(q,it+next);		
		}		
		inqueue(q,0);	
	}	
	queue_show(q);	
}
void main(){
	printf("下面会生成一个链栈并进行两次出栈操作\n");	
	stacklink s;	
	init(s);	
	int e;	
	while(1){	
		scanf("%d",&e);	
		if(e==0) break;	
			push(s,e);	
	}	
	show(s); 	
	pop(s,e);	
	printf("%d\n",e);	
	pop(s,e);	
	printf("%d\n",e);	
	show(s); 		
	printf("下面会创建一个队列并显示");	
	queue q;	
	queue_init(q);	
	while(1){	
		scanf("%d",&e);		
		if(e==0) break;		
		inqueue(q,e);	
	}	
	queue_show(q); 	
	printf("下面进行一次出队");	
	dequeue(q,e);	
	queue_show(q);		
	printf("调用出队函数把队列q中的元素一一出队,如果是负数直接抛弃;如果是正数,则调用入队函数,插入到q的队尾。\n");	
	aa(q);	
	queue_show(q); 
	printf("创建一个二合一的栈,每次的输入格式为:栈的序号 元素\n");	
	int n;	
	dhstack ds;	
	dhstack_init(ds);	
	while(1){	
		scanf("%d %d",&n,&e);		
		if(e==0) break;		
		dhstack_push(ds,n,e);	
	}	
	dhstack_show(ds); 	
	printf("下面会创建一个可以占满所有空间的循环队列,max=5\n");	
	nbqueue nq;	
	printf("下面对空队列进行出队\n");	
	nbqueue_init(nq);	
	nbqueue_dequeue(nq,e);	
	printf("\n创建一个可以占满所有空间的循环队列\n");	
	while(1){	
		scanf("%d",&e);		
		if(e==0) break;		
		nbqueue_inqueue(nq,e);	
	}	
	nbqueue_show(nq); 	
	printf("下面出队一次,入队一次(元素相同),观察是否符合规律\n");	
	nbqueue_dequeue(nq,e);	
	nbqueue_inqueue(nq,e);	
	nbqueue_show(nq); 	
	printf("下面输入一个杨辉三角形的层数,会返回数值\n");	
	int floor;	
	scanf("%d",&floor);	
	yanghui(floor);  
}
  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值