模板实现顺序表和循环双链表

1.模板实现顺序表

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<string>
using namespace std;
template<class T>
class SeqList 
{ 
public: 
    SeqList()
        :_a(NULL)
        ,_size(0)
        ,_capacity(0)
    {
    }

   SeqList(const SeqList<T>& s)
   {
        _size=s._size ;
       _capacity=_size;
       _a=new T[_size];
       for(size_t i=0;i<_size;i++)
       {
           _a[i]=s._a[i];
       }

   }

  SeqList& operator=(SeqList<T> s)
  {
      swap(_a,s._a);
      swap(_size,s._size);
      swap(_capacity,s._size);
      return *this;
  }

  ~SeqList() 
  {
      if(_a)
      {
          delete[] _a;
      }
  }

void CheckCapacity()
{
    if(_size==_capacity)
    {
        size_t newsize=_capacity ? 2*_capacity : 3 ;
        T* tmp=new T[newsize];//若为自定义类型,将其初始化
        for(size_t i=0;i<_size;i++)
        {
            tmp[i]=_a[i];//string类会存在深浅拷贝问题,string                                       //类的operator=,可解决浅拷贝带来的问题
        }
        delete[] _a;
        _a=tmp;
        _capacity=newsize;
    }
}


void PushBack(const T& x)
{
    CheckCapacity();
    _a[_size++]=x;
}

void PopBack()
{
    if(_size>0)
    {
       _size--;

    }
}

void Insert(size_t pos, const T& x)
{
  CheckCapacity();

  for(int end=(int)_size;end>=(int)pos;end--)
  {
      _a[end+1]=_a[end];
  }
  _a[pos]=x;
  _size++;
}

void Erase(size_t pos)
{
    for(size_t i=pos;i<_size;i++)
    {
        _a[i]=_a[i+1];
    }
    _size--;
}

T& operator[](size_t pos)
{
    return _a[pos];
}

void Print()
{
    for(size_t i=0;i<_size;i++)
    {
        cout<<_a[i]<<"  " ;
    }
    cout<<endl;
}

private: 

    T* _a; 

    size_t _size; 

    size_t _capacity; 

}; 

2.模板实现循环双向链表

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<cassert>

using namespace std;
template<class T>
struct ListNode 

{ 

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

}; 



template<class T> 

class List 

{ 

typedef ListNode<T> Node; 
public: 
   List() 
   {
       _head=new Node(0);
       _head->_next =_head;
       _head->_prev=_head;
   }
   List(const List<T>& l)
   {
       Node* cur=l._head->_next;
       _head=new Node(0);
       _head->_next =_head;
       _head->_prev=_head;
       while(cur!=l._head)
       {
          PushBack(cur->_data);
          cur=cur->_next ;
       }

   }
   List& operator=( List<T> l)
   {
       swap(_head,l._head);
       return *this;
   }
   ~List()
   {
       Node* cur=_head->_next;
       while(cur!=_head)
       {
           Node* del=cur;
           delete del;
           cur=cur->_next;
       }
       _head->_next=NULL;
       _head->_prev=NULL;
       delete _head;
   }
public:
   void PushBack(const T& x) 
   {
     Insert(_head,x);
   }
   void PopBack()
   {

     Erase(_head->_prev);
   }
   void PushFront(const T& x) 
   {
      Insert(_head->_next,x);
   }
   void PopFront() 
   {
      Erase(_head->_next);
    }
   void Insert(Node* pos, const T& x)
   {
       assert(pos);
       Node* prev=pos->_prev ;
       Node* tmp=new Node(x);
       tmp->_prev =prev;
       prev->_next =tmp;
       tmp->_next =pos;
       pos->_prev =tmp;
   }
   void Erase(Node* pos)
   {
       assert(!Empty()&& pos);
       Node* prev=pos->_prev ;
       Node* next=pos->_next ;
       delete pos;
       prev->_next =next;
       next->_prev =prev;

   }
   Node* Find(const T& x)
   {
       Node* cur=_head->_next ;
       while(cur!=_head)
       {
           if(cur->_data==x)
           {
               return cur;
           }
           cur=cur->_next ;
       }
       return NULL;
   }
   void Print()
   {
       Node* cur=_head->_next;
       while(cur!=_head)
       {
           cout<<cur->_data<<" ";
           cur=cur->_next ;
       }
       cout<<endl;
   }
   T& Back()
   {
       assert(!Empty());
       return _head->_next->_data;
   }
   bool Empty()
   {
       return _head==_head->_next;
   }
private: 
    Node* _head; 
}; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值