利用C++實現單鏈表的基本操作
利用C++的模板類,實現了一些對於單鏈表的一些基本操作,包括插入、刪除、查找等基本操作。實現的內容可能相對單薄,但我覺得能夠幫助大家對單鏈表有一個比較好的理解。代碼如下:
#include<iostream>
using namespace std;
template<typename datatype>class listnode;
/*************************鏈表結構聲明*********************************/
template<typename datatype> class linklist
{
public:
//無參數的構造函數
linklist()
{
head = new listnode<datatype>();
length = 0;
}
//有參數的構造函數
linklist(listnode<datatype> *node)
{
head =node;
}
~linklist()
{
delete []head;
}
public:
bool insertNode(int i,datatype data); //在i位置插入值
bool deleteNode(int i); //刪除i位置的值
datatype findValue(int i); //發現i位置的值
int length; //鏈表的長度
private:
listnode<datatype> *head;
//表示鏈表長度
};
/*************************鏈表數據結構聲明*********************************/
template<typename datatype>class listnode
{
public:
//頭結點指針初始爲空
listnode()
{
next = NULL;
}
//頭結點指針初始爲空
listnode(datatype item, listnode<datatype> *nextnode=NULL)
{
data = item;
next = nextnode;
}
~listnode()
{
}
//獲取節點內的數據
datatype getDate()
{
return data;
}
//獲取指針域
datatype *getNext()
{
return next;
}
private:
friend typename linklist<typename>;
datatype data;
listnode<datatype> *next;
};
/*************************主函數*********************************/
int main()
{
listnode<int> note;
linklist<int> list(¬e);
int n;
cout << "輸入鏈表長度" << endl;
cin >> n;
for (int i = 1; i <=n; i++)
{
list.insertNode(i,i);
}
for (int i = 1; i <=list.length; i++)
{
cout << "第" << i << "值=" << list.findValue(i) << ";";
}
cout << "" << endl;
int InsertNumber, Insertdata;
cout << "輸入插入點" << endl;
cin >> InsertNumber;
cout << "輸入插入值" << endl;
cin >> Insertdata;
list.insertNode(InsertNumber, Insertdata);
for (int i = 1; i <= list.length; i++)
{
cout << "第" << i << "值=" << list.findValue(i) << ";";
}
int deleteNumber;
cout << "" << endl;
cout << "輸入刪除點" << endl;
cin >> deleteNumber;
list.deleteNode(deleteNumber);
for (int i = 1; i <= list.length; i++)
{
cout << "第" << i << "值=" << list.findValue(i) << ";";
}
return 0;
}
/*************************鏈表的數據插入*********************************/
template<typename datatype>
bool linklist<datatype>::insertNode(int i, datatype data)
{
listnode<datatype> * p = head; //設置遊標指針,初始化爲頭節點地址
int j=1;
for (j = 1; j <=i-1; j++)
{
p = p->next; //利用遊標指針依次往下查找
if (p==NULL)
{
break;
}
}
if ((p == NULL) && (j>(i-1)))//指針爲空,且未到第i個位置,說明不存在第i個結點
{
cout << "插入位置無效" << endl;
return false;
}
listnode<datatype> * node = new listnode<typename>(data);
node->next = p->next; //將node的指針指向p的後繼結點地址
p->next = node; //p的後繼指針指向node
length = length + 1;
}
/*************************鏈表的指定位置上數據刪除*********************************/
template<typename datatype>
bool linklist<datatype>::deleteNode(int i)
{
listnode<datatype> * p = head; //設置遊標指針,初始化爲頭節點地址
int j = 1;
for (j = 1; j < i; j++)
{
p = p->next; //利用遊標指針依次往下查找
if (p == NULL)
{
break;
}
}
if ((p == NULL) && (j < (i - 1)))//指針爲空,且未到第i個位置,說明不存在第i個結點
{
cout << "插入位置無效" << endl;
return 0;
}
listnode<datatype> * q = p->next;
p->next = q->next;
delete q;
length = length - 1;
}
/*************************查找鏈表內的固定位置的值*********************************/
template<typename datatype>
datatype linklist<datatype>::findValue(int i)
{
listnode<datatype> * p = head; //設置遊標指針,初始化爲頭節點地址
int j =1;
for (j =1; j <= i ; j++)
{
p = p->next; //利用遊標指針依次往下查找
if (p == NULL)
{
break;
}
}
if ((p == NULL) && (j < (i - 1)))//指針爲空,且未到第i個位置,說明不存在第i個結點
{
cout << "插入位置無效" << endl;
return 0;
}
return p->data;
}
如果代碼有什麼問題,也歡迎大家相互交流。