C語言實現(xiàn)單鏈表逆序與逆序輸出實例
單鏈表的逆序輸出分為兩種情況,一種是只逆序輸出,實際上不逆序;另一種是把鏈表逆序。本文就分別實例講述一下兩種方法。具體如下:
1.逆序輸出
實例代碼如下:
#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;
}
逆序輸出可以用三種方法: 遞歸,棧,逆序后輸出。最后一種接下來講到。
2.單鏈表逆序
實例代碼如下:
#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),遞歸。讀者最容易理解的方法就是在紙上自己畫一下。
希望本文所述實例對大家的數(shù)據(jù)結(jié)構(gòu)與算法學(xué)習(xí)能有所幫助。
上一篇:16種C語言編譯警告(Warning)類型的解決方法
欄 目:C語言
本文標(biāo)題:C語言實現(xiàn)單鏈表逆序與逆序輸出實例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3458.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


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


