您的位置:首页 > 理论基础 > 数据结构算法

【数据结构】双向链表的基本操作

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;

}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: