特點:管子,兩邊開口,先進先出 (first in first out)
佇列的三個基本元素:乙個陣列,兩個變數
#include
using namespace std;
struct queue
;typedef struct queue queue;
//將 struct queue 重新命名為 queue
intmain()
while
(q.head < q.tail)
return0;
}
特點:管子,一邊開口,後進先出 (last in first out)
棧的基本元素:乙個陣列,乙個變數
#include
using namespace std;
struct stack
;typedef struct stack stack;
//將 struct stack 重新命名為 stack
intmain()
while
(s.top >0)
return0;
}
question:俗稱 「接竹竿」,就是接牌,有相同的將二者之間的牌收入手裡,一方無牌則敗
分析:兩人手中的牌是佇列,桌面上的牌是棧
約定:牌面 1~9
#include
using namespace std;
struct stack
;struct queue
;typedef struct stack stack;
typedef struct queue queue;
intmain()
//讀入牌 每人六張
cout <<
"input q: "
;for
(int i =
0; i <
6; i++)
cout <<
"input p: "
;for
(int i =
0; i <
6; i++
)while
(q.head < q.tail && p.head < p.tail)
// } //用標記陣列來判斷比遍歷棧要方便
// if (flag == 0)
else
while
(s.data[s.top]
!= t)
;//這裡是取了陣列的巧 }if
(q.head == q.tail)
break
;//p出牌
;//malloc 返回的是 void *,需要強制轉換成 int *
鍊錶的實現:
#include
#include
using namespace std;
struct node
;int
main()
else
q = p;
}//乙個長度為 n 的鍊錶建成
//輸出一下
struct node *t;
t = head;
while
(t != null)
//結束時建議用 free 命令釋放動態申請的空間
free
(head)
;free
(q);
free
(p);
free
(t);
return0;
}
插入結點:
int a;
cin >> a;
t = head;
//遍歷
while
(t != null)
}
就是用兩個陣列,其中乙個陣列做資料域的集合,另乙個陣列做指標域的集合
啊哈演算法 第二章 棧 佇列 鍊錶
2.2 解密回文字串 棧 將一串數字奇數字刪除,偶數字依次移動到末尾,直到只剩下乙個數字,最後將奇數字數字依次回歸,得到最終序列。1 我的解法 題目要求將奇數字刪除再歸位,可理解為跳過奇數字數字,再移動偶數字數字。include intmain else getchar getchar return...
啊哈演算法 鍊錶
今天算是對鍊錶和指標有了乙個新的感悟。以前總是朦朧的概念,特別對於malloc 函式,幾乎是死記硬背,現在認識的清晰了一點。感觸如下 陣列 陣列 for int i 0 i鍊錶 鍊錶 t head 前提是已經定義好t head都是指標型別的,如果不懂可以看完整的題解 t head就相當於陣列中的i ...
棧佇列鍊錶演算法總結
1.佇列 struct queue 2.棧棧的基本操作 定義棧 stacks 入棧 定義棧元素 s.push i 出棧 刪除棧元素 s.pop 返回棧頂元素的值 s.top 判斷棧是否為空 s.empty 值為0說明不空 值為1說明棧為空 棧的用法例項 include include using n...