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

来源:互联网 发布:无网络摄像头安装方法 编辑:程序博客网 时间:2024/05/19 22:01

1、顺序表
C++实现顺序表代码如下:

//Seqlist.h#pragma once#include<iostream>#include<string.h>#include<assert.h>typedef int DataType;class SeqList{public:    SeqList()//构造函数        :_array(NULL)        , _size(0)        ,_capacity(0)    { }    SeqList(const SeqList& s)//拷贝构造        :_array(new DataType[s._capacity])        , _size(s._size)        , _capacity(s._capacity)    {        memcpy(_array, s._array, sizeof(DataType)*_size);    }    //赋值运算符的现代写法和传统写法    //SeqList& operator=(SeqList &s)//赋值运算符重载,传统写法    //{    //  if(this!=&s)    //  {    //      delete[] _array;    //      _array = new DataType[s._size];    //      memcpy(_array, s._array, sizeof(DataType)*s._size);    //      _size = s._size;    //      _capacity = s._capacity;    //  }    //  return *this;    //}    SeqList& operator=(SeqList s)//赋值运算符重载,现代写法    {        Swap(s);        return *this;    }    ~SeqList()    {        if (_array)        {            delete[] _array;            _size = _capacity = 0;        }    }    void Swap(SeqList& s)    {        if (this != &s)        {            swap(_array, s._array);            swap(_size, s._size);            swap(_capacity, s._capacity);        }    }    void PushBack(DataType x)    {        CheckCapcacity();        _array[_size++] = x;    }    void PopBack()    {        if (_size == 0)        {            cout << "It is empty!" << endl;        }        else        {            --_size;        }    }    void PushFront(DataType x)    {        CheckCapcacity();        for (size_t i = _size; i >=1; i--)        {            _array[i] = _array[i-1];        }        _array[0] = x;        _size++;    }    void PopFront()    {        if (_size == 0)        {            cout << "It is empty!" << endl;        }        else        {            for (size_t i = 0; i < _size - 1; i++)            {                _array[i] = _array[i + 1];            }            --_size;        }    }    void Insert(size_t pos, DataType x)//pos前插入数据    {        assert(pos < _size);        CheckCapcacity();        if (pos == 0)        {            PushFront(x);        }        else        {            for (size_t i = _size; i >pos; i--)            {                _array[i] = _array[i - 1];            }            _array[pos] = x;            _size++;        }    }    void Erase(size_t pos)    {        assert(pos < _size);        assert(_size>0);        if (pos == _size - 1)        {            PopBack();        }        else if (pos == 0)        {            PopFront();        }        else        {            for (size_t i = pos; i < _size - 1; i++)            {                _array[i] = _array[i + 1];            }            _size--;        }    }    size_t Find(const DataType& x)    {        for (size_t i = 0; i < _size; i++)        {            if (_array[i] == x)            {                return i;            }        }        return npos;    }    DataType& operator[](size_t pos)    {        assert(pos < _size);        return _array[pos];    }    void CheckCapcacity()    {        if (_size >= _capacity)        {            _capacity = _capacity > 0 ? _capacity * 2 : 3;            DataType* tmp = new DataType[_capacity];            memcpy(tmp, _array, sizeof(DataType)*_size);            _array = tmp;        }    }    void Print()    {        for (size_t i = 0; i < _size; i++)        {            cout << _array[i] << "  ";        }        cout << endl;    }    static size_t npos;private:    DataType* _array;    size_t _size;    size_t _capacity;};size_t SeqList::npos = -1;

顺序表测试代码如下:

//test.cppusing namespace std;#include"Seqlist.h"void Teat1Seq(){    SeqList seq1;    seq1.PushBack(1);    seq1.PushBack(2);    seq1.PushBack(3);    seq1.PushBack(4);    seq1.Print();    seq1.PushFront(4);    seq1.PushFront(3);    seq1.PushFront(2);    seq1.PushFront(1);    seq1.PushFront(0);    seq1.Print();    seq1.PopBack();    seq1.PopBack();    seq1.Print();    seq1.PopFront();    seq1.Print();    seq1.Insert(4, 5);    seq1.Insert(5, 6);    seq1.Print();    seq1.Erase(6);    seq1.Erase(6);    seq1.Print();    SeqList seq2(seq1);    SeqList seq3;    seq2.Print();    seq3 = seq2;        seq3.Print();    cout << seq3.Find(4) << endl;    cout << seq3.operator[](5) << endl;}int  main(){    Teat1Seq();    system("pause");    return 0;}

