1. 程式人生 > >循環鏈表簡單操作 C++

循環鏈表簡單操作 C++

code ace turn 是否 view mage 節點 頭部 size

帶有頭節點的循環鏈表。頭節點的數據域為空,在查找某元素是否在鏈表中時,可用與存放該元素。頭節點的next指針指向第一個元素。最後一個元素指向頭節點。如圖:技術分享

//CircularList.h文件
#pragma once
template<class T>
struct listNode{
    T element;
    listNode<T>* next;
    listNode(){}
    listNode(const T&theElement){ this->element = theElement; }
    listNode(
const T&theElement, listNode<T>*theNext){ this->element = theElement; this->next = theNext; } }; //有頭節點的循環鏈表,頭節點的指針域指向第一個元素 template<class T> class CircularList { public: CircularList(); CircularList(const CircularList<T>&); ~CircularList(); void insertFirstNode(const
T& element); void insertLastNode(const T&element); void listInsert(int index, const T&element); void listDelete(int index); void print(); int listNodeFine(const T&); private: listNode<T>* ahead; int listSize; };
//CircularList.cpp文件
#include "CircularList.h
" #include<iostream> using namespace std; #include "CircularList.h" #include<iostream> using namespace std; template<class T> //構造函數 CircularList<T>::CircularList() { ahead = new listNode<T>(); ahead->next = ahead; listSize = 0; } template<class T> //復制構造函數 CircularList<T>::CircularList(const CircularList<T>& List) { ahead = new listNode<T>; ahead->next = ahead; //首先形成一個空的循環鏈表 listNode<T>*current = ahead; listNode<T>*ListCurrent = List.ahead->next; if (List.ahead->next != List.ahead) //被拷貝的鏈表不為空 { while (ListCurrent->next != List.ahead) { listNode<T>*newNode = new listNode<T>(ListCurrent->element, current->next); current->next = newNode; current = current->next; ListCurrent = ListCurrent->next; listSize++; } if (ListCurrent->next == List.ahead) { listNode<T>*newNode = new listNode<T>(ListCurrent->element, current->next); current->next = newNode; listSize++; } } } template<class T> //析構函數 CircularList<T>::~CircularList() { while (ahead != NULL) { listNode<T>*current = ahead->next; delete ahead; ahead = current; } cout << "析構函數調用" << endl; } template<class T> //在頭部插入元素 void CircularList<T>::insertFirstNode(const T& element) { listNode<T>*newNode = new listNode<T>(element,this->ahead->next); this->ahead->next = newNode; listSize++; } template<class T> //在尾部插入元素 void CircularList<T>::insertLastNode(const T&element) { listNode<T>*current = this->ahead; while (current->next != this->ahead) current = current->next; listNode<T>*newNode = new listNode<T>(element,current->next); current->next = newNode; listSize++; } template<class T> void CircularList<T>::listInsert(int index, const T&element) { listNode<T>*current = this->ahead->next; for (int i = 0; i < index-1; i++) current = current->next; listNode<T>*newNode = new listNode<T>(element, current->next); current->next = newNode; listSize++; } template<class T> void CircularList<T>::print() { listNode<T>*current = this->ahead->next; while (current != this->ahead) { cout << current->element << " "; current = current->next; } cout << endl; } template<class T> void CircularList<T>::listDelete(int index) { if (index<0 || index>this->listSize) { cout << "刪除範圍有錯誤,請重新輸入!" << endl; return; } if (index == 0) { listNode<T>*current = this->ahead->next; ahead->next = current->next; delete current; } else { listNode<T>*current = this->ahead->next; for (int i = 0; i < index - 1; i++) current = current->next; listNode<T>*temp = current->next; current->next = temp->next; delete temp; } listSize--; } template<class T> int CircularList<T>::listNodeFine(const T& A) { this->ahead->element = A; listNode<T>*current = this->ahead->next; int index = 0; while (current->element != A) { index++; current = current->next; } if (current == ahead) return -1; else return index; }

測試代碼:

技術分享
#include<iostream>
#include<string>
#include"CircularList.cpp"
#include"CircularList.h"
using namespace std;
int main()
{
    CircularList<int>y1;
    y1.insertFirstNode(1);
    y1.insertFirstNode(2);
    y1.insertLastNode(5);
    y1.insertLastNode(6);
    y1.listInsert(2, 10);
    y1.print();
    CircularList<int>y3(y1);
    y3.print();
    y1.listDelete(0);
    y1.print();
    y1.listDelete(1);
    y1.print();
    y1.listDelete(2);
    y1.print();
    int index = y1.listNodeFine(2);
    cout << index;

    cout << "hello world" << endl;
    system("pause");
    return 0;
}
View Code

循環鏈表簡單操作 C++