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

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

C語言

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

C++數(shù)據(jù)結(jié)構(gòu)與算法之反轉(zhuǎn)鏈表的方法詳解

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

本文實(shí)例講述了C++數(shù)據(jù)結(jié)構(gòu)與算法之反轉(zhuǎn)鏈表的方法。分享給大家供大家參考,具體如下:

算法概述:要求實(shí)現(xiàn)將一條單向鏈表反轉(zhuǎn)并考慮時間復(fù)雜度。

算法分析:

數(shù)組法(略):

將列表元素逐個保存進(jìn)數(shù)組,之后再逆向重建列表
點(diǎn)評:實(shí)現(xiàn)邏輯最簡單,需要額外的內(nèi)存開銷。

移動指針:

通過三個指針逐個從鏈表頭開始逐一反轉(zhuǎn)鏈表元素的指針
點(diǎn)評:不需要額外的內(nèi)存開銷,會改變原始鏈表。

遞歸:

以遞歸的方式首先找到鏈表尾部,再逐一反轉(zhuǎn)指針
點(diǎn)評:不需要額外的內(nèi)存開銷,不會改變原始鏈表。

算法實(shí)現(xiàn):

構(gòu)建鏈表結(jié)構(gòu)

/* 節(jié)點(diǎn)結(jié)構(gòu) */
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;
 }
}

移動指針

/* 反轉(zhuǎn)列表 */
void reverse(NODE** head) {
 struct NODE* pre = NULL;
 struct NODE* cur = *head;
 struct NODE* nxt;
 while (cur != NULL) {
  // 反轉(zhuǎn)指針
  nxt = cur->next;
  cur->next = pre;
  // 移動指針
  pre = cur;
  cur = nxt;
 }
 *head = pre;
}

遞歸

/* 反轉(zhuǎn)列表-復(fù)制原表返回反轉(zhuǎn)表 */
NODE* reverse(NODE* head) {
 if (head == NULL || head->next == NULL) {
  return head;
 }
 NODE* new_head = reverse(head->next);
 // 反轉(zhuǎn)指針
 head->next->next = head;
 head->next = NULL;
 return new_head;
}

打印鏈表

/* 打印隊(duì)列 */
void print(NODE* head) {
 NODE* temp = head;
 while (temp != NULL) {
  std::cout << temp->data << std::endl;
  temp = temp->next;
 }
}

完整代碼如下:

#include <iostream>
/* 節(jié)點(diǎn)結(jié)構(gòu) */
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;
  }
}
/* 反轉(zhuǎn)列表 */
void reverse(NODE** head) {
  struct NODE* pre = NULL;
  struct NODE* cur = *head;
  struct NODE* nxt;
  while (cur != NULL) {
    // 反轉(zhuǎn)指針
    nxt = cur->next;
    cur->next = pre;
    // 移動指針
    pre = cur;
    cur = nxt;
  }
  *head = pre;
}
/* 反轉(zhuǎn)列表-復(fù)制原表返回反轉(zhuǎn)表 */
NODE* reverse(NODE* head) {
  if (head == NULL || head->next == NULL) {
    return head;
  }
  NODE* new_head = reverse(head->next);
  // 反轉(zhuǎn)指針
  head->next->next = head;
  head->next = NULL;
  return new_head;
}
/* 打印隊(duì)列 */
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++程序設(shè)計(jì)有所幫助。

上一篇:C++二分查找算法實(shí)例

欄    目:C語言

下一篇:如何獲取C++類成員虛函數(shù)地址的示例代碼

本文標(biāo)題:C++數(shù)據(jù)結(jié)構(gòu)與算法之反轉(zhuǎn)鏈表的方法詳解

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1241.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)所有