C语言栈与队列操作

一、实验目的

  1. 理解栈和队列的逻辑结构和物理结构,尤其是栈的顺序存储和队列的链式存储;
  2. 理解栈和队列的相关基本算法;
  3. 编程对相关算法进行验证;
  4. 栈和队列中的元素要从终端输入,具体的输入和输出格式不限;
  5. 算法要具有较好的健壮性,对运行过程中的错误操作要做适当处理。

二、实验任务

  1. 构造一个栈S,用顺序存储方式存储该栈,将构造好的栈输出;
  2. 在第1步所构造的栈S中将元素e入栈,并将更新后的栈S输出;
  3. 在第2步更新后所得到的栈S中将栈顶元素出栈,用变量r返回该元素,并将更新后的栈S输出;
  4. 构造一个队列Q,用链式存储方式存储该队列,将构造好的队列输出;
  5. 在第4步所构造的队列Q中将元素e入队,并将更新后的队列Q输出;
  6. 在第5步更新后所得到的队列Q中将队头元素出队,用变量f返回该元素,并将更新后的队列Q输出。

三、实验过程与结果

1.栈

#include"stdafx.h"
#include<iostream>
using namespace std;

template<typename T>
class SqStack{
private:
	int top;
	int stackSize;
	int stackSpaceIncr;
	T *base;
public:
	SqStack(int maxSize=100){		//构造函数初始化栈
		stackSize=maxSize;
		top=-1;
		base=new T[stackSize];
		stackSpaceIncr=20;
	}

	void push(T e){			//入栈操作
		if(top==stackSize-1){
			base=(T *)realloc(base,(stackSpaceIncr+stackSize)*sizeof(T));
		}

		base[++top]=e;
	}

	void pop(){			//出栈操作(不返回栈顶元素)
		top--;
	}

	void pop(T &e){		//出栈操作(返回栈顶元素r)
		e=base[top];
		top--;
	}

	T getTop(){			//取栈顶元素
		return base[top];
	}

	int size(){			//栈大小
		return top+1;
	}

	bool isEmpty(){		//栈是否为空
		return size()==0?true:false;
	}

	void stackPrint(){	//栈的遍历打印
		for(int i=0;i<size();i++){
			cout<<base[i]<<"  ";
		}
		cout<<endl;
	}
};
int main(){
	SqStack<int> s(20);
	for(int i=0;i<10;i++)
		s.push(i+1);
	cout<<"初始栈为:"<<endl;
	s.stackPrint();
	cout<<endl;

	int e=1111;
	s.push(e);
	cout<<"元素"<<e<<"入栈后为:"<<endl;
	s.stackPrint();
	cout<<endl;

	int r;
	s.pop(r);
	cout<<"出栈元素为  "<<r<<endl;
	cout<<"出栈后为:"<<endl;
	s.stackPrint();
	cout<<endl;

	return 0;
}

2.队列

#include"stdafx.h"
#include<iostream>
using namespace std;

template<typename T>
struct node{
	T date;
	node* next;
};

template<typename T>
class Queue{
private:
	node<T> *front,*rear,*p;
	int queueSize;
public:
	Queue(){
		Init();
	}

	void Init(){
		front=rear=p=NULL;
		queueSize=0;
	}

	void DestroyQueue(){
		node<T> *t;
		for(p=front;p!=NULL;){
			t=p->next;
			delete p;
			p=t;
		}
		queueSize=0;
	}

	void ClearQueue(){
		DestroyQueue();
		Init();
	}

	bool empty(){
		return queueSize==0?true:false;
	}

	int size(){
		return queueSize;
	}

	void push(T e){
		p=new node<T>;
		p->date=e;
		p->next=NULL;

		if(front==NULL){
			front=rear=p;
		}
		else{
			rear->next=p;
			rear=p;
		}
		queueSize++;
	}

	void pop(){
		if(front==NULL)
			return;
		else if(queueSize==1){
			delete front;
			front=rear=NULL;
		}
		else{
			p=front->next;
			delete front;
			front=p;
		}
		queueSize--;
	}

	void pop(T &e){
		if(front==NULL)
			return;
		else if(queueSize==1){
			e=front->date;
			delete front;
			front=rear=NULL;
		}
		else{
			e=front->date;
			p=front->next;
			delete front;
			front=p;
		}
		queueSize--;
	}

	T getFront(){
		return front->date;
	}

	T getRear(){
		return rear->date;
	}

	void print(){
		for(p=front;p!=NULL;p=p->next){
			if(p==front)
				cout<<p->date;
			else
				cout<<"->"<<p->date;
		}
		cout<<endl;
	}
};

int main(){
	Queue<int> Q;
	for(int i=0;i<10;i++){
		Q.push(i+1);
	}
	Q.print();
	cout<<endl;

	int e=111;
	Q.push(e);
	cout<<"元素"<<e<<"入队后为:"<<endl;
	Q.print();
	cout<<endl;

	int f;
	Q.pop(f);
	cout<<"出队元素为"<<f<<endl;
	cout<<"出队后为:"<<endl;
	Q.print();
	cout<<endl;

	return 0;
}
posted @ 2026-01-06 21:47  东血  阅读(0)  评论(0)    收藏  举报

载入天数...载入时分秒...