单链表(不带头结点)

    这个链表的实现只是针对整型,当然也可以使用模板实现任意型别的链表操作。
    头文件:
   
#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()
{
  LinkList list;
  list =new List;
  list->head=list->tail=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 InsertElemToEnd(LinkList list,int elem)
{//在表尾插入结点
  Link p;
  p=new Node;
  if(!p)
    cout<<"Faild."<<endl;
  p->value=elem;
  if(IsEmptyList(*list))
  {
    p->next=NULL;
    list->head=list->tail=p;
  }
  else
  {
    p->next=NULL;
    list->tail->next=p;    
    list->tail=p;
  }
  ++list->length;
}

void InsertElemToHead(LinkList list,int elem)
{//在表头插入结点
  Link p;
  p=new Node;
  if(!p)
    cout<<"Faild."<<endl;
  p->value=elem;
  if(IsEmptyList(*list))
  {
    p->next=NULL;
    list->head=list->tail=p;
  }
  else
  {
    p->next=list->head;
    list->head=p;
  }
  ++list->length;
}

void InsertElemToList(LinkList list,int pos,int elem)
{//在指定位置插入结点
  Link p,q;
  q=list->head;
  int len=ListLength(*list);
  if(pos>ListLength(*list)+1 || pos<1)
  {
    cout<<"The position is invalidate."<<endl;
  }
  else
  {
    if(pos==1)
    {//插在表头
      InsertElemToHead(list,elem);            
    }
    else if(pos==len+1)
    {//插在表尾
      InsertElemToEnd(list,elem);        
    }
    else
    {//插在表中间
      p=new Node;
      if(!p)
        cout<<"Failed!"<<endl;
      for(int i=1;i<pos-1;++i)    
      {
        q=q->next; //找到第pos-1个结点
      }
      p->value=elem;
      p->next=q->next;
      q->next=p;
      ++list->length;
    }
  }
}

void DeleteElemFromHead(LinkList list,int &elem)
{//删除表头
  Link p;
        if(!IsEmptyList(*list))
  {
    p=list->head;
    if((list->length)>1)
    {
      list->head=p->next;
    }
    else if(list->length==1)
    {
      list->head=list->tail=NULL;
    }
    elem=p->value;
    free(p);
    --list->length;
  }
  else
  {
    cout<<"The list is empty."<<endl;
  }
}

void DeleteElemFromEnd(LinkList list,int &elem)
{//删除表尾
  Link p,q;
  if(!IsEmptyList(*list))
  {
    if(list->length==1)
    {
      q=list->tail;
      list->head=list->tail=NULL;
    }
    else
    {
      p=list->head;
      q=list->tail;
      while(p->next!=q)
        p=p->next;//找到表尾的前一个结点
      list->tail=p;
      p->next=NULL;//不要忘记这句话
    }
    elem=q->value;
    free(q);
    --list->length;
  }
  else
  {
    cout<<"The list is empty."<<endl;
  }
}

void DeleteElemFromList(LinkList list,int pos,int &elem)
{//删除指定位置结点
  Link p,q;
  if(!IsEmptyList(*list))
  {
    if(pos>ListLength(*list) || pos<1)
    {
      cout<<"The position is invalidate."<<endl;
    }
    else
    {
      p=list->head;
      if(pos==1)
      {
        DeleteElemFromHead(list,elem);
      }
      else if(pos==ListLength(*list))
      {
        DeleteElemFromEnd(list,elem);
      }
      else
      {
        for(int i=1;i<pos-1;++i)    
        {
          p=p->next; //找到第pos-1个结点
        }
        q=p->next;
        p->next=q->next;
        elem=q->value;
        free(q);
                                                                --list->length;
      }    
    }    
  }
}
void ClearList(LinkList list)
{//清空链表
  if(list)
  {
    list->head=list->tail=NULL;
  }
  list->length=0;
}

void DestroyList(LinkList list)
{//销毁链表
  Link p,q;    
  p=q=list->head;
  if(list)
  {
    while(p)
    {//释放每一个结点指针
      q=p->next;
      free(p);
      p=q;
    }
  }
}

bool PreElem(List list,int pos,int &elem)
{//返回pos位置处的前驱
  Link p=list.head;
  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;
  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;
  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->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 VisitElemOfList(List list)
{//遍历链表,输出链表中的元素
  Link p;
  p=list.head;
  while(p)
  {
    cout<<p->value<<' ';
    p=p->next;
  }
  cout<<endl;
}
    测试代码:
   
#include "stdafx.h"
#include <string>
#include <conio.h>
#include "list.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  LinkList list;
  list=NewList();
  cout<<"The length of list is:"<<ListLength(*list)<<endl;
  if(IsEmptyList(*list))
    cout<<"The list is empty."<<endl;
  InsertElemToEnd(list,3);
  InsertElemToEnd(list,5);
  InsertElemToEnd(list,8);
  VisitElemOfList(*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;
  }
  InsertElemToHead(list,1);
  InsertElemToHead(list,12);
  VisitElemOfList(*list);
  ClearList(list);
  cout<<"The length of the list is:"<<ListLength(*list)<<endl;
  InsertElemToHead(list,5);
  InsertElemToHead(list,90);
  InsertElemToHead(list,120);
  VisitElemOfList(*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;
  }
  InsertElemToList(list,3,88);
  InsertElemToList(list,6,66);
  VisitElemOfList(*list);
  cout<<"The length of the list is:"<<ListLength(*list)<<endl;
  InsertElemToList(list,1,22);
  VisitElemOfList(*list);
  InsertElemToList(list,5,44);
  VisitElemOfList(*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;
  }
  int elem;
  DeleteElemFromHead(list,elem);
  VisitElemOfList(*list);
  cout<<"The length of the list is:"<<ListLength(*list)<<endl;
  DeleteElemFromEnd(list,elem);
  VisitElemOfList(*list);
  cout<<"The length of the list is:"<<ListLength(*list)<<endl;
  DeleteElemFromList(list,4,elem);
  VisitElemOfList(*list);
  cout<<"The length of the list is:"<<ListLength(*list)<<endl;
  InsertElemToList(list,-5,90);
  DeleteElemFromList(list,20,elem);
  int temp;
  if(PreElem(*list,1,temp))
  {
    cout<<"The pre_element of this position is:"<<temp<<endl;
  }
  if(PreElem(*list,3,temp))
  {
    cout<<"The pre_element of this position is:"<<temp<<endl;
  }
  if(NextElem(*list,2,temp))
  {
    cout<<"The next_element of this position is:"<<temp<<endl;
  }
  if(NextElem(*list,4,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;
  }
  DestroyList(list);
  cout<<"The address of the head is:"<<list->head<<endl;
  getch();
  return 0;
}

   
   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章