Basic operations of stacks and queues of data structures

1. Basic operations when the top of the sequential stack is initialized to -1:

#define Maxsize 50
typedef struct{
	ElemType data[Maxsize];    //存放栈中元素 
	int top;					//栈顶指针 
}SqStack; 

//初始化操作
void InitStack(SqStack &S)
{
	S.top=-1;
 } 
 
 //判断栈是否为空
 bool StackEmpty(SqStack S)
 {
 	if(S.top==-1)
 	return true;
 	else
 	return false;
}

//进栈
bool push(SqStack &S,ElemType x)
{
	if(S.top==Maxsize-1)   //进栈的第一步就是判断栈是否为满 
	{
		return false;
	}
	S.data[++S.top]=x;
	return true;
 } 
 
 //出栈
 bool pop(SqStack &S,ElemType &x)
{
	if(S.top==-1)   //出栈的第一步就是判断栈是否为空 
	{
		return false;
	}
	x=S.data[S.top--];
	return true;
 }  
 
 //读取栈顶元素
 bool GetTop(SqStack S,ElemType &x)
{
	if(S.top=-1)   //读栈的第一步就是判断栈是否为空 
	{
		return false;
	}
	x=S.data[S.top];
	return true;
 }  

2. Basic operations when the top of the sequential stack is initialized to 0: 

#define Maxsize 50
typedef struct{
	ElemType data[Maxsize];    //存放栈中元素 
	int top;					//栈顶指针 
}SqStack; 

 //初始化操作
void InitStack(SqStack &S)
{
	S.top=0;
 } 
 
 //判断栈是否为空
 bool StackEmpty(SqStack S)
 {
 	if(S.top==0)
 	return true;
 	else
 	return false;
}

//进栈
bool push(SqStack &S,ElemType x)
{
	if(S.top==Maxsize)   //进栈的第一步就是判断栈是否为满 
	{
		return false;
	}
	S.data[S.top++]=x;
	return true;
 } 
 
 //出栈
 bool pop(SqStack &S,ElemType &x)
{
	if(S.top==0)   //出栈的第一步就是判断栈是否为空 
	{
		return false;
	}
	x=S.data[--S.top];
	return true;
 }  
 
 //读取栈顶元素
 bool GetTop(SqStack S,ElemType &x)
{
	if(S.top==0)   //读栈的第一步就是判断栈是否为空 
	{
		return false;
	}
	x=S.data[S.top-1];
	return true;
 }  

 3. The basic operation of the chain stack without the head node: 

typedef struct Linknode{
	ElemType data;           //数据域 
	struct Linknode *next;    //指针域 
} *LiStack; 
 
 //初始化栈 
 bool InitStack(LiStack &S)
 {
 	S=(LiStack)malloc(sizeof(LiStack));
 	S->data=0;
 	S->next=NULL;
 }
 
 //入栈 
 bool PushStack(LiStack &S,ElemTyped x)
 {
 	LiStack p;
	p=(LiStack)malloc(sizeof(LiStack));
	p->data=x;
	p->next=S;
	S=p;
	return true;	
 }
 
 //出栈
 bool PopStack(LiStack &S,ElemTyped &x)
 {
 	LiStack p;
 	if(S==NULL)
	 	return false;
	p=S;
	x=S->data;
	S=S->next;
	free(p);
	return true;	
 }
 
 //判断栈是否为空
  bool StackEmpty(LiStack &S)
 {
 	if(S==NULL)
	return true;
	else
	return false;	
 } 

 4. The basic operation of the chain stack with the leading node:  

typedef struct Linknode{
	ElemType data;           //数据域 
	struct Linknode *next;    //指针域 
} *LiStack; 
 
 //初始化栈 
 bool InitStack(LiStack &S)
 {
 	S=(LiStack)malloc(sizeof(LiStack));
 	S->data=0;
 	S->next=NULL;
 }
 
 //入栈 
 bool PushStack(LiStack &S,ElemTyped x)
 {
 	LiStack p;
	p=(LiStack)malloc(sizeof(LiStack));
	p->data=e;
	p->next=S->next;  //由于栈是LIFO的特性,故用头插法 
	S->next=p;
	return true;	
 }
 
 //出栈
 bool PopStack(LiStack &S,ElemTyped &x)
 {
 	LiStack p;
 	if(S->next==NULL)
	 	return false;
	p=S->next;
	x=S->data;
	S->next=p->next;   //只能在头部删除 
	free(p);
	return true;	
 }
 
 //判断栈是否为空
  bool StackEmpty(LiStack &S)
 {
 	if(S->next==NULL)
	return true;
	else
	return false;	
 } 

5. Basic operations of sequentially stored queues:

 (1) Sequential storage of queues

