Stack and queue of C + + Learning (C language part)

Posted by mckooter on Mon, 09 Dec 2019 08:56:01 +0100

data structure
1. Save data 2. Process data
Array + operation
Censored deletion

Stack and queue
It is a linear table with limited operation

Stack is the operation of inserting and deleting at one end -- > the other end of the stack is called the stack bottom (stack and stack area are two concepts) (it is a data structure)
The queue is first in, first out. It is inserted and deleted at both ends. At one end of the insertion, it is called the end of the queue. At the other end of the deletion, it is called the head of the queue

Stack needs to backoff operation back to the previous step (repentance)
Queues are mostly used for time-related messages, such as messages (mouse click messages, keyboard messages), first in, first out processing -- > server requests
(take a disgusting example of spitting -- > stack eating and pulling -- > queue)

Example
1. Base conversion (stack)
2. Example of queue entering and leaving

 

 

 

The test code notes are as follows:

  1 //Using stack to realize base conversion
  2 #if 0
  3 #include<stdio.h>
  4 #include<stdbool.h>  //Judge true or false c++Medium
  5     //Stack 1.Linear list (realization of sequential list linked list)
  6 struct STACK
  7 {
  8     int arr[40];  //array
  9     int size;  //Stack size how many elements can be stored in the stack at most
 10     int top;  //The top stack doesn't care about how many elements there are. It's about where to insert and delete
 11 };
 12 
 13 //Initialization stack
 14 void initStack(struct STACK*p)  
 15 {
 16     p->top = 0;  //Indicates that there are no elements
 17     p->size = 20;  //Stack size
 18     //The top of the stack is the location of the data to be inserted
 19     //Is the stack full  top==size
 20     //top==0  Indicates that there are no elements in the stack
 21 }
 22 
 23 //Insertion and insertion of stack/Pressing stack
 24 void pushStack(struct STACK*p,int data)
 25 {
 26     if (p->top >= p->size)//The stack is full.
 27     {
 28         printf("Stack full, stack pressing failed!\n");
 29     }
 30     else
 31     {
 32         p->arr[p->top++] = data;
 33     }
 34 }
 35 
 36 //Delete stack
 37 int popStack(struct STACK*p)  //To get an element, you need to get the return value of the element
 38 {
 39     if (p->top <= 0)  //Stack space
 40     {
 41         printf("No element, stack out failed!\n");
 42         return 0;
 43     }
 44     else
 45     {
 46         return p->arr[--(p->top)];  //Last element
 47     }
 48 }
 49 
 50 //Judge whether the stack is empty
 51 int isStackEmpty(struct STACK*p)  //Used when operating the stack 
 52 {
 53     return p->top <= 0;  //If the return value is 1, the stack is empty
 54 }
 55 
 56 int main()
 57 {
 58     //Base conversion code realizes division of 2 to 0
 59     //Stack implementation remainder stack
 60     //Decimal conversion
 61     int x = 66666;
 62     printf("The values to convert are:%d\n", x);
 63     //scanf_s("%d", &x);
 64     struct STACK stack;
 65     initStack(&stack); //Stack initialization
 66     while (x != 0)
 67     {
 68         pushStack(&stack, x % 2);
 69         x /= 2;  //or x>>=1;  Moving right equals 1, which is faster than division
 70     }
 71     //After entering the stack, exit the stack
 72     printf("The converted binary is:");
 73     while (isStackEmpty(&stack) != 1)  //Indicates that the stack is not empty
 74     {
 75         printf("%d", popStack(&stack));  //Stack out  %x Print hex
 76     }
 77     //For other bases  
 78     //Stack processing 10-15  use A-F replace -->if
 79     getchar();
 80     return 0;
 81 }
 82 #endif
 83 
 84 /***************************Dividing line*********************************/
 85 //Example queue end insert queue head delete 
 86 #if 1
 87 //team
 88 struct QUEUE
 89 {
 90     int arr[20];
 91     int size;
 92     int begin;  //Team leader
 93     int end;  //Team tail
 94 };
 95 
 96 //Initialization
 97 void initQueue(struct QUEUE*p)
 98 {
 99     p->begin = p->end = 0;  //The queue just started has no elements
100     p->size = 20;  //Array subscript
101     /*
102     begin==end  Team empty
103     end+1-->begin We're full
104     */
105 }
106 
107 //Operation after entering the team
108 void inQueue(struct QUEUE*p, int data)
109 {
110     if ((p->end + 1) % p->size == p->begin)  //Determine whether the queue is full
111     {
112         return;  //When the queue is full, it just means that the exit function has no other meaning
113     }
114     else
115     {
116         p->arr[p->end++] = data;  //insert
117         p->end %= p->size;  //ensure end+1 The last end Or less than size
118     }
119 }
120 
121 //Team leader operation
122 int outQueue(struct QUEUE*p)
123 {
124     if (p->begin == p->end)  //Judge whether the team is full if the team is empty or not
125     {
126         return 0;  //Return a 0
127     }
128     else
129     {
130         int x = p->arr[p->begin];  //Elements to leave the team
131         p->begin = (p->begin + 1) % p->size;  //Prevent begin After moving back size
132         return x;  //Return elements to be queued
133     }
134 }
135 
136 int main()
137 {
138     struct QUEUE queue;
139     initQueue(&queue);
140     int y = 666;
141     //Join the team
142     while (y != 0)
143     {
144         inQueue(&queue, y % 16);  //Put the rest in the team
145         y >>= 4;  
146     }
147     //Team out
148     while (queue.begin!=queue.end)  
149     {
150         printf("%x", outQueue(&queue));
151     }
152 
153 
154     getchar();
155     return 0;
156 }
157 #endif
158 
159 #if 0
160 //ctf Game code section
161 //flag{c4es4r_variation}
162 //bg[`sZ*Zg'dPfP`VM_SXVd
163 #include<stdio.h>
164 int main()
165 {
166     int arr[] = { 98, 103, 91, 96, 115, 90, 42, 90, 103, 39, 100, 80, 102, 80, 96, 86, 77, 95, 83, 88, 86, 100 };
167     printf("Original output:\n");
168     for (int i = 0; i < 22; ++i)
169     {
170         printf("%d\t", arr[i]);
171     }
172     printf("\n");
173     printf("Carry out operations:\n");
174     int brr[] = { 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25 };
175     printf("Result output:\n");
176     int crr[22];
177     for (int i = 0; i < 22;++i)
178     crr[i] = arr[i] + brr[i];
179     for (int i = 0; i < 22; ++i)
180         printf("%d\t", crr[i]);
181     getchar();
182     return 0;
183 }
184 #endif

 

 

2019-03-31  19:41:48

Topics: C++ less REST