C語(yǔ)言實(shí)現(xiàn)單鏈表逆序與逆序輸出實(shí)例
單鏈表的逆序輸出分為兩種情況,一種是只逆序輸出,實(shí)際上不逆序;另一種是把鏈表逆序。本文就分別實(shí)例講述一下兩種方法。具體如下:
1.逆序輸出
實(shí)例代碼如下:
#include<iostream> #include<stack> #include<assert.h> using namespace std; typedef struct node{ int data; node * next; }node; //尾部添加 node * add(int n, node * head){ node * t = new node; t->data = n; t->next = NULL; if (head == NULL){ head = t; } else if (head->next == NULL){ head->next = t; } else{ node * p = head->next; while (p->next != NULL){ p = p->next; } p->next = t; } return head; } //順序輸出 void print(node * head){ node * p = head; while (p != NULL){ cout << p->data << " "; p = p->next; } cout << endl; } //遞歸 void reversePrint(node * p){ if (p != NULL){ reversePrint(p->next); cout << p->data << " "; } } //棧 void reversePrint2(node * head){ stack<int> s; while (head != NULL){ s.push(head->data); head = head->next; } while (!s.empty()){ cout << s.top() << " "; s.pop(); } } int main(){ node * head = NULL; for (int i = 1; i <= 5; i++){ head = add(i, head); } print(head); reversePrint(head); reversePrint2(head); system("pause"); return 0; }
逆序輸出可以用三種方法: 遞歸,棧,逆序后輸出。最后一種接下來(lái)講到。
2.單鏈表逆序
實(shí)例代碼如下:
#include<iostream> #include<stack> #include<assert.h> using namespace std; typedef struct node{ int data; node * next; }node; node * add(int n, node * head){ node * t = new node; t->data = n; t->next = NULL; if (head == NULL){ head = t; } else if (head->next == NULL){ head->next = t; } else{ node * p = head->next; while (p->next != NULL){ p = p->next; } p->next = t; } return head; } //循環(huán) node * reverse(node * head){ if (head == NULL || head->next == NULL){ return head; } node * p1 = head; node * p2 = head->next; node * p3 = NULL; head->next = NULL; while (p2 != NULL){ p3 = p2; p2 = p2->next; p3->next = p1; p1 = p3; } head = p1; return head; } void print(node * head){ node * p = head; while (p != NULL){ cout << p->data << " "; p = p->next; } cout << endl; } //遞歸 node * reverse2(node * p){ if (p == NULL || p->next == NULL){ return p; } node * newHead = reverse2(p->next); p->next->next = p; p->next = NULL; return newHead; } int main(){ node * head = NULL; for (int i = 1; i <= 5; i++){ head = add(i, head); } print(head); head = reverse(head); print(head); head = reverse2(head); print(head); system("pause"); return 0; }
這里鏈表逆序用了兩種方法:循環(huán),遞歸。讀者最容易理解的方法就是在紙上自己畫(huà)一下。
希望本文所述實(shí)例對(duì)大家的數(shù)據(jù)結(jié)構(gòu)與算法學(xué)習(xí)能有所幫助。
- 用C語(yǔ)言實(shí)現(xiàn)單鏈表的各種操作(一)
- C語(yǔ)言之單鏈表的插入、刪除與查找
- C語(yǔ)言單循環(huán)鏈表的表示與實(shí)現(xiàn)實(shí)例詳解
- C語(yǔ)言實(shí)現(xiàn)帶頭結(jié)點(diǎn)的鏈表的創(chuàng)建、查找、插入、刪除操作
- c語(yǔ)言鏈表基本操作(帶有創(chuàng)建鏈表 刪除 打印 插入)
- C語(yǔ)言單向鏈表的表示與實(shí)現(xiàn)實(shí)例詳解
- C語(yǔ)言創(chuàng)建和操作單鏈表數(shù)據(jù)結(jié)構(gòu)的實(shí)例教程
- C語(yǔ)言雙向鏈表的表示與實(shí)現(xiàn)實(shí)例詳解
- C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(單鏈表)
- C語(yǔ)言實(shí)現(xiàn)帶頭雙向環(huán)形鏈表
相關(guān)文章
Qt實(shí)現(xiàn)UDP多線程數(shù)據(jù)處理及發(fā)送的簡(jiǎn)單實(shí)例
本文主要介紹了Qt實(shí)現(xiàn)UDP多線程數(shù)據(jù)處理及發(fā)送的簡(jiǎn)單實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10QT實(shí)現(xiàn)讀寫(xiě)ini文件的示例代碼
.ini文件是Initialization?File的縮寫(xiě),即初始化文件,本文主要給大家介紹了關(guān)于Qt讀寫(xiě)ini文件的相關(guān)方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07詳解C++編程中的靜態(tài)成員與可變數(shù)據(jù)成員
這篇文章主要介紹了詳解C++編程中的靜態(tài)成員與可變數(shù)據(jù)成員,是C++入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-01-01C++?數(shù)據(jù)結(jié)構(gòu)超詳細(xì)講解順序表
程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要?jiǎng)?chuàng)建這種元素組,用變量記錄它們,傳進(jìn)傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個(gè)數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲(chǔ)區(qū)里,元素間的順序關(guān)系由它們的存儲(chǔ)順序自然表示2022-03-03