Stack and Queue (Sequential Storage)

Posted by vboyz on Fri, 10 Dec 2021 18:05:11 +0100

Because of the lack of c language library functions, when doing some arithmetic problems, when you want to call some functions of the data structure, you always have to tap them manually or look around, so I think why not summarize some commonly used functions into it. When you write down a small book, you can only use Ctrl+C,V once (doge) to import library functions manually. Well!

Catalog

Stack

Push

Stack Out

Initialization stack

Is stack empty

Is stack full

Top of stack

Status of Print Stack

Stack length

Testing stacks

queue

Entry

Queue

Initialize Queue

Determine if the queue is empty

Determine whether the queue is full or not

queue length

Take Team Leader

Print Team Status

Queue Testing

This time we remember the stack and queue structure, stack and queue are also a linear table in nature, but they give special meaning to the header or tail of the stack, that is, the header element of the stack is called the bottom of the stack, the tail element is called the top of the stack, and related operations are always at the top of the stack, so they are called Last In First Out (LIFO). Linear; whereas queue head elements are called queue heads, and table tail elements are called queue tails, and related operations are always performed in the queue head, opposite to the stack, so they are called linear First In First Out (FIFO). 

Here are some important points:

Stack

First, define a structure stack that contains an array of data [], which holds element data, and then set a top stack pointer (which is not really a pointer to store addresses here, but a marker under the array subscript) to point to the next element on the top of the stack in real time (unlike the one shown above).

typedef struct
{
    SElemType data[MAXSIZE];
    int top;	//For stack top pointer
}SqStack;

Push

The core idea of stacking is to assign an element to the element that top refers to (that is, the next element on the top of the original stack), then move top one bit later.

Status Push(SqStack *S, SElemType e)
{
    if(S->top == MAXSIZE){	//Full stack
    	printf("The stack is full and cannot be stacked");
        return ERROR;
    }
    S->data[S->top] = e;
    S->top++;
    return OK;
}

Stack Out

The core idea of stacking is to move top one bit forward, and then save the element value that top refers to in an element e.

Status Pop(SqStack *S, SElemType *e)
{
    if(S->top == 0){
    printf("The stack is empty, you cannot delete the stack elements!");	
    return ERROR;
    }
    S->top--;	//Stack top pointer-1
    *e = S->data[S->top];	//Assign the top stack element to e for deletion
    return OK;
}

Operations on other stacks:

Initialization stack

Status InitStack(SqStack *S)
{
	S->top = 0;	
	return OK;
}

Is stack empty

Status StackEmpty(SqStack *S)
{		//Empty Return 1 
    if (S->top == 0)
        return TRUE;
    else
        return FALSE;
}

Is stack full

Status StackFull(SqStack *S)
{		//Full Return 1 
    if (S->top == MAXSIZE)
        return TRUE;
    else
        return FALSE;
}

Top of stack

Status GetTop(SqStack *S, SElemType *e){
	if(S->top == 0){
    printf("The stack is empty, you can't get the top of it!");	
    return ERROR;
    }
    *e = S->data[S->top-1];	//The top element of the stack is assigned to e
    return OK;
}

Status of Print Stack

void DisplaySqStack(SqStack *S){
	int i;
	for(i=S->top-1;i>=0;i--){
		printf("%-3d", S->data[i]);
		putchar('\n');
	}
}

Stack length

int SqStackLength(SqStack *S){
	return S->top;
}

