單鏈表基礎操作C++實現

來源:互聯網
上載者:User

下面通過C++代碼實現單鏈表的如下簡單操作

viod Add(T val);    //在鏈表尾部添加元素

bool InsertAt(int pos, T val);   //在索引為pos的位置插入元素val

bool Remove();  //刪除鏈表尾部元素

boo RemoveAt(int pos); //刪除任意指定索引所在元素

 

 

T GetHeadVal(); //返回鏈表頭部元素的值

T GetTailVal(); //尾部元素的值

int Find(T val);  //如果找到鏈表中有此元素返回索引號,沒找到返回-1

bool IsEmpty();  //判斷鏈表是否為空白

void Clear();  //清空鏈表

int Size();  //返回鏈表元素總個數

 

1.節點類定義

template<class T>

struct Node

{

T val;  //結點值

Node<T>* next;  //指向下一個元素的指標

Node(T nVal)

{

val = nVal;

next = NULL;

}

Node(void){}

};

 

2.鏈表類的定義

#include "Node.h"

#include <iostream>

using namespace std;

 

template<class T>

class LinkList

{

int size; //元素個數

public:

Node<T>* head; //鏈表頭部指標

Node<T>* tail;  //鏈表尾部指標

LinkList(void) //建構函式

{

head = tail= NULL;

size = 0;

}

~LinkList(void) {Clear();}

 

void Add(T val) //尾部添加元素

{

Node<T>* pNode = new Node<T>(val);

if(head == NULL)  //當鏈表為空白時

{

head = pNode;

tail = pNode;

}

else{

tail->next = pNode;

tail = pNode;

}

size ++;

}

 

 

bool InsertAt(int pos,T val)  //插入元素

{

Node<T>* pNode = NULL;

if(pos < 0 || pos > size){

cout<<"out of range"<<endl;

return false;

}

 

if(pos == size)  //在尾部插入元素

{

Add(val);

returntrue;

}

elseif(pos
== 0)  //在頭部插入元素

{

pNode = new Node<T>(val);

pNode->next = head;

head = pNode;

}

else{

Node<T>* pNode = GetPointerAt(pos - 1); //返回插入位置前面元素指標

Node<T>* newNode = new Node<T>(val);

newNode->next = pNode->next;

pNode->next = newNode;

}

size ++;

return  true;

}

 

bool Remove(){//刪除尾部元素

return RemoveAt(size - 1);

}

 

bool RemoveAt(int pos) //刪除指定位置元素

{

Node<T>* pNode = NULL;

if(size == 0 ){

cout<<"list is empty"<<endl;

returnfalse;

}

if(pos < 0 || pos > size - 1){

cout<<"out of range"<<endl;

returnfalse;

}

 

if(size == 1) //只有一個元素時相當清空鏈表

{

Clear();

}

else{

if(pos == 0) //刪除頭部元素

{

pNode = head;

head = head->next;

delete pNode;

}

else{

Node<T>* pPreNode = GetPointerAt(pos - 1);

pNode = pPreNode->next;

pPreNode->next = pNode->next;

delete pNode;

if(pos == size - 1) //尾部元素

tail = pPreNode;

}

}

size -- ;

return true;

}

 

T GetHeadVal() {

if(size == 0){

cout<<"list is empty"<<endl;

return NULL;

}

return head->val;

}

T GetTailVal() {

if(size == 0){

cout<<"list is empty"<<endl;

return NULL;

}

return tail->val;

}

 

int Find(T val) //尋找元素

{

int index = 0;

Node<T>* ip = head;

while(ip != NULL){

if(ip->val == val)

return  index;

ip = ip->next;

index++;

}

return  -1;

}

 

bool IsEmpty(){return size == 0 ?true:false;}

int Size(){ return
size;}

void Clear(){

 while(head != NULL){

Node<T>* tmp = head->next;

delete head;

head = tmp;

}

tail = NULL;

size = 0;

}

 

private:

 Node<T>* GetPointerAt(int pos){

Node<T>* pNode = NULL;

if(pos < 0 || pos > size - 1)

cout<<"out of range"<<endl;

else{

pNode = head;

for(int
i = 1; i <= pos; i++)

pNode = pNode->next;

}

return pNode;

}

 

}

;

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.