【数据结构】双向链表的基本操作
2015-12-29 22:49
591 查看
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct DuLNode
{
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode, *DuLinkList;
Status CreateDuLinkList(DuLinkList &L)
{
int len;
int i, value;
DuLNode *p;
DuLNode *s = (DuLNode *)malloc(sizeof(DuLNode));
if (s==NULL)
{
cout << "存储分配失败!" << endl;
exit(ERROR);
}
s->data = 0;
s->next = NULL;
s->prior = NULL;
L = s;
cout << "请输入您想要创建的双向链表的长度:" << endl;
cin >> len;
cout << "请输入双向链表元素:" << endl;
for (i = 0; i < len; i++)
{
cin >> value;
p=(DuLNode *)malloc(sizeof(DuLNode));
if (p == NULL)
{
cout << "存储分配失败!" << endl;
exit(ERROR);
}
p->data = value;
s->next = p;
p->prior = s;
p->next = NULL;
s = p;
}
return OK;
}
Status DuLinkListLength(DuLinkList L)
{
DuLNode *p;
int i = 0;
p = L->next;
if (!p)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
while (p)
{
i++;
p = p->next;
}
cout << "双向链表的长度为:"<<i << endl;
return i;
}
DuLNode *GetElem(DuLinkList L, int pos,ElemType &e)
{
int j = 1;
DuLNode *p = L->next;
while (j < pos&&p)
{
p = p->next;
j++;
}
e = p->data;
if (p!=NULL)
return p;
return NULL;
}
Status InsertDuLNode(DuLinkList &L, int pos, ElemType e)
{
DuLNode *p, *s;
s = new DuLNode;
s->data = e;
ElemType E;
if (pos == 1)//若插入的是第一个位置
{
s->next = L->next;
if (L->next != NULL)
L->next->prior = s;
s->prior = L;
L->next = s;
}
else
{
p = GetElem(L, pos - 1, E);
if (p == NULL)
return ERROR;
else
{
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
}
}
return OK;#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct DuLNode
{
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode, *DuLinkList;
Status CreateDuLinkList(DuLinkList &L)
{
int len;
int i, value;
DuLNode *p;
DuLNode *s = (DuLNode *)malloc(sizeof(DuLNode));
if (s==NULL)
{
cout << "存储分配失败!" << endl;
exit(ERROR);
}
s->data = 0;
s->next = NULL;
s->prior = NULL;
L = s;
cout << "请输入您想要创建的双向链表的长度:" << endl;
cin >> len;
cout << "请输入双向链表元素:" << endl;
for (i = 0; i < len; i++)
{
cin >> value;
p=(DuLNode *)malloc(sizeof(DuLNode));
if (p == NULL)
{
cout << "存储分配失败!" << endl;
exit(ERROR);
}
p->data = value;
s->next = p;
p->prior = s;
p->next = NULL;
s = p;
}
return OK;
}
Status DuLinkListLength(DuLinkList L)
{
DuLNode *p;
int i = 0;
p = L->next;
if (!p)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
while (p)
{
i++;
p = p->next;
}
cout << "双向链表的长度为:"<<i << endl;
return i;
}
DuLNode *GetElem(DuLinkList L, int pos,ElemType &e)
{
int j = 1;
DuLNode *p = L->next;
while (j < pos&&p)
{
p = p->next;
j++;
}
e = p->data;
if (p!=NULL)
return p;
return NULL;
}
Status InsertDuLNode(DuLinkList &L, int pos, ElemType e)
{
DuLNode *p, *s;
s = new DuLNode;
s->data = e;
ElemType E;
if (pos == 1)//若插入的是第一个位置
{
s->next = L->next;
if (L->next != NULL)
L->next->prior = s;
s->prior = L;
L->next = s;
}
else
{
p = GetElem(L, pos - 1, E);
if (p == NULL)
return ERROR;
else
{
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
}
}
return OK;
}
Status DeleteDLNode(DuLinkList &L, int pos)
{
if (L->next == NULL)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
DuLNode *p, *s;//保存删除的结点
ElemType E;
if (pos ==1)//若删除的是第一个位置
{
s = L->next;
L->next = s->next;
if (s->next != NULL)
s->next->prior = L;
free(s);
}
else
{
p = GetElem(L, pos - 1, E);
if (p == NULL)
{
cout << "第" << pos << "个结点不存在!" << endl;
return ERROR;
}
else
{
s = p->next;
p->next = s->next;
if (s->next != NULL)
s->next->prior = p;
free(s);
}
}
return OK;
}
Status PrintDuLinkList(DuLinkList L)
{
DuLNode *p;
p = L->next;
if (!p)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
cout << "双向链表的元素为:" << endl;
while (p)
{
cout << p->data<<" ";
p = p->next;
}
cout << endl;
return OK;
}
int main()
{
DuLinkList L;
CreateDuLinkList(L);
PrintDuLinkList(L);
DuLinkListLength(L);
int pos;
ElemType e;
cout << "请输入您要的到哪个位置处的元素:" << endl;
cin >> pos;
GetElem(L,pos,e);
cout << "您输入对应位置处的元素为:" << endl;
cout << e << endl;
cout << "请输入您要插入的位置及元素:" << endl;
cin >> pos >> e;
InsertDuLNode(L, pos, e);
PrintDuLinkList(L);
cout << "请输入您要删除结点的位置:" << endl;
cin >> pos;
DeleteDLNode(L, pos);
PrintDuLinkList(L);
return OK;
}
}
Status DeleteDLNode(DuLinkList &L, int pos)
{
if (L->next == NULL)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
DuLNode *p, *s;//保存删除的结点
ElemType E;
if (pos ==1)//若删除的是第一个位置
{
s = L->next;
L->next = s->next;
if (s->next != NULL)
s->next->prior = L;
free(s);
}
else
{
p = GetElem(L, pos - 1, E);
if (p == NULL)
{
cout << "第" << pos << "个结点不存在!" << endl;
return ERROR;
}
else
{
s = p->next;
p->next = s->next;
if (s->next != NULL)
s->next->prior = p;
free(s);
}
}
return OK;
}
Status PrintDuLinkList(DuLinkList L)
{
DuLNode *p;
p = L->next;
if (!p)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
cout << "双向链表的元素为:" << endl;
while (p)
{
cout << p->data<<" ";
p = p->next;
}
cout << endl;
return OK;
}
int main()
{
DuLinkList L;
CreateDuLinkList(L);
PrintDuLinkList(L);
DuLinkListLength(L);
int pos;
ElemType e;
cout << "请输入您要的到哪个位置处的元素:" << endl;
cin >> pos;
GetElem(L,pos,e);
cout << "您输入对应位置处的元素为:" << endl;
cout << e << endl;
cout << "请输入您要插入的位置及元素:" << endl;
cin >> pos >> e;
InsertDuLNode(L, pos, e);
PrintDuLinkList(L);
cout << "请输入您要删除结点的位置:" << endl;
cin >> pos;
DeleteDLNode(L, pos);
PrintDuLinkList(L);
return OK;
}
using namespace std;
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct DuLNode
{
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode, *DuLinkList;
Status CreateDuLinkList(DuLinkList &L)
{
int len;
int i, value;
DuLNode *p;
DuLNode *s = (DuLNode *)malloc(sizeof(DuLNode));
if (s==NULL)
{
cout << "存储分配失败!" << endl;
exit(ERROR);
}
s->data = 0;
s->next = NULL;
s->prior = NULL;
L = s;
cout << "请输入您想要创建的双向链表的长度:" << endl;
cin >> len;
cout << "请输入双向链表元素:" << endl;
for (i = 0; i < len; i++)
{
cin >> value;
p=(DuLNode *)malloc(sizeof(DuLNode));
if (p == NULL)
{
cout << "存储分配失败!" << endl;
exit(ERROR);
}
p->data = value;
s->next = p;
p->prior = s;
p->next = NULL;
s = p;
}
return OK;
}
Status DuLinkListLength(DuLinkList L)
{
DuLNode *p;
int i = 0;
p = L->next;
if (!p)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
while (p)
{
i++;
p = p->next;
}
cout << "双向链表的长度为:"<<i << endl;
return i;
}
DuLNode *GetElem(DuLinkList L, int pos,ElemType &e)
{
int j = 1;
DuLNode *p = L->next;
while (j < pos&&p)
{
p = p->next;
j++;
}
e = p->data;
if (p!=NULL)
return p;
return NULL;
}
Status InsertDuLNode(DuLinkList &L, int pos, ElemType e)
{
DuLNode *p, *s;
s = new DuLNode;
s->data = e;
ElemType E;
if (pos == 1)//若插入的是第一个位置
{
s->next = L->next;
if (L->next != NULL)
L->next->prior = s;
s->prior = L;
L->next = s;
}
else
{
p = GetElem(L, pos - 1, E);
if (p == NULL)
return ERROR;
else
{
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
}
}
return OK;#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct DuLNode
{
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode, *DuLinkList;
Status CreateDuLinkList(DuLinkList &L)
{
int len;
int i, value;
DuLNode *p;
DuLNode *s = (DuLNode *)malloc(sizeof(DuLNode));
if (s==NULL)
{
cout << "存储分配失败!" << endl;
exit(ERROR);
}
s->data = 0;
s->next = NULL;
s->prior = NULL;
L = s;
cout << "请输入您想要创建的双向链表的长度:" << endl;
cin >> len;
cout << "请输入双向链表元素:" << endl;
for (i = 0; i < len; i++)
{
cin >> value;
p=(DuLNode *)malloc(sizeof(DuLNode));
if (p == NULL)
{
cout << "存储分配失败!" << endl;
exit(ERROR);
}
p->data = value;
s->next = p;
p->prior = s;
p->next = NULL;
s = p;
}
return OK;
}
Status DuLinkListLength(DuLinkList L)
{
DuLNode *p;
int i = 0;
p = L->next;
if (!p)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
while (p)
{
i++;
p = p->next;
}
cout << "双向链表的长度为:"<<i << endl;
return i;
}
DuLNode *GetElem(DuLinkList L, int pos,ElemType &e)
{
int j = 1;
DuLNode *p = L->next;
while (j < pos&&p)
{
p = p->next;
j++;
}
e = p->data;
if (p!=NULL)
return p;
return NULL;
}
Status InsertDuLNode(DuLinkList &L, int pos, ElemType e)
{
DuLNode *p, *s;
s = new DuLNode;
s->data = e;
ElemType E;
if (pos == 1)//若插入的是第一个位置
{
s->next = L->next;
if (L->next != NULL)
L->next->prior = s;
s->prior = L;
L->next = s;
}
else
{
p = GetElem(L, pos - 1, E);
if (p == NULL)
return ERROR;
else
{
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
}
}
return OK;
}
Status DeleteDLNode(DuLinkList &L, int pos)
{
if (L->next == NULL)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
DuLNode *p, *s;//保存删除的结点
ElemType E;
if (pos ==1)//若删除的是第一个位置
{
s = L->next;
L->next = s->next;
if (s->next != NULL)
s->next->prior = L;
free(s);
}
else
{
p = GetElem(L, pos - 1, E);
if (p == NULL)
{
cout << "第" << pos << "个结点不存在!" << endl;
return ERROR;
}
else
{
s = p->next;
p->next = s->next;
if (s->next != NULL)
s->next->prior = p;
free(s);
}
}
return OK;
}
Status PrintDuLinkList(DuLinkList L)
{
DuLNode *p;
p = L->next;
if (!p)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
cout << "双向链表的元素为:" << endl;
while (p)
{
cout << p->data<<" ";
p = p->next;
}
cout << endl;
return OK;
}
int main()
{
DuLinkList L;
CreateDuLinkList(L);
PrintDuLinkList(L);
DuLinkListLength(L);
int pos;
ElemType e;
cout << "请输入您要的到哪个位置处的元素:" << endl;
cin >> pos;
GetElem(L,pos,e);
cout << "您输入对应位置处的元素为:" << endl;
cout << e << endl;
cout << "请输入您要插入的位置及元素:" << endl;
cin >> pos >> e;
InsertDuLNode(L, pos, e);
PrintDuLinkList(L);
cout << "请输入您要删除结点的位置:" << endl;
cin >> pos;
DeleteDLNode(L, pos);
PrintDuLinkList(L);
return OK;
}
}
Status DeleteDLNode(DuLinkList &L, int pos)
{
if (L->next == NULL)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
DuLNode *p, *s;//保存删除的结点
ElemType E;
if (pos ==1)//若删除的是第一个位置
{
s = L->next;
L->next = s->next;
if (s->next != NULL)
s->next->prior = L;
free(s);
}
else
{
p = GetElem(L, pos - 1, E);
if (p == NULL)
{
cout << "第" << pos << "个结点不存在!" << endl;
return ERROR;
}
else
{
s = p->next;
p->next = s->next;
if (s->next != NULL)
s->next->prior = p;
free(s);
}
}
return OK;
}
Status PrintDuLinkList(DuLinkList L)
{
DuLNode *p;
p = L->next;
if (!p)
{
cout << "双向链表为空!" << endl;
return ERROR;
}
cout << "双向链表的元素为:" << endl;
while (p)
{
cout << p->data<<" ";
p = p->next;
}
cout << endl;
return OK;
}
int main()
{
DuLinkList L;
CreateDuLinkList(L);
PrintDuLinkList(L);
DuLinkListLength(L);
int pos;
ElemType e;
cout << "请输入您要的到哪个位置处的元素:" << endl;
cin >> pos;
GetElem(L,pos,e);
cout << "您输入对应位置处的元素为:" << endl;
cout << e << endl;
cout << "请输入您要插入的位置及元素:" << endl;
cin >> pos >> e;
InsertDuLNode(L, pos, e);
PrintDuLinkList(L);
cout << "请输入您要删除结点的位置:" << endl;
cin >> pos;
DeleteDLNode(L, pos);
PrintDuLinkList(L);
return OK;
}
相关文章推荐
- 数据结构课程设计-12月29日
- 数据结构--冒泡排序
- [Coursera]数据结构基础_Week4_字符串_Q3
- [Coursera]数据结构基础_Week4_字符串_Q2
- [Coursera]数据结构基础_Week4_字符串_Q1
- 数据结构(三)
- 第十六周--数据结构--项目一-- 插入排序之排序中输出每一趟的中间结果
- 第十六周--数据结构--项目一-- 插入排序之希尔排序
- 04 两个栈构造队列 & 两个队列构造栈
- 第十六周--数据结构--项目一--插入排序之折半插入排序
- 数据结构之快排(山理工3398)
- 用Python编写数据结构的冒泡排序
- 拟阵理论简述
- 数据结构(期末小结)——开始
- Python编写数据结构中的二分查找
- 数据结构 之 哈希表
- H264学习_基本数据结构
- 数据结构 JAVA描述(三) 队列 + 栈与队列的比较
- Android 打造任意层级树形控件 考验你的数据结构和设计
- 串的抽象数据结构