雙向鏈表(帶頭結點)

    頭文件:
   
#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=new Node;//爲表頭結點分配空間
  list->tail=list->head;
  list->tail->next=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=list->head->next;
  Link q;
        while(p!=list->tail)
  {
    q=p->next;
    free(p);
    p=q;
  }
  free(p);//釋放尾結點
  list->tail=list->head;
  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->next=p;
        list->tail=p;
        p->pre=list->head;
        p->next=NULL;
      }
      else
      {//非空表
        p->next=list->head->next;
        list->head->next->pre=p;
        list->head->next=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->next;
      for(int i=1;i<pos-1;++i)
      {
        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(pos<1 || pos>ListLength(*list))
  {
    cout<<"The position is invalidate."<<endl;
  }
  else
  {
    if(IsListEmpty(*list))
    {//表空
      cout<<"The list is empty,you can't delete element."<<endl;
    }
    else
    {//表非空
      if(pos==1)
      {//刪除表頭
        if(ListLength(*list)==1)
        {//表長爲1
          list->tail=list->head;
          list->head->next=NULL;
        }
        else
        {
          Link q=list->head->next;
          list->head->next=q->next;
          q->next->pre=NULL;
          elem=q->value;
          free(q);
        }
      }
      else if(pos==ListLength(*list))
      {
        Link q=list->tail;
        list->tail=list->tail->pre;
        list->tail->next=NULL;
        elem=q->value;
        free(q);
      }
      else
      {
        Link q=list->head->next;
        for(int i=1;i<pos;++i)
        {//找到pos位置的結點
          q=q->next;
        }
        q->pre->next=q->next;
        q->next->pre=q->pre;
        elem=q->value;
        free(q);
      }
      --list->length;
    }
  }
}

bool PreElem(List list,int pos,int &elem)
{//判斷 pos位置的結點有無前驅,若有則用elem接收其後繼的值
  if(IsListEmpty(list))
  {
    cout<<"Sorry,the list is empty.";
  }
  else
  {
    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->next;
        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)
{//判斷 pos位置的結點有無後繼,若有則用elem接收其後繼的值
  if(IsListEmpty(list))
  {
    cout<<"Sorry,the list is empty.";
  }
  else
  {
    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->next;
        for(int i=1;i<pos;++i)
        {
          p=p->next;
        }
        elem=p->next->value;
        return true;
      }
    }
  }    
}

bool CurrentElem(List list,int pos,int &elem)
{//獲得pos位置處得元素,並用elem接收
  if(IsListEmpty(list))
  {
    cout<<"Sorry,the list is empty.";
    return false;
  }
  else
  {
    if(pos<1 || pos>ListLength(list))
    {
      cout<<"The position is invalidate."<<endl;
      return false;
    }
    else
    {

      Link p=list.head->next;
      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->next->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->next;
  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[])
{
  int e;
  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,2,5);
  VisitElemFromList(*list);
  ClearList(list);
  InsertElemToList(list,1,8);
  InsertElemToList(list,3,9);
  InsertElemToList(list,ListLength(*list),10);
  InsertElemToList(list,ListLength(*list)+1,33);
  VisitElemFromList(*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 tail_elem of the list is:"<<e<<endl;
  }
  InsertElemToList(list,2,22);
  InsertElemToList(list,4,55);
  InsertElemToList(list,ListLength(*list),99);
  VisitElemFromList(*list);
  int elem;
  DeleteElemFromList(list,1,elem);
  VisitElemFromList(*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 tail_elem of the list is:"<<e<<endl;
  }
  DeleteElemFromList(list,3,elem);
  VisitElemFromList(*list);
  cout<<"The length of the list is:"<<ListLength(*list)<<endl;
  DeleteElemFromList(list,ListLength(*list),elem);
  VisitElemFromList(*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 tail_elem of the list is:"<<e<<endl;
  }
  int temp;
  if(PreElem(*list,2,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(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(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);
  if(CurrentElem(*list,2,temp))
    cout<<"The current_element of this position is:"<<temp<<endl;
  getch();
  return 0;
}

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