1. 程式人生 > >C++實現順序表和單鏈表

C++實現順序表和單鏈表

順序表:順序表是在計算機記憶體中以陣列的形式儲存的線性表,是指用一組地址連續的儲存單元依次儲存資料元素的線性結構。
確定了起始位置,就可通過公式計算出表中任一元素的地址:LOC(ai)=LOC(a1)+(i-1)*L  1≤i≤n (L是元素佔用儲存單元的長度)
這裡寫圖片描述
順序表的實現一般是實現連續開闢一段空間,然後在進行資料的增刪查改(靜態順序表)
其程式碼實現如下:

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

typedef int DataType;

class SeqList 
{
public:
    SeqList
() :_array(NULL) ,_size(0) ,_capacity(0) {} SeqList(const SeqList&s) { _array = (DataType*)malloc(s._size*sizeof(DataType)); memcpy(_array,s._array,s._size*sizeof(DataType)); _size = s._size; _capacity = s._size; } /*SeqList
&operator(SeqList s) {*/ //傳統寫法 /*if(this != &s) { free(_array); _array =(DataType*)malloc(sizeof(DataType)*s._size); memcpy(_array,s._array,sizeof(DataType)*s._size); _size = s._size; _capacity = s._size; }*/ SeqList
&operator=(SeqList s) { //現代寫法 free(_array); Swap(s); return *this; } ~SeqList() { if(_array) { free(_array); _array = NULL; _size = _capacity = 0; } } void Print() { for(size_t i = 0; i < _size; i++) { cout << _array[i] << ""; } cout << endl; } void CheakCapacity() { if(_size == _capacity) { _capacity = 2* _capacity +3; _array = (DataType*)realloc(_a,_capacity*sizeof(DataType)); assert(_array); } } void Swap(SeqList& s) { swap(_array,s._array); swap(_size,s._size); swap(_capacity,s._capacity); } void PushBack(DataType x) { Insert(_size,x); } void PopBack() { Erase(_size); } void PushFront(DataType x) { Insert(0,x); } void PopFront() { Erase(0); } void Insert(size_t pos, DataType x) { assert(pos <= _size); CheakCapacity(); int end = (int)_size - 1; if(pos == 0) { while(end >= 0) { _array[end+1] = _array[end]; end--; } _array[0] = x; } else { while(end >= (int)pos) { _array[end+1] = _array[end]; end--; } _array[pos] = x; } _size++; } void Erase(size_t pos) { assert(pos < _size); { if(_size > 0) { //popfront的實現 if(pos == 0) { int end = 0; while(end <(int)_size -1) { _array[end] = _array[end+1]; end++; } _size--; } //popBack的實現 else if(pos == _size) { _size--; } else { int end = pos; while(end < (int)_size -1) { _array[end+1] = _array[end]; end++; } _size--; } } return; } } DataType& operator[](size_t pos) { assert(pos < _size); return _array[pos]; } private: DataType* _array; size_t* _size; size_t* _capacity; };

單鏈表:單鏈表是一種鏈式存取的資料結構
這裡寫圖片描述
Tail指標是為了更方便的查詢到末尾,在末尾插入元素。
單鏈表則是一次只開闢一個結點的空間,用來儲存當前要儲存的資料及指向下一個結點或NULL的指標

#include<iostream>
#include<windows.h>
#include<assert.h>
using namespace std;
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)
:_head(NULL)
        , _tail(NULL)
    {
        Node*cur = s._head;
        while (cur)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
SList& operator=(const SList& s)
{
    Clear();
    Node* cur = s._head;
        while (cur)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    return *this;
}
~SList() 
{
    Clear();
}
void Clear()
{
    Node* cur = _head;
    while(_head != NULL)
    {
        cur = _head;
        _head = cur->_next;
        delete cur;
    }
    _head = _head = 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->_next==NULL)
    {
        delete _head;
        _head = _tail =NULL;
    }
    else
    {
        Node* tmp = _head;
            while (tmp->_next->_next != NULL)
            {
                tmp = tmp->_next;
            }
            _tail = tmp;
            tmp->_next = NULL;
    }
}
void PushFront(DataType x)
{
    if((_head == NULL)&&(_tail == NULL))
    {
        _head = _tail = new Node(x);
    }
    else
    {
        Node* tmp = new Node(x);
        tmp->_next = _head;
        _head = tmp;
    }
}
void PopFront()
{
    if(_head == NULL)
    {
        return;
    }
    else
    {
        Node* cur = _head;
        _head = _head->_next;
        delete cur;
    }
}
Node* Find(DataType x)
{
    Node* tmp = _head;
    while(tmp)
    {
        if(tmp->_data == x)
        {
            return tmp;
        }
        tmp = tmp->_next;
    }
    return NULL;
}
void Insert(Node* pos, DataType x)
{
    assert(pos);
    if(pos == NULL)
    {
        PopFront();
    }
    else
    {
        Node* cur =_head;
        while(cur->_next != pos)
        {
            cur = cur->_next;
        }
        Node* tmp = new Node(x);
        tmp->_next = pos;
        cur->_next = tmp;
    }
}
void Erase(Node* pos)
{
        assert(pos);
        if(pos->_next==NULL)
        {
            PopBack();
        }
        else
        {

            Node*cur = _head;
            while (cur->_next != pos)
            {
                cur = cur->_next;

            }

            cur->_next = pos->_next;
            delete pos;
        }

    }

void Print()
{
    Node* tmp = _head;
    while(tmp != NULL)
    {
        cout<<tmp->_data<<" ";
        tmp = tmp->_next;
    }
    cout<<endl;
}
private: 
Node* _head; 
Node* _tail; 
};