按照自己的想法和思路寫了一下帶頭結點的單鏈表,並進行了測試,畢竟自己能力有限,可能有的地方沒有測試到,還可能存在一些潛在的錯誤。
頭文件:
#include <iostream>
using namespace std;
typedef struct Node
{//結點結構
int value;
struct Node * next;
}Node,*Link;
typedef struct List
{
Link head,tail;
int length;
}List,*LinkList;
LinkList NewList(void)
{//新建一個空鏈表
LinkList list;
list=new List;//爲鏈表指針分配空間
list->head=new Node;//爲頭結點分配空間
if(!list->head)//分配失敗
cout<<"Failed."<<endl;
list->tail=list->head;
list->tail->next=NULL;
list->length=0;
return list;
}
int ListLength(List list)
{
return list.length;
}
bool IsEmptyList(List list)
{
if(list.length==0)
return true;
return false;
}
void InsertElemToHead(LinkList list,int elem)
{
Link p;
p=new Node;
p->value=elem;
if(IsEmptyList(*list))
{
list->head->next=p;
list->tail=p;
p->next=NULL;
}
else
{
p->next=list->head->next;
list->head->next=p;
}
++list->length;
}
void InsertElemToEnd(LinkList list,int elem)
{
Link p;
p=new Node;
p->value=elem;
p->next=NULL;//
if(IsEmptyList(*list))
{
list->head->next=p;
list->tail=p;
}
else
{
list->tail->next=p;
list->tail=p;//改變表尾指針
}
++list->length;
}
void InsertElemToList(LinkList list,int pos,int elem)
{
if(pos<1 || pos>ListLength(*list)+1)
cout<<"The position is invalidate."<<endl;
else
{
if(pos==1)
{
InsertElemToHead(list,elem);
}
else if(pos==ListLength(*list)+1)
{
InsertElemToEnd(list,elem);
}
else
{
Link p,q;
p=new Node;
p->value=elem;
q=list->head->next;//q指向第一個結點
for(int i=1;i<pos-1;++i)
{//找到第pos-1個結點
q=q->next;
}
p->next=q->next;
q->next=p;
++list->length;
}
}
}
void DeleteElemFromHead(LinkList list,int &elem)
{
Link p=list->head->next;
if(IsEmptyList(*list))
{//表空
cout<<"The list is empty,you can't delete element."<<endl;
}
else
{//表非空
if(ListLength(*list)==1)
{//表長爲1
list->head->next=NULL;
list->tail=list->head;
elem=p->value;
free(p);
}
else
{
list->head->next=p->next;
elem=p->value;
free(p);
}
--list->length;
}
}
void DeleteElemFromEnd(LinkList list,int &elem)
{
Link p=list->head->next;
Link q;
if(IsEmptyList(*list))
{
cout<<"The list is empty,you can't delete element."<<endl;
}
else
{
if(ListLength(*list)==1)
{
list->head->next=NULL;
list->tail=list->head;
elem=p->value;
free(p);
}
else
{
q=list->tail;
while(p->next!=q)
p=p->next;//找到表尾的前一個結點
list->tail=p;
p->next=NULL;
elem=q->value;
free(q);
}
--list->length;
}
}
void DeleteElemFromList(LinkList list,int pos,int &elem)
{
Link p,q;
if(pos<1 || pos>ListLength(*list))
cout<<"The position is invalidate."<<endl;
else
{
if(pos==1)
{
DeleteElemFromHead(list,elem);
}
else if(pos==ListLength(*list))
{
DeleteElemFromEnd(list,elem);
}
else
{
q=list->head->next;//q指向第一個結點
for(int i=1;i<pos-1;++i)
{//找到第pos-1個結點
q=q->next;
}
p=q->next;
q->next=p->next;
elem=p->value;
free(p);
--list->length;
}
}
}
bool PreElem(List list,int pos,int &elem)
{//返回pos位置處的前驅
Link p=list.head->next;
if(!IsEmptyList(list))
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else if(pos==1)
{
cout<<"This position doesn't have pre_element."<<endl;
return false;
}
else
{
for(int i=1;i<pos-1;++i)
{
p=p->next; //找到pos-1位置
}
elem=p->value;
return true;
}
}
}
bool NextElem(List list,int pos,int &elem)
{//返回pos位置處的後繼
Link p=list.head->next;
if(!IsEmptyList(list))
{
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 next_element."<<endl;
return false;
}
else
{
for(int i=1;i<pos;++i)
{
p=p->next;//找到pos位置
}
elem=p->next->value;
return true;
}
}
}
bool CurrentElem(List list,int pos,int &elem)
{//得到pos位置處的元素
Link p=list.head->next;
if(!IsEmptyList(list))
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
for(int i=1;i<pos;++i)
{
p=p->next;//找到pos位置
}
elem=p->value;
return true;
}
}
}
bool GetHead(List list,int &elem)
{//如果鏈表不空,則返回表頭元素,用elem接收
if(IsEmptyList(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
elem=list.head->next->value;
return true;
}
}
bool GetTail(List list,int &elem)
{//如果鏈表不空,則返回表尾元素,用elem接收
if(IsEmptyList(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
elem=list.tail->value;
return true;
}
}
void ClearList(LinkList list)
{//清空鏈表並釋放所有結點空間
if(!IsEmptyList(*list))
{
Link p=list->head->next; //p指向第一個結點
Link q;
while(p!=list->tail)
{//釋放每個結點空間
q=p->next;
free(p);
p=q;
}
free(p);//釋放尾結點
list->length=0;
}
}
void DestroyList(LinkList list)
{//銷燬鏈表
ClearList(list);//先清空鏈表
free(list->head);//釋放頭結點
}
void VistElemOfList(List list)
{//遍歷鏈表,輸出鏈表元素
Link p;
p=list.head->next;
if(!IsEmptyList(list))
{
while(p)
{
cout<<p->value<<' ';
p=p->next;
}
}
cout<<endl;
}
using namespace std;
typedef struct Node
{//結點結構
int value;
struct Node * next;
}Node,*Link;
typedef struct List
{
Link head,tail;
int length;
}List,*LinkList;
LinkList NewList(void)
{//新建一個空鏈表
LinkList list;
list=new List;//爲鏈表指針分配空間
list->head=new Node;//爲頭結點分配空間
if(!list->head)//分配失敗
cout<<"Failed."<<endl;
list->tail=list->head;
list->tail->next=NULL;
list->length=0;
return list;
}
int ListLength(List list)
{
return list.length;
}
bool IsEmptyList(List list)
{
if(list.length==0)
return true;
return false;
}
void InsertElemToHead(LinkList list,int elem)
{
Link p;
p=new Node;
p->value=elem;
if(IsEmptyList(*list))
{
list->head->next=p;
list->tail=p;
p->next=NULL;
}
else
{
p->next=list->head->next;
list->head->next=p;
}
++list->length;
}
void InsertElemToEnd(LinkList list,int elem)
{
Link p;
p=new Node;
p->value=elem;
p->next=NULL;//
if(IsEmptyList(*list))
{
list->head->next=p;
list->tail=p;
}
else
{
list->tail->next=p;
list->tail=p;//改變表尾指針
}
++list->length;
}
void InsertElemToList(LinkList list,int pos,int elem)
{
if(pos<1 || pos>ListLength(*list)+1)
cout<<"The position is invalidate."<<endl;
else
{
if(pos==1)
{
InsertElemToHead(list,elem);
}
else if(pos==ListLength(*list)+1)
{
InsertElemToEnd(list,elem);
}
else
{
Link p,q;
p=new Node;
p->value=elem;
q=list->head->next;//q指向第一個結點
for(int i=1;i<pos-1;++i)
{//找到第pos-1個結點
q=q->next;
}
p->next=q->next;
q->next=p;
++list->length;
}
}
}
void DeleteElemFromHead(LinkList list,int &elem)
{
Link p=list->head->next;
if(IsEmptyList(*list))
{//表空
cout<<"The list is empty,you can't delete element."<<endl;
}
else
{//表非空
if(ListLength(*list)==1)
{//表長爲1
list->head->next=NULL;
list->tail=list->head;
elem=p->value;
free(p);
}
else
{
list->head->next=p->next;
elem=p->value;
free(p);
}
--list->length;
}
}
void DeleteElemFromEnd(LinkList list,int &elem)
{
Link p=list->head->next;
Link q;
if(IsEmptyList(*list))
{
cout<<"The list is empty,you can't delete element."<<endl;
}
else
{
if(ListLength(*list)==1)
{
list->head->next=NULL;
list->tail=list->head;
elem=p->value;
free(p);
}
else
{
q=list->tail;
while(p->next!=q)
p=p->next;//找到表尾的前一個結點
list->tail=p;
p->next=NULL;
elem=q->value;
free(q);
}
--list->length;
}
}
void DeleteElemFromList(LinkList list,int pos,int &elem)
{
Link p,q;
if(pos<1 || pos>ListLength(*list))
cout<<"The position is invalidate."<<endl;
else
{
if(pos==1)
{
DeleteElemFromHead(list,elem);
}
else if(pos==ListLength(*list))
{
DeleteElemFromEnd(list,elem);
}
else
{
q=list->head->next;//q指向第一個結點
for(int i=1;i<pos-1;++i)
{//找到第pos-1個結點
q=q->next;
}
p=q->next;
q->next=p->next;
elem=p->value;
free(p);
--list->length;
}
}
}
bool PreElem(List list,int pos,int &elem)
{//返回pos位置處的前驅
Link p=list.head->next;
if(!IsEmptyList(list))
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else if(pos==1)
{
cout<<"This position doesn't have pre_element."<<endl;
return false;
}
else
{
for(int i=1;i<pos-1;++i)
{
p=p->next; //找到pos-1位置
}
elem=p->value;
return true;
}
}
}
bool NextElem(List list,int pos,int &elem)
{//返回pos位置處的後繼
Link p=list.head->next;
if(!IsEmptyList(list))
{
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 next_element."<<endl;
return false;
}
else
{
for(int i=1;i<pos;++i)
{
p=p->next;//找到pos位置
}
elem=p->next->value;
return true;
}
}
}
bool CurrentElem(List list,int pos,int &elem)
{//得到pos位置處的元素
Link p=list.head->next;
if(!IsEmptyList(list))
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
for(int i=1;i<pos;++i)
{
p=p->next;//找到pos位置
}
elem=p->value;
return true;
}
}
}
bool GetHead(List list,int &elem)
{//如果鏈表不空,則返回表頭元素,用elem接收
if(IsEmptyList(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
elem=list.head->next->value;
return true;
}
}
bool GetTail(List list,int &elem)
{//如果鏈表不空,則返回表尾元素,用elem接收
if(IsEmptyList(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
elem=list.tail->value;
return true;
}
}
void ClearList(LinkList list)
{//清空鏈表並釋放所有結點空間
if(!IsEmptyList(*list))
{
Link p=list->head->next; //p指向第一個結點
Link q;
while(p!=list->tail)
{//釋放每個結點空間
q=p->next;
free(p);
p=q;
}
free(p);//釋放尾結點
list->length=0;
}
}
void DestroyList(LinkList list)
{//銷燬鏈表
ClearList(list);//先清空鏈表
free(list->head);//釋放頭結點
}
void VistElemOfList(List list)
{//遍歷鏈表,輸出鏈表元素
Link p;
p=list.head->next;
if(!IsEmptyList(list))
{
while(p)
{
cout<<p->value<<' ';
p=p->next;
}
}
cout<<endl;
}
測試代碼:
#include "stdafx.h"
#include <conio.h>
#include "list.h"
int _tmain(int argc, _TCHAR* argv[])
{
LinkList list;
list=NewList();
if(IsEmptyList(*list))
cout<<"The list is empty."<<endl;
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
InsertElemToHead(list,5);
InsertElemToHead(list,9);
ClearList(list);
cout<<"The address of head is:"<<list->head<<endl;
InsertElemToHead(list,8);
InsertElemToEnd(list,10);
InsertElemToEnd(list,22);
InsertElemToEnd(list,36);
VistElemOfList(*list);
int e;
if(GetHead(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
InsertElemToList(list,3,21);
InsertElemToList(list,1,88);
InsertElemToList(list,8,99);
InsertElemToList(list,10,100);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
int elem;
DeleteElemFromEnd(list,elem);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromHead(list,elem);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
if(GetHead(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
DeleteElemFromList(list,4,elem);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,1,elem);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,ListLength(*list)+1,elem);
VistElemOfList(*list);
int temp;
if(PreElem(*list,1,temp))
{
cout<<"The pre_element of this position is:"<<temp<<endl;
}
if(PreElem(*list,2,temp))
{
cout<<"The pre_element of this position is:"<<temp<<endl;
}
if(NextElem(*list,1,temp))
{
cout<<"The next_element of this position is:"<<temp<<endl;
}
if(NextElem(*list,ListLength(*list),temp))
{
cout<<"The next_element of this position is:"<<temp<<endl;
}
if(CurrentElem(*list,2,temp))
{
cout<<"The cur_element of this position is:"<<temp<<endl;
}
if(CurrentElem(*list,12,temp))
{
cout<<"The cur_element of this position is:"<<temp<<endl;
}
ClearList(list);
VistElemOfList(*list);
getch();
return 0;
}
#include <conio.h>
#include "list.h"
int _tmain(int argc, _TCHAR* argv[])
{
LinkList list;
list=NewList();
if(IsEmptyList(*list))
cout<<"The list is empty."<<endl;
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
InsertElemToHead(list,5);
InsertElemToHead(list,9);
ClearList(list);
cout<<"The address of head is:"<<list->head<<endl;
InsertElemToHead(list,8);
InsertElemToEnd(list,10);
InsertElemToEnd(list,22);
InsertElemToEnd(list,36);
VistElemOfList(*list);
int e;
if(GetHead(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
InsertElemToList(list,3,21);
InsertElemToList(list,1,88);
InsertElemToList(list,8,99);
InsertElemToList(list,10,100);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
int elem;
DeleteElemFromEnd(list,elem);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromHead(list,elem);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
if(GetHead(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
DeleteElemFromList(list,4,elem);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,1,elem);
VistElemOfList(*list);
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,ListLength(*list)+1,elem);
VistElemOfList(*list);
int temp;
if(PreElem(*list,1,temp))
{
cout<<"The pre_element of this position is:"<<temp<<endl;
}
if(PreElem(*list,2,temp))
{
cout<<"The pre_element of this position is:"<<temp<<endl;
}
if(NextElem(*list,1,temp))
{
cout<<"The next_element of this position is:"<<temp<<endl;
}
if(NextElem(*list,ListLength(*list),temp))
{
cout<<"The next_element of this position is:"<<temp<<endl;
}
if(CurrentElem(*list,2,temp))
{
cout<<"The cur_element of this position is:"<<temp<<endl;
}
if(CurrentElem(*list,12,temp))
{
cout<<"The cur_element of this position is:"<<temp<<endl;
}
ClearList(list);
VistElemOfList(*list);
getch();
return 0;
}