模板實現雙向鏈表和順序表

首先是帶頭結點的雙向鏈表

#include <iostream>
#include <stdio.h>
#include <assert.h>

using namespace std;
template <class T>
struct ListNode
{

  ListNode<T> *_prev;
  ListNode<T> *_next;
  T _data;

  ListNode(const T& x)
    :_prev(NULL)
    ,_next(NULL)
    ,_data(x)
  {}

};

template <class T>
class List
{
  typedef ListNode<T> Node;
public:
  List()
  {
    _head = new Node(T());
    _head->_next = _head;
    _head->_prev = _head;
  }



  ~List()
  {
    if(_head == NULL)
    {
      return;
    }
    //if(_head->_next == _head)
    //{
    //  delete _head;
    //}
    Node *cur = _head->_next;
    while(cur != _head)
    {
      Node *next = cur->_next;
      _head->_next = next;
      next->_prev = _head;

      delete cur;
    }
  }


  void PushBack(const T& x)
  {
    Insert(_head,x);
  }

  void PopBack()
  {
    assert(_head->_next != _head);
    Erase(_head->_prev);
  }
  //在pos之前插入
  void Insert(Node *pos,const T& x)
  {
    assert(pos);

    Node *new_node = new Node(x);
    Node *cur = pos;
    Node *pos_prev = pos->_prev;

    new_node->_next = cur;
    cur->_prev = new_node;

    new_node->_prev = pos_prev;
    pos_prev->_next = new_node;
  }


  void Erase(Node *pos)
  {
    assert(pos && pos != NULL);

    Node *pos_prev = pos->_prev;
    Node *pos_next = pos->_next;

    delete pos;

    pos_prev->_next = pos_next;
    pos_next->_prev = pos_prev;

  }

  void Print()
  {
    Node *cur = _head->_next;

    while(cur != _head)
    {
      cout<<cur->_data<<endl;
      cur = cur->_next;
    }
    cout<<endl; 
  }
protected:
  Node *_head;
};



int main()
{
  List<string> l;
  l.PushBack("1111");
  l.PushBack("2222");
  l.PushBack("3333");
  l.PushBack("4444");

  l.Print();

  List<int> l1;
  l1.PushBack(1);
  l1.PushBack(2);
  l1.PushBack(3);
  l1.PushBack(4);
  l1.PopBack();

  l1.Print();
  return 0;
}

順序表

#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>
using namespace std;

template<class T>

//vector是類名
//vector<T>是類型
class Vector
{
public:
  Vector()
    :_start(NULL),
     _finish(NULL),
     _endofstorage(NULL)
  {}

  ~Vector()
  {
    delete _start;
    _start = _finish = _endofstorage = NULL;
  }

  Vector(const Vector<T>& v);
  Vector<T>& operator=(const Vector<T>& v);

  size_t Size()
  {
    return _finish-_start;
  }
  size_t Capacity()
  {
    return _endofstorage-_start;
  }

  void PushBack(const T& x)
  {
    Insert(Size(),x);
  }

  void PopBack()
  {
    if(Size() == 0)
    {
      return;
    }
    --_finish;
    return;
  }
  void Expand(size_t n)
  {
    if(Size() == 0)
    {
      _start = new T[3];
      _finish = _start;
      _endofstorage = _start + n;
    }
    T* new_n = new T[n];
    size_t size = Size();
    memcpy(new_n,_start,sizeof(T) * size);
    delete []_start;

    _start = new_n;
    _finish = _start + size;
    _endofstorage = _start + n;
  }
  void Insert(size_t pos,const T& x)
  {
    assert(pos <= Size());
    if(Size() == Capacity())
    {
      Expand(Capacity() * 2);
    }
    T* end = _finish-1;
    while(end >= _start + pos)
    {
      *(end + 1) = *end;
      --end;
    }
    _start[pos] = x;
    ++_finish;
  }
  //刪除指定位置的值
  void Erase(size_t pos)
  {
    if(pos > Size())
    {
      return;
    }
    //從pos位置的下一個位置起
    T *cur = _start + pos; 
    while(cur != _finish)
    {
      *cur = *(cur + 1);
      cur++;
    }
    --_finish;
    return;
  }
  void Print()
  {
    T *cur = _start;
    while(cur != _finish)
    {
      cout<<*cur<<" ";
      ++cur;
    }
    cout<<endl;
  }
protected:
  T *_start;
  T *_finish;
  T *_endofstorage;
};



int main()
{
  Vector<int> v;
  v.PushBack(1);
  v.PushBack(2);
  v.PushBack(3);
  v.PushBack(4);
  v.PopBack();
  v.Erase(1);
  v.Print();
}
發佈了55 篇原創文章 · 獲贊 17 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章