数据结构与算法:栈笔记

栈的两种常见表达形式:链表&&数组

链表形式

// function:
Struct Node;
typedef struct Node* Stack;

Stack CreateStack() ;  		//创建stack
int IsEmpty(Stack S); 			
void DisposeStack(Stack S);	//销毁stack
void MakeEmpty(Stack S); 		//格式化stack
void Push(int x, Stack S);		//push in
void Pop(Stack S);					//pop out
int Top(Stack S);			//返回最顶部元素

struct Node
{
	int nul;
	Stack next;
}

// 创建

Stack CreateStack()
{
    
    
	Stack S;
	S = new struct Node;
	if(S == nullptr)
	{
    
    
		cerr<<"Out of space\n";
		return S;
	}
	S->next = nullptr;
	return S;
}

//格式化

void MakeEmpty(Stack S)
{
    
    
	if(S == nullptr)
	{
    
    
		cerr<<"no static\n";
		exit(EXIT_FAILURE);
	}
	while(!IsEmpty(S))
	{
    
    
		Pop(S);
	}
}

检验是否有static,在检查是否是空,不是则pop

//Push和Pop

void Pop(Stack S)
{
    
    
	if(S->next == nullptr)
	{
    
    
		cerr<<"Empty stack\n" ;
		return ;
	}
	Stack temp = S->next;
	S->next = temp->next;
	delete temp ;
}

void Push(int x,Stack S)
{
    
    
	Stack temp;
	temp = new struct Node;
	if(temp == nullptr)
	{
    
    
		cerr<<"out of space\n";
		return ;
	}
	temp->num =x;
	temp->next = S->next;
	S->next = temp;
}

注意栈表的Base就是头结点,Top就是头结点后面那个结点

//返回top

int Top(Stack S)
{
    
    
	if(S->next == nullptr)
	{
    
    
		cerr<<"Empty stack\n";
		return 0;
	}
	return S->next->num;
}

//销毁

void DisposeStack(Stack S)
{
    
    
	while(S->next!=nullptr)
		Pop(S);
	delete S;
}

//main.cpp

#include"stack.h"
#include<ctime>
int main()
{
    
    
	Stack s1;
	s1 = CreateStack();
	srand(time(0));
	int k=20,i=0;
	cout<<"This is Push:\n";
	while(i<k)
	{
    
    
		Push((int)rand()%100,s1);
		cout<<i<<": "<<Top(s1)<<endl;
		i++;
	}
	cout<<"This is Pop:\n";
	while(!IsEmpty(s1))
	{
    
    
		cout<<--i<<": "<<Top(s1)<<endl;
		Pop(s1);
	}
	DisposeStack(s1);
	return 0;
}

在这里插入图片描述

数组形式

栈要设置最大容量,base设为-1,然后用capacity来保存最大容量。然后设一个指向数组的int指针(其他类型自便)

//function
struct Record
{
	int Capacity;
	int Top;
	int* Array;
};

typedef struct Record* Stack;
Stack CreateStack(int MaxSize);
int IsEmpty(Stack S);
int IsFull(Stack S);
void MakeEmpty(Stack S);
void Push(int x,Stack S);
void Pop(Stack S);
int Top(Stack S);
void DisposeStack(Stack S);
int showpos(Stack S);
int TopAndPop(Stack S);

//创建

Stack CreateStack(int MaxSize)
{
    
    
	Stack S;
	if(MaxSize<MINSIZE)
	{
    
    
		cerr<<"Too small\n";
		exit(EXIT_FAILURE);
	}
	S = new struct Record;			//first new
	S->Capacity = MaxSize;
	S->Top = BASE;
	S->Array = new int[MaxSize];		//second new
	if(S->Array == nullptr)
	{
    
    
		cerr<<"out of space\n";
		exit(EXIT_FAILURE);
	}
	return S; 
}

要用到两次new

//isempty&&isfull

int IsEmpty(Stack S)
{
    
    
	return S->Top == BASE;
}

int IsFull(Stack S)
{
    
    
	return S->Top == S->Capacity-1 ;   
}

检查isfull的时候,是看top是否等于最大容量-1,因为数组从0开始,且下面push的方式决定。

//格式化、push&&pop

void MakeEmpty(Stack S)
{
    
    
	S->Top = BASE; 
}

void Push(int x,Stack S)
{
    
    
	if(IsFull(S))
	{
    
    
		cerr<<"No space\n";
		return;
	}
	else
		S->Array[++S->Top] = x;
}

void Pop(Stack S)
{
    
    
	if(IsEmpty(S))
	{
    
    
		cerr<<"Empty stack\n";
		return;
	}
	S->Top -- ;
}

makeempty的方法直接让top等于base就好,不用理会Array,因为等会如果用到会直接把他修改掉。Push的top是++top

//销毁

void DisposeStack(Stack S)
{
    
    
	if(S!= nullptr)
	{
    
    
		delete [] S->Array;
		delete S;
	}
}

用两个delete

//top&pop

int TopAndPop(Stack S)
{
    
    
	if(IsEmpty(S))
	{
    
    
		cerr<<"empty stack\n";
		exit(EXIT_FAILURE);
	}
	return S->Array[S->Top--];
}

这样主程序直接调用会美观很多

//main.cpp

#include"stacka.h"
#include<ctime> 
#include<iomanip>
int main()
{
    
    
	Stack s1;
	s1 = CreateStack(15);
	srand(time(0));
	while(!IsFull(s1))
	{
    
    
		Push(rand()%100,s1);
		cout<<setw(2)<<showpos(s1)<<" : "<<Top(s1)<<endl;
	}	
	
	cout<<"This is the content of Stack:\n";
	while(!IsEmpty(s1))
	{
    
    
		cout<<setw(2)<<showpos(s1)+1<<" : "<<TopAndPop(s1)<<endl;
	}
	return 0;
}

在这里插入图片描述
充分体现了LIFO

猜你喜欢

转载自blog.csdn.net/ZmJ6666/article/details/108721844
今日推荐