国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

c++鏈表

2019-11-06 07:19:03
字體:
來源:轉載
供稿:網友

貼兩個關于c++鏈表的代碼

代碼一是一個雙向鏈表

#include <iostream>using namespace std;typedef struct PNODE { int m; struct PNODE *next; struct PNODE *PRevious;} node, *Pnode;int main(){ int n; cin >> n; Pnode PHead = new(node); Pnode PLast = new(node); PHead->next = PLast; PHead->previous = NULL; PLast->next = NULL; PLast->previous = PHead; Pnode PTail = PHead; for (int i = 0; i < n; i++) { Pnode PNew = new(node); cin >> PNew->m; PNew->previous = PTail; PNew->next = PTail->next; PTail->next = PNew; PNew->next->previous = PNew; PTail = PTail->next; } PTail = PLast->previous; while (PTail->previous != NULL) { cout << PTail->m << endl; PTail = PTail->previous; } return 0;}

代碼二是順序表元素的比較和刪除,題目如下

已知a、b和c三個遞增有序的線性表,現在要求對a做如下操作:刪除其中既即在b中出現又在c中出現的元素(注意同一表中的元素有可能重復)。

#include <iostream>using namespace std;typedef struct PNODE { int m; struct PNODE *next;}PNode, *Node;//這一塊函數在寫的時候調了好幾次,個人認為可以加深對指針的理解void Input(Node *P, int n){ *P = new(PNode); Node PHead = *P; PHead->next = NULL; Node PTail = PHead; for (int i = 0; i < n; i++) { Node PNew = new(PNode); cin >> PNew->m; PNew->next = NULL; PTail->next = PNew; PTail = PNew; }}int main(){ int n, m, l; cin >> n >> m >> l; Node PNHead, PMHead, PLHead; Input(&PNHead, n); Input(&PMHead, m); Input(&PLHead, l); Node PNTail = PNHead; Node PMTail = PMHead; Node PLTail = PLHead; while (PNTail->next != NULL) { //這個地方&&前后不能顛倒,因為根據c++(c)語言編譯器的特性從左向右判斷,顛倒后就會報錯 while (PMTail->next != NULL && PNTail->next->m > PMTail->next->m) PMTail = PMTail->next; while (PLTail->next != NULL && PNTail->next->m > PLTail->next->m) PLTail = PLTail->next; if (PMTail->next == NULL || PLTail->next == NULL) break; if (PNTail->next->m == PMTail->next->m && PNTail->next->m == PLTail->next->m) { Node P = PNTail->next; PNTail->next = P->next; //這個地方建議養成這個習慣,減少寫大東西的時候不必要的內存泄露 delete(P); } else PNTail = PNTail->next; } PNTail = PNHead->next; while (PNTail != NULL) { cout << PNTail->m << endl; PNTail = PNTail->next; } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 丰原市| 邻水| 弋阳县| 云南省| 九寨沟县| 双鸭山市| 原平市| 鄂伦春自治旗| 清涧县| 平陆县| 广东省| 通化县| 河池市| 惠来县| 福建省| 阳谷县| 平湖市| 闵行区| SHOW| 凌云县| 博爱县| 连江县| 会理县| 桦甸市| 新野县| 英德市| 深水埗区| 惠州市| 札达县| 通海县| 麻栗坡县| 综艺| 云和县| 普兰县| 天峨县| 体育| 民丰县| 安庆市| 无极县| 永吉县| 阳山县|