網站首頁 編程語言 正文
1.思考-1
為什么棧用數組來模擬比用鏈表來模擬更優一些?
隊列也可以數組和鏈表的結構實現,使用鏈表的結構實現更優一些,因為如果使用數組的結構,出隊列在數組頭上出數據,效率會比較低,時間復雜度為O(n)。
2.棧基本操作的實現
2.1 初始化棧
void StackInit(stack*ps) { assert(ps); ps->_a = (StackDate*)malloc(sizeof(StackDate) * 4); ps->_top = 0; ps->_capacity = 4; }
2.2 入棧
void StackPush(stack*ps, StackDate x) { assert(ps); if (ps->_top == ps->_capacity) { stack*tmp = (StackDate*)realloc(ps, sizeof(StackDate)*(ps->_capacity) * 2); if (NULL == tmp) { printf("realloc failed\n"); exit(-1); } ps = tmp; ps->_capacity *= 2; } ps->_a[ps->_top] = x; ps->_top++; }
2.3 出棧
void StackPop(stack*ps) { assert(ps); assert(!StackIsEmpty(ps)); --ps->_top; }
注意: 出棧并不是真正意義上的刪除數據,而是將_top向后挪動了一個位置。
2.4 獲取棧頂數據
StackDate StackTop(stack*ps) { assert(ps); assert(!StackIsEmpty(ps)); return ps->_a[ps->_top - 1]; }
2.5 獲取棧中有效元素個數
int StackSize(stack*ps) { assert(ps); return ps->_top; }
2.6 判斷棧是否為空
bool StackIsEmpty(stack*ps) { assert(ps); return ps->_top == 0; }
2.7 銷毀棧
void StackDestory(stack*ps) { assert(ps); free(ps->_a); ps->_a = NULL; ps->_top = ps->_capacity = 0; }
3.測試
3.1 測試
void test() { //插入數據 stack st; StackInit(&st); StackPush(&st,1); StackPush(&st,2); StackPush(&st,3); StackPush(&st,4); while (!StackIsEmpty(&st)) { printf("%d ", StackTop(&st)); StackPop(&st); } printf("\n"); //獲取棧頂數據 StackPush(&st, 1); StackPush(&st, 2); printf("%d ", StackTop(&st)); printf("\n"); //棧中有效數據個數 printf("%d ", StackSize(&st)); //銷毀棧 StackDestory(&st); }
3.2 測試結果
4.思考-2
為什么隊列用鏈表模擬比數組模擬更加合適?
棧的實現一般可以使用數組或者鏈表實現,相對而言數組的結構實現更優一些。因為數組在尾上插入數據的代價小。
5.隊列的基本操作實現
5.1 初始化隊列
void QueueInit(Queue*pq) { assert(pq); pq->_head = pq->_tail = NULL; }
5.2 隊尾入隊列
void QueuePush(Queue*pq, QueueDate x) { assert(pq); QueueNode*newnode = (QueueNode*)malloc(sizeof(QueueNode)); if (NULL == newnode) { printf("malloc failed\n"); exit(-1); } newnode->next = NULL; newnode->val = x; if (NULL == pq->_tail) { pq->_head = pq->_tail = newnode; } else { pq->_tail->next = newnode; pq->_tail = newnode; } }
5.3 隊頭出隊列
void QueuePop(Queue*pq) { assert(pq); assert(!QueueIsEmpty(pq)); if (NULL == pq->_head->next) { free(pq->_head); pq->_head = pq->_tail = NULL; } else { QueueNode*next = pq->_head->next; free(pq->_head); pq->_head = next; } }
代碼分析:
5.4 隊列中有效元素的個數
int QueueSize(Queue*pq) { assert(pq); int count = 0; QueueNode*cur = pq->_head; while (cur) { ++count; cur = cur->next; } return count; }
5.5 判斷隊列是否為空
bool QueueIsEmpty(Queue*pq) { assert(pq); return pq->_head == NULL; }
5.6 獲取隊頭數據
QueueDate QueueFront(Queue*pq) { assert(pq); assert(!QueueIsEmpty(pq)); return pq->_head->val; }
5.7 獲取隊尾的數據
QueueDate QueueBack(Queue*pq) { assert(pq); assert(!QueueIsEmpty(pq)); return pq->_tail->val; }
5.8 銷毀隊列
void QueueDestory(Queue*pq) { assert(pq); while (pq->_head) { if (pq->_head == pq->_tail) { free(pq->_head); pq->_head = pq->_tail = NULL; } else { QueueNode*next = pq->_head->next; free(pq->_head); pq->_head = next; } } }
注意: 和隊頭出隊列一樣分析。
6.測試
6.1 測試
void test1() { //插入數據 Queue q; QueueInit(&q); QueuePush(&q, 1); QueuePush(&q, 2); QueuePush(&q, 3); QueuePush(&q, 4); //有效數據的個數 printf("%d ", QueueSize(&q)); printf("\n"); //獲取隊頭數據 printf("%d",QueueFront(&q)); printf("\n"); //獲取隊尾數據 printf("%d",QueueBack(&q)); printf("\n"); //出隊 QueuePop(&q); while (!QueueIsEmpty(&q)) { printf("%d ", QueueFront(&q)); QueuePop(&q); } printf("\n"); //銷毀 QueueDestory(&q); printf("\n"); }
6.2 測試結果
今天數據結構棧與隊列的相關知識點就分享到這里了,感謝你的瀏覽,如果對你有幫助的話,可以給個關注,順便來個贊。
原文鏈接:https://blog.csdn.net/weixin_59799963/article/details/123794370
相關推薦
- 2023-06-16 Dubbo?系列JDK?SPI?原理解析_服務器其它
- 2022-09-09 pycharm中創建sql文件及模板的過程_python
- 2023-05-06 pandas中groupby操作實現_python
- 2024-01-31 Mybatis plus并發更新時的問題
- 2023-10-27 獲取html中元素的寬高
- 2022-11-25 Centos?8.2?升級內核通過elrepo源的方法_云其它
- 2022-06-11 C#把EXCEL數據轉換成DataTable_C#教程
- 2022-10-17 python?文件讀寫和數據清洗_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支