1. 程式人生 > >C++實現 簡單 單鏈表

C++實現 簡單 單鏈表

轉自: http://blog.csdn.net/wonggonghong/article/details/21527577

  我們首先建立一個<List.h>標頭檔案,宣告一個單鏈表結構:

#include "List.h"

  1. //建立一個單鏈表結構,包含一些常見的操作
  2. #ifndef _List_H_
  3. #define _List_H_
  4. #include <iostream>
  5. struct Node{  
  6.     int element;  //節點儲存資訊可以根據需要修改!
  7.     Node* next;  
  8. };  
  9. Node* CreateLists(); //建立一個空表,並返回表頭
  10. void DeleteLists(Node* head); //刪除表頭為head的該連結串列
  11. bool IsLast(Node* P);  
  12. Node* Find(int X, Node* head);  
  13. Node* FindPrevious(int X, Node* head);  
  14. void Delete(int X, Node* head);  
  15. void Insert(Node* P, int X); //在節點P後面插入X
  16. void OutputLists(Node* head); //輸出連結串列中所有元素的值
  17. #endif    

    然後在<List.cpp>

檔案裡實現標頭檔案中的連結串列操作:

#include "List.cpp"

  1. #include "list.h"
  2. Node* CreateLists()  
  3. {  
  4.     Node* head = new Node;  
  5.     head->next = NULL;  
  6.     return head;  
  7. }  
  8. void DeleteLists(Node* head)  
  9. {  
  10.     Node* P = head->next, *temp;  
  11.     head->next = NULL;  
  12.     while(P)  
  13.     {  
  14.         temp = P->next;  
  15.         delete P;  
  16.         P = temp;  
  17.     }  
  18. }  
  19. bool IsLast(Node* P)  
  20. {  
  21.     return P->next == NULL;  
  22. }  
  23. Node* Find(int X, Node* head)  
  24. {  
  25.     Node* P = head->next;  
  26.     while(P && P->element!=X)  
  27.         P = P->next;  
  28.     return P;  
  29. }  
  30. Node* FindPrevious(int X, Node* head)  
  31. {  
  32.     Node* P=head;  
  33.     while(P->next && P->next->element!=X)  
  34.         P=P->next;  
  35.     return P;  
  36. }  
  37. void Delete(int X, Node* head)  
  38. {  
  39.     Node* P = FindPrevious(X,head), *temp; //如果沒找到X,則返回的是連結串列最後一項
  40.     if(P->next)  
  41.     {  
  42.         temp = P->next;  
  43.         P->next = temp->next;  
  44.         delete temp;  
  45.     }  
  46. }  
  47. void Insert(Node* P, int X)  
  48. {  
  49.     Node* tempX = new Node;  
  50.     tempX->element = X;  
  51.     tempX->next = P->next;  
  52.     P->next = tempX;  
  53. }  
  54. void OutputLists(Node* head)  
  55. {  
  56.     Node* P = head->next;  
  57.     while(P)  
  58.     {  
  59.         std::cout<<P->element<<"   ";  
  60.         P = P->next;  
  61.     }  
  62.     std::cout<<std::endl;  
  63. }  
最後,我們用一段main程式碼驗證一下正確性:
  1. #include <iostream>
  2. #include <assert.h>
  3. #include "list.h"
  4. usingnamespace std;  
  5. int main()  
  6. {  
  7.     int Date[8] = {2,9,5,8,15,32,7,4};  
  8.     Node *head = CreateLists();  
  9.     Node *P = head;  
  10.     for(int i=0;i<8;i++)  
  11.     {  
  12.         Insert(P,Date[i]);  
  13.         P = P->next;  
  14.     }  
  15.     cout<<"打印出連結串列中所有元素:\n";  
  16.     OutputLists(head);  
  17.     if(IsLast(P))  
  18.         cout<<"該Lists的最後一個節點為:"<<P->element<<endl;  
  19.     else
  20.         cout<<"P不是最後一個節點!!P等於:"<<P->element<<endl;  
  21.     if(Find(15,head))  
  22.         cout<<"Find函式在該Lists中找到了值為15的節點!!!\n";  
  23.     else
  24.         cout<<"Find函式沒找到值為15的節點!!\n";  
  25.     cout<<"FindPrevious函式找到節點15的前一個節點為:"<<FindPrevious(15,head)->element<<endl;  
  26.     cout<<"而節點15的前一個節點應該為“8”!\n";  
  27.     Delete(8, head);  
  28.     if(Find(8,head))  
  29.         cout<<"Delete(8, head)後,在該Lists中找到了值為8的節點!!!\n";  
  30.     else
  31.         cout<<"Delete(8, head)後,Find函式沒找到值為8的節點!!\n";  
  32.     DeleteLists(head);   
  33.     if(head->next)  
  34.         cout<<"DeleteLists函式未成功刪除連結串列!!\n";  
  35.     else
  36.         cout<<"連結串列已經為空!DeleteLists函式沒有問題!!!\n";  
  37.     return 0;  
  38. }  
結果如下:

相關推薦

C++實現 簡單 單鏈

轉自: http://blog.csdn.net/wonggonghong/article/details/21527577   我們首先建立一個<List.h>標頭檔案,宣告一個單鏈表結構: #include "List.h" //建立一個單鏈

c語言實現簡單單鏈

