雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C語言實現(xiàn)單鏈表逆序與逆序輸出實例

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

單鏈表的逆序輸出分為兩種情況,一種是只逆序輸出,實際上不逆序;另一種是把鏈表逆序。本文就分別實例講述一下兩種方法。具體如下:

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語言

下一篇:C++輸入輸出注意事項總結(jié)

本文標(biāo)題:C語言實現(xiàn)單鏈表逆序與逆序輸出實例

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3458.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有