In the sequential storage of the queue, there are two pointers, namely front and rear, the pointer front points to the element at the head of the queue, and the rear pointer at the rear points to the next position of the element at the tail of the queue.

Initial state (queue empty condition): Q.front=Q.rear=0;

Entering the queue operation: when the queue is not full, first send the value to the end of the queue, and then add 1 to the pointer at the end of the queue;

Dequeue operation: when the team is not empty, first fetch the element at the head of the queue, and then add 1 to the pointer at the head of the queue;

 (1.1) Circular queue

Initially (empty condition): Q.front=Q.rear=0;

Team head pointer enters 1: Q.front=(Q.front+1)%Maxsize;

The tail pointer enters 1: Q.rear=(Q.rear+1)%Maxsize;

Queue length: (Q.rear-Q.front+Maxsize)%Maxsize;

Team full condition: (Q.rear+1)%Maxsize==Q.front; or Q.front=Q.rear&&Q.size==Maxsize;or Q.front=Q.rear&&tag=1;

(2) Basic operations of circular queues:

#define Maxsize 50
typedef struct {
	ElemType data[Maxsize];      //存放队列元素
	int front,rear;             //存放队头和队尾指针 
}SqQueue; 

//初始化操作
void InitQueue(SqQueue &Q)
{
	Q.front=Q.rear=0;
 } 
 
 //判断队空
 bool Isempty(SqQueue Q)
 {
 	if(Q.front==Q.rear)
 	return true;
 	else
 	return false;
  } 
  
 //入队
 bool EnQueue(SqQueue &Q,ElemType x)
 {
 	if((Q.rear+1)%Maxsize==Q.front)
 	return false;
 	Q.data[Q.rear]=x;
 	Q.rear=(Q.rear+1)%Maxsize;
 	return true;
  } 
  
  //出队
bool DeQueue(SqQueue &Q,ElemType &x)
 {
 	if(Q.rear==Q.front)
 	return false;
 	x=Q.data[Q.front];
 	Q.front=(Q.front+1)%Maxsize;
 	return true;
  } 

 (3) The basic operation of the chain queue of the leading node:

typedef struct LinkNode{
	ElemType data;                //存放队列结点 
	struct LinkNode *next;
}LinkNode; 

typedef struct{
	LinkNode *front,*rear;      //队列的队头和队尾指针 
}LinkQueue;
  
//初始化
void InitQueue(LinkQueue &Q)
{
	Q.front=Q.rear=(LinkNode *)malloc(sizeof(LinkNode));  //建立头结点
	Q.front->next=NULL; 
 } 
 
 //判队空
 bool Isempty(LinkQueue Q)
 {
 	if(Q.front==Q.rear)
 	return true;
 	else
 	return false;
  } 
  
//入队
void EnQueue(LinkQueue &Q,ElemType x)
{
	LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
	s->data=x;
	s->next=NULL;
	Q.rear->next=s;
	Q.rear=s;
 } 
 
 //出队
 void DeQueue(LinkQueue &Q,ElemType &x)
{
	if(Q.front==Q.rear)
	return false;
	LinkNode *p=Q.front->next;
	x=p->data;
	Q.front->next=p->next;
	if(Q.rear==p)   //判断是不是最后一个结点 
	{
		Q.rear=Q.front;
	} 
	free(p);
	return true; 
 } 

(4) The basic operation of the chain queue without the leader node:

typedef struct LinkNode{
	ElemType data;                //存放队列结点 
	struct LinkNode *next;
}LinkNode; 

typedef struct{
	LinkNode *front,*rear;      //队列的队头和队尾指针 
}LinkQueue;
  
//初始化
void InitQueue(LinkQueue &Q)
{
	Q.front=Q.rear=NULL; 
 } 
 
 //判队空
 bool Isempty(LinkQueue Q)
 {
 	if(Q.front==NULL)
 	return true;
 	else
 	return false;
  } 
  
//入队
void EnQueue(LinkQueue &Q,ElemType x)
{
	LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
	s->data=x;
	s->next=NULL;
	if(Q.front==NULL)  //第一个结点 
	{
		Q.front=s;
		Q.rear=s;
	}
	else
	{
		Q.rear->next=s;
		Q.rear=s;	
	}
 } 
 
 //出队
 void DeQueue(LinkQueue &Q,ElemType &x)
{
	if(Q.front==NULL)
	return false;
	LinkNode *p=Q.front;
	x=p->data;
	Q.front=p->next;
	if(Q.rear==p)   //判断是不是最后一个结点 
	{
		Q.rear=Q.front=NULL;
	} 
	free(p);
	return true; 
 } 

Guess you like

Origin blog.csdn.net/m0_51769031/article/details/125037579