C++实现顺序表和单链表

#pragma once
typedef int DataType;
class SeqList
{
public:
 SeqList()
  :_a(NULL)
  ,_size(0)
  ,_capacity(0)
 {}
 // 拷贝构造
 SeqList(const SeqList& s)
  :_a(new DataType[s._size])
  ,_size(s._size)
  ,_capacity(s._size)
 {
  memcpy(_a, s._a, sizeof(DataType)*s._size);
 }
 SeqList& operator=(SeqList s)
 {
  swap(_a, s._a);
  swap(_size, s._size);
  swap(_capacity, s._capacity);
  return *this;
 }
 void PushBack(const DataType& x)
 {
  CheckCapacity();
  _a[_size++] = x;
 }
 void PopBack()
 {
  assert(_size > 0);
  --_size;
 }
 void Insert(size_t pos, const DataType& x)
 {
  assert(pos >= 0 && pos<_size);
  CheckCapacity();
  size_t i = 0;
  for (i = _size; i>pos; i--)
  {
   _pArr[i] = _pArr[i - 1];
  }
  _pArr[pos] = d;
  _size++;
 }
 void Erase(size_t pos)
 {
  assert(pos >= 0 && pos<_size);
  if (_size == 0)
   return;
  else 
  {
   size_t i = 0;
   for (i = pos; i<_size - 1; i++)
   {
    _pArr[i] = _pArr[i + 1];
   }
   _size--;
  }
 }
 size_t Find(const DataType& x)
 {
  for (size_t i = 0 ; i < _size; ++i)
  {
   if (x == _a[i])
   {
    return i;
   }
  }
  return nPos;
 }
 DataType& operator[](size_t pos)
 {
  assert(pos < _size);
  return _a[pos];
 }
 ~SeqList()
 {
  if(_a)
  {
   delete[] _a;
   _size = _capacity = 0;
  }
 }
 void CheckCapacity()
 {
  if (_size >= _capacity)
  {
   _capacity = _capacity > 0 ? _capacity*2 : 3;
   DataType* tmp = new DataType[_capacity];
   memcpy(tmp, _a, sizeof(DataType)*_size);
   delete[] _a;
   _a = tmp;
  }
 }
 void Print()
 {
  for (size_t i = 0; i < _size; ++i)
  {
   cout<<_a[i]<<" ";
  }
  cout<<endl;
 }
 static size_t nPos;
private:
 DataType* _a;
 size_t _size;
 size_t _capacity;
};
size_t SeqList::nPos = -1;
void TestSeqList()
{
 SeqList s1;
 s1.PushBack(1);
 s1.PushBack(2);
 s1.PushBack(3);
 s1.PushBack(4);
 s1.Print();
 SeqList s2(s1);
 s2.Print();
 SeqList s3;
 s3.PushBack(10);
 s3.PushBack(10);
 s3.Insert(2,30);
 s3.Print();
 s3.Erase(3);
 s3.Print();

 s1 = s3;
 s1.Print();
 s3.Print();
}

#pragma once
typedef int DataType;
struct SListNode
{
 SListNode* _next;
 DataType _data;
 SListNode(const DataType& x)
  :_data(x)
  ,_next(NULL)
 {}
};
class SList
{
 typedef SListNode Node;
public:
 SList()
  :_head(NULL)
  ,_tail(NULL)
 {}
 SList(const SList& l)
  :_head(NULL)
  ,_tail(NULL)
 {
  Copy(l._head);
 }
 SList& operator=(SList l)
 {
  swap(_head, l._head);
  swap(_tail, l._tail);
  return *this;
 }
 void Clear()
 {
  Node* cur = _head;
  while (cur)
  {
   Node* next = cur->_next;
   delete cur;
   cur = next;
  }
  _head = _tail = NULL;
 }
 void Copy(Node* head)
 {
  Node* cur = head;
  while (cur)
  {
   PushBack(cur->_data);
   cur = cur->_next;
  }
 }
 ~SList()
 {
  Clear();
 }
 void PushBack(const DataType& x)
 {
  if (_tail == NULL)
  {
   _head = _tail = new Node(x);
  }
  else
  {
   _tail->_next = new Node(x);
   _tail = _tail->_next;
  }
 }
 void PopBack()
 {
  if (_head == NULL) //没有节点
  {
   return;
  }
  else if (_head == _tail) //有一个节点
  {
   delete _head;
   _head = NULL;
   _tail = NULL;
  }
  else    //有多个节点
  {
   Node *prev = _head;
   while (prev->_next != _tail)
   {
    prev = prev->_next;
   }
   prev->_next = _head;
   delete _tail;
   _tail = prev;
  }
 }
 void PushFront(const DataType& x)
 {
  if (_head == NULL)
  {
   _head = _tail = new Node(x);
  }
  else
  {
   _head->_next = new Node(x);
   _tail = _tail->_next;
  }
 }
 void PopFront()
 {
  if (_head == NULL) //没有节点
  {
   return;
  }
  else if (_head == _tail) //有一个节点
  {
   delete _head;
   _head = NULL;
   _tail = NULL;
  }
  else
  {
    Node* cur = _head;
   _head = _head->_next;
   delete cur;
   _tail->_next = _head;
  }
 }
 void Insert(Node* pos, const DataType& x)
 {
  Node* tmp = new Node(x);
  if (_head == pos)
  {
   if (_head == NULL)
   {
    _head = _tail = tmp;
   }
   else
   {
    Node* prev = NULL;
    Node* next = NULL;
    tmp->next = _head;
    _head->prev = tmp;
    _head = tmp;
   }
  }
 }
 void Erase(Node* pos)
 {
  if (_head == pos)
  {
   PopFront();
  }
  else if (_tail == pos)
  {
   PopBack();
  }
  else
  {
   Node* prev = NULL;
   Node* next = NULL;
   Node* tmp = NULL;
   tmp = pos->next;
   delete pos;
   pos->next->prev = tmp;
  }
  
  }
 Node* Find(const DataType& x)
 {
  if (_Head == NULL) //没有节点
  {
   return NULL;
  }
  LinkNode* begin = _Head;
  do{
   if (begin->_data == x)
   {
    cout << "find " << x << endl;
    return begin;
   }
   begin = begin->_next;
  } while (begin != _Head);
  return NULL;
 }
 void Print()
 {
  Node* cur = _head;
  while (cur)
  {
   cout<<cur->_data<<" ";
   cur = cur->_next;
  }
  cout<<endl;
 }
private:
 Node* _head;
 Node* _tail;
};
void TestSList()
{
 SList l1;
 l1.PushBack(1);
 l1.PushBack(2);
 l1.PushBack(3);
 l1.PushBack(4);
 l1.Print();
 SList l2(l1);
 l2.Print();
 l1.PopBack(4);
 l1.PopBack(3);
 l1.PopBack(2);
 l1.PopBack(1);
 l1.Print();
 SList l3;
 l3.PushBack(4);
 l3.PushBack(4);
 l3.PushBack(4);
 l3.PushBack(4);
 l1 = l3;
 l1.Print();
 l3.Print();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值