LinkedStack链式栈
链式栈无栈满问题,空间可扩充
插入与删除仅在栈顶处执行
链式栈的栈顶在链头
适合于多栈操作
主函数
#include "LinkedStack.h"
#include <fstream>
#include <cassert>
using namespace std;
int main(){
LinkedStack<int> sta;
ifstream fin("data.txt");
assert(fin);
int data;
while (!fin.eof()){
assert(fin >> data);
sta.Push(data);
}
cout << "The initial Stack in the file is:\n" << sta;
cout << "The current size of the Stack is: " << sta.getSize() << endl;
sta.getTop(data);
cout << "The current Top of the Stack is : " << data << endl;
sta.Pop(data);
cout << "\nDo a Pop operation, then the stack is:\n" << sta << endl;
cout << "The data popped is: " << data << endl;
sta.getTop(data);
cout << "The current Top of the Stack is : " << data << endl;
cout << "\nTest the state of the stack:\n";
if (sta.IsEmpty()) cout << "The stack is empty now!\n";
else if (sta.IsFull()) cout << "The stack is full now!\n";
else cout << "The stack is not empty and not full now!\n";
sta.makeEmpty();
cout << "Now make the stack empty, then the state of the stack is:\n";
if (sta.IsEmpty()) cout << "The stack is empty now!\n";
else if (sta.IsFull()) cout << "The stack is full now!\n";
else cout << "The stack is not empty and not full now!\n";
return 0;
}
StackNode结点类定义
template <typename T>struct StackNode{
T data;
StackNode<T> *link;
StackNode(T d = 0, StackNode<T> *next = NULL):link(next),data(d){}
};
LinkedStack类定义
template <typename T>class LinkedStack{
private:
StackNode<T> *top;
public:
LinkedStack():top(NULL){};
~LinkedStack(){
makeEmpty();
}
void Push(const T &x);
bool Pop(T &x);
bool getTop(T &x)const;
int getSize()const;
bool IsEmpty()const{
return top == NULL;
}
bool IsFull()const{
return false; //我们等会儿看看这个sb函数有没有被调用
}
void makeEmpty();
friend ostream& operator << (ostream &os, LinkedStack<T> &s){
os << "Stack Size: " << s.getSize() << endl;
StackNode<T> *p = s.top;
int i = 0;
while (p){
os << ++i << ": " << p->data << endl;
p = p->link;
}
return os;
}
};
makeEmpty函数定义
从栈链头开始删除
template <typename T>void LinkedStack<T>::makeEmpty(){
StackNode<T> *p;
while (top){//最后top为NULL
p = top;
top = top->link;
delete p;
}
}
Pop和Push函数
template <typename T>void LinkedStack<T>::Push(const T &x){
top = new StackNode<T>(x, top); //先执行赋值语句右边这个内存分配语句
//然后移动top指针到新到结点
assert(top);
}
template <typename T>bool LinkedStack<T>::Pop(T &x){
if (IsEmpty()){
return false;
}
StackNode<T> *p = top;
top = top->link;
x = p->data;
delete p;
return true;
}
getTop和getSize函数
template <typename T>bool LinkedStack<T>::getTop(T &x)const{
if (IsEmpty()) return false;
x = top->data;
return true;
}
template <typename T>int LinkedStack<T>::getSize()const{
StackNode<T> *p = top;
int k = 0;
while (p){
p = p->link;
k++;
}
return k;
}
整一个头文件
#ifndef LINKEDSTACK_H
#define LINKEDSTACK_H
#include <iostream>
#include <cassert>
using namespace std;
template <typename T>struct StackNode{
T data;
StackNode<T> *link;
StackNode(T d = 0, StackNode<T> *next = NULL):link(next),data(d){}
};
template <typename T>class LinkedStack{
private:
StackNode<T> *top;
public:
LinkedStack():top(NULL){}//ÎÞÍ·½áµã
~LinkedStack(){
makeEmpty();
}
void Push(const T &x);
bool Pop(T &x);
bool getTop(T &x)const;
int getSize()const;
bool IsEmpty()const{
return top == NULL;
}
bool IsFull()const{
return false;
}
void makeEmpty();
friend ostream& operator << (ostream &os, LinkedStack<T> &s) {
os << "Stack Size: " << s.getSize() << endl;
StackNode<T> *p = s.top;
int i = 0;
while (p){
os << ++i << ": " << p->data << endl;
p = p->link;
}
return os;
}
};
template <typename T>void LinkedStack<T>::makeEmpty(){
StackNode<T> *p;
while (top){//×îºótopΪNULL
p = top;
top = top->link;
delete p;
}
}
template <typename T>void LinkedStack<T>::Push(const T &x){
top = new StackNode<T>(x, top);
assert(top);
}
template <typename T>bool LinkedStack<T>::Pop(T &x){
if (IsEmpty()){
return false;
}
StackNode<T> *p = top;
top = top->link;
x = p->data;
delete p;
return true;
}
template <typename T>bool LinkedStack<T>::getTop(T &x)const{
if (IsEmpty()) return false;
x = top->data;
return true;
}
template <typename T>int LinkedStack<T>::getSize()const{
StackNode<T> *p = top;
int k = 0;
while (p){
p = p->link;
k++;
}
return k;
}
#endif