鏈隊 尹成

http://blog.csdn.net/itcastcpp/article/details/39271691

?今天,我們一起用C++寫一個鏈對,具體如下所示。

LinkQueue.h具體內容如下:

[cpp]?view plain?copy
  1. #include?"QueueNode.h"??
  2. template<typename?Type>?class?LinkQueue{??
  3. public:??
  4. ????LinkQueue()?:m_prear(NULL),?m_pfront(NULL){}??
  5. ????~LinkQueue(){??
  6. ????????MakeEmpty();??
  7. ????}??
  8. ????void?Append(const?Type?item);???//insert?data??
  9. ????Type?Delete();??????????????????//delete?data??
  10. ????Type?GetFront();????????????????//get?data??
  11. ????void?MakeEmpty();???????????????//make?the?queue?empty??
  12. ????void?Print();???????????????????//print?the?queue??
  13. ??
  14. ????bool?IsEmpty()?const{??
  15. ????????return?m_pfront?==?NULL;??
  16. ????}??
  17. ??
  18. private:??
  19. ????QueueNode<Type>?*m_prear,?*m_pfront;??
  20. };??
  21. ??
  22. template<typename?Type>?void?LinkQueue<Type>::MakeEmpty(){??
  23. ????QueueNode<Type>?*pdel;??
  24. ????while?(m_pfront){??
  25. ????????pdel?=?m_pfront;??
  26. ????????m_pfront?=?m_pfront->m_pnext;??
  27. ????????delete?pdel;??
  28. ????}??
  29. }??
  30. ??
  31. template<typename?Type>?void?LinkQueue<Type>::Append(const?Type?item){??
  32. ????if?(m_pfront?==?NULL){??
  33. ????????m_pfront?=?m_prear?=?new?QueueNode<Type>(item);??
  34. ????}??
  35. ????else{??
  36. ????????m_prear?=?m_prear->m_pnext?=?new?QueueNode<Type>(item);??
  37. ????}??
  38. }??
  39. ??
  40. template<typename?Type>?Type?LinkQueue<Type>::Delete(){??
  41. ????if?(IsEmpty()){??
  42. ????????cout?<<?"There?is?no?element!"?<<?endl;??
  43. ????????exit(1);??
  44. ????}??
  45. ????QueueNode<Type>?*pdel?=?m_pfront;??
  46. ????Type?temp?=?m_pfront->m_data;??
  47. ????m_pfront?=?m_pfront->m_pnext;??
  48. ????delete?pdel;??
  49. ????return?temp;??
  50. }??
  51. ??
  52. template<typename?Type>?Type?LinkQueue<Type>::GetFront(){??
  53. ????if?(IsEmpty()){??
  54. ????????cout?<<?"There?is?no?element!"?<<?endl;??
  55. ????????exit(1);??
  56. ????}??
  57. ????return?m_pfront->m_data;??
  58. }??
  59. ??
  60. template<typename?Type>?void?LinkQueue<Type>::Print(){??
  61. ????QueueNode<Type>?*pmove?=?m_pfront;??
  62. ????cout?<<?"front";??
  63. ????while?(pmove){??
  64. ????????cout?<<?"--->"?<<?pmove->m_data;??
  65. ????????pmove?=?pmove->m_pnext;??
  66. ????}??
  67. ????cout?<<?"--->rear"?<<?endl?<<?endl?<<?endl;??
  68. }??
QueueNode.h具體內容如下:

[cpp]?view plain?copy
  1. template<typename?Type>?class?LinkQueue;??
  2. ??
  3. template<typename?Type>???
  4. class?QueueNode??
  5. {??
  6. private:??
  7. ????friend?class?LinkQueue?<?Type?>?;??
  8. ????QueueNode(const?Type?item,?QueueNode<Type>?*next?=?NULL)??
  9. ????????:m_data(item),?m_pnext(next){}??
  10. private:??
  11. ????Type?m_data;??
  12. ????QueueNode<Type>?*m_pnext;??
  13. };??
