一、棧的介紹
棧作為一種資料結構,是一種只能在一端進行插入和刪除操作。它按照先進後出的原則儲存資料,先進入的資料被壓入棧底,最後的資料在棧頂,需要讀資料的時候從棧頂開始彈出資料(最後一個資料被第一個讀出來)。棧被使用於非常多的地方,例如瀏覽器中的後退按鈕,文字編輯器中的撤銷機制。
進棧的時候是1先進,然後是2、3、4、5、6,出棧的時候是先6出,然後是5、4、3、2、1
二、棧中常用的方法
作為一個棧(用stack來表示),最基本的方法有下面幾個:
-
stack.push(e): 將元素e新增到S的棧頂
-
stack.pop(): 從棧S中移除並返回棧頂的元素,如果此時棧是空的,那麼這個操作將會報錯
-
stack.top(): 不移除棧頂元素,但返回棧頂元素,如果此時棧是空的,那麼這個操作將會報錯
-
stack.is_empty(): 如果棧為空,則返回True,否則返回False
-
len(stack): 返回棧中元素的數量,使用len的特殊方法實現
- stack.travel()遍歷棧裡面的元素
三、棧的python程式碼實現
class Stack(): """ 以list為基礎實現的棧 """ def __init__(self): self._data = [] def __len__(self): return len(self._data) def is_empty(self): if len(self._data) == 0: return True else: return False def push(self, e): self._data.append(e) def pop(self): if self.is_empty(): print("棧為空") return return self._data.pop() def top(self): if self.is_empty(): print("棧為空") return return self._data[-1] def travel(self): for i in range(0,len(self._data)): print("%d"%self._data[i]) if __name__ == '__main__': print("建立棧") stack = Stack() stack.pop() print("驗證是否為空:",end="") empty = stack.is_empty() if empty == True: print("空棧") else: print("不是空") print("進棧") stack.push(1) stack.push(2) stack.push(3) stack.push(4) stack.push(5) stack.push(6) print("遍歷驗證進棧") stack.travel() print("判斷是否為空:",end=" ") empty = stack.is_empty() if empty == True: print("空棧") else: print("不是空") print("出棧:",end=" ") pop = stack.pop() print(pop) stack.travel() print("驗證棧頂元素:",end=" ") top = stack.top() print(top)
執行結果為:
建立棧 棧為空 驗證是否為空:空棧 進棧 遍歷驗證進棧 1 2 3 4 5 6 判斷是否為空: 不是空 出棧: 6 1 2 3 4 5 驗證棧頂元素: 5
四、棧的C語言程式碼實現
// main.m // 棧 // Created by 侯壘 on 2019/7/3. // Copyright © 2019 可愛的侯老師. All rights reserved. # include<stdio.h> typedef struct N { int num; struct N *next; }Node; Node * createNode(int num) { Node *node = (Node *)malloc(sizeof(Node)); node->num = num; node->next = NULL; return node; } Node * createStack() { Node *head = NULL; return head; } int is_empty(Node *head) { if (head == NULL) { return 1; } else { return 0; } } int length(Node *head) { Node *current = head; if (is_empty(head)) { return 0; } int count = 1; while (current->next!=NULL) { count++; current = current->next; } return count; } Node *push(Node *head, int num) { Node *node = createNode(num); if (is_empty(head)==1) { head = node; } else { Node *current = head; while (current->next!=NULL) { current = current->next; } current->next = node; } return head; } Node *pop(Node *head) { if (is_empty(head) == 1) { printf("站為空"); return head; } else { Node *current = head; int len = length(head); if (len == 1) { head = NULL; } else { for (int i=0; i<len-2;i++) { current = current->next; } current->next = NULL; } } return head; } int top(Node *head) { if (is_empty(head)) { printf("棧為空"); return -1; } return head->num; } void travel(Node *head) { if (is_empty(head) == 1) { printf("站為空\n"); } else { Node *current = head; int len = length(head); for (int i = 0; i<len; i++) { printf("%d ",current->num); current = current->next; } printf("\n"); } } int main(int argc, const char * argv[]) { printf("建立棧\n"); Node *head = createStack(); printf("驗證是否為空: "); int empty = is_empty(head); if (empty == 1) { printf("棧為空\n"); } else { printf("棧不為空\n"); } printf("驗證進棧\n"); head = push(head, 1); travel(head); head = push(head, 2); head = push(head, 3); head = push(head, 4); head = push(head, 5); head = push(head, 6); travel(head); printf("驗證棧頂元素\n"); int t = top(head); printf("top = %d\n",t); printf("驗證出棧\n"); head = pop(head); travel(head); head = pop(head); travel(head); head = pop(head); travel(head); head = pop(head); travel(head); head = pop(head); travel(head); head = pop(head); travel(head); return 0; }
執行結果為:
建立棧 驗證是否為空: 棧為空 驗證進棧 1 1 2 3 4 5 6 驗證棧頂元素 top = 1 驗證出棧 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 站為空