您的位置:首页 > 其它

双向链表

2015-07-23 21:35 330 查看

双向链表

双向链表,不解释。
#ifndef DOUBLELINKLIST_H_INCLUDED
#define DOUBLELINKLIST_H_INCLUDED
#include <iostream>

using namespace std;

struct DNode{
int Data;
DNode *Pre;
DNode *Next;
};

class doublelist
{
DNode *head;
public:
doublelist(){head = NULL;}
DNode* GetHead(){return head;}
void insertdoublelist(int aData);/**< 插入 */
void insertdistinctdoublelist(int aData);/**< 无重复插入 */
void outputdoublelist();/**< 输出 */
void deletedoublelist(int aData);/**< 删除节点 */
bool duplicatednode(int aData);/**< 判断是否重复 */
void insertdoublelistorderedasc(int aData);/**< 升序插入 */
void insertdoublelistordereddesc(int aData);/**< 降序插入 */
void reversedoublelist();/**< 反转 */
};

void doublelist::insertdoublelist(int aData)
{
DNode *p,*q,*s;
s = (DNode*)new(DNode);
s->Data = aData;
s->Pre = NULL;
s->Next = NULL;
p = head;
if (head == NULL)
{
head = s;
}
else
{
while (p != NULL)
{
q = p;
p = p->Next;
}
q->Next = s;
s->Pre = q;
}
}

void doublelist::outputdoublelist()
{
DNode *current = head;
while(current)
{
cout<<current->Data<<" ";
current = current->Next;
}
cout<<endl;
}

void doublelist::deletedoublelist(int aData)
{
if (head == NULL)
{
cout<<"List is Empty"<<endl;
}
else
{
DNode *p,*q;
p = head;
while ((p != NULL) && (p->Data != aData))
{
q = p;
p = p->Next;
}
if (p == NULL)
{
cout<<"Do not exist"<<aData<<endl;
}
else
{
q->Next = p->Next;
p->Next->Pre = q;
free(p);
}
}
}

bool doublelist::duplicatednode(int aData)
{
DNode *p;
p = GetHead();
if (p == NULL)
{
return false;
}
else
{
while ((p != NULL) && (p->Data != aData))
{
p = p->Next;
}
if (p == NULL)
{
return false;
}
else
{
return true;
}
}
}

void doublelist::insertdoublelistorderedasc(int aData)
{
DNode *p,*q,*s;
s = (DNode*)new(DNode);
s->Data = aData;
s->Pre = NULL;
s->Next = NULL;
p = head;
q = head;
if (head == NULL)
{
head = s;
return;
}
if (p->Data > aData)
{
s->Next = head;
head->Pre = s;
head = s;
return;
}
else
{
for(q = head,p = head->Next;p;q = p,p= p->Next)
{
if (p->Data > aData)
{
q->Next = s;
s->Next = p;
p->Pre = s;
s->Pre = q;
return;
}
}
q->Next = s;
s->Pre= q;
return;
}
}

void doublelist::insertdoublelistordereddesc(int aData)
{
DNode *p,*q,*s;
s = (DNode*)new(DNode);
s->Data = aData;
s->Pre = NULL;
s->Next = NULL;
p = head;
q = head;
if (head == NULL)
{
head = s;
return;
}
if (p->Data < aData)
{
s->Next = head;
head->Pre = s;
head = s;
return;
}
else
{
for(q = head,p = head->Next;p;q = p,p= p->Next)
{
if (p->Data < aData)
{
q->Next = s;
s->Next = p;
p->Pre = s;
s->Pre = q;
return;
}
}
q->Next = s;
s->Pre= q;
return;
}
}

void doublelist::insertdistinctdoublelist(int aData)
{
if (duplicatednode(aData))
{
cout<<"Duplicate Node:"<<aData<<endl;
}
else
{
insertdoublelist(aData);
}
}

void doublelist::reversedoublelist()
{
DNode *p,*q,*s;
if (head == NULL || head->Next ==NULL)
{
cout<<"linklist Can not be reversed"<<endl;
return;
}
s = head->Next;
p = s->Next;
s->Next = head;
head->Pre = s;
head->Next = NULL;

while(p)
{
q = p->Next;
p->Next = s;
s->Pre = p;
s = p;
head = s;
p = q;
}
}

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