栈的实现
- 栈
-
- 顺序栈(语言c++,思想:面向过程)
-
- 结构体的定义
- 基本函数概览
-
- 创建一个空栈
- 判断栈是否为空
- 入栈
- 出栈
- 获得栈顶元素
- 输出栈的基本信息
- 主函数及调试分析
- 链栈(运用C++模板类,面向过程)
-
- 结构体定义
-
- 创建一个空栈
- 判断栈是否为空
- 返回栈的大小
- 入栈
- 出栈
- 获得栈顶元素
- 获得栈底元素
- 输出栈的基本信息
- 主函数及调试分析
- 附录
-
栈
顺序栈(语言c++,思想:面向过程)
结构体的定义
const int MAX_SIZE = 10;
struct Stack{
int data[MAX_SIZE];
int top ;};
基本函数概览
Stack *create();
bool isEmpty(Stack *L);
int push(Stack *s,int x);
int pop(Stack *s);
int getTop(Stack *s);
void output(Stack *s);
创建一个空栈
Stack *create()
{ cout<<"Create a empty stack"<<endl;
Stack *s ;
s = new Stack;
s->top = -1;
return s;
}
判断栈是否为空
bool isEmpty(Stack *L)
{
if(L->top == -1)
return 1;
else
return 0;
}
入栈
int push(Stack *s,int x)
{
if(s->top == MAX_SIZE - 1)
{
cout<<" 栈已满"<<endl ;
return 0;
}
s->data[++s->top] = x;
return 1;
}
出栈
int pop(Stack *s)
{
if(isEmpty(s))
{ cout<<" 栈已空"<<endl;
return 0;
}
int x = s->data[s->top];
s->top--;
return x;
}
获得栈顶元素
int getTop(Stack *s)
{
if(isEmpty(s))
{cout << " 栈已空"<<endl;}
else
{return s->data[s->top];}
}
输出栈的基本信息
void output(Stack *s)
{
int t = s->top;
cout<<"At this time ,IsEmpty = "<<isEmpty(s)<<endl;
while(t >=0 )
{
if(t == s->top)
cout<<"The top element of the stack is: "<<s->data[t]<<endl;
else if(t == 0)
cout<<"The bottom of the stack element is: "<<s->data[0]<<endl;
else
cout<<"The "<<s->top - t + 1<<" element of stack is: "<<s->data[t]<<endl;
t--;
}
cout<<"The stack has "<<s->top+1<<" elements"<<endl;
}
主函数及调试分析
int main()
{
Stack *s;
s = create();
cout<<"At this time ,IsEmpty = "<<isEmpty(s)<<endl;
cout<<"Pleas input five int data:"<<endl;
for(int i=0;i<5;i++)
{
int t;cin >>t;
push(s,t);
}
cout<<"Push these five datas to stack"<<endl;
output(s);
cout<<endl<<endl;
cout<<"Pop three elems :"<<endl;
pop(s);pop(s);pop(s);
output(s);
}
输出结果展示:
链栈(运用C++模板类,面向过程)
结构体定义
template<typename T>
struct Stack{
T data;
Stack *next;
};
创建一个空栈
template<typename T>
Stack<T> *create()
{
Stack<T> *s ;
s = new Stack<T>;
s->next = NULL;
return s;
}
判断栈是否为空
template<typename T>bool isEmpty(Stack<T> *s)
{
if(s->next == NULL)
return 0;
else
return 1;
}
返回栈的大小
template<typename T>
int size(Stack<T> *s)
{
int count = 0;
Stack<T> *q = s ;
while(q->next != NULL)
{
count++;
q = q->next;
}
return count;
}
入栈
template<typename T>int push(Stack<T> *s ,T x)
{
Stack<T> *p;
p = new Stack<T>;
p->next = s->next;
s->next = p;
p->data = x;
return 1;
}
出栈
template<typename T> T pop(Stack<T> *s)
{
Stack<T> *p;
T t;
p = s->next;
s->next = s->next->next;
t =p->data;
delete p;
return t;
}
获得栈顶元素
template<typename T>
T top(Stack<T> *s)
{
if(s->next != NULL)
return s->next->data;
}
获得栈底元素
template<typename T>T end(Stack<T> *s)
{
Stack<T> *q = s;
while(1)
{
if(q->next == NULL)
{
return q->data;
}
q = q->next;
}
}
输出栈的基本信息
template<typename T>void output(Stack<T> *s)
{
Stack<T> *p=s;
int count=0;
while(p->next != NULL)
{ count++;
if(count == 1)
cout<<" 此时栈顶元素为:"<<p->next->data<<endl;
else if(p->next->next == NULL)
cout<<" 栈底元素为:"<<p->next->data<<endl;
else
cout<<" 第"<<count<<" 号元素为:"<<p->next->data<<endl;
p=p->next;
}
if(count == 0)
cout<<" 该栈中没有元素"<<endl;
else
cout<<" 该栈中共有"<<count<<" 个元素"<<endl;
}
主函数及调试分析
int main()
{ Stack<int> *s;s=create<int>();
cout<<" IsEmpty? "<<isEmpty(s)<<endl;
output(s);
cout<<" 输入五个int型元素,并压入栈中:";
for(int i=0;i<5;i++)
{ int t; cin >> t ;push(s,t); }
output(s);cout<<endl;
cout<<" 压出两个元素后"<<endl;
pop(s); pop(s);
output(s);
cout<<endl;
cout<<" 再压出三个元素后"<<endl;
pop(s);pop(s);pop(s);
output(s);
}
效果展示:
附录
顺序栈源代码
#include
const int MAX_SIZE = 10;
struct Stack{
int data[MAX_SIZE];
int top ;};
using namespace std;
Stack *create();
bool isEmpty(Stack *L);
int push(Stack *s,int x);
int pop(Stack *s);
int getTop(Stack *s);
void output(Stack *s);
int main()
{
Stack *s;
s = create();
cout<<"At this time ,IsEmpty = "<<isEmpty(s)<<endl;
cout<<"Pleas input five int data:"<<endl;
for(int i=0;i<5;i++)
{
int t;cin >>t;
push(s,t);
}
cout<<"Push these five datas to stack"<<endl;
output(s);
cout<<endl<<endl;
cout<<"Pop three elems :"<<endl;
pop(s);pop(s);pop(s);
output(s);
}
void output(Stack *s)
{
int t = s->top;
cout<<"At this time ,IsEmpty = "<<isEmpty(s)<<endl;
while(t >=0 )
{
if(t == s->top)
cout<<"The top element of the stack is: "<<s->data[t]<<endl;
else if(t == 0)
cout<<"The bottom of the stack element is: "<<s->data[0]<<endl;
else
cout<<"The "<<s->top - t + 1<<" element of stack is: "<<s->data[t]<<endl;
t--;
}
cout<<"The stack has "<<s->top+1<<" elements"<<endl;
}
Stack *create()
{ cout<<"Create a empty stack"<<endl;
Stack *s ;
s = new Stack;
s->top = -1;
return s;
}
bool isEmpty(Stack *L)
{
if(L->top == -1)
return 1;
else
return 0;
}
int push(Stack *s,int x)
{
if(s->top == MAX_SIZE - 1)
{
cout<<" 栈已满"<<endl ;
return 0;
}
s->data[++s->top] = x;
return 1;
}
int pop(Stack *s)
{
if(isEmpty(s))
{ cout<<" 栈已空"<<endl;
return 0;
}
int x = s->data[s->top];
s->top--;
return x;
}
int getTop(Stack *s)
{
if(isEmpty(s))
{cout << " 栈已空"<<endl;}
else
{return s->data[s->top];}
}
链栈源代码
#include
using namespace std;
template<typename T>
struct Stack{
T data;
Stack *next;
};
template<typename T>
Stack<T> *create()
{
Stack<T> *s ;
s = new Stack<T>;
s->next = NULL;
return s;
}
template<typename T>
int size(Stack<T> *s)
{
int count = 0;
Stack<T> *q = s ;
while(q->next != NULL)
{
count++;
q = q->next;
}
return count;
}
template<typename T>
T top(Stack<T> *s)
{
if(s->next != NULL)
return s->next->data;
}
template<typename T>T end(Stack<T> *s)
{
Stack<T> *q = s;
while(1)
{
if(q->next == NULL)
{
return q->data;
}
q = q->next;
}
}
template<typename T>bool isEmpty(Stack<T> *s)
{
if(s->next == NULL)
return 0;
else
return 1;
}
template<typename T>int push(Stack<T> *s ,T x)
{
Stack<T> *p;
p = new Stack<T>;
p->next = s->next;
s->next = p;
p->data = x;
return 1;
}
template<typename T> T pop(Stack<T> *s)
{
Stack<T> *p;
T t;
p = s->next;
s->next = s->next->next;
t =p->data;
delete p;
return t;
}
template<typename T>void output(Stack<T> *s)
{
Stack<T> *p=s;
int count=0;
while(p->next != NULL)
{ count++;
if(count == 1)
cout<<" 此时栈顶元素为:"<<p->next->data<<endl;
else if(p->next->next == NULL)
cout<<" 栈底元素为:"<<p->next->data<<endl;
else
cout<<" 第"<<count<<" 号元素为:"<<p->next->data<<endl;
p=p->next;
}
if(count == 0)
cout<<" 该栈中没有元素"<<endl;
else
cout<<" 该栈中共有"<<count<<" 个元素"<<endl;
}
int main()
{ Stack<int> *s;s=create<int>();
cout<<" IsEmpty? "<<isEmpty(s)<<endl;
output(s);
cout<<" 输入五个int型元素,并压入栈中:";
for(int i=0;i<5;i++)
{ int t; cin >> t ;push(s,t); }
output(s);cout<<endl;
cout<<" 压出两个元素后"<<endl;
pop(s); pop(s);
output(s);
cout<<endl;
cout<<" 再压出三个元素后"<<endl;
pop(s);pop(s);pop(s);
output(s);
}