1.比較順序表和連結串列的優缺點,說說他們正在什麼場景下使用? 答: 對於順序表,無論是動態還是靜態,他們有都會死連續的儲存空間,在讀取時間效率上比較短,但在插入和刪除時會比較麻煩,需要不斷的遍歷去找到尾節點。 對於連結串列,因為是

c實現單鏈

clu eof print 實現 ext 實現簡單 std [0 des #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next

C++實現單鏈通用模板

     這兩天覆習了一下單鏈表,決定用C++寫一個單鏈表的統一模板,方便使用時可直接呼叫。所謂單鏈表,是指一串相同的結構體,其中,前一個結構體中儲存了指向下一個結構體的指標,是陣列的進化形式。單鏈表示意圖如下:   連結串列的常見操作有:查詢(Find)、插入(

C語言 ,單鏈實現佇列(初始化,入隊,出隊,元素個數,隊首元素,是否為空)

單鏈表實現佇列: 連結串列為空的判斷條件:pQueue->pFront==pQueue->pRear或者若結構體中存在數的個數時,判斷pQueue->size==0,即元素個數為0 標頭檔案:佇列.h #pragma once #include<

c++學習筆記—單鏈基本操作的實現

用c++語言實現的單鏈表基本操作,包括單鏈表的建立(包括頭插法和尾插法建表)、結點的查詢、刪除、排序、列印輸出、逆置、連結串列銷燬等基本操作。 IDE:vs2013 具體實現程式碼如下: #include "stdafx.h" #include <malloc.h

C++類中單鏈實現(頭插、尾插、頭刪、尾刪、指定位置插入、指定位置刪除、連結串列長度、清空連結串列、連結串列排序)

#include<iostream> using namespace std; class Node { public:Node():next(NULL){}Node(int n,Node *p = NULL):value(n),next(p){}int val

C++中用模板類(結點類,連結串列類)實現單鏈的合併操作!

程式碼通俗易通,如下 List.h #include<stdio.h> template <class T> class ListNode { T data; ListNode<T>* link; public:

C語言】單鏈實現

單鏈表 單鏈表是一種鏈式存取的資料結構,用一組地址任意的儲存單元存放線性表中的資料元素。 單鏈表結構如下: typedef int DataType; typedef struct Node { struct Node* next; /

C語言:單鏈實現(二) 就地逆置,就地歸併

#include<iostream> #include<stdio.h> #include<math.h> #define LEN sizeof(struct Nodelist) using namespace std; typedef

資料結構C語言版--單鏈的基本功能實現

/* * 構造一個鏈式儲存的線性表(當輸入9999時,結束構造過程),然後輸出該線性表 * 並統計該線性連結串列的長度 。 *注:new和delete是C++的運算子 malloc和free是C++/C的標準庫函式 */ #include<st

列表的c++實現(類模板,包含順序實現單鏈、雙鏈)

#ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #include <iostream> using namespace std; template <class elemType> class List{ public:

C語言線性單鏈相關函式和演算法的基本實現

備考期間嘗試寫了一些基本資料結構的C語言實現,現做以下記錄(基本資料元以int型為例):全域性定義及依賴:#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 #d

一、(1)C++ 實現簡單的線性(順序儲存結構)

作為新手,初學C++和資料結構,也想發發博文,分享點自己的學習所得,也請諸位看官幫忙指正,能提提意見是極好的了! 首先呢,我給各位看官講個笑話:一個統計學家在調查了大量的資料後發現,兩個互不串通的人同時帶炸彈上飛機的概率幾乎是零,於是他每次坐飛機都會隨身攜帶一個炸彈。哈哈

C】利用單鏈資料結構實現通訊錄,連結串列的增刪改查

C語言中實現連結串列,是需要利用到C語言中比較難的結構體與指標才能實現。 結構體中放一個指向後接節點的指標與每一個結點應該存放的資訊。 下面做一個命令列的通訊錄來說明連結串列的增刪改查這個問題。 一開始讓使用者輸入連結串列,按1可以輸出,按3可以刪除。 可以修改: 可以

資料結構之---c語言實現迴圈單鏈操作

wechat:812716131 ------------------------------------------------------ 技術交流群請聯絡上面wechat ----------------------------------------------

C/C++,資料結構單鏈實現約瑟夫環

約瑟夫環——圍成一圈,定義一個數值K,從任意位置開始計數,每走K步刪除當前位置結點,直到剩下最後一個結點,求最後一個結點//單鏈表結構以及Find函式參見 2016-1-2 13:56 發表部落格SLi

C語言」單鏈/雙向鏈的建立/遍歷/插入/刪除

ins lin mon 雙向鏈表 gte aix5 tag cbe ssp MVC%E6%9E%B6%E6%9E%84%E5%AD%A6%E4%B9%A0%E4%B9%8BEasyFirst%E2%80%94%E2%80%94%E5%BF%AB%E7%82%B9%E5%A4

C++實現簡單的文本查詢

ber number map () first begin ifstream adf times 1 該程序將讀取用戶指定的任意文本文件,然後允許用戶從該文件中查找單詞。查詢的結果是該單詞出現的次數,並列出每次出現所在的行。如果某單詞在同一行中多次出現,程序將只顯示該

數據結構-編程實現一個單鏈的測長

.com 元素 頭結點 png 指針 控制 data amp 技術分享 1:代碼如下: // ConsoleApplication15.cpp : 定義控制臺應用程序的入口點。 // #include "stdafx.h" #include <malloc.h&g