线性表(cpp)

本文通过实例代码详细介绍了C++中线性表的实现,主要使用了vector容器,涵盖了SeqList.h和main.cpp两个关键文件的内容。
摘要由CSDN通过智能技术生成

浅学一下vector.
SeqList.h文件

#include<iostream>
#include<cstring>
using namespace std;    
typedef size_t size_type;
#define NotFound NULL
#define NotFoundIndex -1
template<class T>
class SeqList{
    protected:
        T *start;
        T *finish;
        T *end_of_storage;
    public:
        SeqList();
        SeqList(size_type size);
        T* begin(){return start;};
        T* end(){return finish;}
        size_type size(){return size_type(end()-begin());};
        size_type capacity(){return size_type(end_of_storage-begin());};
        T& operator[](size_type index);
        void push_back(const T& x);
        //1.插入
        void insertAfter(const T& target,const T& x);
        void insertBefore(const T& target,const T& x);
        void insert(const T& index,const T& x);
        //2.删除
        void deleteByValue(const T& value);
        void deleteByIndex(size_type index);
        //3.显示
        void show();
        //4.查找
        T* getByValue(const T& value);
        //5.定位
        size_type getIndex(const T& value);
        //6.更新
        void set(size_type index,const T& x){*(start+index)=x;}
};


template<class T>
SeqList<T>::SeqList()
{   
    start=finish=end_of_storage=NULL;
}
template<class T>
SeqList<T>::SeqList(size_type size)
{   
    start=new T[size*sizeof(T)];
    finish=start;
    end_of_storage=start+size;
}
template<class T>
T& SeqList<T>::operator[](size_type index)
{
    return *(start+index);
}
template<class T>
void SeqList<T>::push_back(const T& x)
{
    if(finish!=end_of_storage)
    {
        *finish=x;
        finish++;
    }
    else 
    {
        const size_type old_size=size();
        const size_type len=old_size!=0?old_size*2:1;

        T* new_start=new T[len];
        T* new_finish=new_start+size();
        T* new_storage=new_start+len;
        
        memcpy(new_start,start,sizeof(T)*size());
        delete[] start;
        
        start=new_start;
        finish=new_finish;
        end_of_storage=new_storage;
        
        push_back(x);
    }
}
//3.显示
template<class T>
void SeqList<T>::show()
{
    cout<<"{"<<'\n';
    cout<<"  size: "<<size()<<'\n';
    cout<<"  capacity: "<<capacity()<<'\n';
    for(T* s=begin();s!=end();s++)
        cout<<"  "<<*s<<'\n';
    cout<<"}"<<'\n';
};
//4.查找
template<class T>
T* SeqList<T>::getByValue(const T& value)
{
    for(T* s=begin();s!=end();s++)
    {
        if(*s==value)return s;
    }
    return NotFound;
}
//5.定位
template<class T>
size_type SeqList<T>::getIndex(const T& value)
{
    T* reference=getByValue(value);
    if(reference!=NotFound)
        return size_type(reference-start);
    return size_type(NotFoundIndex);
}
//1.插入
//tip:没有对插入的成功与否进行返回.
//    插入目标无重复且插入目标存在的情况下可以保证插入成功.
template<class T>
void SeqList<T>::insertAfter(const T& target,const T& x)
{
    T* reference=getByValue(target);
    if(reference!=end()-1)
    {
        T temp=*(end()-1);
        for(T* e=end()-1;e!=reference+1;e--)
            *e=*(e-1);
        *(reference+1)=x;
        push_back(temp);
    }
    else push_back(x);
}
template<class T>
void SeqList<T>::insertBefore(const T& target,const T& x)
{
    T* reference=getByValue(target);
    T temp=*(end()-1);
    T* e=end()-1;
    for(;e!=reference;e--)
        *e=*(e-1);
    *e=x;
    push_back(temp);
}
template<class T>
void SeqList<T>::insert(const T& index,const T& x)
{
    insertBefore(*(start+index-1),x);
}

//2.删除
template<class T>
void SeqList<T>::deleteByValue(const T& value)
{
    for(T* s=begin();s!=end();s++)
    {
        if(*s==value)
        {
            while(s!=end()-1)
            {
                *s=*(s+1);
                s++;
            }
            finish--;
            return ;
        }
    }
}
template<class T>
void SeqList<T>::deleteByIndex(size_type index)
{
    deleteByValue(*(start+index-1));
}

main.cpp文件

#include <iostream>
#include"SeqList.h"
//int main() {
//	std::cout << "Hello world!\n";
//}
signed main()
{
    cout<<"SeqList b:"<<'\n';
    SeqList<int>b;
    for(int i=0;i<5;i++)b.push_back(i+1);
    b[3]=1;
    b.show();
    cout<<"\ntest for insertAfter:\n";
    b.insertAfter(1,51);
    b.insertAfter(5,11);
    b.show();
    cout<<"\ntest for insertBefore:\n";
    b.insertBefore(51,2);
    b.insertBefore(1,33);
    b.show();
    cout<<"\ntest for insert:\n";
    b.insert(1,22);
    b.insert(3,44);
    b.show();
    cout<<"\ntest for deleteByValue:\n";
    b.deleteByValue(44);
    b.deleteByValue(22);
    b.show();
    cout<<"\ntest for deleteByIndex:\n";
    b.deleteByValue(2);
    b.deleteByValue(33);
    b.show();
}
C++中,线性表是一种基础的数据结构,通常可以使用数组(Array)或链表(Linked List)来实现。这里分别给出简单版本的数组和链表实现。 ### 1. 数组实现 (动态数组 - std::vector) ```cpp #include <iostream> #include <vector> // 数组版本的线性表 class ArrayLinearList { public: // 构造函数,初始化大小 ArrayLinearList(size_t size) : data(size), capacity(size) {} // 添加元素到列表尾部,自动扩容 void push_back(int value) { if (data.size() == capacity) { resize(capacity * 2); // 扩容两倍 } data.push_back(value); } // 访问指定索引的元素 int get(int index) const { return data[index]; } private: std::vector<int> data; size_t capacity; // 当前存储容量 void resize(size_t new_capacity) { data.resize(new_capacity); } }; int main() { ArrayLinearList list(5); list.push_back(1); list.push_back(2); std::cout << "Element at index 0: " << list.get(0) << std::endl; return 0; } ``` ### 2. 链表实现 (单向链表) ```cpp #include <iostream> // 单向链表节点 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; // 链表版本的线性表 class LinkedListLinearList { public: void push_back(int value) { ListNode* newNode = new ListNode(value); if (!head) { head = newNode; } else { ListNode* current = head; while (current->next) { current = current->next; } current->next = newNode; } } // 返回当前链表长度,如果为空则返回0 size_t size() const { size_t count = 0; for (ListNode* node = head; node != nullptr; ++node, ++count) {} return count; } private: ListNode* head; }; int main() { LinkedListLinearList list; list.push_back(1); list.push_back(2); std::cout << "Length of the list: " << list.size() << std::endl; return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值