Testing stacks

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
//Sequential structure representation of stack
typedef struct
{
    SElemType data[MAXSIZE];
    int top;	//For stack top pointer
}SqStack;
//Initialization stack 
Status InitStack(SqStack *S)
{
	S->top = 0;
	return OK;
}
//Determine if the stack is full 
Status StackFull(SqStack *S)
{		//Full Return 1 
    if (S->top == MAXSIZE)
        return TRUE;
    else
        return FALSE;
}
//Determine if stack is empty
Status StackEmpty(SqStack *S)
{		//Empty Return 1 
    if (S->top == 0)
        return TRUE;
    else
        return FALSE;
}
//Push 
Status Push(SqStack *S, SElemType e)
{
    if(S->top == MAXSIZE){	//Full stack
    	printf("The stack is full and cannot be stacked");
        return ERROR;
    }
    S->data[S->top] = e;
    S->top++;
    return OK;
}
//Stack Out 
Status Pop(SqStack *S, SElemType *e)
{
    if(S->top == 0){
    printf("The stack is empty, you cannot delete the stack elements!");	
    return ERROR;
    }
    S->top--;
    *e = S->data[S->top];	//Assign the top stack element to e for deletion
    return OK;
}
//Gettop 
Status GetTop(SqStack *S, SElemType *e){
	if(S->top == 0){
    printf("The stack is empty, you can't get the top of it!");	
    return ERROR;
    }
    *e = S->data[S->top-1];	//The top element of the stack is assigned to e
    return OK;
}
//Print out stack existing elements 
void DisplaySqStack(SqStack *S){
	int i;
	for(i=S->top-1;i>=0;i--){
		printf("%-3d", S->data[i]);
		putchar('\n');
	}
}
//Return stack length
int SqStackLength(SqStack *S){
	return S->top;
}
int main(){
	SqStack S;
	InitStack(&S);
	int i, n;
	SElemType e;
	printf("How many elements do you need to stack?\n");
	scanf("%d", &n);
	printf("Specific element values\n");
	for(i=0;i<n;i++){
		scanf("%d", &e);
		Push(&S,e);
	}
	printf("Stack Status\n");
	DisplaySqStack(&S);
	printf("After stack operation\n");
	Pop(&S,&e);
	DisplaySqStack(&S);
	printf("The top element of the stack is:");
	GetTop(&S,&e);
	printf("%d\n", e);
	printf("Stack Status\n");
	DisplaySqStack(&S);
	return 0;
}

queue

For practical purposes, circular queues are described here.

