1. 程式人生 > >連結串列類CList成員的使用

連結串列類CList成員的使用

使用時 要

#include <afxtempl.h>

Construction

CList

Constructs an empty ordered list.

建立一個連結串列

example:

CList<int,int> myList;//建立一個int連結串列

CList<CString,CString&> myList(16);//建立一個cstring的連結串列,後面的16表示連結串列裡面資料的個數,如果不寫的話,可能是不限個數?

CList<MYTYPE,MYTYPE&> myList;//建立一個MYTYPE型別(自定義)的連結串列

如果不存入資料的話,剛建立的連結串列是空的,頭尾都為空

Head/Tail Access

Returns the head element of the list (cannot be empty).

返回連結串列的頭資料

例子:

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

int tmp=myList.GetHead();//tmp被賦予了0

Returns the tail element of the list (cannot be empty).

返回連結串列的尾資料

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

int tmp=myList.GetTail();//tmp被賦予了9999

Operations

Removes the element from the head of the list.

移除連結串列頭資料,連結串列資料個數減1,返回縮減前的頭資料

例子:

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

int tmp=myList.RemoveHead();//tmp被賦予了之前的頭資料:0;同時資料個數變為9999;

Removes the element from the tail of the list.

移除連結串列尾資料,連結串列資料個數減1,返回縮減前的尾資料

例子:

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

int tmp=myList.RemoveTail();//tmp被賦予了之前的尾資料:9999;同時資料個數變為9999;

Adds an element (or all the elements in another list) to the head of the list (makes a new head).

在連結串列頭處插入新資料,連結串列資料個數加1,返回新的連結串列頭位置(POSITION);

例子:

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.AddHead(int(314));//連結串列有了一個新的頭資料:314;同時連結串列個數變為10001;pos為新的頭的位置;

Adds an element (or all the elements in another list) to the tail of the list (makes a new tail).

在連結串列尾處插入新資料,連結串列資料個數加1,返回新的連結串列尾位置(POSITION);

例子:

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.AddTail(int(314));//連結串列有了一個新的尾資料:314;同時連結串列個數變為10001;pos為新的尾的位置;

Removes all the elements from this list.

清空連結串列,其頭尾皆變成空指標;

Iteration

Returns the position of the head element of the list.

返回連結串列頭的位置;

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.GetHeadPosition();//獲得連結串列頭的位置

Returns the position of the tail element of the list.

返回連結串列尾的位置;

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.GetTailPosition();//獲得連結串列尾的位置

Gets the next element for iterating.

返回當前位置的資料,之後,位置後移一位;

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.GetHeadPosition();//獲得連結串列頭的位置

int tmp=myList.GetNext(pos);//tmp被賦予了頭資料的值:0;同時pos指向第二個資料1;

Gets the previous element for iterating.

返回當前位置的資料,之後,位置前移一位;

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.GetTailPosition();//獲得連結串列尾的位置

int tmp=myList.GetNext(pos);//tmp被賦予了尾巴資料的值:9999;同時pos指向倒數第二個資料9998;

Retrieval/Modification

GetAt

Gets the element at a given position.

返回指定位置的資料;

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.GetTailPosition();//獲得連結串列尾的位置,還可以繼續改變pos,以指向其他資料

int tmp=myList.GetAt(pos);//tmp被賦予連結串列尾的資料

SetAt

Sets the element at a given position.

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.GetTailPosition();//獲得連結串列尾的位置,還可以繼續改變pos,以指向其他資料

myList.SetAt(pos,int(222));//將連結串列尾部的資料賦成222

Removes an element from this list, specified by position.

清除指定位置處的資料;同時資料個數減1;

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.GetTailPosition();//獲得連結串列尾的位置

myList.RemoveAt(pos);//連結串列pos(尾部)位置的資料被清除,資料個數變為9999;

Insertion

Inserts a new element before a given position.

在指定位置前插入一個新資料,資料個數加1;

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.GetHeadPosition();//獲得第一個資料的位置

myList.InsertBefore(pos,int(123));//在第一個資料前插入一個新資料: 123,同時資料個數變為10001

Inserts a new element after a given position.

在指定位置後插入一個新資料,資料個數加1;

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.GetHeadPosition();//獲得第一個資料的位置

myList.InsertAfter(pos,int(123));//在第一個資料後插入一個新資料: 123,同時資料個數變為10001

Searching

Find

Gets the position of an element specified by pointer value.

返回指定資料對應的位置;

CList<int,int> myList;

for(int i=0;i<10000;i++) { myList.AddTail(int(i)); }//存入資料

POSITION pos=myList.Find(int(0));//獲得0(連結串列頭)的位置

Gets the position of an element specified by a zero-based index.

返回索引號對應的位置;

POSITION pos=myList.FindIndex(0);//0表示連結串列頭,以此類推

Status

Returns the number of elements in this list.

返回連結串列的資料個數

int num=myList.GetCount();//獲得連結串列的資料個數

Tests for the empty list condition (no elements).

判定連結串列是否為空;

返回1表示連結串列是空,返回0表示連結串列非空;

BOOL empty=myList.IsEmpty();

相關推薦

連結串列CList成員的使用

