王道数据结构——顺序栈、链栈实现

顺序栈

初始化

这里的栈顶指针可以置为0或者-1,两者的区别在于入栈还有出栈时候的处理不一致。

void InitStack(SqStack &S){
    
    
	S.top=-1;//初始化栈顶指针 
}

判断栈空

/*判断栈空*/
bool IsEmpty(SqStack &S){
    
    
	return (S.top==-1)?true: false;
}

入栈

如果栈顶指针设置为-1,那么入栈的时候需要先先对栈顶指针+1之后再往栈里写入数据,反之,先写入数据再+1。

bool Push(SqStack &S,ElemType x){
    
    
	if(S.top==MAXSIZE-1){
    
    
		cout<<"栈满,入栈失败!"<<endl;
		return false;//栈满 
	}
	/*
	S.top++;//top指针+1 
	S.data[S.top]=x;//入栈 */
	S.data[++S.top]=x;
	return true;
}

出栈

如果栈顶指针设置为-1,那么出栈的时候元素先出栈,栈顶指针再-1,反之,先减再出栈。

/*出栈 删除元素在栈的位置 */
bool Pop(SqStack &S,ElemType &x){
    
    
	if(S.top==-1){
    
    
		cout<<"空栈,出栈失败!"<<endl;
		return false;
	}
	/*
	x=S.data[S.top];//栈顶元素先出栈
	S.top--; /栈顶指针-1 
	*/
	x=S.data[S.top--];
	return true;
}

读取栈顶元素

与出栈的区别在于:
读取栈顶元素并没有下移栈顶指针

bool GetTop(SqStack &S,ElemType &x){
    
    
	if(S.top==-1){
    
    
		cout<<"空栈,读取失败!"<<endl;
		return false;
	}
	x=S.data[S.top];
	return true;
}

具体实现

#include<iostream>
using namespace std;

#define MAXSIZE 100
typedef int ElemType;
/*栈的结构体定义*/
typedef struct{
    
    
	ElemType data[MAXSIZE];//静态数组存放栈中元素 
	int top;//栈顶指针 
}SqStack;//sequence顺序 
 
 
/*初始化栈*/ 
void InitStack(SqStack &S){
    
    
	S.top=-1;//初始化栈顶指针 
}

/*判断栈空*/
bool IsEmpty(SqStack &S){
    
    
	return (S.top==-1)?true: false;
}

/*进栈*/
bool Push(SqStack &S,ElemType x){
    
    
	if(S.top==MAXSIZE-1){
    
    
		cout<<"栈满,入栈失败!"<<endl;
		return false;//栈满 
	}
	/*
	S.top++;//top指针+1 
	S.data[S.top]=x;//入栈 */
	S.data[++S.top]=x;
	return true;
}

/*出栈 删除元素在栈的位置 */
bool Pop(SqStack &S,ElemType &x){
    
    
	if(S.top==-1){
    
    
		cout<<"空栈,出栈失败!"<<endl;
		return false;
	}
	/*
	x=S.data[S.top];//栈顶元素先出栈
	S.top--; /栈顶指针-1 
	*/
	x=S.data[S.top--];
	return true;
}

/* 读取栈顶元素 */
bool GetTop(SqStack &S,ElemType &x){
    
    
	if(S.top==-1){
    
    
		cout<<"空栈,读取失败!"<<endl;
		return false;
	}
	x=S.data[S.top];
	return true;
}

/*打印栈中的元素*/
void PrintStack(SqStack &S){
    
    
	int js=0;
	for(int i=S.top;i>=0;i--){
    
    
		cout<<S.data[i]<<" ";
		js++;
		if(!(js%5)){
    
    
			cout<<endl;			
		}	
	}
		
	cout<<endl;
}



int main(){
    
    
	SqStack S;
	InitStack(S);
	
	if(IsEmpty(S))cout<<"空栈"<<endl;
	else cout<<"非空栈";
	
	for(int i=0;i<100;i++)
		Push(S,i) ;
		
	PrintStack(S);
	return 0;
}

运行结果:
在这里插入图片描述


链栈

这里给出的代码实现是采用不带头结点的方式。

初始化

/* 初始化链栈*/
void InitStack(LinkStack &S){
    
    
	//创建一个空的链栈,不带头节点,栈顶指针为空。 
	S=NULL;
}

判断是否为空

bool IsEmpty(LinkStack &S){
    
    
	return (!S)?true:false;
}

入栈

bool Push(LinkStack &S,ElemType x){
    
    
	//创建结点 
	Snode *p=(Snode *)malloc(sizeof(Snode));
	if(p==NULL)return false;//创建结点失败 
	
	p->data=x;
	p->next=S; 
	S=p;//S指向p 
	
	return true;
}

出栈

bool Pop(LinkStack &S,ElemType &x){
    
    
	
	if(IsEmpty(S)){
    
    
		cout<<"栈空,出栈失败!";
		return false;
	} 
	
	Snode *p=S;
	x=p->data;
	S=S->next;
	free(p);//释放p结点 
	
	return true;
}

获得栈顶元素

int  GetTop(LinkStack &S){
    
    
	if(S) return S->data;
	cout<<"error!"<<endl;
	return 0;	
}

具体实现

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef int ElemType;

typedef struct Snode{
    
    
	ElemType data;
	struct Snode *next;
}Snode,*LinkStack ;

/* 初始化链栈*/
void InitStack(LinkStack &S){
    
    
	//创建一个空的链栈,不带头节点,栈顶指针为空。 
	S=NULL;
}

/*判断链栈是否为空*/
bool IsEmpty(LinkStack &S){
    
    
	return (!S)?true:false;
}

/*链栈的入栈*/
bool Push(LinkStack &S,ElemType x){
    
    
	//创建结点 
	Snode *p=(Snode *)malloc(sizeof(Snode));
	if(p==NULL)return false;//创建结点失败 
	
	p->data=x;
	p->next=S; 
	S=p;//S指向p 
	
	return true;
}

/*链栈的出栈*/
bool Pop(LinkStack &S,ElemType &x){
    
    
	
	if(IsEmpty(S)){
    
    
		cout<<"栈空,出栈失败!";
		return false;
	} 
	
	Snode *p=S;
	x=p->data;
	S=S->next;
	free(p);//释放p结点 
	
	return true;
}

/*获取链栈的栈顶元素*/
int  GetTop(LinkStack &S){
    
    
	if(S) return S->data;
	cout<<"error!"<<endl;
	return 0;	
}

/*输出链栈*/
void PrintLinkStack(LinkStack &S){
    
    
	Snode *p=S;
	int js=0;
	while(p) {
    
    
		cout<<p->data<<" ";
		if(!(js%5)){
    
    
			cout<<endl;
		}
		js++;
		p=p->next;
	}
	cout<<endl;
}

int main(){
    
    
	LinkStack S;
	InitStack(S);
	if(IsEmpty)cout<<"空链栈"<<endl;
	else cout<<"非空链栈"<<endl;
	
	for(int i=1;i<=100;i++)
		Push(S,i);
	
	PrintLinkStack(S);
	cout<<"当前出栈的元素为:";
	int x;
	Pop(S,x);
	cout<<x<<endl;
	
	cout<<"出栈之后,当前栈顶元素为:";
	x=GetTop(S);
	cout<<x<<endl;

	return 0;
}

运行结果:
在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/qq_42242452/article/details/124555371