http://www.cnblogs.com/jingliming/p/4602458.html
栈是一种限定只在表尾进行插入或删除操作,栈也是线性表表头称为栈的底部,表尾称为栈的顶部,表为空称为空栈,栈又称为后进先出的线性表,栈也有两种表示:顺序栈与链式栈顺序栈是利用一组地址连续的存储单元,依次存放从栈底到栈顶的数据元素,附设一个指针指示栈顶的元素在栈中的位置。
1 //顺序栈的实现 2 #define INFINITY 65535 3 #define MAXSIZE 1000 4 #define ElemType int 5 6 typedef struct { 7 ElemType data[MAXSIZE]; //栈的大小 8 int top; //栈顶的游标 9 }Stack; 10 11 class ArraryStack{ 12 public: 13 void initStack(Stack *s); //初始化栈 14 15 bool isEmpty(Stack *s); //判断栈是否为空 16 17 ElemType Top(Stack *s); //返回栈顶的元素 18 19 ElemType Pop(Stack *s); //返回并删除栈顶的元素 20 21 void Push(Stack *s,ElemType e); //将元素e压栈 22 23 void Print(Stack *s); //输出从栈底到栈顶的元素 24 25 void Clear(Stack *s); //清空栈元素 26 27 }; 28 29 void ArraryStack::initStack(Stack *s) 30 { 31 s->top=-1; 32 } 33 34 bool ArraryStack::isEmpty(Stack *s) 35 { 36 if (s->top==-1) 37 { 38 return true; 39 } 40 return false; 41 } 42 43 ElemType ArraryStack::Top(Stack *s) 44 { 45 if (!isEmpty(s)) 46 { 47 return s->data[s->top]; 48 } 49 return INFINITY; 50 } 51 52 ElemType ArraryStack::Pop(Stack *s) 53 { 54 if (!isEmpty(s)) 55 { 56 return s->data[s->top--]; 57 } 58 return INFINITY; 59 } 60 61 void ArraryStack::Push(Stack *s,ElemType e) 62 { 63 if(s->top>=MAXSIZE-1) 64 return; 65 ++s->top; 66 s->data[s->top]=e; 67 68 } 69 70 void ArraryStack::Print(Stack *s) 71 { 72 for (int i=0;i<=s->top;i++) 73 { 74 printf("%d ",s->data[i]); 75 } 76 printf("\n"); 77 } 78 79 void ArraryStack::Clear(Stack *s) 80 { 81 s->top=-1; 82 }
第二部分:链式栈的实现
1 //链式栈的声明 2 template<typename T> 3 struct LinkNode{ 4 5 LinkNode* next; 6 T data; 7 }; 8 9 //链式栈的实现 10 template<typename T> 11 class LinkStack 12 { 13 public: 14 LinkStack(); 15 ~LinkStack(); 16 void Push(T value); 17 T Pop(); 18 T Top(); 19 int Size(); 20 bool isEmpty(); 21 22 private: 23 LinkNode<T> *pHead; 24 }; 25 26 template<typename T> 27 LinkStack<T>::LinkStack() 28 { 29 30 pHead=new LinkNode<T>; 31 if(pHead==NULL) 32 cout<<"构建头结点空间失败。"<<endl; 33 else 34 { 35 pHead->next=NULL; 36 pHead->data=NULL; 37 } 38 } 39 40 template<typename T> 41 LinkStack<T>::~LinkStack() 42 { 43 44 } 45 46 template<typename T> 47 void LinkStack<T>::Push(T value) 48 { 49 LinkNode<T> *p=new LinkNode<T>; 50 p->data=value; 51 p->next=pHead->next; 52 pHead->next=p; 53 } 54 55 template<typename T> 56 T LinkStack<T>::Pop() 57 { 58 T value; 59 LinkNode<T> *p=pHead->next; 60 if (p!=NULL) 61 { 62 value= p->data; 63 pHead->next=p->next; 64 delete p; 65 p=NULL; 66 return value; 67 } 68 else 69 { 70 71 cout<<"没有结点"<<endl; 72 return NULL; 73 } 74 75 } 76 77 template<typename T> 78 T LinkStack<T>::Top() 79 { 80 LinkNode<T> *p=pHead->next; 81 if (p!=NULL) 82 { 83 T value=p->data; 84 return value; 85 } 86 else 87 { 88 89 cout<<"头结点为空。"<<endl; 90 return NULL; 91 } 92 } 93 94 template<typename T> 95 int LinkStack<T>::Size() 96 { 97 int count=0; 98 LinkNode<T> *p=pHead->next; 99 while(p!=NULL) 100 { 101 102 ++count; 103 p=p->next; 104 } 105 return count; 106 } 107 108 template<typename T> 109 bool LinkStack<T>::isEmpty() 110 { 111 LinkNode<T> *p=pHead->next; 112 if (p==NULL) 113 { 114 return true; 115 } 116 return false; 117 }
测试用例:
1 int main() 2 { 3 LinkStack<int> sta; 4 sta.Push(1); 5 sta.Push(2); 6 sta.Push(3); 7 cout << "The size of the stack now is " << sta.Size() << endl; 8 sta.Pop(); 9 cout << "The top element is " << sta.Top() << endl; 10 cout << "The size of the stack now is" << sta.Size() << endl; 11 if (sta.isEmpty()) 12 { 13 cout << "This stack is empty." << endl; 14 } 15 else 16 { 17 cout << "This stack is not empty." << endl; 18 } 19 system("pause"); 20 return 0; 21 }