写了一个关于双链表的操作,功能还没添加完,持续更新中......
#include<iostream>
using namespace std;
#define x NULL
#define Node Elemtype //定义
typedef int Datatype; //别名
//构建一个Node类
class Node
{
public:
int data;
Node *pre;
Node *next;
};
//构建一个双链表类
class Linklist
{
public:
Linklist();//初始化链表
~Linklist();//销毁链表
void CreateLinklist(int n);//创建长度为n的链表
void TravalLinklist();//遍历双链表
int Getlength();//获取链表长度
bool isEmpty();//判断是否为空
Elemtype *Find(Datatype data);//查找结点
void InsertElemAtEnd(Datatype);//在尾部插入数据
void InsertElemAtindex(Datatype Data, Datatype n);//在指定位置插入数据
void InsertElemAthead(Datatype Data); //在首部插入指定元素
void DeleteElemAtEnd(); //在尾部删除元素
void DeleteAll();; //删除所有元素
void DeleteElemAtPoint(Datatype data); //删除指定元素
void DeleteElemAtHead(); //删除头部元素
Elemtype invert(); //链表反转
int getfirst();
int getlast();
private:
Node *head;
Node *last;
int N;
};
Linklist::Linklist()//初始化链表
{
head = new Node;
last = new Node;
head->data = NULL;
head->next = NULL;
head->pre = NULL;
last->data = 0;
last->next = 0;
last->pre = 0;
N = 0;
}
Linklist::~Linklist()//销毁链表
{
delete head;
delete last;
}
void Linklist::CreateLinklist(int n)//创建长度为n的链表
{
Elemtype *p, *pnew;
p = head;
if (n < 0)
{
cout << "你输入的数据有误" << endl;
}
else
{
for (int i = 0; i < n; i++)
{
pnew = new Node;//这就是最后一个数字
cout << "请输入" << i + 1 << "个数据" << endl;
cin >> pnew->data;
pnew->next =NULL;
pnew->pre = p;
p->next = pnew;
p = pnew;
N++;
}
}
}
void Linklist::TravalLinklist()//遍历双链表
{
if (head->next == 0 || head == 0)
{
cout << "这是一个空链表" << endl;
}
else
{
Elemtype *p = head;
while (p->next)
{
p = p->next;
cout << p->data <<" ";
}
cout << endl;
}
}
int Linklist::Getlength()//获取链表长度
{
return N;
}
bool Linklist::isEmpty()//判断是否为空
{
if (head->next == NULL)
{
return true;
}
else
{
return false;
}
}
Elemtype *Linklist::Find(Datatype data)//查找结点
{
Elemtype *p = head;
if (p == NULL)
{
cout << "此链表为空链表" << endl;
return x;
}
else
{
while (p)
{
if (p->data == data)
{
return p;
}
else
{
p = p->next;
}
}
return NULL;
}
}
void Linklist::InsertElemAtEnd(Datatype data)//在尾部插入数据
{
Elemtype *p = head;
Elemtype *newnode = new Node;//这就是尾部的数据
newnode->data = data;
//newnode->next =NULL;
if (p == NULL)
{
head = newnode;
N++;
}
else
{
while (p->next)
{
p = p->next;
}
p->next = newnode;
newnode->pre = p;
newnode->next = NULL;
N++;
}
}
void Linklist::InsertElemAtindex(Datatype data, Datatype n)//在指定位置插入数据
{
Elemtype *p = head;
Elemtype *pnew = new Node;
pnew->data = data;
if (p == NULL)
{
head = pnew;
N++;
}
else
{
int i = 1;//设置一个索引值表示下标
while (n > i)
{
p = p->next;
i++;
}
p->next->pre = pnew;
pnew->next = p->next;
p->next = pnew;
pnew->pre = p;
N++;
cout << "添加数据成功" << endl;
}
}
void Linklist::InsertElemAthead(Datatype data) //在首部插入指定元素
{
Elemtype *p = head;
Elemtype *pnew = new Node;
pnew->data = data;
if (head == NULL)
{
head = pnew; //说明原来链表是空链表,则将数据直接放入
}
else
{
pnew->next = p->next;
p->next->pre = pnew;
p->next = pnew;
pnew->pre = p;
N++;
}
}
void Linklist::DeleteElemAtEnd() //在尾部删除元素
{
Elemtype *p = head;
Elemtype *ptemp = NULL;
while (p->next)
{
ptemp = p;
p = p->next;
}
delete p;
p = NULL;
ptemp->next = NULL;//此时ptemp为最后一个节点
N--;
}
void Linklist::DeleteAll() //删除所有元素
{
head->next=NULL;
N = 0;
}
void Linklist::DeleteElemAtPoint(Datatype data) //删除指定元素
{
Elemtype *ptemp = Find(data);
Elemtype *p = head;
while (p->next!=ptemp)
{
p = p->next;
}
p->next = ptemp->next;
ptemp->next->pre = p;
delete ptemp;
ptemp = NULL;
N--;
}
void Linklist::DeleteElemAtHead() //删除头部元素
{
Elemtype *p = head;
if (p == NULL || p->next == NULL) //说明是空链表
{
cout << "这是个空链表" << endl;
}
else
{
Elemtype *ptemp = NULL;
p = p->next;
ptemp = p->next;
delete p;
p = NULL;
head->next = ptemp;
ptemp->pre = head;
}
N--;
}
Elemtype Linklist::invert() //链表反转
{
if (head->next == NULL || head->next->next == NULL)
{
return *head; /*链表为空或只有一个元素则直接返回*/
}
Elemtype *t = NULL;
Elemtype *p = head->next;
Elemtype *q = head->next->next;
while (q != NULL)
{
t = q->next;
q->next = p;
p = q;
q = t;
}
head->next->next = NULL; /*设置链表尾*/
head->next = p; /*调整链表头*/
return *p;
}
int Linklist::getfirst() //得到首节点
{
if (isEmpty())
{
return NULL;
}
cout << "首节点为:" << head->next->data << endl;
}
int Linklist::getlast()//得到尾结点
{
if (isEmpty())
{
return NULL;
}
Elemtype *p = head;
while (p->next)
{
p = p->next;
}
cout << p->data;
}
int main()
{
Linklist L;
while (1)
{
cout << "******************************************************************************" << endl;
cout << "1.创建单链表 2.遍历单链表 3.获取单链表长度 4.判断单链表是否为空 5.反转" << endl;
cout << "6.获取节点 7.在尾部插入指定元素 8.在指定位置插入指定元素 9.在头部插入指定元素" << endl;
cout << "10.在尾部删除指定元素 11.删除所有元素 12.删除指定元素 13.删除头部元素 14.得到首节点 15.得到尾部节点 0.退出" << endl;
cout << "************************************************************************************" << endl;
int i;
cout << "请输入你的选择:";
cin >> i;
switch (i)
{
case 1:
int n;
cout << "请输入单链表的长度:";
cin >> n;
L.CreateLinklist(n);
system("pause");
system("cls");
continue;
case 2:
L.TravalLinklist();
system("pause");
system("cls");
continue;
case 3:
cout << "单链表的长度为:" << L.Getlength() << endl;
system("pause");
system("cls");
continue;
case 4:
if (L.isEmpty() == 1)
{
cout << "该单链表为空" << endl;
}
else
{
cout << "该单链表不为空" << endl;
}
system("pause");
system("cls");
continue;
case 5:
L.invert();
system("pause");
system("cls");
continue;
case 6:
Datatype data;
cout << "请输入要获取节点的值" << endl;
cin >> data;
cout << "该节点的值为" << L.Find(data)->data << endl;
system("pause");
system("cls");
continue;
case 7:
Datatype enddata;
cout << "请输入在尾部要插入的数据" << endl;
cin >> enddata;
L.InsertElemAtEnd(enddata);
system("pause");
system("cls");
continue;
case 8:
Datatype pointdata;
int index;
cout << "请输入要插入的数据" << endl;
cin >> pointdata;
cout << "请输入要插入的数据的位置" << endl;
cin >> index;
L.InsertElemAtindex(pointdata, index);
system("pause");
system("cls");
continue;
case 9:
Datatype begindata;
cout << "请输入在头部要插入的数据" << endl;
cin >> begindata;
L.InsertElemAthead(begindata);
system("pause");
system("cls");
continue;
case 10:
L.DeleteElemAtEnd();
system("pause");
system("cls");
continue;
case 11:
L.DeleteAll();
system("pause");
system("cls");
continue;
case 12:
Datatype pointdeletedata;
cout << "请输入要删除的数据" << endl;
cin >> pointdeletedata;
L.DeleteElemAtPoint(pointdeletedata);
system("pause");
system("cls");
continue;
case 13:
L.DeleteElemAtHead();
system("pause");
system("cls");
continue;
case 14:
L.getfirst();
system("pause");
system("cls");
continue;
case 15:
L.getlast();
system("pause");
system("cls");
continue;
default:
break;
}
}
system("pause");
return 0;
}