向大厂进击——栈&队列

栈是一种限制插入和删除操作只能在一个位置上进行的表,该位置是表的末端,称为栈的顶。对栈的基本操作是push(进栈)和pop(出栈),前者相当于插入,后者则是删除最后插入的元素,top可以取出位于栈顶的元素。栈有时又称为LIFO(后进先出)表。
在这里插入图片描述
由于栈是一个表,因此任何实现表的方式都可以实现栈,因此常用的有两种实现栈的操作,数组和链表。栈的所有操作都是常量时间的操作。

数组实现
#ifndef _STACK_H_
#define _STACK_H_
#include<iostream>
#include<stdlib.h>
using namespace std;
const int DEFAULT_SIZE = 10;
template<typename Object>
class Stack {
public:
	Stack(size_t sz);
	~Stack();
public:
	bool isEmpty()const;
	bool isFull()const;
	void push(const Object& obj);
	void pop();
	Object& getTop()const;
	void Print()const;
private:
	Object* arr;
	int count;
	int top;
};
template<typename Object>
Stack<Object>::Stack(size_t sz) {
	count = sz > DEFAULT_SIZE ? sz : DEFAULT_SIZE;
	arr = new Object[count];
	top = 0;
}
template<typename Object>
Stack<Object>::~Stack() {
	delete[]arr;
	count = top = 0;
}
template<typename Object>
bool Stack<Object>::isEmpty()const {
	return top <= 0;
}
template<typename Object>
bool Stack<Object>::isFull()const {
	return top >= count;
}
template<typename Object>
void Stack<Object>::push(const Object& obj) {
	if (!isFull()) {
		arr[top++] = obj;
	}
}
template<typename Object>
void Stack<Object>::pop() {
	if (!isEmpty()) {
		top--;
	}
}
template<typename Object>
Object& Stack<Object>::getTop()const {
	if (!isEmpty()) {
		return arr[top-1];
	}
}
template<typename Object>
void Stack<Object>::Print()const {
	int i = 0;
	for (; i <top; ++i) {
		cout << arr[i] << "->";
	}
	cout << endl;
}
#endif

