国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 栾城县| 高碑店市| 历史| 托克逊县| 南城县| 桃园县| 双牌县| 洪泽县| 香港| 大姚县| 汝阳县| 喀喇沁旗| 鄂尔多斯市| 沁水县| 高淳县| 梅州市| 泸定县| 清新县| 江津市| 宁远县| 嵊泗县| 福海县| 通城县| 泰兴市| 绥德县| 双辽市| 广宗县| 剑阁县| 静安区| 秭归县| 三亚市| 大方县| 丹凤县| 郸城县| 金秀| 涪陵区| 西青区| 淮阳县| 甘孜县| 营口市| 潢川县|