c++实现顺序表、单链表和双向链表

c++实现顺序表、单链表和双向链表

1.顺序表

//SeqList.h
#pragma once

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

typedef int DataType;

class SeqList
{
public:
    SeqList()
        :_array(NULL)
        ,_size(0)
        ,_capacity(0)
    {}
    SeqList(const SeqList& s)
    {
        _array=(DataType*)malloc(sizeof(DataType)*s._size);
        memcpy(_array,s._array,sizeof(DataType)*s._size);
        _size=_capacity=s._size;
    }


    SeqList& operator=(SeqList& s)
    {
        Swap(s);
        return *this;

    }

    void Swap(SeqList& s)
    {
        swap(_array,s._array);
        swap(_size,s._size);
        swap(_capacity,s._capacity);

    }

    ~SeqList()
    {
        if(_array)
        {
            free(_array);
            _size=_capacity=0;
            _array=NULL;
        }
    }

    void CheckCapcacity()
    {
        if(_size==_capacity)
        {
            _capacity=_capacity*2+3;
            _array=(DataType*)realloc(_array,_capacity*sizeof(DataType));
        }
    }

    void PushBack(DataType x)
    {
        CheckCapcacity();
        _array[_size++]=x;
    }

    void PopBack()
    {
        assert(_size);
        --_size;
    }

    void PushFront(DataType x)
    {
        CheckCapcacity();
        int end=_size-1;
        while(end>=0)
        {
            _array[end+1]=_array[end];
            --end;
        }
        _array[0]=x;
        ++_size;
    }

   void  PopFront()
   {
       assert(_size);
       for(size_t i=1;i<_size;++i)
       {
           _array[i-1] = _array[i];
       }
       --_size;  
   }

   inline void Insert(size_t pos, DataType x)
   {
       assert(pos<_size);
       for(int end=_size-1;(int)pos<end;--end)
       {
           _array[end+1]=_array[end];
       }
       _array[pos] = x;
       ++_size;
   }

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

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

   inline DataType& operator[](size_t pos)
   {
       assert(pos<_size);
       return _array[pos];
   }

private:
    DataType* _array;
    size_t _size;
    size_t _capacity;

};
//SeqList.cpp
#include<iostream>
#include"SeqList.h"


using namespace std;


void TestSeqList()
{
    SeqList s1;

    s1.PushBack(1);
    s1.PushBack(2);
    s1.PushBack(3);
    s1.PushBack(4);
    s1.Print();

    s1.PushFront(0);
    s1.Print();

    s1.Insert(2,10);
    s1.Print();
    s1.Insert(0,20);
    s1.Print();

    s1.Erase(2);
    s1.Print();

    SeqList s(s1);
    s.PushBack(5);
    s.Print();

    s1=s;
    s1.Print();
}
int main()
{
      TestSeqList();
      return 0;
}

这里写图片描述

2.单链表

//SList.h
#pragma once

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

typedef int DataType;
struct SListNode
{
    DataType _data;
    SListNode* _next;

    SListNode(DataType x)
        :_data(x)
        ,_next(NULL)
    {

    }

};

class SList
{
 typedef SListNode Node;
public:
    SList()
        :_head(NULL)
        ,_tail(NULL)
    {

    }

    SList(const SList& s)
        :_head(NULL)
        ,_tail(NULL)
    {
        Node *cur=s._head;
        while(cur)
        {
             PushBack(cur->_data);
             cur=cur->_next;
        }
    }

    SList& operator=(SList s)
    {
        swap(_head, s._head);
        swap(_tail, s._tail);
        return *this;
    }

    ~SList()
    {
        Node *cur=_head;
        while(cur)
        {
            Node* next=cur->_next;
            delete cur;
            cur=next;
        }
        _head=_tail=NULL;
    }

    void PushBack(DataType x)
    {
        //链表为空
        //链表有一个以上节点
        if(_head==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=_tail=NULL;
        }
        else
        {
            Node* prev=_head;
            while(prev->_next!=_tail)
            {
                prev=prev->_next;
            }
            delete _tail;
            _tail=prev;
            _tail->_next=NULL;
        }
    }

void PushFront(DataType x) 
{
    //无节点
    //有一个以上节点
    if(_head==NULL)
    {
        _head=_tail=new Node(x);
    }
    else
    {
       Node* tmp=new Node(x);
       tmp->_next=_head;
       _head=tmp;
    }
}

void PopFront()
{
    //无节点
    //有一个节点
    //有一个以上节点
    if(_head==NULL)
    {
        return;
    }
    else if(_head==_tail)
    {
         delete _head;
         _head=_tail=NULL;
    }
    else
    {
        Node * tmp=_head;
        _head=_head->_next;
        delete tmp;
    }
}

 //插入一个节点在pos的前面 
void Insert(Node* pos, DataType x)
{
   assert(pos);
   if(pos==_head)
   {
       PushFront(x);
   }
   else
   {
       Node* prev=_head;
       while(prev->_next!=pos)
       {
           prev=prev->_next;
       }
       Node* tmp=new Node(x);
       prev->_next=tmp;
       tmp->_next=pos;
   }
}





void Erase(Node* pos)
{
    assert(pos);
    if(pos==_head)
    {
        PopFront();
    }
    else if(pos==_tail)
    {
        PopBack();
    }
    else
    {
        Node *prev=_head;
        while(prev->_next!=pos)
        {
            prev=prev->_next;
        }
        prev->_next=pos->_next;
        delete pos;
    }
}

SListNode* Find(DataType x)
{
  Node* cur=_head;
  while(cur)
  {
      if(cur->_data==x)
      {
          return cur;
      }
      cur=cur->_next;
  }
   return NULL;
}


void Print()
{
    Node* cur=_head;
    while(cur)
    {
        cout<<cur->_data<<" ";
        cur=cur->_next;
    }
    cout<<endl;
}

private:
    Node* _head;
    Node* _tail;

};
//SListNode.cpp
#include<iostream>
#include"SList.h"
using namespace std;


void TestSList1()
{
    SList s1;
    s1.PushBack(1);
    s1.PushBack(2);
    s1.PushBack(3);
    s1.PushBack(4);
    s1.Print();

    s1.PopBack();
    s1.PopBack();
    s1.PopBack();
    s1.PopBack();
    s1.PopBack();
    s1.Print();

    s1.PushFront(0);
    s1.PushFront(1);
    s1.PushFront(2);
    s1.PushFront(3);
    s1.Print();

    s1.PopFront();
    s1.PopFront();
    s1.PopFront();
    s1.PopFront();
    s1.PopFront();
    s1.Print();
}

void TestSList2()
{
    SList s2;
    s2.PushBack(1);
    s2.PushBack(2);
    s2.PushBack(3);
    s2.PushBack(4);
    s2.Print();

    SListNode* pos=s2.Find(2);
    s2.Insert(pos,10);
    s2.Print();

    s2.Erase(pos);
    s2.Print();

}

void TestSlist3()
{
    SList s1;
    s1.PushBack(1);
    s1.PushBack(2);
    s1.PushBack(3);
    s1.PushBack(4);
    s1.Print();

    SList s2(s1);
    s2.PushBack(10);
    s2.PushBack(11);
    s2.Print();

    s1 = s2;
    s1.Print();
}
int main()
{
    //TestSList1();
    //TestSList2();
    TestSlist3();
    return 0;
}

TestSList1();
这里写图片描述
TestSList2();
这里写图片描述
TestSList3();
这里写图片描述

3.双向链表

//ListNode.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;


typedef int DataType;

struct ListNode 
{ 
    ListNode* _next; 
    ListNode* _prev; 
    DataType _data; 
    ListNode(const DataType d)
    :_data(d)
    ,_next(NULL)
    ,_prev(NULL)
    {

    }
}; 

class List 
{ 
    typedef ListNode Node; 
public: 
    List();    //构造
    List(const List& d);  //拷贝构造
    List& operator=(const List& d); 
    ~List(); 

    void PushBack(DataType x); 
    void PopBack();
    void PushFront(DataType x); 
    void PopFront(); 
    // 在pos的前面插入一个 
    void Insert(Node* pos, DataType x); 
    void Erase(Node* pos); 
    Node* Find(DataType x); 
    void Reverse();
    void Print();
private: 
    Node* _head; 
    Node* _tail; 
};
//ListNode.cpp
#include"ListNode.h"
#include<iostream>

List::List()//构造
:_head(NULL)
,_tail(NULL)
{

}

List::List(const List& d)//拷贝构造
:_head(NULL)
,_tail(NULL)
{
    if(d._head==NULL)
    {
        return;
    }
    Node* tmp=d._head;
    while(tmp)
    {
        PushBack(tmp->_data);
        tmp=tmp->_next;
    }
}