#include "Stack.h"
using namespace std;
int main() {
	Stack<int>st(20);
	st.push(1);
	st.push(2);
	st.push(3);
	st.Print();
	st.pop();
	st.Print();
	cout << st.getTop() << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

单向链表实现
#pragma once
#include<iostream>
#include<stdarg.h>
#ifndef _STACK_LIST_H_
#define _STACK_LIST_H_
using namespace std;
template<typename Object>
class Stack {
public:
	Stack();
	~Stack();
	void push(const Object& val);
	void pop();
	const Object& getTop()const;
	bool isEmpty()const;
	void Print()const;
private:
	struct node {
		Object val;
		node* next;
		node(Object val, node* next=nullptr) {
			this->val = val;
			this->next = next;
		}
	};
	node* top;
};
template<typename Object>
Stack<Object>::Stack() {
	top = nullptr;
}
template<typename Object>
Stack<Object>::~Stack() {
	node* ptr = nullptr;
	while (top != nullptr) {
		ptr = top->next;
		delete top;
		top = ptr;
	}
}
template<typename Object>
void Stack<Object>::push(const Object& val) {
	node* ptr = new node(val,top);
	top = ptr;
}
template<typename Object>
void Stack<Object>::pop() {
	node* ptr = top->next;
	delete top;
	top = ptr;
}
template<typename Object>
const Object& Stack<Object>::getTop()const {
	return top->val;
}
template<typename Object>
bool Stack<Object>::isEmpty()const {
	return top == nullptr;
}
template<typename Object>
void Stack<Object>::Print()const {
	node* ptr = top;
	while (ptr) {
		cout << ptr->val << "->";
		ptr = ptr->next;
	}
	cout << endl;
}
#endif


#include"Stack_list.h"
using namespace std;
int main() {
	Stack<int>st;
	st.push(1);
	st.push(2);
	st.push(3);
	st.Print();
	st.pop();
	st.Print();
	bool isempty = st.isEmpty();
	cout <<isempty << endl;
	cout << st.getTop() << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

队列

队列和栈一样,都是表的一种,然而队列在插入是在一端进行,删除是在另一端进行,队列的基本操作是入队(在队尾插入元素),出队(在表头删除元素)。
在这里插入图片描述
同样队列的实现也有两种方式,数组和链表实现,不过为了减小空间,可以使用循环数组和链表实现。

循环数组实现(参考)
#ifndef _QUEUE_ARR_H
#define _QUEUE_ARR_H
#include<iostream>
#include<stdlib.h>
#include<vector>
using namespace std;
template<typename Object>
class Queue_arr {
public:
	Queue_arr(int k) {
		arr = new Object[k];
		head = -1;
		tail = -1;
		size = k;
	}
	~Queue_arr() {
		delete[]arr;
	}
	void enQueue(const Object& val);
	void deQueue();
	const Object& Front()const;
	const Object& Back()const;
	bool isEmpty()const;
	bool isFull()const;
	void Print()const;
private:
	Object* arr;
	int head;
	int tail;
	int size;
};
template<typename Object>
void Queue_arr<Object>::enQueue(const Object& val) {
	if (isFull())return;
	if (isEmpty())
		head = 0;
	tail = (tail + 1) % size;
	arr[tail] = val;
}
template<typename Object>
void Queue_arr<Object>::deQueue() {
	if (isEmpty())return ;
	if (head == tail) {
		head = -1;
		tail = -1;
		return ;
	}
	head = (head + 1) % size;
}
template<typename Object>
const Object& Queue_arr<Object>::Front()const {
	if (isEmpty())return -1;
	return arr[head];
}
template<typename Object>
const Object& Queue_arr<Object>::Back()const {
	if (isEmpty())return -1;
	return arr[tail];
}
template<typename Object>
bool Queue_arr<Object>::isEmpty()const {
	return head == -1;
}
template<typename Object>
bool Queue_arr<Object>::isFull()const {
	return ((tail + 1) % size) == head;
}
template<typename Object>
void Queue_arr<Object>::Print()const {
	if (isEmpty())
		cout << "The queue is empty.";
	else {
		int index = head;
		while (index <= tail) {
			cout << arr[index++] << "->";
		}
	}
	cout << endl;
}
#endif


#include"Queue_arr.h"
using namespace std;
int main() {
	Queue_arr<int>q(3);
	q.enQueue(1);
	q.enQueue(2);
	q.enQueue(3);
	q.Print();
	q.deQueue();
	q.Print();
	cout << q.Front() << endl;
	cout << q.Back() << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

单向链表实现
#ifndef _QUEUE_LIST_H
#define _QUEUE_LIST_H
#include<iostream>
#include<stdlib.h>
template<typename Object>
class Queue_list {
public:
	Queue_list();
	~Queue_list();
public:
	bool isEmpty()const;
	void enQueue(const Object& val);
	void deQueue();
	const Object& Front()const;
	const Object& Back()const;
	void Print()const;
private:
	struct node {
		Object val;
		node* next;
		node(const Object& val) {
			this->val = val;
			next = nullptr;
		}
	};
	node* head;
	node* tail;
};
template<typename Object>
Queue_list<Object>::Queue_list(){
	head = nullptr;
	tail = nullptr;
}
template<typename Object>
Queue_list<Object>::~Queue_list() {
	node* temp=nullptr;
	while (head) {
		temp = head->next;
		delete head;
		head = temp;
	}
}
template<typename Object>
bool Queue_list<Object>::isEmpty() const{
	return head == nullptr;
}
template<typename Object>
void Queue_list<Object>::enQueue(const Object& val) {
	if (head == nullptr) {
		head = new node(val);
		tail = head;
	}
	else {
		tail->next = new node(val);
		tail = tail->next;
	}
}
template<typename Object>
void Queue_list<Object>::deQueue() {
	if (head == nullptr)return;
	node* oldNode = head;
	head = head->next;
	delete oldNode;
}
template<typename Object>
const Object& Queue_list<Object>::Front() const{
	if (isEmpty())return -1;
	return head->val;
}
template<typename Object>
const Object& Queue_list<Object>::Back() const{
	if (isEmpty())return -1;
	return tail->val;
}
template<typename Object>
void Queue_list<Object>::Print()const {
	node* ptr = head;
	while (ptr) {
		cout << ptr->val << "->";
		ptr = ptr->next;
	}
	cout << endl;
}
#endif



#include"Queue_list.h"
using namespace std;
int main() {
	Queue_list<int>q;
	q.enQueue(1);
	q.enQueue(2);
	q.enQueue(3);
	q.Print();
	q.deQueue();
	q.Print();
	cout << q.Front() << endl;
	cout << q.Back() << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值