C++數(shù)據(jù)結構與算法之反轉鏈表的方法詳解
本文實例講述了C++數(shù)據(jù)結構與算法之反轉鏈表的方法。分享給大家供大家參考,具體如下:
算法概述:要求實現(xiàn)將一條單向鏈表反轉并考慮時間復雜度。
算法分析:
數(shù)組法(略):
將列表元素逐個保存進數(shù)組,之后再逆向重建列表
點評:實現(xiàn)邏輯最簡單,需要額外的內存開銷。
移動指針:
通過三個指針逐個從鏈表頭開始逐一反轉鏈表元素的指針
點評:不需要額外的內存開銷,會改變原始鏈表。
遞歸:
以遞歸的方式首先找到鏈表尾部,再逐一反轉指針
點評:不需要額外的內存開銷,不會改變原始鏈表。
算法實現(xiàn):
構建鏈表結構
/* 節(jié)點結構 */ struct NODE { int data; struct NODE* next; }; /* 添加元素-壓棧 */ void push(NODE** head, int dat) { struct NODE* new_node = new NODE(); new_node->data = dat; new_node->next = *head; *head = new_node; } /* 添加元素-添加 */ void add(NODE** head, int dat) { struct NODE* new_node = new NODE(); new_node->data = dat; new_node->next = NULL; if (*head != NULL) { struct NODE* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = new_node; } else { *head = new_node; } }
移動指針
/* 反轉列表 */ void reverse(NODE** head) { struct NODE* pre = NULL; struct NODE* cur = *head; struct NODE* nxt; while (cur != NULL) { // 反轉指針 nxt = cur->next; cur->next = pre; // 移動指針 pre = cur; cur = nxt; } *head = pre; }
遞歸
/* 反轉列表-復制原表返回反轉表 */ NODE* reverse(NODE* head) { if (head == NULL || head->next == NULL) { return head; } NODE* new_head = reverse(head->next); // 反轉指針 head->next->next = head; head->next = NULL; return new_head; }
打印鏈表
/* 打印隊列 */ void print(NODE* head) { NODE* temp = head; while (temp != NULL) { std::cout << temp->data << std::endl; temp = temp->next; } }
完整代碼如下:
#include <iostream> /* 節(jié)點結構 */ struct NODE { int data; struct NODE* next; }; /* 添加元素-壓棧 */ void push(NODE** head, int dat) { struct NODE* new_node = new NODE(); new_node->data = dat; new_node->next = *head; *head = new_node; } /* 添加元素-添加 */ void add(NODE** head, int dat) { struct NODE* new_node = new NODE(); new_node->data = dat; new_node->next = NULL; if (*head != NULL) { struct NODE* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = new_node; } else { *head = new_node; } } /* 反轉列表 */ void reverse(NODE** head) { struct NODE* pre = NULL; struct NODE* cur = *head; struct NODE* nxt; while (cur != NULL) { // 反轉指針 nxt = cur->next; cur->next = pre; // 移動指針 pre = cur; cur = nxt; } *head = pre; } /* 反轉列表-復制原表返回反轉表 */ NODE* reverse(NODE* head) { if (head == NULL || head->next == NULL) { return head; } NODE* new_head = reverse(head->next); // 反轉指針 head->next->next = head; head->next = NULL; return new_head; } /* 打印隊列 */ void print(NODE* head) { NODE* temp = head; while (temp != NULL) { std::cout << temp->data << std::endl; temp = temp->next; } } int main() { struct NODE* n = NULL; add(&n, 1); add(&n, 2); add(&n, 3); n = reverse(n); print(n); return 0; }
希望本文所述對大家C++程序設計有所幫助。
上一篇:C++二分查找算法實例
欄 目:C語言
本文標題:C++數(shù)據(jù)結構與算法之反轉鏈表的方法詳解
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1241.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結構課程設計- 解析最少換車次數(shù)的問題詳解
- 01-10數(shù)據(jù)結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10深入理解atoi()與itoa()函數(shù)的用法
- 01-10C++大數(shù)模板(推薦)


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?