First, define a structured queue that contains an array of data [], which holds element data, and then set a queue header (front) and a queue tail pointer (not a pointer to actually store addresses here, but a token under the array subscript) to mark the last element in the queue, respectively. (

When the initial setup queue is empty, simply have the front and rear point to the first element at the same time to prevent False Overflow Status , a circular queue allows a front to be larger than a rear (the queue head is larger than the subscript at the end), but the entire array space is not fully utilized, leaving a place, that is

                      

So determine if the queue is fullTo determine if the queue is empty.

Entry

The core idea of enlistment is to assign element e to the element that rear refers to, and then logically move rear one bit backward, namely rear=(rear+1)%MAXSIZE.

Status EnQueue(SqQueue *Q, QElemType e){
	if((Q->rear+1)%MAXSIZE == Q->front){
		printf("The queue is full and we can't join!");
		return ERROR;
	}
	Q->data[Q->rear]=e;
	Q->rear=(Q->rear+1)%MAXSIZE;
	return OK;
}

Queue

The core idea of queuing is to save the element data referred to by the front in e first, then logically move the front one bit backward, that is, front=(front+1)%MAXSIZE.

Status DeQueue(SqQueue *Q, QElemType *e){
	if(Q->front == Q->rear){
		printf("Queue empty, unable to queue!");
		return ERROR;
	}
	*e = Q->data[Q->front];
	Q->front=(Q->front+1)%MAXSIZE;
	return OK;
}

Operation of other queues:

Initialize Queue

Status InitQueue(SqQueue *Q){
	Q->front=Q->rear=0;
	return OK;
}

Determine if the queue is empty

Status QueueEmpty(SqQueue *Q){
	if(Q->front == Q->rear)
	return TRUE;
	else	return FALSE;
}

Determine whether the queue is full or not

 Status QueueFull(SqQueue *Q){
	if((Q->rear+1)%MAXSIZE == Q->front)
	return TRUE;
	else	return FALSE;
}

queue length

int QueueLength(SqQueue *Q){
	return (Q->rear-Q->front+MAXSIZE)%MAXSIZE;
}

Take Team Leader

Status GetHead(SqQueue *Q, QElemType *e){
	if(Q->front == Q->rear){
		printf("Queue empty, can't fetch the header element!");
		return ERROR;
	}
	*e = Q->data[Q->front];
	return OK;
}

Print Team Status

void DisplayQueue(SqQueue *Q){
	int i;
	if(Q->rear > Q->front){
		for(i=Q->front;i<Q->rear;i++){		//Normal state 
		printf("%-3d", Q->data[i]);
		}
	}
	else{
		for(i=Q->front;i<Q->rear+MAXSIZE;i++){		//False Overflow Status 
		if(i>=MAXSIZE){
			printf("%-3d", Q->data[i-MAXSIZE]);		
		}
		else{
			printf("%-3d", Q->data[i]);			
		}
		}
	}
}

Queue Testing

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 10000
typedef int Status;
typedef int QElemType;
typedef struct{
	QElemType data[MAXSIZE];
	int front, rear;
}SqQueue;
//Initialize Queue 
Status InitQueue(SqQueue *Q){
	Q->front=Q->rear=0;
	return OK;
}
//Determine if the queue is empty 
Status QueueEmpty(SqQueue *Q){
	if(Q->front == Q->rear)
	return TRUE;
	else	return FALSE;
}
//Determine whether the queue is full or not
 Status QueueFull(SqQueue *Q){
	if((Q->rear+1)%MAXSIZE == Q->front)
	return TRUE;
	else	return FALSE;
}
//queue length 
int QueueLength(SqQueue *Q){
	return (Q->rear-Q->front+MAXSIZE)%MAXSIZE;
}
//Entry 
Status EnQueue(SqQueue *Q, QElemType e){
	if((Q->rear+1)%MAXSIZE == Q->front){
		printf("The queue is full and we can't join!");
		return ERROR;
	}
	Q->data[Q->rear]=e;
	Q->rear=(Q->rear+1)%MAXSIZE;
	return OK;
}
//Queue
Status DeQueue(SqQueue *Q, QElemType *e){
	if(Q->front == Q->rear){
		printf("Queue empty, unable to queue!");
		return ERROR;
	}
	*e = Q->data[Q->front];
	Q->front=(Q->front+1)%MAXSIZE;
	return OK;
}
//Take Team Leader 
Status GetHead(SqQueue *Q, QElemType *e){
	if(Q->front == Q->rear){
		printf("Queue empty, can't fetch the header element!");
		return ERROR;
	}
	*e = Q->data[Q->front];
	return OK;
}
//Print Queue 
void DisplayQueue(SqQueue *Q){
	int i;
	if(Q->rear > Q->front){
		for(i=Q->front;i<Q->rear;i++){		//Normal state 
		printf("%-3d", Q->data[i]);
		}
	}
	else{
		for(i=Q->front;i<Q->rear+MAXSIZE;i++){		//False Overflow Status 
		if(i>=MAXSIZE){
			printf("%-3d", Q->data[i-MAXSIZE]);		
		}
		else{
			printf("%-3d", Q->data[i]);			
		}
		}
	}
}
void main(){
	SqQueue Q;
	InitQueue(&Q);
	int i;
	QElemType e, n;
	printf("How many elements do you need to be in the queue?\n");
	scanf("%d", &n);
	printf("Specific element values\n");
	for(i=0;i<n;i++){
		scanf("%d", &e);
		EnQueue(&Q,e);
	}
	printf("Status of the queue\n");
	DisplayQueue(&Q);
	printf("\n After queue operation\n");
	DeQueue(&Q,&e);
	DisplayQueue(&Q);
	printf("\n The queue head elements are:");
	GetHead(&Q,&e);
	printf("%d\n", e);
	printf("Status of the queue\n");
	DisplayQueue(&Q);
	printf("\n Rejoin the team:");
	scanf("%d", &e);
	EnQueue(&Q,e);
	printf("Status of the queue\n");
	DisplayQueue(&Q);
}

Topics: C Algorithm data structure queue stack