鏈表

template <class T>
class ListNode
{
  T data;
  ListNode<T> * link;
public:
  ListNode();
  ListNode(T value);
  ~ListNode();
  void SetLink(ListNode<T>* next);
  ListNode<T> * GetLink();
  T& Getdata();
};


template <class T>
void ListNode<T>::SetLink(ListNode<T> *next)
{
  link=next;
}

template <class T>
ListNode<T> * ListNode<T>::GetLink()
{
  return link;
}

template <class T>
T& ListNode<T>::Getdata()
{
  return data;
}


template <class T>
ListNode<T>::ListNode()
{
  link=NULL;
}

template <class T>
ListNode<T>::ListNode(T value)
{
  data=value;
  link=NULL;
}

  
#include "ListNode.h"

template <class T>    
class List
{
  ListNode<T> * first;
  ListNode<T> * tail;
public:
  List();
    
  bool AddTail(T value);
  bool RemoveTail();
  bool Insert(int index,T value);
  bool Remove(int index);
  T& Get(int index);
  bool IsEmpty();
  int GetCount();
  void RemoveAll();
  ListNode<T> * GetHead();
  ListNode<T> * GetTail();
  void SetTail(ListNode<T> * newTail);
  ListNode<T> * GetNode(int index);
  ListNode<T> * GetCur();
  ListNode<T> * GetCurNext();
};



template <class T>
List<T>::List()
{
  first=tail=new ListNode<T>;
  tail->SetLink(NULL);
}

template <class T>
bool List<T>::AddTail(T value)
{
  ListNode<T> * add=new ListNode<T>(value);
  tail->SetLink(add);
  tail=tail->GetLink();
  if(tail!=NULL)
    return true;
  else
    return false;
}

template <class T>
bool List<T>::RemoveTail()
{
  return Remove(this->GetCount()-1);
}

template <class T>
bool List<T>::Insert(int index, T value)
{
  if(index<0 || index>this->GetCount()-1)
  {
    cout<<"插入位置錯誤!"<<endl;
    return false;
  }
  ListNode<T> * curr=first;
  while(index)
  {
    curr=curr->GetLink();
    --index;
  }

  ListNode<T> * add=new ListNode<T>(value);
  add->SetLink(curr->GetLink());
  curr->SetLink(add);

  if(curr->GetLink() !=NULL)
  {
    return true;
  }
  else
    return false;
}

template <class T>
bool List<T>::Remove(int index)
{
  if(index<0 || index>this->GetCount()-1)
  {
    cout<<"刪除位置錯誤!"<<endl;
    return false;
  }

  ListNode<T> * cur,* curpre;
  cur=first;
  curpre=cur->GetLink();

  while(index)
  {
    cur=cur->GetLink();
    curpre=curpre->GetLink();
    --index;
  }
  if(tail==curpre)
  {
    tail=cur;
  }
  cur->SetLink(curpre->GetLink());
  if(curpre!=NULL)
  {
    return true;
  }
  else
  {
    return false;
  }
}


template <class T>
T& List<T>::Get(int index)
{
  if(index<0 || index>this->GetCount()-1)
  {
    cout<<"返回位置錯誤!"<<endl;
  }

  ListNode<T> * cur;
  cur=first->GetLink();
  while(index)
  {
    cur=cur->GetLink();
    --index;
  }
  return cur->Getdata();
}

template <class T>
bool List<T>::IsEmpty()
{
  return first->GetLink()==NULL;
}

template <class T>
int List<T>::GetCount()
{
  ListNode<T> * cur;
  cur=first->GetLink();
  int count=0;
  while(cur!=NULL)
  {
    cur=cur->GetLink();
    count++;
  }
  return count;
}

template <class T>
void List<T>::RemoveAll()
{
  ListNode<T> * cur;
  while(first->GetLink()!=NULL)
  {
    cur=first->GetLink();
    first->SetLink(cur->GetLink());
    delete cur;
  }
  tail=first;
}

template <class T>
ListNode<T> * List<T>::GetHead()
{
  return first;
}

template <class T>
ListNode<T> * List<T>::GetTail()
{
  return tail;
}

template <class T>
void List<T>::SetTail(ListNode<T> *newTail)
{
  tail=newTail;
}

template <class T>
ListNode<T> * List<T>::GetNode(int index)
{
  if(index<0 || index>this->GetCount()-1)
  {
    cout<<"位置錯誤!"<<endl;
  }

  ListNode<T> * cur;
  cur=first->GetLink();
  while(index)
  {
    cur=cur->GetLink();
    --index;
  }
  return cur;
}

template <class T>
ListNode<T> * List<T>::GetCurNext()
{
  ListNode<T> * cur;
  cur=this->GetCur();
  cur=cur->GetLink();
  return cur;
}



兩個順序鏈表合併:
#include <iostream>
#include "List.h"
using namespace std;

int main()
{
  List<int> listFirst;
  List<int> listSecond;
  listFirst.AddTail(1);
  listFirst.AddTail(5);
  listFirst.AddTail(8);
  listFirst.AddTail(9);
  listFirst.AddTail(13);

  listSecond.AddTail(0);
  listSecond.AddTail(3);
  listSecond.AddTail(4);
  listSecond.AddTail(6);
  listSecond.AddTail(11);
  listSecond.AddTail(17);

  while(listSecond.GetCount()!=0)
  {
    int indexFirst=0;
    while(listSecond.Get(0)>listFirst.Get(indexFirst))
    {
      ++indexFirst;
      if(indexFirst==listFirst.GetCount())
      {
        break;
      }
    }

    if(indexFirst==listFirst.GetCount())
    {
      listFirst.AddTail(listSecond.Get(0));
      listSecond.Remove(0);
    }
    else
    {
      listFirst.Insert(indexFirst,listSecond.Get(0));
      listSecond.Remove(0);
    }
  }

  for(int i=0;i<listFirst.GetCount();i++)
  {
    cout<<listFirst.Get(i)<<endl;
  }
  return 0;

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