string及顺序表、链表(C++)

首先,了解下string库函数:
具体参考:http://www.cplusplus.com/reference/string/string/?kw=string
1.constructor
这里写图片描述
对于7个构造函数依次如下:
这里写图片描述
2.capacity
这里写图片描述
注:size( )和length( )都是string大小,推荐使用size( );
reserve( )是直接扩容;
resize( )是可以扩容可以缩容,并且初始化;(后面有具体代码)
clear( )是在下标为0的位置放’\0’,size置0,但不释放空间;
3.Modifiers
这里写图片描述
这里写图片描述
注:replace( )是替换;assign是把对象之前值清理掉赋值为目标值,如果空间不够会扩容;
operator+=和append都是连接字符串,但建议使用operator+=,因为可读性更高。
接下里为用c++写的顺序表和链表:
1.下标的顺序表:

#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
//将字符串转为整数
//int main()
//{
//  string str("     12345");
//  int index = 0;
//  //先处理空格
//  while (index < str.size())
//  {
//      if (str[index] == ' ')
//          index++;
//      else
//          break;
//  }
//  int flag = 1;
//  if (str[index] == '+')
//  {
//      index++;
//      flag = 1;
//  }
//  if (str[index] == '-')
//  {
//      index++;
//      flag = -1;
//  }
//  int value = 0;
//  while (index<str.size())
//  {
//      if (str[index] >= '0'&&str[index] <= '9')
//      {
//          value = value * 10 + (str[index] - '0');
//          index++;
//      }
//      //不合法数值表达式直接返回
//      else
//          return 0;
//  }
//  printf("%d\n", value*flag);
//  system("pause");
//  return 0;
//}
typedef int Datatype;
class Myvector
{
public:
    Myvector(int n)//构造函数
    {
        _a = new Datatype[n];
        _size = 0;
        _capacity = n;
    }
    Myvector(const Myvector& v)//拷贝构造
    {
        _a = new Datatype[v._size];
        memcpy(_a, v._a, sizeof(Datatype)*v._size);
        _size = v._size;
        _capacity = v._capacity;
    }
    Myvector& operator=( Myvector v)//赋值
    {
        //用现代写法
        if (this != &v)
        {
            swap(_a, v._a);
            swap(_size, v._size);
            swap(_capacity, v._capacity);
        }
        return *this;
    }
    void PushBack(const Datatype& x)//尾插,用引用是因为Datatype可能是字符串
    {
        if (_size == _capacity)//扩容
        {
            _capacity *= 2;
            _a = (Datatype*)realloc(_a,sizeof(Datatype)*_capacity);

        }
        _a[_size] = x;
        _size++;
    }
    void PopBack()//尾删
    {
        if (_size > 0)
        {
            _size--;
        }
    }
    void Insert(size_t pos,  const Datatype& x)
    {
        assert(pos >= 0 && pos <= _size);
        if (_size == _capacity)
        {
            _capacity *= 2;
            Datatype* a = new Datatype(_capacity);
            memcpy(a, _a, sizeof(Datatype)*_size);
            _a = a;
            //_a = (Datatype*)realloc(_a, sizeof(Datatype)*_capacity);
        }
        size_t index = _size;
        while (index > pos)
        {
            _a[index] = _a[index - 1];
            index--;
         }
        _a[pos] = x;
        _size++;
    }
    void Erase(size_t pos)
    {
        assert(pos >= 0 && pos < _size);
        int index = pos;
        while (index < _size-1)
        {
            _a[index] = _a[index + 1];
            index++;
        }
        _size--;
    }
    Datatype& operator[](size_t pos)
    {
        return _a[pos];
    }
    ~Myvector()
    {
        if (_a)
        {
            delete[]_a;
            _a = NULL;
            _size = 0;
            _capacity = 0;
        }
    }
    void print()
    {
        for (int index = 0; index < _size; index++)
            printf("%d ", _a[index]);
        printf("\n");
    }
private:
    Datatype* _a;
    size_t _size;
    size_t _capacity;
}

2.指针的顺序表:

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

typedef int Datatype;
class  Vector
{
public:
    Vector()//构造
        :_first(NULL)
        , _finish(NULL)
        , _endofstorage(NULL)
    {
    }

