雙向鏈表寫的少,總是忘記指定一個結點的前驅。
頭文件:
#include <iostream>
using namespace std;
typedef struct Node
{//結點結構
int value;
struct Node *pre,*next;//前驅指針和後繼指針
}Node,*Link;
typedef struct List
{//鏈表結構
Link head,tail;
int length;
}List,*LinkList;
LinkList NewList(void)
{//構造一個空的鏈表
LinkList list;
list=new List;
list->head=list->tail=NULL;
list->length=0;
return list;
}
int ListLength(List list)
{
return list.length;
}
bool IsListEmpty(List list)
{
if(list.length==0)
return true;
return false;
}
void ClearList(LinkList list)
{//清空鏈表,並釋放每個結點的空間
Link p,q;
p=list->head;
while(p!=list->tail)
{
q=p->next;
free(p);
p=q;
}
free(p);//釋放尾結點
list->tail=list->head=NULL;
list->length=0;
}
void DestroyList(LinkList list)
{
ClearList(list);
free(list->head);
free(list->tail);
}
void InsertElemToList(LinkList list,int pos,int elem)
{
Link p;
p=new Node;
p->value=elem;
if(pos<1 || pos>ListLength(*list)+1)
cout<<"The position is invalidate."<<endl;
else
{
if(pos==1)
{//插在表頭
if(IsListEmpty(*list))
{
list->head=list->tail=p;
p->next=NULL;
}
else
{
p->next=list->head;
list->head->pre=p;
list->head=p;
}
}
else if(pos==ListLength(*list)+1)
{//插在表尾
list->tail->next=p;
p->pre=list->tail;
list->tail=p;
p->next=NULL;
}
else
{
Link q=list->head;
for(int i=1;i<pos-1;++i)
{//找到pos-1位置的結點
q=q->next;
}
p->next=q->next;
p->pre=q;
q->next->pre=p;
q->next=p;
}
++list->length;
}
}
void DeleteElemFromList(LinkList list,int pos,int &elem)
{
if(IsListEmpty(*list))
{
cout<<"The list is empty,you can't delete element."<<endl;
}
else
{
if(pos<1 || pos>ListLength(*list))
{//刪除位置不正確
cout<<"The position is invalidate."<<endl;
return;//如果條件不成立,就結束
}
else
{
if(pos==1)
{//刪除表頭
if(ListLength(*list)==1)
{//表長爲1
Link p=list->head;
list->head=list->tail=NULL;
elem=p->value;
free(p);
}
else
{//表長大於1
Link p=list->head;
list->head=p->next;
p->next->pre=NULL;
elem=p->value;
free(p);
}
}
else if(pos==ListLength(*list))
{//刪除表尾
Link q=list->tail;
list->tail=q->pre;
list->tail->next=NULL;
elem=q->value;
free(q);
}
else
{
Link p=list->head;
for(int i=1;i<pos;++i)
{//找到pos位置的結點
p=p->next;
}
p->pre->next=p->next;
p->next->pre=p->pre;
elem=p->value;
free(p);
}
--list->length;
}
}
}
bool PreElem(List list,int pos,int &elem)
{//判斷 pos位置的結點有無前驅,若有則將其值存入elem
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
if(pos==1)
{
cout<<"This position doesn't have a pre_element."<<endl;
return false;
}
else
{
Link p=list.head;
for(int i=1;i<pos;++i)
{//找到pos位置處結點
p=p->next;
}
elem=p->pre->value;
return true;
}
}
}
bool NextElem(List list,int pos,int &elem)
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
if(pos==ListLength(list))
{
cout<<"This position doesn't have a next_element."<<endl;
return false;
}
else
{
Link p=list.head;
for(int i=1;i<pos;++i)
{//找到pos位置處結點
p=p->next;
}
elem=p->next->value;
return true;
}
}
}
bool CurrentElem(List list,int pos,int &elem)
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
Link p=list.head;
for(int i=1;i<pos;++i)
{
p=p->next;
}
elem=p->value;
return true;
}
}
bool GetHead(List list,int &elem)
{//如果鏈表不空,則返回表頭元素,用elem接收
if(IsListEmpty(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
elem=list.head->value;
return true;
}
}
bool GetTail(List list,int &elem)
{//如果鏈表不空,則返回表尾元素,用elem接收
if(IsListEmpty(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
elem=list.tail->value;
return true;
}
}
void VisitElemFromList(List list)
{
Link p=list.head;
if(!IsListEmpty(list))
{
while(p)
{
cout<<p->value<<' ';
p=p->next;
}
cout<<endl;
}
}
using namespace std;
typedef struct Node
{//結點結構
int value;
struct Node *pre,*next;//前驅指針和後繼指針
}Node,*Link;
typedef struct List
{//鏈表結構
Link head,tail;
int length;
}List,*LinkList;
LinkList NewList(void)
{//構造一個空的鏈表
LinkList list;
list=new List;
list->head=list->tail=NULL;
list->length=0;
return list;
}
int ListLength(List list)
{
return list.length;
}
bool IsListEmpty(List list)
{
if(list.length==0)
return true;
return false;
}
void ClearList(LinkList list)
{//清空鏈表,並釋放每個結點的空間
Link p,q;
p=list->head;
while(p!=list->tail)
{
q=p->next;
free(p);
p=q;
}
free(p);//釋放尾結點
list->tail=list->head=NULL;
list->length=0;
}
void DestroyList(LinkList list)
{
ClearList(list);
free(list->head);
free(list->tail);
}
void InsertElemToList(LinkList list,int pos,int elem)
{
Link p;
p=new Node;
p->value=elem;
if(pos<1 || pos>ListLength(*list)+1)
cout<<"The position is invalidate."<<endl;
else
{
if(pos==1)
{//插在表頭
if(IsListEmpty(*list))
{
list->head=list->tail=p;
p->next=NULL;
}
else
{
p->next=list->head;
list->head->pre=p;
list->head=p;
}
}
else if(pos==ListLength(*list)+1)
{//插在表尾
list->tail->next=p;
p->pre=list->tail;
list->tail=p;
p->next=NULL;
}
else
{
Link q=list->head;
for(int i=1;i<pos-1;++i)
{//找到pos-1位置的結點
q=q->next;
}
p->next=q->next;
p->pre=q;
q->next->pre=p;
q->next=p;
}
++list->length;
}
}
void DeleteElemFromList(LinkList list,int pos,int &elem)
{
if(IsListEmpty(*list))
{
cout<<"The list is empty,you can't delete element."<<endl;
}
else
{
if(pos<1 || pos>ListLength(*list))
{//刪除位置不正確
cout<<"The position is invalidate."<<endl;
return;//如果條件不成立,就結束
}
else
{
if(pos==1)
{//刪除表頭
if(ListLength(*list)==1)
{//表長爲1
Link p=list->head;
list->head=list->tail=NULL;
elem=p->value;
free(p);
}
else
{//表長大於1
Link p=list->head;
list->head=p->next;
p->next->pre=NULL;
elem=p->value;
free(p);
}
}
else if(pos==ListLength(*list))
{//刪除表尾
Link q=list->tail;
list->tail=q->pre;
list->tail->next=NULL;
elem=q->value;
free(q);
}
else
{
Link p=list->head;
for(int i=1;i<pos;++i)
{//找到pos位置的結點
p=p->next;
}
p->pre->next=p->next;
p->next->pre=p->pre;
elem=p->value;
free(p);
}
--list->length;
}
}
}
bool PreElem(List list,int pos,int &elem)
{//判斷 pos位置的結點有無前驅,若有則將其值存入elem
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
if(pos==1)
{
cout<<"This position doesn't have a pre_element."<<endl;
return false;
}
else
{
Link p=list.head;
for(int i=1;i<pos;++i)
{//找到pos位置處結點
p=p->next;
}
elem=p->pre->value;
return true;
}
}
}
bool NextElem(List list,int pos,int &elem)
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
if(pos==ListLength(list))
{
cout<<"This position doesn't have a next_element."<<endl;
return false;
}
else
{
Link p=list.head;
for(int i=1;i<pos;++i)
{//找到pos位置處結點
p=p->next;
}
elem=p->next->value;
return true;
}
}
}
bool CurrentElem(List list,int pos,int &elem)
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
Link p=list.head;
for(int i=1;i<pos;++i)
{
p=p->next;
}
elem=p->value;
return true;
}
}
bool GetHead(List list,int &elem)
{//如果鏈表不空,則返回表頭元素,用elem接收
if(IsListEmpty(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
elem=list.head->value;
return true;
}
}
bool GetTail(List list,int &elem)
{//如果鏈表不空,則返回表尾元素,用elem接收
if(IsListEmpty(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
elem=list.tail->value;
return true;
}
}
void VisitElemFromList(List list)
{
Link p=list.head;
if(!IsListEmpty(list))
{
while(p)
{
cout<<p->value<<' ';
p=p->next;
}
cout<<endl;
}
}
測試代碼:
#include "stdafx.h"
#include <conio.h>
#include "double_direction_list.h"
int _tmain(int argc, _TCHAR* argv[])
{
LinkList list;
list=NewList();
if(IsListEmpty(*list))
cout<<"The list is empty."<<endl;
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
InsertElemToList(list,1,3);
InsertElemToList(list,1,5);
InsertElemToList(list,ListLength(*list)+1,8);
VisitElemFromList(*list);
InsertElemToList(list,-1,2);
InsertElemToList(list,9,21);
int elem;
DeleteElemFromList(list,1,elem);
VisitElemFromList(*list);
InsertElemToList(list,1,22);
InsertElemToList(list,1,45);
InsertElemToList(list,4,23);
InsertElemToList(list,ListLength(*list),90);
InsertElemToList(list,ListLength(*list)+1,88);
VisitElemFromList(*list);
int e;
if(GetHead(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The tail_elem of the list is:"<<e<<endl;
}
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,2,elem);
VisitElemFromList(*list);
DeleteElemFromList(list,ListLength(*list),elem);
VisitElemFromList(*list);
DeleteElemFromList(list,1,elem);
VisitElemFromList(*list);
if(GetHead(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The tail_elem of the list is:"<<e<<endl;
}
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,ListLength(*list)+1,elem);
DeleteElemFromList(list,-2,elem);
int temp;
if(PreElem(*list,4,temp))
cout<<"The pre_element of this position is:"<<temp<<endl;
if(PreElem(*list,1,temp))
cout<<"The pre_element of this position is:"<<temp<<endl;
if(PreElem(*list,12,temp))
cout<<"The pre_element of this position is:"<<temp<<endl;
if(NextElem(*list,2,temp))
cout<<"The nex_element of this position is:"<<temp<<endl;
if(NextElem(*list,ListLength(*list),temp))
cout<<"The next_element of this position is:"<<temp<<endl;
if(NextElem(*list,-3,temp))
cout<<"The next_element of this position is:"<<temp<<endl;
if(CurrentElem(*list,2,temp))
cout<<"The current_element of this position is:"<<temp<<endl;
if(CurrentElem(*list,ListLength(*list)+2,temp))
cout<<"The current_element of this position is:"<<temp<<endl;
ClearList(list);
DeleteElemFromList(list,1,temp);
getch();
return 0;
}
#include <conio.h>
#include "double_direction_list.h"
int _tmain(int argc, _TCHAR* argv[])
{
LinkList list;
list=NewList();
if(IsListEmpty(*list))
cout<<"The list is empty."<<endl;
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
InsertElemToList(list,1,3);
InsertElemToList(list,1,5);
InsertElemToList(list,ListLength(*list)+1,8);
VisitElemFromList(*list);
InsertElemToList(list,-1,2);
InsertElemToList(list,9,21);
int elem;
DeleteElemFromList(list,1,elem);
VisitElemFromList(*list);
InsertElemToList(list,1,22);
InsertElemToList(list,1,45);
InsertElemToList(list,4,23);
InsertElemToList(list,ListLength(*list),90);
InsertElemToList(list,ListLength(*list)+1,88);
VisitElemFromList(*list);
int e;
if(GetHead(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The tail_elem of the list is:"<<e<<endl;
}
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,2,elem);
VisitElemFromList(*list);
DeleteElemFromList(list,ListLength(*list),elem);
VisitElemFromList(*list);
DeleteElemFromList(list,1,elem);
VisitElemFromList(*list);
if(GetHead(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The tail_elem of the list is:"<<e<<endl;
}
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,ListLength(*list)+1,elem);
DeleteElemFromList(list,-2,elem);
int temp;
if(PreElem(*list,4,temp))
cout<<"The pre_element of this position is:"<<temp<<endl;
if(PreElem(*list,1,temp))
cout<<"The pre_element of this position is:"<<temp<<endl;
if(PreElem(*list,12,temp))
cout<<"The pre_element of this position is:"<<temp<<endl;
if(NextElem(*list,2,temp))
cout<<"The nex_element of this position is:"<<temp<<endl;
if(NextElem(*list,ListLength(*list),temp))
cout<<"The next_element of this position is:"<<temp<<endl;
if(NextElem(*list,-3,temp))
cout<<"The next_element of this position is:"<<temp<<endl;
if(CurrentElem(*list,2,temp))
cout<<"The current_element of this position is:"<<temp<<endl;
if(CurrentElem(*list,ListLength(*list)+2,temp))
cout<<"The current_element of this position is:"<<temp<<endl;
ClearList(list);
DeleteElemFromList(list,1,temp);
getch();
return 0;
}