1. 程式人生 > >C++學習(三十)(C語言部分)之 棧和隊列

C++學習(三十)(C語言部分)之 棧和隊列

emp 等於 -s etc amp class font pri 先進先出

數據結構
1.保存數據 2.處理數據
數組+操作
增查刪改

棧和隊列
是一種操作受限的線性表

棧 是先進後出 是在一端進行插入刪除的操作--->棧頂 另一端叫做棧底(棧和棧區是兩個概念)(是一種數據結構)
隊列 是先進先出 是在兩端進行插入刪除的操作 在插入的一端叫做隊尾 在刪除的一端叫做隊頭

棧 需要回退操作 退回上一步(悔棋)
隊列 多用於和時間有關的 比如消息(鼠標點擊消息 鍵盤消息) 先來的先處理-->服務器請求
(舉個比較惡心的例子 吃了吐--->棧 吃了拉---->隊列)

例子
1、進制轉換(棧)
2、隊列的例子 入隊 出隊

測試代碼筆記如下:

  1 //用棧實現 進制轉換
  2 #if 0
  3 #include<stdio.h>
  4 #include<stdbool.h>  //判斷真假 c++中的
  5     //棧 1.線性表 (順序表 鏈表的實現)
  6 struct STACK
  7 {
  8     int arr[40];  //數組
  9     int size;  //棧的大小  棧中最多可以存放多少的元素
 10     int top;  //棧頂 棧中不關心有多少元素 關心的是插入刪除的位置
 11 };
 12 
 13 //初始化棧
 14 void initStack(struct
STACK*p) 15 { 16 p->top = 0; //表示沒有任何元素 17 p->size = 20; //棧的大小 18 //棧頂就是即將要插入的數據的位置 19 //棧 是否是滿 top==size 20 //top==0 表示棧中沒有元素 21 } 22 23 //棧的插入 入棧/壓棧 24 void pushStack(struct STACK*p,int data) 25 { 26 if (p->top >= p->size)//表示棧滿 27 { 28
printf("棧滿,壓棧失敗!\n"); 29 } 30 else 31 { 32 p->arr[p->top++] = data; 33 } 34 } 35 36 //棧的刪除 出棧 37 int popStack(struct STACK*p) //出一個元素 需要在外面得到這個元素 需要返回值 38 { 39 if (p->top <= 0) //棧空 40 { 41 printf("沒有元素,出棧失敗!\n"); 42 return 0; 43 } 44 else 45 { 46 return p->arr[--(p->top)]; //出最後一個元素 47 } 48 } 49 50 //判斷棧是否為空 51 int isStackEmpty(struct STACK*p) //操作棧的時候會用 52 { 53 return p->top <= 0; //返回值是1表示空 不然棧沒空 54 } 55 56 int main() 57 { 58 //進制轉換 代碼實現 不斷除2求余 直到除到0的位置 59 //棧實現 余數棧 60 //十進制轉換 61 int x = 66666; 62 printf("要轉換的值是:%d\n", x); 63 //scanf_s("%d", &x); 64 struct STACK stack; 65 initStack(&stack); //棧初始化 66 while (x != 0) 67 { 68 pushStack(&stack, x % 2); 69 x /= 2; //或x>>=1; 右移等於1相當於除以二 比除法快 70 } 71 //入完棧之後 出棧 72 printf("轉換出的二進制是:"); 73 while (isStackEmpty(&stack) != 1) //表示棧沒有空 74 { 75 printf("%d", popStack(&stack)); //出棧 %x打印16進制 76 } 77 //對於其他進制 78 //入棧 做處理 10-15 用A-F替換 -->if 79 getchar(); 80 return 0; 81 } 82 #endif 83 84 /***************************分割線*********************************/ 85 //隊列的例子 隊尾插入 隊頭刪除 86 #if 1 87 // 88 struct QUEUE 89 { 90 int arr[20]; 91 int size; 92 int begin; //隊頭 93 int end; //隊尾 94 }; 95 96 //初始化 97 void initQueue(struct QUEUE*p) 98 { 99 p->begin = p->end = 0; //剛剛開始隊列沒有元素 100 p->size = 20; //數組下標 101 /* 102 begin==end 隊空 103 end+1-->begin的位置 隊滿 104 */ 105 } 106 107 //入隊 隊尾操作 108 void inQueue(struct QUEUE*p, int data) 109 { 110 if ((p->end + 1) % p->size == p->begin) //判斷隊列是否已滿 111 { 112 return; //隊列已滿情況 只是表示退出函數 沒有別的意思 113 } 114 else 115 { 116 p->arr[p->end++] = data; //插入 117 p->end %= p->size; //保證end+1之後 最後的end還是小於size 118 } 119 } 120 121 //出隊 隊頭操作 122 int outQueue(struct QUEUE*p) 123 { 124 if (p->begin == p->end) //隊空 不操作 判斷隊是否滿 125 { 126 return 0; //返回一個0 127 } 128 else 129 { 130 int x = p->arr[p->begin]; //要出隊的元素 131 p->begin = (p->begin + 1) % p->size; //防止begin往後移動之後 等於size 132 return x; //返回要出隊的元素 133 } 134 } 135 136 int main() 137 { 138 struct QUEUE queue; 139 initQueue(&queue); 140 int y = 666; 141 //入隊 142 while (y != 0) 143 { 144 inQueue(&queue, y % 16); //將余數入隊 145 y >>= 4; 146 } 147 //出隊 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比賽 密碼部分 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("原樣輸出:\n"); 168 for (int i = 0; i < 22; ++i) 169 { 170 printf("%d\t", arr[i]); 171 } 172 printf("\n"); 173 printf("進行運算:\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("結果輸出:\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

C++學習(三十)(C語言部分)之 棧和隊列