//List& List::operator=(const List& d)//赋值运算符重载
//{
//  if(this!=&d)
//  {
//      swap(_head,d._head);
//      swap(_tail,d._tail);
//  }
//  return *this;
//}

List::~List()//析构
{
    Node* cur=_head;
    while(cur!=_tail)
    {
        _head=_head->_next;
        delete cur;
        cur=_head;
    }
    _head=NULL;
    _tail=NULL;
}

void List::PushBack( DataType d)//尾插
{
    if(_head==NULL)
    {
        _head=_tail=new Node(d);
    }
    else
    {
        Node* tmp=new Node(d);
        _tail->_next=tmp;
        tmp->_prev=_tail;
        _tail=_tail->_next;
    }
}

void List::PopBack() //尾删
{
     if(_head==NULL||_tail==_head)
     {
       delete _tail;
       _head=_tail=NULL;
     }
     else
     {
         _tail=_tail->_prev;
         _tail->_next=NULL;
     }
}

void List::PushFront(DataType x)//头插
{
   if(_head==NULL)
   {
        _head=_tail=new Node(x);
   }
   else
   {
       Node* tmp=new Node(x);
       tmp->_next=_head;
       _head->_prev=tmp;
       _head=tmp;
   }
}

void List::PopFront()  //头删
{
    if(_head==NULL||_head==_tail)
    {
        delete _tail;
        _head=_tail=NULL;
    }
    else
    {
        _head=_head->_next;
        delete _head->_prev; 
    }
}

void List::Insert(Node* pos, DataType x) //任意位置插入一个节点
{
    assert(pos);
    if(pos==_head)
    {
        PushFront(x);
    }
    else
    {
        Node* prev=_head;
        while(prev->_next!=pos)
        {
            prev=prev->_next;
        }
        Node* tmp=new Node(x);
        prev->_next=tmp;
        tmp->_next=pos;
    }
}


ListNode* List::Find(DataType x) //在链表中查找一个节点
{
    Node* cur=_head;
    while(cur)
    {
        if(cur->_data==x)
        {
            return cur;
        }
        cur=cur->_next;
    }
    return NULL;

}

void List::Erase(Node* pos) //删除任意节点
{
    assert(pos);
    if(pos==_head)
    {
        PopFront();
    }
    else if(pos==_tail)
    {
        PopBack();
    }
    else
    {
        Node* prev=_head;
        while(prev->_next!=pos)
        {
            prev=prev->_next;
        }
        prev->_next=pos->_next;
        delete pos;
    }
}

void List:: Reverse()//双向链表逆置
{
    if(_head==_tail)
    {
        return;
    }
    else
    {
        ListNode* cur=_head;
        ListNode* tmp=cur;
        ListNode* NewHead=NULL;
        while(cur)
        {
            tmp=cur;
            cur=cur->_next;
            tmp->_next=NewHead;
            tmp->_prev=cur;
            NewHead=tmp;
        }
        _head=NewHead;
    }
}

void List::Print()   //打印链表
{
   if(_head==NULL)
   {
      cout<<"The List is Empty"<<endl;
       return;
   }
   else
   {
       Node* tmp=_head;
       while(tmp)
       {
           cout<<tmp->_data<<"  " ;
         tmp=tmp->_next;
       }
       cout<<endl;
   }
}
//Test.cpp
#include"ListNode.h"
#include<stdlib.h>
#include<iostream>
using namespace std;


void test1()
{
   List list1;
   list1.PushBack(0);
   list1.PushBack(1);
   list1.PushBack(2);
   list1.PushBack(3);
   list1.Print();
   list1.PopBack();
   list1.Print();
   list1.PushFront(4);
   list1.Print();
   list1.PopFront();
   list1.Print();
   list1.Reverse();
   list1.Print();

}

void test2()
{
     List s1;
     s1.PushBack(1);
     s1.PushBack(2);
     s1.PushBack(3);
     s1.PushBack(4);
     s1.Print();
     ListNode* pos=s1.Find(2);
     s1.Insert(pos,10);
     s1.Print();
     s1.Erase(pos);
     s1.Print();
}


int main()
{
    //test1();
    test2();
    return 0;
}

text1();
这里写图片描述

test2();
Alt text

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值