在刚开始时,会提示让用户先输入链表的值
以“2658 265 23 5658”为例,在菜单界面可以选择1进行遍历内容
可以通过菜单界面进行2.3操作来获取判空和取长
插入结点,在第三位插入323,再进行遍历操作可以查看插入后的链表
删除结点,删除链表的第四位,再进行遍历操作可以查看删除后的链表
按位查找,输入位置即可查找链表的值
按值查找,输入要查找的值可以查看位置
源码如下所示:
#include <iostream>
using namespace std;
//双向循环链表
template<typename T>
struct doubleNode
{
T data;
doubleNode<T>* prior;
doubleNode<T>* next;
};
template <class T>
class doubleLinklist
{
public:
doubleLinklist();
void createlist(int x);
~doubleLinklist();
bool empty(); //判空
void insert(T i, int x);//插入
void remove(int x);//删除
void printlist();//遍历
int getlength();//求长度
T getitem(int i);//按位查找
int locate(T x);//按值查找
private:
doubleNode<T>* head;
};
//创建链表
template<class T>
void doubleLinklist<T>::createlist(int x)
{
doubleNode<T>* p = new doubleNode<T>;
p->next = head;
p->prior = head->prior;
(head->prior)->next = p;
head->prior = p;
p->data = x;
}
template <class T>
doubleLinklist<T>::doubleLinklist()
{
head = new doubleNode<T>;
head->next = head;
head->prior = head;
}
template <class T>
doubleLinklist<T>::~doubleLinklist()
{
}
template<class T>
int doubleLinklist<T>::getlength()
{
doubleNode<T>* p = head;
int count = 0;
while (p->next != head)
{
count++;
p = p->next;
}
return count;
}
template<class T>
T doubleLinklist<T>::getitem(int i)
{
doubleNode<T>* p = head;
int j = 0;
if (i <= 0 && i > getlength())
{
throw"输入位置错误";
}
while (j < i)
{
p = p->next;
j++;
}
return p->data;
}
template<class T>
int doubleLinklist<T>::locate(T x)
{
doubleNode<T>* p = head->next;
int j = 1;
while (p != head)
{
if (p->data == x)
{
return j;
}
j++;
p = p->next;
}
return 0;
}
template<class T>
void doubleLinklist<T>::printlist()
{
doubleNode<T>* p = head->next;
while (p != head)
{
cout << p->data << " ";
p = p->next;
}
}
template <class T>
bool doubleLinklist<T>::empty()
{
doubleNode<T>* p = head->next;
if (p != head)
{
return 1;
}
else
{
return 0;
}
}
template <class T>
void doubleLinklist<T>::insert(T i, int x)
{
doubleNode<T>* p = head;
doubleNode<T>* t;
if (x<0 || x>getlength() + 1)
{
throw"插入位置错误";
}
int q = 0;
while (q < x - 1)
{
p = p->next;
q++;
}
t = new doubleNode<T>;
t->data = i;
t->next = p->next;
t->prior = p;
(p->next)->prior = t;
p->next = t;
}
template <class T>
void doubleLinklist<T>::remove(int x)
{
doubleNode<T>* p = head;
doubleNode<T>* t;
if (x<0 || x>getlength())
{
throw"插入位置错误";
}
int count = 0;
while (count < x - 1)
{
p = p->next;
count++;
}
t = p->next;
(t->prior)->next = t->next;
(t->next)->prior = t->prior;
delete t;
}
int main()
{
int k, m;
int b;
doubleLinklist<int> d;
cout << "请输入链表的值:";
for (;;)
{
if (getchar() == '\n')
{
break;
}
cin >> b;
d.createlist(b);
}
for (;;)
{
system("cls");
int choose;
cout << "请选择功能:" << endl;
cout << "1.遍历链表内容" << endl;
cout << "2.判断是否为空" << endl;
cout << "3.获取链表长度" << endl;
cout << "4.插入结点" << endl;
cout << "5.删除结点" << endl;
cout << "6.按位查找" << endl;
cout << "7.按值查找" << endl;
cout << "0.退出程序" << endl;
cin >> choose;
switch (choose)
{
case 1:
cout << "链表的值为:";
d.printlist();
cout << endl;
system("pause");
break;
case 2:
if (d.empty() == 1)
{
cout << "双向链表不为空" << endl;
}
else
cout << "双向链表为空" << endl;
system("pause");
break;
case 3:
cout << "链表长度为" << d.getlength() << endl;
system("pause");
break;
case 4:
cout << "请输入你要插入的值和位置:";
cin >> k >> m;
d.insert(k, m);
cout << endl;
system("pause");
break;
case 5:
cout << "请输入删除的位置:";
cin >> k;
d.remove(k);
system("pause");
break;
case 6:
cout << "请输入所在值的位置:";
cin >> k;
cout << "该值为:" << d.getitem(k) << endl;
system("pause");
break;
case 7:
cout << "请输入要查找的值:";
cin >> m;
if (d.locate(m) == 0)
{
cout << "未查到该值" << endl;
}
else
{
cout << "该值在" << d.locate(m) << "号位置" << endl;
}
system("pause");
break;
case 0:
return 0;
default:
cout << "输入错误,请重新输入";
system("pause");
break;
}
}
return 0;
}