当前位置:网站首页>Stack and Queue
Stack and Queue
2022-06-25 05:16:00 【LF19971222】
Stack
Definition :
1、 Linear tables that are only allowed to be inserted and deleted at one end
2、 The side that allows insertion and deletion is called To the top of the stack , The other end is called At the bottom of the stack .
characteristic :
Last in, first out (LIFO)

Diagram of stack in and out
Here top Called top of stack pointer , But it's not really a pointer , It simply points to the element position of the array .
The stack entry operation is to put top++, Re embedded value
The stack out operation is to first fetch the current top Value pointing to position , After the top--.

Framework construction of sequence stack
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
enum ret_val{MALLOC_OK=100,MALLOC_NO,CREATE_OK,CREATE_NO,FULL_OK,FULL_NO,EMPTY_OK,EMPTY_NO,PUSH_OK,PUSH_NO,POP_OK,POP_NO};
struct stack_node
{
int stack_data[SIZE];
int top;
};
typedef struct stack_node Stack;
void create_stack(Stack ** stack)
{
*stack=(Stack *)malloc(sizeof(Stack));
if(MALLOC_OK==is_malloc_ok(Stack *stack))
{
return CREATE_OK;
}
return CREATE_NO;
}
void init_stack(Stack *stack)
{
stack->top=-1;
}
int is_full(Stack * stack)
{
if(stack->top>=SIZE-1)
{
return FULL_OK;
}
return FULL_NO;
}
int push_stack(Stack * stack,int num)
{
if(FULL_NO==is_full(stack)&&CREATE_OK==create_stack(Stack *stack))
{
//stack->top++;
stack->stack_data[++stack->top]=num;
printf("%d ",stack->stack_data[stack->top]);
return PUSH_OK;
}
printf("stack is full!\n");
return PUSH_NO;
}
int is_empty(Stack * stack)
{
if(stack->top<0)
{
return EMPTY_OK;
}
return EMPTY_NO;
}
int pop_stack(Stack * stack)
{
if(EMPTY_NO==is_empty(stack))
{
return stack->stack_data[stack->top--];
}
printf("stack is empty!\n");
return POP_NO;
}
int is_malloc_ok(Stack * stack)
{
if(NULL==stack)
{
return MALLOC_NO;
}
return MALLOC_OK;
}
int main()
{
Stack * stack=NULL;
int i;
int num=0;
create_stack(&stack);
init_stack(stack);
for(i=0;i<10;i++)
{
if(PUSH_OK==push_stack(stack,i+1))
{
printf("push stack success!\n");
}
else
{
printf("push stack fail!\n");
}
}
for(i=0;i<10;i++)
{
num=pop_stack(stack);
if(POP_NO!=num)//pop_stack(stack)
{
printf("%d ",num);//pop_stack(stack)
printf("pop stack success!\n");
}
else
{
printf("pop stack fail!\n");
}
}
free(stack);
return 0;
}Chain stack

There is no stack full problem in this kind of stack , Space expandable , Inserts and deletions are performed only at the top of the stack , The top of the stack is at the head of the chain , Suitable for multi stack operation .
queue
Definition :
Queues are only allowed to be deleted at one end , The end of the linear table inserted at the other end that allows deletion is called the queue head , The end allowed to be inserted is called the end of the line .
characteristic :
fifo (FIFO)
Queue in queue out queue operation
- When entering the team, the pointer at the end of the team is one rear = rear + 1, Then press the new element rear Indicate where to add .
- When leaving the team, the leader of the team is one ahead front = front + 1, And then subscript front Take out the elements of .
- Entering the team when the team is full will overflow and make mistakes ; When the team is empty, go out of the team and deal with it .
- One of the solutions : Put the queue elements in the array, and connect the beginning and end of the array , Form a cycle ( ring ) queue .

Circular queue
Queue storage arrays are treated as end-to-end tables .
Team head 、 End of line pointer plus 1 From the QueueSize -1 Go straight to 0, Modulo of available languages ( remainder ) Operation implementation .
- Team leader pointer advance 1: front = (front+1) % QueueSize;
- At the end of the line 1: rear = (rear+1) % QueueSize;
- Queue initialization :front = rear = 0;
- Team air condition :front == rear;
- Team full conditions :(rear+1) % QueueSize == front

边栏推荐
- Array: force deduction dichotomy
- 2021-03-23
- Object creation and invocation code example
- Jason learning
- UVA816 Abbott’s Revenge
- Mobile number regular expression input box loses focus verification
- Penetration information collection steps (simplified version)
- February 20ctf record
- JS function to realize simple calculator
- CTFHub-rce
猜你喜欢
![[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear](/img/12/d41f8d5abcb61d2632a8b117bf1604.jpg)
[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear

Customize the console plot result style

Detailed summary of flex layout

渗透测试-提权专题

Penetration test - directory traversal vulnerability

2021-03-23

Prototypical Networks for Few-shot Learning

Rce code execution & command execution (V)

Baidu ueeditor set toolbar initial value

Create an environment for new projects
随机推荐
Qdebug June 2022
buuctf web
DOM document object model (I)
Google Earth engine (GEE) - Global jrc/gsw1_ 1 / batch download of yearlyhistory dataset (China region)
Matlab notes
XSS (cross site script attack) summary (II)
Can bus extended frame
What if win11 Bluetooth fails to connect? Solution of win11 Bluetooth unable to connect
TeeChart Pro ActiveX 2022.1
Penetration test - right raising topic
Deeply understand the characteristics of standard flow and off standard elements
A brief talk on media inquiry
Mysql interactive_ Timeout and wait_ Timeout differences
PHP calls map API
Array: force deduction dichotomy
Professional things use professional people
Use serialize in egg to read and write split tables
Read the general components of antd source code
Use js to simply implement the apply, call and bind methods
Fundamentals of C language