C++實現 簡單 單鏈表
轉自: http://blog.csdn.net/wonggonghong/article/details/21527577
我們首先建立一個<List.h>標頭檔案,宣告一個單鏈表結構:
#include "List.h"
- //建立一個單鏈表結構,包含一些常見的操作
- #ifndef _List_H_
- #define _List_H_
- #include <iostream>
- struct Node{
- int element; //節點儲存資訊可以根據需要修改!
- Node* next;
- };
-
Node* CreateLists(); //建立一個空表,並返回表頭
- void DeleteLists(Node* head); //刪除表頭為head的該連結串列
- bool IsLast(Node* P);
- Node* Find(int X, Node* head);
- Node* FindPrevious(int X, Node* head);
- void Delete(int X, Node* head);
- void Insert(Node* P, int X); //在節點P後面插入X
- void OutputLists(Node* head); //輸出連結串列中所有元素的值
- #endif
然後在<List.cpp>
#include "List.cpp"
- #include "list.h"
- Node* CreateLists()
- {
- Node* head = new Node;
- head->next = NULL;
- return head;
- }
- void DeleteLists(Node* head)
- {
- Node* P = head->next, *temp;
- head->next = NULL;
- while(P)
- {
-
temp = P->next;
- delete P;
- P = temp;
- }
- }
- bool IsLast(Node* P)
- {
- return P->next == NULL;
- }
- Node* Find(int X, Node* head)
- {
- Node* P = head->next;
- while(P && P->element!=X)
- P = P->next;
- return P;
- }
- Node* FindPrevious(int X, Node* head)
- {
- Node* P=head;
- while(P->next && P->next->element!=X)
- P=P->next;
- return P;
- }
- void Delete(int X, Node* head)
- {
- Node* P = FindPrevious(X,head), *temp; //如果沒找到X,則返回的是連結串列最後一項
- if(P->next)
- {
- temp = P->next;
- P->next = temp->next;
- delete temp;
- }
- }
- void Insert(Node* P, int X)
- {
- Node* tempX = new Node;
- tempX->element = X;
- tempX->next = P->next;
- P->next = tempX;
- }
- void OutputLists(Node* head)
- {
- Node* P = head->next;
- while(P)
- {
- std::cout<<P->element<<" ";
- P = P->next;
- }
- std::cout<<std::endl;
- }
- #include <iostream>
- #include <assert.h>
- #include "list.h"
- usingnamespace std;
- int main()
- {
- int Date[8] = {2,9,5,8,15,32,7,4};
- Node *head = CreateLists();
- Node *P = head;
- for(int i=0;i<8;i++)
- {
- Insert(P,Date[i]);
- P = P->next;
- }
- cout<<"打印出連結串列中所有元素:\n";
- OutputLists(head);
- if(IsLast(P))
- cout<<"該Lists的最後一個節點為:"<<P->element<<endl;
- else
- cout<<"P不是最後一個節點!!P等於:"<<P->element<<endl;
- if(Find(15,head))
- cout<<"Find函式在該Lists中找到了值為15的節點!!!\n";
- else
- cout<<"Find函式沒找到值為15的節點!!\n";
- cout<<"FindPrevious函式找到節點15的前一個節點為:"<<FindPrevious(15,head)->element<<endl;
- cout<<"而節點15的前一個節點應該為“8”!\n";
- Delete(8, head);
- if(Find(8,head))
- cout<<"Delete(8, head)後,在該Lists中找到了值為8的節點!!!\n";
- else
- cout<<"Delete(8, head)後,Find函式沒找到值為8的節點!!\n";
- DeleteLists(head);
- if(head->next)
- cout<<"DeleteLists函式未成功刪除連結串列!!\n";
- else
- cout<<"連結串列已經為空!DeleteLists函式沒有問題!!!\n";
- return 0;
- }
相關推薦
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