1 栈
栈(stack)是限制对元素的插入(push)和删除(pop)只能在一个位置上进行的表,该位置是表的末端,叫做栈的栈顶(top)
1.1 顺序栈类实现
top = 0,即elem[0] 不存储数据
template<class ItemType>
class SqStack
{
private:
int top; //栈顶指针
int maxSize = 100; //最大容量
ItemType elem[100]; //一维数组
public:
SqStack() {top = 0;}
~SqStack() {delete[] this->elem;}
void setEmpty() {top = 0;} //置为空
bool isEmpty() //判断是否为空
{return (this->top == 0 ? true : false); }
void push(ItemType e); //入栈
ItemType pop(); //出栈
void printOut(); //打印
};
template<class ItemType>
void SqStack<ItemType>::printOut()
{
if(this->top == 0) cout << "is Empty." << endl;
else
{
for(int k = this->top; k > 0; k--)
cout << "the elem[" << k << "] is : " << elem[k] << endl;
}
}
template<class ItemType>
void SqStack<ItemType>::push(ItemType e)
{
if(top == this->maxSize-1)
{
cout << "\n this Stack is full." << endl;
}
else
{
this->top++; //top=0 栈底不存储元素
this->elem[top] = e;
}
}
template<class ItemType>
ItemType SqStack<ItemType>::pop()
{
if(this->top == 0)
throw "is Empty.";
ItemType e = this->elem[this->top];
this->top--;
return e;
}
//test
SqStack<int> *stack = new SqStack<int>();
int e;
for(int i = 0; i < 3; i++)
{
cout << "Input num : "; cin >> e;
stack->push(e);
}
stack->printOut();
cout << stack->pop() << endl;
stack->printOut();
1.2 链表栈类实现
栈底不存储数据
struct LSNode
{
int data;
LSNode *next;
};
class LSStack
{
private:
LSNode *top; //头指针
public:
LSStack();
~LSStack();
void setEmpty(); //置为空
bool isEmpty(); //判断为空
void display(); //显示
void push(int e);//入栈
int pop(); //出栈
int length(); //长度
};
LSStack::LSStack() {top = NULL;}
LSStack::~LSStack() {this->setEmpty();}
void LSStack::setEmpty()
{
if(this->top != NULL)
{
LSNode *p, *q;
p = this->top;
q = p->next;
while (p != NULL)
{
delete p;
p = q;
if(q != NULL) q = p->next;
}
this->top = NULL;
}
}
int LSStack::length()
{
LSNode *p = this->top;
int count = 0;
while (p != NULL)
{
count++;
p = p->next;
}
return count;
}
bool LSStack::isEmpty()
{
return this->length() == 0 ? true : false;
}
void LSStack::display()
{
LSNode *p = this->top;
while (p != NULL)
{
cout << "data : " << p->data << endl;
p = p->next;
}
}
void LSStack::push(int e)
{
LSNode *newNode = new LSNode;
newNode->data = e;
newNode->next = this->top;
this->top = newNode;
}
int LSStack::pop()
{
if(this->isEmpty())
throw "lsStack is empty.";
LSNode *p = this->top;
this->top = this->top->next;
int data = p->data;
delete p;
return data;
}
//test
LSStack *lss = new LSStack();
int e;
cout << "empty ? : " << lss->isEmpty() << endl;
for(int i = 0;i < 3; i++)
{
cout << "Input num : "; cin >> e;
lss->push(e);
}
lss->display();
cout << "length : " << lss->length() << endl;
cout << "empty ? : " << lss->isEmpty() << endl;
cout << "pop e :" << lss->pop() << endl;
lss->display();
2 队列
队列只允许在后端(称为rear)进行插入操作,在前端(称为front)进行删除操作。
队列的操作方式和堆栈类似,唯一的区别在于队列只允许新数据在后端进行添加。
2.1 顺序(循环)队列
一开始:front = rear = -1
空队列条件:front == rear
满队列条件:(rear + 1) % MaxSize == front
插入:rear = (rear + 1) % MaxSize
删除:front = (front + 1) % MaxSize
const int MaxSize = 6; // need a const
class SeQueue
{
private:
int elem[MaxSize];
int front, rear; //头指针,尾指针
public:
SeQueue();
~SeQueue();
bool isEmpty(); //判断是否为空
void display(); //显示
void addQ(int e);//入队
int delQ(); //出队
};
SeQueue::SeQueue()
{
this->front = -1;
this->rear = -1;
}
bool SeQueue::isEmpty()
{
return (this->front == this->rear ? true : false);
}
void SeQueue::display()
{
int p = this->front;
while ((p % MaxSize) != this->rear)
{
p++;
cout << "elem[" << p << "] : "
<< this->elem[p] << endl;
}
}
void SeQueue::addQ(int e)
{
this->rear = (this->rear + 1) % MaxSize;
this->elem[this->rear] = e;
cout << "add Success!" << endl;
}
int SeQueue::delQ()
{
//判断是否为空
if(this->isEmpty())
throw "Queue is empty.";
this->front = (this->front + 1) % MaxSize;
return this->elem[this->front];
}
//for test
SeQueue *sq = new SeQueue();
cout << sq->isEmpty() << endl;
int e;
for(int i = 0; i < 2; i++)
{
cout << "Input num : "; cin >> e;
sq->addQ(e);
}
sq->display();
cout << "************" << endl;
cout << "del : " << sq->delQ() << endl;
cout << "************" << endl;
sq->display();
for(int i = 0; i < 2; i++)
{
cout << "Input num : "; cin >> e;
sq->addQ(e);
}
2.2 链表队列
struct QueNode
{
int data;
QueNode *next;
};
class LsQueue
{
private:
QueNode *front; //头结点 [出队]
QueNode *rear; //尾结点 [入队]
public:
LsQueue();
~LsQueue();
bool isEmpty();
void display();
void addQ(int e);
int delQ();
};
LsQueue::LsQueue()
{
this->front = new QueNode;
this->rear = this->front;
this->front->next = NULL;
}
bool LsQueue::isEmpty()
{
return (this->front == this->rear ? true : false);
}
void LsQueue::display()
{
if(this->isEmpty())
{
cout << "LsQueue is empty." << endl;
return;
}
QueNode *p = this->front;
while (p != this->rear)
{
cout << "out data : " << p->next->data << endl;
p = p->next;
}
}
void LsQueue::addQ(int e)
{
QueNode *addE = new QueNode;
addE->data = e;
addE->next = NULL;
this->rear->next = addE;
this->rear = addE;
cout << "add success." << endl;
}
int LsQueue::delQ()
{
if(this->isEmpty())
throw "LsQueue is empty";
QueNode *delE = this->front;
this->front = this->front->next;
delete delE;
return this->front->data;
}
// for test
LsQueue *lq = new LsQueue();
cout << "is empty : " << lq->isEmpty() << endl;
int e;
for(int i = 0; i < 3; i++)
{
cout << "Input num : "; cin >> e;
lq->addQ(e);
}
lq->display();
cout << "************" << endl;
cout << "del : " << lq->delQ() << endl;
cout << "************" << endl;
lq->display();