单链表的顺序实现例程[动态分配存储空间]

//-----------------------------------------//
// Single List of Sequence with Dynamic Memory Allocation
// by SuperXu 2006.11
// Version: Final
//-----------------------------------------//

#include <iostream>
using namespace std;

class SeqList {
public:
 //constructor
 SeqList(int initSize = INITSIZE) : m_listSize(initSize), m_length(0)
 {
  if (initSize == 0)
   initSize = 1;
  m_elem = new int[initSize];
 }

 //destructor
 ~SeqList()
 {
  delete[] m_elem;
 }

 //copy constructor
 SeqList(const SeqList& rhs)
 {
  m_listSize = m_length = rhs.m_length;
  m_elem = new int[m_listSize];
  for (int i=0; i<m_length; ++i)
   m_elem[i] = rhs.m_elem[i];
  /*
  if using pointer to copy, need to note:
  1. the pointer of m_elem should be at the first of the array;
  2. the pointer of the rhs should be not changed;  
  */
 }

 //assign operator
 SeqList& operator=(const SeqList& rhs)
 {
  if (this == &rhs)
   return *this;
  delete[] m_elem;
  m_listSize = m_length = rhs.m_length;
  m_elem = new int[m_listSize];
  for (int i=0; i<m_length; ++i)
   m_elem[i] = rhs.m_elem[i];
  return *this;
 }

 //Address operator
 SeqList* operator&()
 {
  return this;
 }

 //const address operator
 const SeqList* operator&() const
 {
  return this;
 }

 bool isEmpty() const
 {
  return (m_length == 0);
 }

 int Length() const
 {
  return m_length;
 }

 //insert into a element into the list decided by location
 void Insert(const int& rhs, int location)
 {
  if ((location < 0) && (location >=m_length))
   cout << "insert location error: " << location << endl;
  else
  {
   //allocate more memory
   if (m_length >= m_listSize)
   {
    int* newmem = new int[m_length+INCREAMENT];
    for (int i=0; i<m_length; ++i)
     newmem[i] = m_elem[i];
    delete[] m_elem;
    m_elem = newmem;
   }
   //move the element backward
   for (int i=m_length; i>=location; --i)
    m_elem[i] = m_elem[i-1];
  }
  m_elem[location-1] = rhs;  
  ++m_length;
 }

 //delete a element from the list by location and return the element
 int Delete(int location)
 {
  if ((location < 0) && (location >=m_length))
   return -1;
  int delElem = m_elem[location-1];
  for (int i=location; i<m_length; ++i)
   m_elem[i-1] = m_elem[i];
  /*
  //or it could be implemented as follow:
  int* temp = &(m_elem[location-1]);
  int* tail = m_elem + m_length-1;
  for (++temp; temp<=tail; ++temp)
   *(temp-1) = *temp;
  */
  --m_length;
  return delElem;
 }

 //delete all the element in the list equal to 'elem'
 void DeleteElem(const int& elem)
 {
  for (int i=0; i<m_length; ++i)
   if (m_elem[i] == elem)
   {
    for (int j=i+1; j<m_length; ++j)
     m_elem[j-1] = m_elem[j];
    --m_length;
   }
 }

 //locate the spcecial element(first one)
 int Locate(const int& elem) const
 {
  int i = 0;
  while ((i<m_length) && (m_elem[i] != elem))
   ++i;
  if (i >= m_length)
   return -1;
  return i+1;
 }

 //get element from the list by location
 int GetElem(int location) const
 {
  if ((location < 0) && (location >=m_length))
   return -1;
  return m_elem[location-1];
 }

 void DisplayList() const
 {
  for(int i=0; i<m_length; ++i)
   cout << m_elem[i] << endl;
 }

 /*--------------------------------------------*/
 //-----all functions below using the above
 //-----member functions to implement
 /*--------------------------------------------*/

 //get the prior element of an assigned element
 //use the member function "Locate(const int&)"
 int GetPriorElem(const int& elem) const
 {
  int location = Locate(elem);
  if (location == -1)
   return -1;
  return m_elem[location-2];
 }

 //get the next element of an assigned element
 //use the member function "Locate(const int&)"
 int GetNextElem(const int& elem) const
 {
  int location = Locate(elem);
  if (location == -1)
   return -1;
  return m_elem[location];
 }
private:
 static const int INITSIZE;
 static const int INCREAMENT;
 int* m_elem;
 int  m_length;
 int  m_listSize;
};

const int SeqList::INITSIZE = 10;
const int SeqList::INCREAMENT = 5; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值