    ~Vector()//析构
    {
        if (_first)
        {
            delete[] _first;
            _first = NULL;
            _finish = NULL;
            _endofstorage = NULL;
        }

    }
    Vector(const Vector& v)//拷贝构造
    {
        _first = new Datatype[v.Size()];
        _finish = _first + v.Size();
        _endofstorage = _first + v.Size();
        memcpy(_first, v._first, sizeof(Datatype)*v.Size());
    }
    Vector& operator=(Vector v)
    {
        //现代写法
        swap(_first, v._first);
        swap(_finish, v._finish);
        swap(_endofstorage, v._endofstorage);
        return *this;
    }
    size_t Size() const
    {
        return _finish - _first;
    }
    size_t Capacity() const
    {
        return _endofstorage - _first;
    }
    void Reverse(size_t n) //单纯扩容
    {
        if (n > Capacity())
        {
            Expand(n);
        }
    }
    void Resize(size_t n,Datatype value)//扩容并且初始化 
    {
        if (n < Size())//缩容
        {
            _finish = _first + n;
        }
        else
        {
            if (n > Capacity())//先扩容
            {
                Expand(n);
            }
            Datatype* end = _first + n;
            while (_finish < end)
            {
                *_finish = value;
                ++_finish;
            }
        }
    }
    void PushBack(Datatype x)//尾插
    {
        /*if (_finish == _endofstorage)
        {
            if (Capacity() == 0)
                Expand(3);
            else
                Expand(Capacity() * 2);
        }
        *_finish = x;
        ++_finish;*/
        Insert(_finish, x);
    }    
    void PopBack()//尾删
    {
        /*if (_finish != _first)
        {
            _finish--;
        }*/
        Erase(_finish - 1);
    }
    Datatype *Find(Datatype x)   
    {
        Datatype *cur = _first;
        while (cur != _finish)
        {
            if (*cur == x)
                return cur;
            else
                cur++;
        }
        return NULL;
    }
    void Insert(Datatype *pos, Datatype x)//在pos前插入元素
    {
        int num = pos - _first;
        if (_finish == _endofstorage)//需要扩容
        {
            if (_finish == NULL)
                Expand(3);//一个元素都没有,可以先开3个空间
            else
                Expand(Capacity() * 2);
                pos = _first + num;//注意pos发生变化
        }
        Datatype *tmp = _finish;
        while (tmp > pos)
        {
            *tmp = *(tmp - 1);
            tmp--;
        }
        *pos = x;
        _finish++;
    }
    void Erase(Datatype *pos)//在pos位置删除元素
    {
        assert(_first <= pos&&pos < _finish);
        while (pos < (_finish - 1))
        {
            *pos = *(pos + 1);
            pos++;
         }
        _finish--;
    }
    Datatype& operator[](size_t n)
    {
        return _first[n];
    }
    void Print()
    {
        Datatype *cur = _first;
        while (cur!=_finish)
        {
            printf("%d ", *cur);
            cur++;
        }
        printf("\n");
    }
private:
    void Expand(size_t n)
    {
        if (n > Capacity())
        {
            size_t size = Size();//需要先保存起来
            Datatype *tmp = new Datatype[n];
            memcpy(tmp, _first, sizeof(Datatype)*size);
            delete[] _first;
            _first = tmp;
            _finish = _first + size;
            _endofstorage = _first + n;
        }
    }
private:
    Datatype *_first;//相当于_a
    Datatype *_finish;//相当于_size
    Datatype *_endofstorage;//相当于_capacity
};

3.链表(带头双向)

