C++双链表各项操作(含代码)
目录
功能描述
- 双链表的初始化
- 头插法插入信息
- 尾插法插入信息
- 按照序号查找节点信息
- 按照信息查找(此处为按照学号查找)
- 插入结点操作,把目标信息插入到第i个结点
- 删除第i个数据
- 显示当前信息函数show(内置于各个操作中)
- 求当前链表长度函数length(内置于各个操作中)
代码实现
#include<iostream>
#define OVERFLOE -2
#define ERROR -1
#define OK 1
using namespace std;
typedef struct {
int no;
string name;
int score;
}elemType;
typedef struct Dnode{
elemType data;
struct Dnode* next;
struct Dnode* prior;
}Dnode,*DLinkList;
int initDLinkList(DLinkList& l) {
l = new Dnode;
if (l != NULL)
{
l->next = NULL;
l->prior = NULL;
return OK;
}
else
return ERROR;
}
DLinkList creatDLinkList1(DLinkList& l) {
Dnode* node; elemType e;
if (l != NULL)
initDLinkList(l);
cout << "输入学号,姓名,分数:"; cin >> e.no >> e.name >> e.score;
while (e.no != 000) {
node = new Dnode;
node->data = e;
node->next = l->next;
if(l->next != NULL)
l->next->prior = node;
node->prior = l;
l->next = node;
cout << "输入学号,姓名,分数:"; cin >> e.no >> e.name >> e.score;
}
return l;
}
DLinkList creatDLinkList2(DLinkList& l)
{
if (l != NU