# Data structure - stack and queue

Keywords: data structure

# Stack and queue

## Stack

### Concept of stack:

stack is a linear table that allows insertion and deletion at one end. It is a linear table with limited operation.

• Insert and delete operations are only allowed at one end, which is called the top of the stack and the other end is called the bottom of the stack
• The insertion of stack is called "stacking" or "stacking".
• The deletion of stack is called "out of stack" or "out of stack".
• When there are no elements in the stack, it is called "empty stack".

### Sequential stack:

The data elements in the stack are implemented with a preset one-dimensional array of sufficient length: ElemType elem[Max]

The top of the stack changes with insertion and deletion. int top is used as the pointer to the top of the stack to indicate the position of the current top of the stack,

Both elem and top are encapsulated in one structure.

```#define Max 100
typedef struct
{
ELemType elem[Max];
int top;
}SeqStack;
```

Define a pointer to the sequential stack:

```SeqStack *s;
```

Generally, the 0 subscript break is set to the bottom of the stack, so that when the stack is empty, the top pointer s - > Top = - 1; the top pointer + 1, that is, s - > Top + +;

The basic operations of the sequence stack are as follows:

(1) Empty stack:

```SeqStack *InitStack()
{
SeqStack *s;
s=(SeqStack *)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
```

(2) Empty stack

```int empty(SeqStack *s)
{
if(s->top==-1)
return 1;
else
return 0;
}
```

(3) Stack

```int Push(SeqStack *s,ElemType x)
{
if(s->top==Max-1)
return 0;
else
{
s->top++;
s->elem[s->top]=x;
return 1;
}
}
```

(4) Out of stack

```int Pop(SeqStack *s,ElemType *x)
{
if(empty(s))
return 0;
else
{
*x=s->elem[s->top];
s->top--;
return 1;
}
}
```

(5) Get stack top element

```ElemType GetTop(SeqStack *s)
{
if(empty(s))
return 0;
else
return (s->elem[s->top]);

}
```

### Chain storage of stack

Characteristics of chain stack:

• It will not cause space overflow like stack.
• In a chain stack, the bottom of the stack is the last node of the linked list, and the top of the stack is always the first node of the linked list.

The C language definition of chain stack is as follows:

```typedef struct Stacknode
{
DataType data;
struct Stacknode *next;
} slStacktype;
```

(1) Stack operation:

```//Push the element x into the top of the chain stack
int PushLstack(slStacktype *top,DataType x)
{
slStacktype *p;//Apply for a node
if((p=(slStacktype *)malloc(sizof(slStacktype))==NULL)
return 0;
p->data=x;
p->next=top->next;
top->next=p;
return 1;
}
```

(2) Out of stack operation

```//Delete stack top element from chain stack top
DataType PopLstack(slStacktype *top)
{
slStack *p;
DataType x;
if(top->next==NULL)//Empty stack
{
printf("This stack is empty!");
return 0;
}
p=top->next;
top->next=p->next;
x=p->data;
free(p);
return x;
}
```

## Queue

### Concept of queue:

A queue is a finite linear table. First in, first out

• It is only allowed to be inserted at one end of the table, which is called the end of the queue.
• The deletion is to be at the other end of the table, which is called the head of the team.
• The insertion operation is called "joining" or "joining", and the deletion operation is called "leaving" or "leaving".
• When there are no elements in the queue, it is called "empty queue".

The types of sequential queues are defined as follows:

```#define Max 100
typedef struct
{
ElemType elem[Max];
}SeQueue;
```

Define a pointer variable to the queue:

```SeQueue *sq;
```

Request storage space for a sequential queue:

```sq=(SeQueue *)malloc(sizeof(SeQueue));
```

The data area of the queue is:

```sq->elem[0]~elem[Max-1]
```

```sq->front
```

The number of elements in the queue is:

```m=(sq->rear)-(q->front);
```

When setting the empty team:

```sq->front=sq->rear=-1;
```

### Circular queue:

The process of joining and leaving the queue may make the whole queue move backward, as shown in the figure:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-dwfkjv9t-1633783679870) (C: \ users \ onlylove \ appdata \ roaming \ typora \ user images \ image-20211007233919421. PNG)]

Characteristics of circular queue:

• It can solve the phenomenon of false overflow

• It is a circular structure with head and tail connected

• The relationship between the head and tail pointers remains unchanged, that is, the head indicator front always points to the front of the actual queue head element in the queue, while the tail indicator rear always points to the queue tail element.

The operation of adding one to the end of the queue pointer when joining the queue is modified to:

```sq->rear=(sq->rear+1)%Max;
```

The operation of adding one to the queue head pointer when leaving the queue is modified to:

```sq->front=(sq->front+1)%Max;
```

The type of circular queue is defined as follows:

```typedef struct
{
ElemType elem[Max];
int front ,rear;
}CSeQueue;
```

(1) Empty team

```CSeQueue *InitQueue()
{
q=(CSeQueue *)malloc(sizeof(CSeQueue));
q->front=q->reat=Max-1;
return q;
}
```

(2) Join the team

```int InSeQueue(CSeQueue *q,ElemType x)
{
if((q->rear+1)%Max==q->front)
{
printf("The team is full");
return 0;
}
else
{
q->rear=(q->rear+1)%Max;
q->elem[q->rear]=x;
return 1;
}
}
```

(3) Out of the team

```int OutSeQueue(CSeQueue *q,ElemType *x)
{
if(q->front==q->rear)
{
printf("Team air");
return 0;
}
else
{
q->front=(q->front+1)%Max;
*x=q->elem[q->front];
return 1;
}
}
```

(4) Judge the team empty

```int EmptySeQueue(CSeQueue *q)
{
if(q->front==q->rear)
return 0;
else
return 1;
}
```

### Chain queue:

The data types are described as follows:

```typedef struct node
{
DataType data;
struct node *next;
}QNode;
typedef struct
{
QNnode *front;
QNnode *rear;
}LQueue;
```

Define a pointer to the chain queue:

```LQueue *q;
```

The implementation of various operations in the chain queue is also similar to that of the single chain table, except that the insertion operation is limited to the end of the table and the deletion operation is carried out in the header. It should be noted that in the deletion operation of the chain queue, for the special case of only one element node, the tail pointer needs to be modified after deletion.

(1) Create an empty team of leading nodes

```LQueue *InitQueue()
{
LQueue *q;
QNode *p;
q=(LQueue *)malloc(sizeof(LQueue));//Application header and footer pointer node
p=(QNode *)malloc(sizeof(QNode));//Application chain queue header node
p->next=NULL;
q->front=q->rear=p;
return q;
}
```

(2) Join the team

```void InLQueue(LQueue*q,DataType x)
{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
p->data=x;
p->next=NULL;
q->rear->next=p;
q->rear=p;
}
```

(3) Judge the team empty

```int empty(LQueue *q)
{
if(q->front==q->rear)
return 0;
else return 1;
}
```

(4) Out of the team

```int output(LQueue *q,DataType *x)
{
QNode *p;
if(empty(q))
{
printf("Team air");
return 0;
}
else
{
p=q->front->next;
q->front->next=p->next;
*x=p->data;
free(p);
if(q->front->next==NULL)
q->rear=q->front;
return 1;
}
}
```

Posted by thecookie on Sat, 09 Oct 2021 06:14:02 -0700