單鏈表(不帶頭結點)

    這個鏈表的實現只是針對整型,當然也可以使用模板實現任意型別的鏈表操作。
    頭文件:
   
#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;
}

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