当前位置:首页 C++ > 正文

C++线性表的定义

作者:野牛程序员:2023-07-16 09:47:42 C++阅读 2661

在C++中,可以使用类来定义线性表。线性表是一种线性的数据结构,其中的元素按照线性的顺序存储,每个元素都有一个唯一的前驱和后继。

下面是一个使用类定义线性表的示例:

#include <iostream>
using namespace std;

const int MAX_SIZE = 100;

class LinearList {
private:
    int data[MAX_SIZE];
    int length;

public:
    LinearList() {
        length = 0;
    }

    bool isEmpty() {
        return length == 0;
    }

    bool isFull() {
        return length == MAX_SIZE;
    }

    int size() {
        return length;
    }

    void insert(int value) {
        if (isFull()) {
            cout << "LinearList is full. Cannot insert." << endl;
            return;
        }

        data[length] = value;
        length++;
    }

    void remove(int value) {
        if (isEmpty()) {
            cout << "LinearList is empty. Cannot remove." << endl;
            return;
        }

        int index = -1;
        for (int i = 0; i < length; i++) {
            if (data[i] == value) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            cout << "Value not found in LinearList. Cannot remove." << endl;
            return;
        }

        for (int i = index; i < length - 1; i++) {
            data[i] = data[i + 1];
        }

        length--;
    }

    void display() {
        if (isEmpty()) {
            cout << "LinearList is empty." << endl;
            return;
        }

        cout << "LinearList elements: ";
        for (int i = 0; i < length; i++) {
            cout << data[i] << " ";
        }
        cout << endl;
    }
};

int main() {
    LinearList myList;

    myList.insert(5);
    myList.insert(10);
    myList.insert(15);

    myList.display();

    myList.remove(10);

    myList.display();

    return 0;
}

在上述示例中,LinearList 类代表线性表。它使用一个数组 data 存储元素,并使用 length 来跟踪线性表的当前长度。该类提供了一些常用的操作,例如插入元素(insert)、删除元素(remove)、判断线性表是否为空(isEmpty)、判断线性表是否已满(isFull)、获取线性表的大小(size),以及显示线性表的元素(display)。

main 函数中,我们创建了一个 LinearList 对象 myList,然后向其中插入一些元素,显示线性表的内容,然后删除一个元素后再次显示线性表的内容。

请注意,这只是一个基本的示例,实际应用中的线性表可能会有更多的操作和功能。


当使用结构体来定义线性表时,可以使用动态分配内存的方式来实现可变长度的线性表。下面是一个使用结构体定义线性表的示例:

#include <iostream>
using namespace std;

struct LinearList {
    int* data;
    int length;
    int capacity;
};

void initialize(LinearList& list, int initialCapacity) {
    list.data = new int[initialCapacity];
    list.length = 0;
    list.capacity = initialCapacity;
}

void destroy(LinearList& list) {
    delete[] list.data;
    list.data = nullptr;
    list.length = 0;
    list.capacity = 0;
}

bool isEmpty(const LinearList& list) {
    return list.length == 0;
}

bool isFull(const LinearList& list) {
    return list.length == list.capacity;
}

int size(const LinearList& list) {
    return list.length;
}

void insert(LinearList& list, int value) {
    if (isFull(list)) {
        int newCapacity = list.capacity * 2;
        int* newData = new int[newCapacity];

        for (int i = 0; i < list.length; i++) {
            newData[i] = list.data[i];
        }

        delete[] list.data;

        list.data = newData;
        list.capacity = newCapacity;
    }

    list.data[list.length] = value;
    list.length++;
}

void remove(LinearList& list, int value) {
    if (isEmpty(list)) {
        cout << "LinearList is empty. Cannot remove." << endl;
        return;
    }

    int index = -1;
    for (int i = 0; i < list.length; i++) {
        if (list.data[i] == value) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        cout << "Value not found in LinearList. Cannot remove." << endl;
        return;
    }

    for (int i = index; i < list.length - 1; i++) {
        list.data[i] = list.data[i + 1];
    }

    list.length--;
}

void display(const LinearList& list) {
    if (isEmpty(list)) {
        cout << "LinearList is empty." << endl;
        return;
    }

    cout << "LinearList elements: ";
    for (int i = 0; i < list.length; i++) {
        cout << list.data[i] << " ";
    }
    cout << endl;
}

int main() {
    LinearList myList;
    initialize(myList, 5);

    insert(myList, 5);
    insert(myList, 10);
    insert(myList, 15);

    display(myList);

    remove(myList, 10);

    display(myList);

    destroy(myList);

    return 0;
}

在上述示例中,LinearList 是一个结构体,包含 data(指向动态分配的整数数组)、length(线性表的当前长度)和 capacity(线性表的容量)字段。

使用函数来操作线性表,比如 initialize(初始化线性表)、destroy(销毁线性表)、isEmpty(判断线性表是否为空)、isFull(判断线性表是否已满)、size(获取线性表的大小)、insert(插入元素)、remove(删除元素)和 display(显示线性表的元素)。

main 函数中,我们创建了一个 LinearList 对象 myList,然后通过调用相应的操作函数来操作线性表。

需要注意的是,在使用结构体来定义线性表时,需要手动管理内存,包括分配和释放内存。示例中使用 new 运算符来分配内存,并使用 delete 运算符来释放内存,以避免内存泄漏。


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击