数据结构入门系列之——栈与队列的基本操作

栈有栈顶和栈尾。栈的进栈出栈都在栈顶进行。
在这里插入图片描述
在这里插入图片描述

栈的顺序存储

定义:

#include <iostream>
#include<iomanip>
using namespace std;
#define maxsize 100
typedef struct
{
	int data[maxsize];
	int top;//栈顶指针(下标)
}SqStack;

要点:

st.top==-1 //1.栈空 
st.top==maxsize-1 //2.栈满
/*3.x进栈*/
{
	st.top++;
	x=st.data[st.top];
}
/*4.x出栈*/
{
	x=st.data[st.top];
	st,top--;
}
/*初始化*/
void InitStack(SqStack &st)//引用参数,子函数跟主函数st都不用是指针
{
	st.top = -1;
}
void DestroyStack(SqStack st)
{}
/*元素进栈*/
int Push(SqStack &st, int x)
{
	if (st.top == maxsize - 1)
		return 0;
	else {
		st.top++;
		st.data[st.top] = x;
		return 1;
	}
}
/*元素出栈*/
int Pop(SqStack &st, int &x)
{
	if (st.top == -1)
		return 0;
	else
	{
		x = st.data[st.top];
		st.top--;
		return 1;
	}
}
/*取栈顶元素*/
int GetTop(SqStack &st, int &x)
{
	if (st.top == -1)
		return 0;
	else
	{
		x = st.data[st.top];
		return 1;
	}
}
/*判断栈空*/
int StackEmpty(SqStack st)
{
	if (st.top == -1)
		return 1;
	else
		return 0;
}
/*输出栈元素*/
void OutputStack(SqStack st)
{
	while (st.top != -1)
	{
		cout << st.data[st.top] << setw(5);
		st.top--;
	}
	cout << endl;
}
void main()
{
	SqStack st;
	InitStack(st);
	int x;
	Push(st, 0);
	Push(st, 1);
	Push(st, 2);
	OutputStack(st);
	Pop(st, x);
	OutputStack(st);
}

栈的链式存储

定义

#include <iostream>
#include<iomanip>
using namespace std;
typedef struct node
{
	int data;
	struct node *next;
}lnode;

其中DestroyStack较难想。

lnode *InitStack()
{//更正
	lnode *s;
	s = new lnode;
	s->next = NULL;
	return s;
}
int EmptyStack(lnode *top)
{
	if (top == NULL)return 1;
	else return 0;
}
/*进栈*/
lnode *Push(lnode *top, int x)
{
	lnode *s;
	s = new lnode;
	s->data = x;
	s->next = top;
	top = s;
	return top;
}
/*出栈*/
lnode *Pop(lnode *top, int *x)
{
	lnode *s;
	if (top == NULL)return NULL;
	else {
		*x = top->data;
		s = top;
		top = top->next;
		delete s;
		s = NULL;
		return top;
	}
}
/*销毁栈----->有点难想<-----*/
void DestroyStack(lnode *l)
{
	lnode *pre, *p;
	pre = l;
	p = l->next;
	while (p)
	{
		delete pre;
		pre = p;
		p = p->next;
	}
	delete pre;
	pre = NULL;
}
int StackEmpty(lnode *top)
{
	if (top == NULL)
		return 1;
	else return 0;
}
int GetTop(lnode *top, int &x)
{
	
	if (top == NULL)
	{
		return NULL;
	}
	else 
		x = top->data;
	return 0;
}
void OutputStack(lnode *top)
{
	lnode *p = top;
	while (p)
	{
		cout << p->data << setw(5);
		p = p->next;
	}
	cout << endl;
}

队列

  1. 通常约定队尾指针指示队尾元素的当前位置,队头指针指示队头元素的前一个位置
  2. 从尾部进队,从头部出队。

队列的顺序存储

定义

#include <iostream>
#include<iomanip>
using namespace std;
#define maxsize 20
typedef struct
{
	int data[maxsize];
	int front, rear;//队头、队尾指针
}SqQueue;

由于普通队列存在假溢出的问题。这里只介绍循环队列。
循环队列
规定:

1.队空条件:
sq.front=sq.rear;
2.队满条件:
(sq.rear+1)%maxsize==sq.front;
3.进队操作:
sq.rear=(sq.rear+1)%maxsize;
sq.data[sq.rear]=x;
4.出队操作:
sq.front=(sq.front+1)%maxsize;
x=sq.data[sq.front];
5.元素个数:
num=(sq.rear-sq.front+maxsize)%maxsize

基本函数

void InitQueue(SqQueue &sq)
{
	sq.front = sq.rear = 0;
}
/*进队*/
int EnQueue(SqQueue &sq, int x)
{
	if ((sq.rear + 1) % maxsize == sq.front)
		return 0; //队列满
	sq.rear = (sq.rear + 1) % maxsize;
	sq.data[sq.rear] = x;
	return 1;
}
/*出队*/
int DeQueue(SqQueue &sq, int &x)
{
	if (sq.rear == sq.front)
		return 0;
	sq.front=(sq.front + 1) % maxsize;
	x = sq.data[sq.front];
	return 1;
}
/*输出元素*/
void OutputQueue(SqQueue &sq)
{
	int i = (sq.front + 1)%maxsize;
	if (sq.front == sq.rear)
		cout << "队列为空" << endl;
	else {
		while (i <= (sq.rear)%maxsize)
		{
			cout << sq.data[i] << setw(5);
			i++;
		}
		cout << endl;
	}
}
void main()
{
	SqQueue L;
	InitQueue(L);
	int i, x, j = 1;
	cout << "请选择操作:" << '\n' << "1:进队" << '\n' << "2:出队(1为出队成功,0为出队失败)" << '\n' << "3: 输出队中所有元素" << '\n' << "0:退出" << endl;
	while (j)
	{
		cout << "请选择操作:";
		cin >> i;
		switch (i)
		{
		case 1:cout << "请输入元素:"; cin >> x; EnQueue(L, x); break;
		case 2:cout << DeQueue(L, x) << endl; break;
		case 3:OutputQueue(L); break;
		case 0:j = 0; break;
		default:cout << "输入错误!请重新输入!";

		}
	}
}

队列的链式存储

在这里插入图片描述
定义:

#include <iostream>
#include<iomanip>
using namespace std;
/*数据结点*/
typedef struct node
{
	int data;
	struct node *next;
}lnode;
/*链队结点*/
typedef struct
{
	lnode *front, *rear;
}LinkQueue;

基本函数:

void InitQueue(LinkQueue *&l)
{
	l = new LinkQueue;
	l->front = l->rear = NULL;
}
/*进队列*/
void EnQueue(LinkQueue *&l, int x)//lnode *enqueue(lnode &l,int x)
{
	lnode *s = new lnode;
	s->data = x;
	s->next = NULL;  //很重要
	if (l->front == NULL)
		l->front = l->rear = s;
	else {
		l->rear->next = s;
		l->rear = s;
	}
}
/*出队列*/
int DeQueue(LinkQueue *&l, int &x)
{
	lnode *s;
	if (l->front == NULL)
		return 0;
	if (l->front == l->rear)
	{
		x = l->front->data;
		l->front = l->rear = NULL;
	}
	else {
		s = l->front;
		x = s->data;
		l->front = s->next;
		delete s;
		return 1;
	}
}
bool QueueEmpty(LinkQueue *qu)
{
	if (qu->front == NULL)
		return 1;
	else return 0;
}
/*输出队列元素*/
void OutputQueue(LinkQueue *l)
{
	lnode *s;
	LinkQueue *q = l;
	if (QueueEmpty(q))
		cout << "empty!" << endl;
	else {
		s = l->front;
		while (s)
		{
			cout << s->data << " ";
			s = s->next;
		}
		cout << endl;
	}
}
void main()
{
	LinkQueue *qu;
	InitQueue(qu);
	int i = 1;
	int j, x, y;
	while (i)
	{
		cout << "请选择操作,1:进队 2:出队 3.列出队列中的元素 0:退出" << endl;
		cin >> j;
		switch (j)
		{
		case 1:cout << "请输入元素:"; cin >> x; EnQueue(qu, x); break;
		case 2:DeQueue(qu, y);  break;
		case 3:OutputQueue(qu); break;
		case 0:i = 0; break;

		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值