用c++普通语法和模板类实现栈结构

栈的实现

    • 顺序栈(语言c++,思想:面向过程)
      • 结构体的定义
      • 基本函数概览
        • 创建一个空栈
        • 判断栈是否为空
        • 入栈
        • 出栈
        • 获得栈顶元素
        • 输出栈的基本信息
      • 主函数及调试分析
    • 链栈(运用C++模板类,面向过程)
      • 结构体定义
        • 创建一个空栈
        • 判断栈是否为空
        • 返回栈的大小
        • 入栈
        • 出栈
        • 获得栈顶元素
        • 获得栈底元素
        • 输出栈的基本信息
      • 主函数及调试分析
  • 附录
    • 顺序栈源代码
    • 链栈源代码

顺序栈(语言c++,思想:面向过程)

结构体的定义

const int MAX_SIZE = 10;
struct Stack{
	int data[MAX_SIZE];
	int top ;}; 

基本函数概览

Stack *create();//创建一个栈并返回指向该栈的指针 
bool isEmpty(Stack *L);//判断栈s是否为空 
int push(Stack *s,int x);//将元素x压入栈s中 
int pop(Stack *s);//从栈s中压出一个元素,并返回该值 
int getTop(Stack *s);//获得栈顶元素 
void output(Stack *s);//输出栈s的基本信息
创建一个空栈
//创造一个空栈 
Stack *create()
{	cout<<"Create a empty stack"<<endl;
	Stack *s ;
	s = new Stack;//给指针s指向的地方申请空间 
	s->top = -1;
	return s;
}
判断栈是否为空
//判断栈是否为空 
bool isEmpty(Stack *L)
{
	if(L->top == -1)
	return 1;
	else
	return 0;
}
入栈
//把x入栈算法 
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++普通语法和模板类实现栈结构_第1张图片

链栈(运用C++模板类,面向过程)

结构体定义

template<typename T>
struct Stack{
	T data;
	Stack *next;
};
创建一个空栈
//以单链表的头部做栈顶,栈顶指针用s->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;
} 
入栈
//将元素x压入栈中 
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>();//创建栈S
    cout<<" IsEmpty? "<<isEmpty(s)<<endl;//输出isEmpty的值
    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); //输出再压出三个元素后的栈信息}
} 

效果展示:
用c++普通语法和模板类实现栈结构_第2张图片

附录

顺序栈源代码

//栈的顺序储存的实现
#include

const int MAX_SIZE = 10;
struct Stack{
	int data[MAX_SIZE];
	int top ;}; 

using namespace std;
Stack *create();//创建一个栈并返回指向该栈的指针 
bool isEmpty(Stack *L);//判断栈s是否为空 
int push(Stack *s,int x);//将元素x压入栈s中 
int pop(Stack *s);//从栈s中压出一个元素,并返回该值 
int getTop(Stack *s);//获得栈顶元素 
void output(Stack *s);//输出栈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指向的地方申请空间 
	s->top = -1;
	return s;
}
//判断栈是否为空 
bool isEmpty(Stack *L)
{
	if(L->top == -1)
	return 1;
	else
	return 0;
}
//把x入栈算法 
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;
};
//以单链表的头部做栈顶,栈顶指针用s->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;
}

//将元素x压入栈中 
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>();//创建栈S
    cout<<" IsEmpty? "<<isEmpty(s)<<endl;//输出isEmpty的值
    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); //输出再压出三个元素后的栈信息}
} 

你可能感兴趣的:(数据结构与算法,c++,指针,数据结构,算法,栈)