按照自己的想法和思路写了一下带头结点的单链表,并进行了测试,毕竟自己能力有限,可能有的地方没有测试到,还可能存在一些潜在的错误。
头文件:
#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;
}