使用時 要 #include <afxtempl.h> Construction CList Constructs an empty ordered list. 建立一個連結串列 example: CList<int,int> myList;//建立一個int

C++連結串列

連結串列是一種線性資料結構,佔用不連續的記憶體,用一個或兩個指標(或元素的代號)儲存與其相鄰的元素。 怎麼用類實現一個連結串列呢?(這裡我們討論雙向連結串列) 首先,我們可以建一個數組,儲存各元素,只不過元素的順序不一定是按照下標排列的。 元素可以用一個結構體表示,它有三個元素:數值

玩轉演算法面試:(五)LeetCode連結串列問題

在連結串列中穿針引線 連結串列和陣列都是線性結構,但是連結串列和陣列的不同在於陣列可以隨機的對於資料進行訪問。給出索引。可以以O(1)的時間複雜度迅速訪問到該元素。 連結串列只能從頭指標開始。 next指標指向哪裡? 206. Reverse Linked List

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

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

Python實現LeetCode連結串列演算法(例子:Merge k Sorted Lists)

連結串列的演算法關鍵點:新建立一個頭結點,並且將這個節點賦值給另外的連結串列物件來完成操作。 例如19. Remove Nth Node From End of List class Soluti

關於連結串列的拷貝建構函式與解構函式

連結串列類的拷貝建構函式需要把整個連結串列複製一遍! (有借鑑網友們的文章) CMyLinkList::CMyLinkList(const CMyLinkList & aList) {     //cout << "呼叫拷貝建構函式" <<

C++連結串列模板

記錄自己寫的一個連結串列類模板,兩個標頭檔案:一個是節點標頭檔案Node.h,一個是連結串列標頭檔案LinkList.h。 Node.h #pragma once #include <iostream> template <ty

Linu c++ 簡單實現連結串列模板

    之前在C語言裡面有實現連結串列,現在用c++簡單的實現連結串列類,只實現了插入節點和排序,我先開個好頭,其餘的程式碼基本上和C語言裡面的程式碼差不多,直接上程式碼咯;#include <iostream>#include <cstdio>usi

連結串列模板

#include <iostream> using namespace std; class CNode //定義一個節點類 { public: CNode *m_pNext; //定義一個節點指標,指向下一個節點 int m_Data; //定義節點的

c++連結串列建立使用

我們知道,陣列式計算機根據事先定義好的陣列型別與長度自動為其分配一連續的儲存單元,相同陣列的位置和距離都是固定的,也就是說,任何一個數組元素的地址都可一個簡單的公式計算出來,因此這種結構可以有效的對陣列元素進行隨機訪問。但若對陣列元素進行插入和刪除操作,則會引起大量資料的移

C++連結串列的運用,簡單實用。

// ------------------------------------------------------------------------- // 檔名 : list1.cpp // 建立者 : 方煜寬 //  郵箱 : [

MFC連結串列CList

一、類的概要和標頭檔案 1. CList類:產生不定型別的列表,是一個集合類,也是一個雙向連結串列類,是一個類模板。 2. #include "Afxtempl.h"   //包含標頭檔案 3. CList的建構函式,舉例如下: CList<CString ,CString&>

連結串列,不用

#include<iostream> #define ok 0 #define error -1 using namespace std; int num ; struct ListNode { int data; ListNode*next; }; void LL_displ

實現 連結串列

剛接觸面相物件,自己猜著寫的,請多指教 #include <stdio.h> #include <stdlib.h> #include <string.h> class List { private : typedef int ElementTyp

連結串列實現大數階乘

連結串列實現大數階乘 題目 大數運算——計算n的階乘 (n≥20)。 基本要求 (1)資料的表示和儲存: ①累積運算的中間結果和最終的計算結果的資料型別要求是整型——這是問題本身的要求。 ②試設計合適的儲存結構,要求每個元素或結點最多儲存資料的3位數值。 (2)資料的操作及其實現: 基於設計的儲

模板容器及迭代器的實現二(基於連結串列

節點類標頭檔案node.h: #ifndef MAIN_WTF_NODE1_H #define MAIN_WTF_NODE1_H #include <cstdlib> namespace main_wtf_6B {template<class Item>class

基於連結串列的容器bag實現

連結串列類標頭檔案: #ifndef MAIN_WTF_NODE1_H #define MAIN_WTF_NODE1_H #include <cstdlib> namespace main_wtf_5 { class node { publi

基於陣列的容器實現 和 基於連結串列結構的容器實現

1. 資料結構   概念 :資料結構是計算機儲存、組織資料的方式。    簡單理解:可以看成是對資料儲存的一種方式   儲存方式   :     1.變數       2.陣列    3.自定義容器 連結串列是一種線性的資料結構是一種最簡單的動態資料

資料結構學習筆記——C++實現雙向迴圈連結串列模板(超詳解)

定義了兩個標頭檔案分別放置結點類模板(Node.h)和雙鏈表模板(DoubleLinkList.h), 然後在原始檔的main函式中測試。 Node.h #pragma once # include <iostream> template <class

佇列(分別用列表和連結串列實現)

#!/usr/bin/python3 class QueueUnderflow(ValueError):     pass class ListQueue():  #列表實現迴圈佇列類     def __init__(self, len_