数据结构之单链表

/***************************************************************************

* file name : LinkNode.h
* created : 2011/11/02
* description :
* author : Gavin Dai XLX
* update :

****************************************************************************/
#ifndef __LINKNODE_H
#define __LINKNODE_H

#include <iostream>
using namespace std;

template <class T>
class SingleList;

template <class T>
class ListNode
{
private:

 friend class SingleList<T>;

 ListNode() : m_pnext(NULL){ }
 
 ListNode(const T item, ListNode<T> *next = NULL) : m_data(item), m_pnext(next) { }

 ~ListNode() { m_pnext = NULL; }

public:

 T getData();

 friend ostream& operator<<(ostream& , ListNode<T>&);

private:

 T m_data;
 
 ListNode<T>* m_pnext;
};

template <class T>
T ListNode<T>::getData()
{
 return this->m_data;
}

template <class T>
ostream& operator<<(ostream& os, ListNode<T>& out)
{
 os<<out.m_data;
 return os;
}

#endif

 

/***************************************************************************

* file name : SingleList.h
* created : 2011/11/02
* description :
* author : Gavin Dai XLX
* update :

****************************************************************************/

#ifndef __SINGLELIST_H
#define __SINGLELIST_H

#include "LinkNode.h"

template <class T>
class SingleList
{

public:
 //----------------------------------------------------
 // Name: SingleList::SingleList - public
 // Description: Construct function, construct a link list contain a head node
 // Arguments: None
 // Return Value: None
 //----------------------------------------------------
 SingleList() : head(new ListNode<T>()) { }

 ~SingleList()
 {
  makeEmpty();
  delete head;
 }

 void makeEmpty();

 int length();

 ListNode<T>* find(T value);

 //ListNode<T>* find(int n);

 bool insert(T item, int n = 0);

 T remove(int n = 0);

 bool removeAll(T item);

 T get(int n);

 void print();

private:

 ListNode<T> *head;

};

template <class T>
void SingleList<T>::makeEmpty()
{
 ListNode<T> *pdel;
 while(head->m_pnext != NULL)
 {
  pdel = head->m_pnext;
  head->m_pnext = pdel->m_pnext;
  delete pdel;
 }
}

template <class T>
int SingleList<T>::length()
{
 ListNode<T> *pmove = head->m_pnext;
 int count = 0;
 while(pmove != NULL)
 {
  pmove = pmove->m_pnext;
  count++;
 }
 return count;
}

//template <class T>
//ListNode<T>* SingleList<T>::find(int n)
//{
// if (n < 0)
// {
//  cout<<" The n is out of boundary"<<endl;
//  return NULL;
// }
// ListNode<T> *pmove = head->m_pnext;
// for (int i = 0; i < n && pmove; i++)
// {
//  pmove = pmove->m_pnext;
// }
// if (pmove == NULL)
// {
//  cout<<"The n is out of boundary"<<endl;
//  return NULL;
// }
// return pmove;
//
//}

template <class T>
ListNode<T>* SingleList<T>::find(T value)
{
 ListNode<T>* pmove = head->m_pnext;
 while(pmove != NULL)
 {
  if (pmove->m_data == value)
  {
   return pmove;
  }
  pmove = pmove->m_pnext;
 }
 return pmove;

}
//----------------------------------------------------------
// Name; SingleList::insert - public
// Description: insert item before node n.
// Arguments: item - the node value you want to insert, n - insert before n.
// Return Value:
//-----------------------------------------------------------
template <class T>
bool SingleList<T>::insert(T item, int n = 0)
{
 if (n < 0)
 {
  cout<<"The n is illegal"<<endl;
  return false;
 }
 ListNode<T>* pmove = head;
 ListNode<T>* pnode = new ListNode<T>(item);
 if (pnode == NULL)
 {
  cout<<"Application error!"<<endl;
  return false;
 }
 for (int i = 0; i < n && pmove; i++)
 {
  pmove = pmove->m_pnext;
 }
 if (pmove == NULL)
 {
  return false;
  
 }
 pnode->m_pnext = pmove->m_pnext;
 pmove->m_pnext = pnode;
 return true;
}

template <class T>
bool SingleList<T>::removeAll(T item)
{
 ListNode<T>* pmove = head;
 ListNode<T>* pdel = head->m_pnext;
 while(pdel != NULL)
 {
  if (pdel->m_data == item)
  {
   pmove->m_pnext = pdel->m_pnext;
   delete pdel;
   pdel = pmove->m_pnext;
   continue;
  }
  pmove = pmove->m_pnext;
  pdel = pdel->m_pnext;
 }
 return true;
}

template <class T>
T SingleList<T>::remove(int n = 0)
{
 if(n < 0)
 {
  cout<<"can't find the element"<<endl;
  exit(1);
 }
 ListNode<T> *pmove = head, *pdel;
 for (int i = 0; i < n && pmove->m_pnext; i++)
 {
  pmove = pmove->m_pnext;
 }
 if (pmove->m_pnext == NULL)
 {
  cout<<"cant't find the element"<<endl;
  exit(1);
 }
 pdel = pmove->m_pnext;
 pmove->m_pnext = pdel->m_pnext;
 T temp = pdel->m_data;
 delete pdel;
 return temp;
}

template <class T>
T SingleList<T>::get(int n)
{
 if (n < 0)
 {
  cout<<"The n is out of boundry"<<endl;
  exit(1);
 }
 ListNode<T> *pmove = head->m_pnext;
 for (int i = 0; i < n; i++)
 {
  pmove = pmove->m_pnext;
  if (NULL == pmove)
  {
   cout<<"The n is out of boundary"<<endl;
   exit(1);
  }
 }
 return pmove->m_data;
}

template <class T>
void SingleList<T>::print()
{
 ListNode<T>* pmove = head->m_pnext;
 cout<<"head ";
 while(pmove)
 {
  cout<<"-->"<<pmove->m_data;
  pmove = pmove->m_pnext;
 }
 cout<<"--->over"<<endl<<endl<<endl;
}
#endif

 

/***************************************************************************

* file name : main.cpp
* created : 2011/11/02
* description :
* author : Gavin Dai XLX
* update :

****************************************************************************/
#include "SingleList.h"

int main()
{
 SingleList<int> list;
 for (int i = 0; i < 20; i++)
 {
  list.insert(3 * i, i);
 }
 list.print();
 list.insert(12,8);
 list.print();
 cout<<"find"<<list.find(18)<<endl;
 //list.removeAll(12);
 cout<<list.remove(3)<<endl;
 list.print();
 list.makeEmpty();
 

 cout<<"the length of the list "<<list.length();
 system("pause");
 return 1;
}

转载于:https://www.cnblogs.com/GavinDai/archive/2011/11/03/2234292.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值