#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
//带头双向链表
typedef int DataListtype;
typedef struct ListNode
{
    DataListtype _data;
    struct ListNode  *_next;
    struct ListNode *_prev;
    struct ListNode (const DataListtype& x)
        :_data(x)
        ,_next(NULL)
        ,_prev(NULL)
    {
    }
}Node;
class Mylist
{
public:
    Mylist()//构造函数
    {
        _head = new Node(DataListtype());//为Node类型,且初始化
        _head = (Node*)malloc(sizeof(Node));
        _head->_next = _head;
        _head->_prev = _head;
    }
    Mylist(const Mylist& l)//拷贝构造
    {
        _head = new Node(DataListtype());//初始化DataListtype(),可能是string类型,匿名对象
        _head->_next = _head;
        _head->_prev = _head;
        Node *cur = l._head->_next;
        while (cur != l._head)
        {
            /*Node*tail = _head->_prev;
            Node* Newnode = new Node(cur->_data);
            tail->_next = Newnode;
            Newnode->_prev = tail;
            Newnode->_next = _head;
            _head->_prev = Newnode;
            cur = cur->_next;*/
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    Mylist& operator=( Mylist l)//赋值
    {
        现代写法
        swap(_head, l._head);
        return *this;
    }
    void PushBack(const DataListtype&x)//尾插
    {
        /*Node* tail = _head->_prev;
        Node* newnode = new Node(x);
        tail->_next = newnode;
        newnode->_prev = tail;
        newnode->_next = _head;
        _head->_prev = newnode;*/
        Insert(_head, x);
    }
    void PushFront(const DataListtype&x)//前插
    {
        /*Node* newnode = new Node(x);
        Node* next = _head->_next;
        newnode->_next = next;
        next->_prev = newnode;
        _head->_next = newnode;
        newnode->_prev = _head;*/
        Insert(_head->_next, x);
    }
    Node* Find(const DataListtype&x)//查找
    {
        Node* cur = _head->_next;
        while (cur != _head && cur->_data != x)
        {
            cur = cur->_next;
        }
        if (cur == _head)
            return NULL;
        return cur;
    }
    void Insert(Node *pos, const DataListtype&x)//插入
    { 
        prev newnode pos
        Node* prev = pos->_prev;
        Node* newnode = new Node(x);
        prev->_next = newnode;
        newnode->_prev = prev;
        newnode->_next = pos;
        pos->_prev = newnode;
    }
    void PopFront()//前删
    {
        assert(_head->_next != _head);
        //_head  next  next->_next
        Node* next = _head->_next;
        _head->_next = next->_next;
        next->_next->_prev = _head;
        delete next;
        next = NULL;
        Erase(_head->_next);
    }
    void PopBack()//尾删
    {
        /*assert(_head->_next != _head);
        Node* tail = _head->_prev;
        _head->_prev = tail->_prev;
        tail->_prev->_next = _head;
        delete tail;
        tail = NULL;*/
        Erase(_head->_prev);
    }
    void Erase(Node* pos)
    {
        prev   pos  next
        assert(pos!= _head);
        Node* prev = pos->_prev;
        Node* next = pos->_next;
        prev->_next = next;
        next->_prev = prev;
        delete pos;
        pos = NULL;
    }
    void print()
    {
        Node*cur = _head->_next;
        while (cur != _head)
        {
            cout << cur->_data << "->";
            cur = cur->_next;
        }
        printf("NULL\n");
    }
    size_t Size()//大小
    {
        size_t size = 0;
        Node* cur = _head->_next;
        while (cur != _head)
        {
            size++;
            cur = cur->_next;
        }
        return size;
    }
    bool empty()//判空
    {
        return _head->_next == _head;
    }
    ~Mylist()
    {
        Node *cur = _head->_next;
        while (cur != _head)
        {
            Node *next = cur->_next;
            delete cur;
            cur = next;
        }
        delete _head;//现在把头释放
        _head = NULL;
    }
private :
    Node* _head;
};
int main()
{
    Mylist l1;
    l1.PushBack(1);
    l1.PushFront(2);
    l1.print();
    Mylist l2(l1);
    Mylist l3;
    l3 = l2;
    l3.print();
    l2.print();
    l1.Insert(l1.Find(2), 3);
    l1.Insert(l1.Find(3)->_prev, 7);
    l1.print();
    l1.PopFront();
    l1.print();
    l1.PopBack();
    l1.print();
    l1.Erase(l1.Find(1));
    l1.PushBack(10);
    l1.PushFront(11);
    l1.print();
    printf("%d\n", l1.Size());
    l1.PopBack();
    l1.PopFront();
    l1.print();
    system("pause");
    return 0;
}

c++和c区别:
c++是自动调构造函数,自动调析构进行清理,面向对象
c面向过程,需要自己写初始化销毁函数等。

猜你喜欢

转载自blog.csdn.net/sophia__yu/article/details/81513553