数据结构实验三--栈与队列
栈
顺序栈
#include<bits/stdc++.h>
using namespace std ;
const int MaxSize = 1000 ;
typedef struct SNode{
int data[MaxSize] ;
int top ;
}SNode;
void init(SNode &p){
p.top = -1 ;
}
void StackPush(SNode &p,int e){
if(p.top < (MaxSize - 1)){
p.top ++ ;
p.data[p.top] = e ;
}
else{
cout << "栈已不能插入" << "\n";
}
}
void StackPop(SNode &p){
if(p.top != -1){
p.top -- ;
}
else{
cout << "栈已空,无法弹出" << "\n" ;
}
}
int GetTop(SNode p){
return p.data[p.top] ;
}
bool IsEmpty(SNode p){
if(p.top == -1) return true ;
return false ;
}
void StackPrint(SNode p){
for (int i = 0; i <= p.top; i ++)
cout << p.data[i] << " ";
cout << "\n";
}
int main(){
SNode p ;
init(p) ;
while(1){
cout << "***********栈************" << "\n" ;
cout << "*****1.入栈**************" << "\n" ;
cout << "*****2.出栈**************" << "\n" ;
cout << "*****3.判空**************" << "\n" ;
cout << "*****4.打印**************" << "\n" ;
cout << "*****5.得到栈顶元素******" << "\n" ;
cout << "*************************"
<< "\n";
int op ;cin >> op ;
if(op == 1) {
cout << "输入插入元素" << "\n" ;
int e ;cin >> e ;
StackPush(p,e) ;
}else if(op == 2){
StackPop(p) ;
}
else if(op == 3){
if(IsEmpty(p)) cout << "栈为空" << "\n" ;
else cout << "栈为不空" << "\n" ;
}else if(op == 4){
if(IsEmpty(p)){
cout << "栈中没有元素"<< "\n";
}else{
StackPrint(p);
}
}else if(op == 5){
int k = GetTop(p) ;
cout << "栈顶元素为:" << k << "\n" ;
}
system("cls");
}
return 0 ;
}
链栈
#include <bits/stdc++.h>
using namespace std;
#define MaxSize 10
typedef struct SNode{
struct SNode *next;
int data ;
}SNode ;
void init(SNode *p){
p->next = NULL;
}
void StackInsert(SNode * &p,int e){
SNode *s = (SNode *)malloc(sizeof(SNode));
s->data = e ;
s->next = p;
p = s;
}
bool IsEmpty(SNode *p){
if(p->next == NULL)
return true;
return false;
}
void StackDelete(SNode * &p){
if(IsEmpty(p)){
cout << "链栈为空"<< "\n";
}
else p->next = p->next->next;
}
void PrintStack(SNode *p){
while(p->next != NULL){
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int GetTop(SNode *p){
return p->data;
}
int main()
{
int op ;
SNode *p;
init(p);
cout << "***********栈************" << "\n" ;
cout << "*****1.判空**************" << "\n" ;
cout << "*****2.入栈**************" << "\n" ;
cout << "*****3.出栈**************" << "\n" ;
cout << "*****4.打印**************" << "\n" ;
cout << "*****5.得到栈顶元素******" << "\n" ;
cout << "*************************"<< '\n';
while(1){
cout << "请输入操作数 :";
cin >> op;
if(op == 1) {
cout << "当前是判空操作:";
if(IsEmpty(p))
cout << "链栈为空" << "\n";
else cout << "链栈不空" << "\n" ;
}else if(op == 2){
cout << "当前是入栈操作:";
cout << "输入插入元素 :";
int e;
cin >> e;
StackInsert(p, e);
}else if(op == 3){
cout << "当前是出栈操作:";
StackDelete(p);
}else if(op == 4){
cout << "当前是输出操作:";
PrintStack(p);
}else if(op == 5){
cout << "栈顶元素为:";
cout << GetTop(p) << endl;
}
}
return 0;
}
队列
链式队列
#include<bits/stdc++.h>
using namespace std ;
typedef struct QNode{
int data ;
struct QNode* next ;
}QNode ;
typedef struct QueueLink
{
QNode *front ;
QNode *rear ;
};
void Init_Queue(QueueLink &q){
q.front = q.rear = new QNode ;
q.front -> next = NULL ;
}
void Queue_Insert(QueueLink &q,int e){
QNode *p = new QNode;
p->data = e ;
p->next = NULL ;
q.rear ->next = p ;
q.rear = p ;
}
void Queue_Delete(QueueLink &q){
if(q.front != q.rear){
QNode *p = new QNode;
p = q.front->next ;
q.front->next = p->next ;
if(q.rear==p){
q.front == q.rear ;
}
delete p ;
}
else cout << "删除失败\n" ;
}
int GetTop(QueueLink q){
return q.front->next->data ;
}
bool Is_Empty(QueueLink q){
if(q.front == q.rear) return true;
return false ;
}
void Print_Queue(QueueLink q){
if(!Is_Empty(q)){
QNode *p = new QNode;
p = q.front->next ;
while(p != NULL){
cout << p->data << " " ;
p = p -> next ;
}
}
}
int main(){
QueueLink q ;
Init_Queue(q) ;
cout << "*****************\n" ;
cout << "**1.插入元素******\n" ;
cout << "**2.删除元素******\n" ;
cout << "**3.得到顶部元素***\n";
cout << "**4.判空**********\n";
cout << "**5.打印链队元素***\n";
char op ;
while(1){
cin >> op ;
if(op == '1'){
int e ;
cout << "输入插入的值:" ;
cin >> e ;
Queue_Insert(q,e) ;
}else if(op == '2'){
Queue_Delete(q) ;
}else if(op == '3'){
int e = GetTop(q) ;
}else if(op == '4'){
if(Is_Empty(q)){
cout << "链队为空\n" ;
}else{
cout << "连队不空\n" ;
}
}else if(op == '5'){
Print_Queue(q) ;
}
}
return 0 ;
}
循环队列
#include<iostream>
using namespace std ;
#define MaxSize 10
typedef struct CQLink{
int front ;
int rear ;
int data[MaxSize] ;
}CQLink;
void InitQueue(CQLink &q){
q.front = q.rear = 0 ;
}
bool Is_Empty(CQLink q){
if(q.front == q.rear) return 1 ;
return 0 ;
}
bool Is_Full(CQLink q){
if(q.front == (q.rear + 1) % MaxSize) return true ;
return false ;
}
void EnterQueue(CQLink &q,int e){
if(!Is_Full(q)){
q.rear = (q.rear+1) % MaxSize ;
q.data[q.rear] = e ;
cout << "Insert element successfully\n" ;
}else{
cout << "The circular linked list is full. Insert failed\n" ;
}
}
void OutQueue(CQLink &q){
if(!Is_Empty(q)){
q.front = (q.front+1)%MaxSize ;
cout << "Deleting the tail element succeeded. \n" ;
}else{
cout << "Failed to delete circular linked list because it was empty\n" ;
}
}
void PrintCircleQueue(CQLink q){
if(Is_Empty(q)){
cout << "The loop queue is empty. Output failed\n" ;
}else{
for(int i = q.front + 1;(i + MaxSize) % MaxSize != q.rear + 1 ; i ++){
cout << q.data[i] << " " ;
}
cout << "\n" ;
}
}
int main(){
CQLink q ;
InitQueue(q) ;
cout << "***********Circular queue************" << "\n" ;
cout << "*****1.Enter the queue***************" << "\n" ;
cout << "*****2.Out the queue*****************" << "\n" ;
cout << "*****3.Judge Empty*******************" << "\n" ;
cout << "*****4.Judge Full********************" << "\n" ;
cout << "*****5.Output loop queue*************" << "\n" ;
cout << "*************************************"<< '\n';
while(1){
cout << "Select operands: " ;
int op ;cin >> op ;
if(op == 1){
int e ;
cout << "Enter the inserted number:" ;
cin >> e;
EnterQueue(q,e) ;
}else if(op == 2){
OutQueue(q) ;
}else if(op == 3){
if(Is_Empty(q)) cout << "The loop queue is empty\n" ;
else cout << "The loop queue is not empty\n" ;
}else if(op == 4){
if(Is_Full(q)) cout << "The loop queue is full\n" ;
else cout << "Circular queue not full\n" ;
}else if(op == 5){
PrintCircleQueue(q) ;
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?