数据结构 实现栈和队列

1.顺序存储结构

#include<iostream>
#include<string>

#define MAX 1024
using namespace std;

typedef int DataType;

class Stack
{
public:
	Stack();
	DataType data[MAX];
	int top;
	void show();
	int push(DataType elem);
	int pop(DataType& elem);
};
Stack::Stack()
{
	this->data[MAX-1] = 0;//不知道为啥,不加-1会出警告
	this->top = -1;
}
void Stack::show()
{
	if (this->top == -1)
	{
		cout << "空栈" << endl;
	}
	else
	{
		cout << "栈中一共有" << this->top + 1 << "个元素" << endl;
		for (int i = 0; i <= this->top; i++)
		{
			cout << this->data[i] << " " << ends;
		}
		cout << endl;
	}
}
int Stack::push(DataType elem)
{
	if (this->top == MAX - 1)
	{
		cout << "栈已经满了" << endl;
		return -1;
	}
	this->top++;
	this->data[this->top] = elem;
	return 0;
}
int Stack::pop(DataType& elem)
{
	if (this->top == -1)
	{
		cout << "空栈" << endl;
		return -1;
	}
	elem = this->data[this->top];
	this->top--;
	return 0;
}
int main()
{
	int a;
	Stack s;
	Stack* ps = &s;
	ps->push(0);
	ps->push(1);
	ps->push(2);
	ps->push(3);
	ps->show();

	ps->pop(a);
	ps->show();
	cout << a << endl;
	system("pause");
	return 0;
}

2.链式存储结构

#include<iostream>
#include<string>

#define MAX 1024
using namespace std;

typedef int DataType;
class Node
{
public:
	DataType data;
	Node* next;
};
class Stack
{
public:
	Stack();
	int count;
	Node* top;
	void show();
	int push(DataType elem);
	int pop(DataType& elem);
};
Stack::Stack()
{
	this->top = nullptr;
	this->count = -1;
}
void Stack::show()
{
	if (this->count == -1)
	{
		cout << "空栈" << endl;
	}
	else
	{
		cout << "一共有" << this->count + 1 << "个元素" << endl;
		for (int i = 0; i <= this->count; i++)
		{
			cout << this->top->data << " " << ends;
			this->top = this->top->next;
		}
		cout << endl;
	}
}
int Stack::push(DataType elem)
{
	Node* newNode = new Node;
	newNode->data = elem;
	newNode->next = this->top;
	
	this->top = newNode;
	this->count++;
	cout << "添加成功" << endl;
	return 0;
	 
}
int Stack::pop(DataType& elem)
{
	if (this->count == -1)
	{
		cout << "空栈" << endl;
		return -1;
	}
	elem = this->top->data;
	this->top = this->top->next;
	this->count--;
	cout << "出栈成功" << endl;
	return 0;
}
int main()
{
	Stack s;
	Stack* ps = &s;
	int a;

	ps->push(0);
	ps->push(1);
	ps->push(2);
	ps->push(3);
	ps->push(4);
	ps->push(5);
	//ps->show();

	ps->pop(a);
	ps->show();
	cout << a << endl;
	
	system("pause");
	return 0;
}

队列

1.循环队列

#include<iostream>
#include<string>

#define MAX 1024
using namespace std;
typedef int DataType;

class Queue
{
public:
	DataType data[MAX];
	int front;
	int rear;
	Queue();
	int getQueueLenth();
	int enQueue(DataType elem);
	int deleteQueue(DataType& elem);
};
Queue::Queue()
{
	this->data[MAX-1] = 0;
	this->front = 0;
	this->rear = 0;
}
int Queue::getQueueLenth()
{
	return (this->rear - this->front + MAX) % MAX;
}
int Queue::enQueue(DataType elem)
{
	if (this->getQueueLenth() == MAX-1)//(this-rear +1)%MAX == this->front
	{
		cout << "满队" << endl;
		return -1;
	}
	this->data[this->rear] = elem;
	this->rear = (this->rear + 1) % MAX;//****

	cout << "入队成功" << endl;
	return 0;
}
int Queue::deleteQueue(DataType& elem)
{
	if (this->getQueueLenth() == 0)//(this-rear +1)%MAX == this->front
	{
		cout << "空队" << endl;
		return -1;
	}
	elem = this->data[this->front];
	this->front = (this->front + 1) % MAX;

	cout << "出队成功" << endl;
	return 0;
}
int main()
{
	Queue q;
	Queue* pq = &q;
	int a;

	pq->enQueue(0);
	pq->enQueue(1);
	pq->enQueue(2);
	cout << pq->getQueueLenth() << endl;

	pq->deleteQueue(a);
	cout << pq->getQueueLenth() << endl;
	cout << "出队元素:" << a << endl;
	system("pause");
	return 0;
}

2.链式存储结构

#include<iostream>
#include<string>

#define MAX 1024
using namespace std;
typedef int DataType;

class Node
{
public:
	DataType data;
	Node* next;
};
class Queue
{
public:
	Node* front;
	Node* rear;
	
	int count;
	Queue();
	int enQueue(DataType elem);
	int deleteQueue(DataType& elem);
};
Queue::Queue()
{
	Node* head = new Node;
	head->data = this->count;
	head->next = nullptr;
	this->front = head;
	this->rear = head;
	this->count = 0;
}
int Queue::enQueue(DataType elem)
{
	Node* newNode = new Node;
	newNode->data = elem;
	newNode->next = nullptr;

	this->rear->next = newNode;

	this->rear = newNode;
	this->count++;
	return 0;
}
int Queue::deleteQueue(DataType& elem)
{
	if (this->count == 0)
	{
		cout << "空队" << endl;
		return -1;
	}
	Node* temp;
	temp = this->front->next;
	elem = temp->data;
	this->front->next = temp->next;
	if (temp == this->rear)
		this->front = this->rear;
	delete temp;
	this->count--;
	return 0;
}
int main()
{
	Queue q;
	Queue* pq = &q;
	int a;
	pq->enQueue(0);
	pq->enQueue(1);
	cout << pq->count << endl;
	pq->deleteQueue(a);
	cout << pq->count << endl;
	
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值