数据结构(三)栈
作者:互联网
#pragma once
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <vector>
#include <algorithm>
#include <string>
#include <math.h>
#include <stack>
using namespace std;
//通过数组创建顺序栈
class StackI
{
public:
StackI(int size);
~StackI();
void push(int element);
void pop();
int top();
bool empty();
private:
int m_iCapacity;
int *array = nullptr;
int m_iTop;
};
//通过链表创建链表栈
class stackNode
{
public:
int data;
stackNode* next;
stackNode(int element):data(element), next(nullptr) {};
};
class StackII
{
public:
StackII();
~StackII();
void push(int element);
void pop();
int top();
bool empty();
int getSize();
void traverse();
private:
stackNode* root;
int size;
stackNode* currentNode;
stackNode* prevNode;
};
//模拟浏览器回退函数
int SimulationBrow();
.cpp代码如下:
#include "DS_stack.h"
StackI::StackI(int size)
{
this->m_iCapacity = size;
m_iTop = -1;
array = new int[m_iCapacity];
}
StackI::~StackI()
{
if (array)
{
delete[] array;
array = nullptr;
}
}
void StackI::push(int element)
{
if (m_iTop == m_iCapacity)
{
return;
}
array[++m_iTop] = element;
}
void StackI::pop()
{
if (empty())
{
return;
}
--m_iTop;
}
int StackI::top()
{
if (empty())
{
return -1;
}
return array[m_iTop];
}
bool StackI::empty()
{
if (m_iTop == -1)
return true;
else
return false;
}
StackII::StackII()
{
root = new stackNode(0);
currentNode = root;
prevNode = nullptr;
//prevNode = new stackNode(0);
size = 0;
}
StackII::~StackII()
{
stackNode* temp = root;
while (temp->next != nullptr)
{
stackNode* node = temp->next;
delete temp;
temp = node;
}
delete temp;
}
void StackII::push(int element)
{
stackNode* temp = new stackNode(element);
//memcpy(prevNode, currentNode, sizeof(stackNode)); //注意另外开辟内存将无法与链表原始地址对接
currentNode->next = temp;
currentNode = temp;
++size;
}
void StackII::traverse()
{
if (empty())
{
return;
}
stackNode* temp = root;
temp = temp->next;
while (temp != nullptr)
{
cout << temp->data << ", ";
temp = temp->next;
}
cout << endl;
}
void StackII::pop()
{
if (empty())
{
return;
}
stackNode* temp = root;
stackNode* prevNode = nullptr;
while (temp != nullptr)
{
prevNode = temp;
temp = temp->next;
if (temp -> next == nullptr)
{
delete temp;
break;
}
}
currentNode = prevNode;
currentNode->next = nullptr;
--size;
}
int StackII::top()
{
if (empty())
{
return -1;
}
return currentNode->data;
}
int StackII::getSize()
{
return size;
}
bool StackII::empty()
{
if (size == 0)
{
return true;
}
return false;
}
void Backspace(StackII& firStack, StackII& secStack)
{
secStack.push(firStack.top());
firStack.pop();
}
void Go(StackII& firStack, StackII& secStack)
{
firStack.push(secStack.top());
secStack.pop();
}
int SimulationBrow()
{
StackII firStack, secStack;
//模拟连续创建四个网页
firStack.push(1);
firStack.push(2);
firStack.push(3);
firStack.push(4);
//原始
cout << "连续打开网页";
firStack.traverse();
cout << endl;
//回退一步
cout << "回退一步的结果"<< endl;
Backspace(firStack, secStack);
cout << "主栈遍历: ";
firStack.traverse();
cout << "副栈遍历: ";
secStack.traverse();
cout << endl;
cout << "回退二步的结果" << endl;
Backspace(firStack, secStack);
cout << "主栈遍历: ";
firStack.traverse();
cout << "副栈遍历: ";
secStack.traverse();
cout << endl;
////前进一步
cout << "前进一步的结果" << endl;
Go(firStack, secStack);
cout << "主栈遍历: ";
firStack.traverse();
cout << "副栈遍历: ";
secStack.traverse();
cout << endl;
firStack.push('e');
firStack.push('f');
firStack.push('g');
//添加新的网页时应清空副栈中的数据
for (int i = 0; i < secStack.getSize(); i++)
{
secStack.pop();
}
//打开新的网页e f g
cout << "打开新的网页 e f g: "<<endl;
cout << "主栈遍历: ";
firStack.traverse();
cout << "副栈遍历: ";
secStack.traverse();
cout << endl;
//回退一步
cout << "回退一步的结果" << endl;
Backspace(firStack, secStack);
cout << "主栈遍历: ";
firStack.traverse();
cout << "副栈遍历: ";
secStack.traverse();
cout << endl;
//回退一步
cout << "回退一步的结果" << endl;
Backspace(firStack, secStack);
cout << "主栈遍历: ";
firStack.traverse();
cout << "副栈遍历: ";
secStack.traverse();
cout << endl;
////前进一步
cout << "前进一步的结果" << endl;
Go(firStack, secStack);
cout << "主栈遍历: ";
firStack.traverse();
cout << "副栈遍历: ";
secStack.traverse();
cout << endl;
return 0;
}
标签:return,temp,int,void,StackII,stackNode,数据结构 来源: https://blog.csdn.net/qq_22379931/article/details/89110338