main.cpp具體內容如下:

[cpp]?view plain?copy
  1. #include?<iostream>??
  2. using?namespace?std;??
  3. #include?"LinkQueue.h"??
  4. int?main(){??
  5. ????LinkQueue<int>?queue;??
  6. ????int?init[10]?=?{?1,?3,?6,?8,?9,?2,?0,?5,?4,?7?};??
  7. ????for?(int?i?=?0;?i?<?10;?i++){??
  8. ????????queue.Append(init[i]);??
  9. ????}??
  10. ????queue.Print();??
  11. ????queue.Delete();??
  12. ????queue.Print();??
  13. ????cout?<<?queue.GetFront()?<<?endl;??
  14. ????queue.Print();??
  15. ????queue.MakeEmpty();??
  16. ????queue.Print();??
  17. ????queue.Delete();??
  18. ????cin.get();??
  19. ????return?0;??
  20. }??
運行效果如圖1所示:

圖1?運行效果


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384067.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384067.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384067.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

強連通分量入門——Trajan算法

今天學習了強連通分量。 【參考博客】 如果覺得我講的有些地方難以理解或者存在問題&#xff08;歡迎留言&#xff09;&#xff0c;可以看一下我借鑒的一些大佬的博客&#xff1a; 傳送門1 傳送門2 【知識儲備】 首先我們需要對幾個定義有一些概念&#xff1a; 強連通&#xff…

最小棧的實現

所謂最小棧, 就是當前棧頂元素最小, 我們可以這樣做, 每次在入棧之前, 將待入棧元素與棧頂元素相比, 每次現將待入棧的元素先入棧, 再將帶入棧的元素和較小的元素入棧, 這樣就可以保證每次棧頂元素是最小元素. 在出棧的時候規定每次出棧兩個元素,這樣就可以保證在出棧之后棧頂元…

用C++實現單鏈表的創建、逆置和輸出 的兩種方法

http://blog.csdn.net/lfeng_coding/article/details/47300563 題目描述&#xff1a;在已知單鏈表頭節點的情況下&#xff0c;設計算法逆置單鏈表并輸出 方法一&#xff1a;采用首先將頭節點指向空&#xff0c;讓其變為尾節點&#xff0c;然后利用中間節點 p、q 將其后的節點一…

兩個棧實現一個隊列

