数据结构--链式栈--C++实现
生活随笔
收集整理的這篇文章主要介紹了
数据结构--链式栈--C++实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
using namespace std;
template<class T>class Stack
{
private:struct Node{T data;Node *next;};Node *head;Node *p;int length;public:Stack(){head = NULL;length = 0;}void push(T n)//入棧{Node *q = new Node;q->data = n;if (head == NULL){q->next = head;head = q;p = q;}else{q->next = p;p = q;}length++;}T pop()//出棧并且將出棧的元素返回{if (length <= 0){abort();}Node *q;int data;q = p;data = p->data;p = p->next;delete(q);length--;return data;}int size()//返回元素個數{return length;}T top()//返回棧頂元素{return p->data;}bool isEmpty()//判斷棧是不是空的{if (length == 0){return true;}else{return false;}}void clear()//清空棧中的所有元素{while (length > 0){pop();}}
};
int main()
{Stack<char> s;s.push('a');s.push('b');s.push('c');while (!s.isEmpty()){cout << s.pop() << endl;}system("pause");return 0;
}
?
總結
以上是生活随笔為你收集整理的数据结构--链式栈--C++实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谷歌家也没有余粮了:用旧的服务器不要扔
- 下一篇: 谷歌 Chrome 浏览器将推“快速删除