顺序表测试运行结果如下:
这里写图片描述

2、单链表
C++实现单链表代码如下:

//Slist.h#pragma once#include<iostream>#include<string.h>#include<assert.h>typedef int DataType;struct SListNode{    SListNode* _next;    DataType _data;    SListNode(DataType x)        :_data(x)        , _next(NULL)    {}};class SList{    typedef SListNode Node;public:    SList() :_head(NULL), _tail(NULL)    {   }    //拷贝构造    SList(const SList& s)    {        Node* cur = s._head;        while (cur)        {            Node* tmp = new Node(cur->_data);            if (_tail == NULL)            {                _head = _tail = tmp;            }            else            {                _tail->_next = tmp;                _tail=_tail->_next;            }            cur = cur->_next;        }    }    //赋值运算符重载,传统写法和现代写法    //传统写法    SList& operator=(const SList& s)    {        if (this != &s)        {            clear();            Copy(s._head);        }        return *this;    }    //现代写法    /*SList& operator=(const SList& s)    {        Copy(s._head);        return *this;    }*/    void Copy(Node* head)    {        Node* cur = head;        while (cur)        {            PushBack(cur->_data);            cur = cur->_next;        }    }    void clear()    {        Node* cur = _head;        while (cur)        {            Node* next = cur->_next;            delete cur;            cur = next;        }        _head = _tail = NULL;    }    ~SList()    {        clear();    }    void PushBack(DataType x)    {        if (_tail == NULL)        {            _head = _tail = new Node(x);        }        else        {            _tail->_next = new Node(x);            _tail = _tail->_next;        }    }    void PopBack()    {        if (_head == _tail)        {            if (_tail == NULL)            {                cout << "It is empty!" << endl;            }            else            {                delete _head;                delete _tail;                _head = _tail = NULL;            }        }        else        {            Node* cur = _head;            while (cur->_next != _tail)            {                cur = cur->_next;            }            delete _tail;            _tail = cur;            _tail->_next = NULL;        }    }    void PushFront(DataType x)    {        if (_tail == NULL)        {            _head = _tail = new Node(x);        }        else        {            Node* tmp = new Node(x);            tmp->_next = _head;            _head = tmp;        }    }    void PopFront()    {        if (_head == _tail)        {            if (_tail == NULL)            {                cout << "It is empty!" << endl;            }            else            {                delete _head;                delete _tail;                _head = _tail = NULL;            }        }        else        {            Node* cur = _head->_next;            delete _head;            _head = cur;        }    }    // 插入一个节点在pos的前面     void Insert(Node* pos, DataType x)    {        if (_head == pos)        {            PushFront(x);        }        else        {            Node* next = pos->_next;            Node* tmp = new Node(x);            tmp->_next=next;            pos->_next = tmp;        }    }    void Erase(Node* pos)    {        if (pos == _head)        {            PopFront();        }        else if (pos == _tail)        {            PopBack();        }        else        {            Node* cur = _head;            while (cur->_next != pos)            {                cur = cur->_next;            }            Node* next = pos->_next;            delete pos;            cur->_next = next;        }    }    void Print()    {        assert(_head != NULL);        Node* cur = _head;        while (cur)        {            cout << cur->_data << " ";            cur = cur->_next;        }        cout << endl;    }private:    Node* _head;    Node* _tail;};

测试代码如下:

using namespace std;#include"Slist.h"void TestSlist(){    SList s1;    s1.PushFront(100);    s1.PushBack(1);    s1.PushBack(2);    s1.PushBack(3);    s1.PushBack(4);    s1.Print();    s1.PushFront(1);    s1.PushFront(2);    s1.PushFront(3);    s1.Print();    s1.PopBack();    s1.PopBack();    s1.Print();    s1.PopFront();    s1.PopFront();    s1.Print();    SList s2(s1);    s2.Print();    SList s3;    s3 = s2;    s3.Print();    SList s4;    s4.PushFront(1);    s4.PushFront(2);    s4.PushFront(3);    s4.PushFront(4);    s4.PushFront(5);    s4.PushFront(6);    s4.Print();    s4.Insert(s4.Find(2), 100);    s4.Insert(s4.Find(6), 100);    s4.Print();    s4.Erase(s4.Find(4));    s4.Print();}int main(){    TestSlist();    system("pause");    return 0;}

测试代码运行结果:
这里写图片描述

原创粉丝点击