利用兩個棧實現一個隊列思路是這樣的. 首先這個隊列包含兩個棧, 然后一個棧用來入隊列, 一個棧用來出隊列 typedef struct QueBy2Stack {SeqStack input;SeqStack output; }QueBy2Stack; 1. 初始化 void QueBy2StackInit(QueBy2Stack* stack) {if(stack NULL){return;//非法…

HDU 5934:Boom——強連通分量+縮點

【題目描述】 There are N bombs needing exploding.Each bomb has three attributes: exploding radius ri, position (xi,yi) and lighting-cost ci which means you need to pay ci cost making it explode.If a un-lighting bomb is in or on the border the exploding ar…

Linux--線程死鎖

http://blog.csdn.net/gebushuaidanhenhuai/article/details/73799824 線程為什會死鎖&#xff1f;&#xff1f;“鎖”又是什么東西&#xff1f;我們這篇博客主要講一下為什么要給線程加鎖&#xff0c;為什么會出現線程死鎖&#xff0c;線程死鎖怎么解決。 互斥鎖 在我的上篇博…

兩個隊列實現一個棧

用兩個隊列實現一個棧的原理是這樣的. 規定兩個隊列, 必須有一個隊列是非空, 一個隊列是空.每次入棧時必須往非空隊列中入, 而每次出棧時, 必須將非空隊列里的元素裝到空隊列中, 直到非空隊列中只有一個元素時, 此時就將剩下的這個元素出棧即可. 而取棧頂元素時, 和出棧一樣, 先…

POJ-1144 Network——Trajan+割點

【題目描述】 A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect together tw…

Linux--生產者與消費者

http://blog.csdn.net/gebushuaidanhenhuai/article/details/74011636 基本概念 提到生產者和消費者&#xff0c;我們最有可能想到的是商店賣東西&#xff0c;顧客在貨架上(緩沖區&#xff09;買東西。 生產者消費者問題&#xff0c;其實是一個多線程同步問題的經典案例。該問…

進程的掛起以及可重入函數

相關接口 ????pause 函數用于將進程掛起. 如果信號的處理動作是終止進程, 則進程終止, pause 函數沒有返回值; 如果信號的處理動作是忽略, 則進程被掛起, pause函數不返回, 如果信號的處理動作是捕捉, 則調用信號處理動作之后pause 返回 -1.來看一段代碼 #include<s…

POJ1236Network of Schools——強連通分量縮點建圖

【題目描述】 A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distri…

C——通過調用函數分配內存

http://blog.csdn.net/u012627502/article/details/3579724 1&#xff09;以返回值方式返回&#xff1a;把動態分配的存儲位置地址&#xff0c;賦值給指針類型返回值&#xff08;不同于被調用函數的自動變量地址&#xff09; 2&#xff09;以形參形式返回&#xff1a;二級指針類…

gdb調試多進程程序

1.gdb下調試多進程程序只需要以下幾條命令即可 ???????? ????除此之外還可以查看正在調試的進程 info inferiors, 同時也可以將當前正在調試的進程切換到另外一個進程中讓其取運行 ????2.代碼調試演示 #include<stdio.h> #include<stdlib.h> #…

BZOJ1123-BLO——強連通分量求割點+計數

【題目描述】 Byteotia城市有n個 towns m條雙向roads. 每條 road 連接 兩個不同的 towns ,沒有重復的road. 所有towns連通。Input 輸入n<100000 m<500000及m條邊Output 輸出n個數&#xff0c;代表如果把第i個點去掉&#xff0c;將有多少對點不能互通。Sample Input 5…

關于memcpy和memmove兩函數的區別

http://blog.csdn.net/caowei840701/article/details/8491836 [cpp] view plaincopy <p> 關于memcpy和memmove兩個c標準庫函數&#xff0c;其功能都是將一塊內存區域中的指定大小內容復制到目標內存中&#xff0c;在翻閱c標準庫實現的源代碼我們發現他們是有區別的。&…

判斷字符串出棧合法性

先來看說一下思路 接下來就是寫代碼了 int StackOrder(SeqStack* stack, char* input, char* output, int size_input, int size_output) {if(stack NULL || input NULL || output NULL){return 0;}int i_input 0;int j_output 0;SeqStackType value;for(; j_output <…

CodeForces - 1200C——小模擬

【題目描述】 Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by n sectors, and the outer area is equally divided by m sectors. A wall exists between each pair of sectors of same area (inner o…

1 單例模式

達內 閔大神 //餓漢單例模式 #include <iostream> using namespace std;class Singleton { public:static Singleton& getInstance(){return s_instance;} private:Singleton(){}Singleton(const Singleton& that){}static Singleton s_instance;//靜態成員變量 …

共享棧

1.定義 所謂共享棧就是利用一個數組實現兩個棧. 先來看一下共享棧的數據結構 typedef struct SharedStack {int top1;int top2;SeqStackType* data; }SharedStack; 2. 初始化 void SharedStackInit(SharedStack* stack) {if(stack NULL){return;//非法輸入}stack -> top…

BZOJ1018 | SHOI2008-堵塞的交通traffic——線段樹維護區間連通性+細節

【題目描述】 BZOJ1018 | SHOI2008-堵塞的交通traffic 有一天&#xff0c;由于某種穿越現象作用&#xff0c;你來到了傳說中的小人國。小人國的布局非常奇特&#xff0c;整個國家的交通系統可 以被看成是一個2行C列的矩形網格&#xff0c;網格上的每個點代表一個城市&#xff0…