[手撕数据结构] 栈与队列

[手撕数据结构] 栈与队列,第1张

线性栈
#include
#include
using namespace std;
#define MaxSize 50
typedef struct {
	int data[MaxSize];
	int top;
}SqStack;

void InitStack(SqStack& s)
{
	s.top = -1;
}

void StackEmpty(SqStack& s)
{
	if (s.top == -1)
		cout << "空栈" << endl;
	else
		cout << "非空" << endl;

}

void Push(SqStack& s,int e)
{
	if (s.top == MaxSize - 1)
	{
		cout << "满栈" << endl;
		return;
	}
	s.data[++s.top] = e;
	return;
}

void Pop(SqStack& s)
{
	if (s.top == -1)
	{
		cout << "空栈" << endl;
		return;
	}
	cout << "栈顶元素" << s.data[s.top--] << "已经弹出" << endl;
	return;
}

void GetTop(SqStack &s)
{
	if (s.top == -1)
	{
		cout << "空栈" << endl;
		return;
	}
	cout << "当前栈顶元素为" << s.data[s.top];
	return;
}
void print(SqStack &s)
{
	
	for(int i=s.top;i>-1;i--)
	{
		cout<

效果如下

线性队列 
#include
#include
#include
#include
#include
using namespace std;
#define MAXSIZE 50
typedef struct queue
{
	int data[MAXSIZE];
	int head;//队头指针
	int tail;//队尾指针
}queue;
void Init_queue(queue &Q)//初始化队列
{
	Q.tail =Q.head= 0;
	return;
}
void Push(queue& Q,int x)//入队
{
	if (Q.tail == MAXSIZE-1)
	{
		cout << "满队"<

效果如下

链栈 
#include
#include
using namespace std;

typedef struct Stack {
	int data;
	struct Stack* next;
}SqStack;

void InitStack(SqStack*& s)
{
	s = NULL;
	return;
}

void StackEmpty(SqStack*& s)
{
	if (s == NULL)
	{
		cout << "空栈" << endl;
		return;
	}
	cout << "非空" << endl;
	return;
}

void Push(SqStack* &s, int e)
{
	SqStack* p = new SqStack;
	p->data = e;
	p->next = s;
	s = p;
	return;
}

void Pop(SqStack* &s)
{
	if (s == NULL)
	{
		cout << "空栈" << endl;
		return;
	}
	SqStack* p = s;
	cout << "栈顶元素" << s->data<< "已经弹出" << endl;
	s = s->next;
	delete p;
	return;
}

void GetTop(SqStack* &s)
{
	if (s==NULL)
	{
		cout << "空栈" << endl;
		return;
	}
	cout << "当前栈顶元素为" << s->data << endl;
	return;
}
void Print (SqStack* s)
{
	if (s == NULL)
	{
		cout << "空栈" << endl;
		return;
	}
	SqStack *p = s;
	while (p)
	{
		cout << p->data <<" ";
		p = p->next;
	}
	cout<<"遍历结束"<next)
	{
		p = p->next;
		i++;
	}
	cout << "此栈共有" << i << "个结点" << endl;
	return;
}
void StackDestroy(SqStack* &s)
{
	while (s)
	{
		SqStack* p = s;
		s = s->next;
		delete p;
	}
	cout << "销毁完毕" << endl;
	return;
}
int main()
{
	SqStack* sqStack;
	InitStack(sqStack);
	Push(sqStack, 5);
	Push(sqStack, 4);
	Push(sqStack, 3);
	Push(sqStack, 2);
	Push(sqStack, 1);
	Print(sqStack);
	Pop(sqStack);
	Pop(sqStack);
	Pop(sqStack);
	Print(sqStack);
	Stack_size(sqStack);
	return 0;
}

效果如下

 链队
#include
#include
#include
#include
#include
using namespace std;

typedef struct queue
{
	int data;
	struct queue* next;
}Qnode;
struct Qptr {
	Qnode* head;//队头指针
	Qnode* tail;//队尾指针
};
void Init_queue(Qptr& Q)//初始化队列
{
	Q.head = Q.tail = NULL;
	return;
}
void Push(Qptr& Q, int x)//入队
{
	Qnode* p = new Qnode;
	p->data = x;
	p->next = NULL;
	if (Q.head == NULL)
	{
		Q.head = Q.tail = p;
		return;
	}
	Q.tail->next = p;
	Q.tail = p;
}
void pop(Qptr& Q)//出队
{
	if (Q.head == NULL)
	{
		cout << "空表" << endl;
		return;
	}
	Qnode* p = Q.head;
	cout << "队头元素" << p->data << "已出队" << endl;
	Q.head = Q.head->next;
	delete p;
}
void Get_Queue_Size(Qptr& Q)//获取队列中元素个数
{
	int i = 1;
	Qnode* p = Q.head;
	while (p != Q.tail)
	{
		p = p->next;
		i++;
	}
	cout << "当前队列共有" << i << "个元素" << endl;
	return;
}
void isEmpty(Qptr& Q)//判空
{
	if (Q.head == NULL)
	{
		cout << "空队" << endl;
		return;
	}
	cout << "非空" << endl;
	return;
}
void Get_head(Qptr& Q)//获得队头元素
{
	cout << "当前队头元素为" << Q.head->data << endl;
	return;
}
void Get_tail(Qptr& Q)//获得队尾元素
{
	cout << "当前队尾元素为" << Q.tail->data << endl;
	return;
}
void Clear_Queue(Qptr& Q)//清空队列
{

	while (Q.head)
	{
		Qnode* p = Q.head;
		Q.head = Q.head->next;
		delete p;
	}
	cout << "队列已清空" << endl;

}
void Print(Qptr& Q)//遍历队列
{
	Qnode* p = Q.head;
	while (p != Q.tail)
	{
		cout << p->data << " ";
		p = p->next;

	}
	cout << p->data << endl << "遍历结束" << endl;
	return;
}
int main()
{
	struct Qptr Q;
	Init_queue(Q);
	Push(Q, 1);
	Push(Q, 5);
	Print(Q);
	Clear_Queue(Q);
	Push(Q, 1);
	Push(Q, 2);
	Push(Q, 3);
	Push(Q, 4);
	Push(Q, 5);
	Push(Q, 6);
    Print(Q);
	pop(Q);
	pop(Q);
	pop(Q);
    Print(Q);
	return 0;
}

效果如下

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/798428.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)