线性表的增删改查

数据结构定义

typedef int ElemType; // 1
typedef struct 	      // 2
{
    ElemType *data;
	int length;
    int capacity;
}SqList;
  1. Elemtype是数据元素的类型,这里定义为int
  2. SqList是顺序表的数据结构,其中包含
    • data:指向存储数据的动态数组的指针。
    • length:当前数据元素的数量。
    • capacity:数组的当前容量(即最大能存储的元素数量)。

初始化顺序表

void InitList(SqList &L, int initalLength, int initalCapacity, int arr[])
{
    L.data = new ElemType[initalCapacity]; // 1
	L.length = initalLength; 			   // 2
    L.capacity = initalCapacity;           // 3
    for (int i = 0; i < L.length; i++)     // 4
        L.data[i] = arr[i];
}
  1. L.data动态分配一个数组,初始容量为initalCapacity
  2. L.length设置顺序表的长度为initalLength,即当前元素个数。
  3. L.capacity设置为initalCapacity,即当前可以存储的最大元素数量。
  4. 通过循环将传入的数组arr赋值给顺序表的data数组。

销毁顺序表

void DestoryList(SqList& L)
{
    delete[] L.data; 	// 1
    L.data = nullptr; 	// 2
    L.length = 0; 		// 3
    L.capacity = 0;
}
  1. delete[] L.data释放动态分配的内存。
  2. L.data设为nullptr,防止悬挂指针。
  3. L.lengthL.capacity为0。

扩展顺序表

void ExpandList(SqList &L)
{
    int newCapacity = L.capacity * 2; 			   // 1
    ElemType *newData = new ElemType[newCapacity]; // 2
    for (int i = 0; i < L.length; i++)
        newData[i] = L.data[i]; 				   // 3
    delete[] L.data; 							   // 4
    L.data = newData;
    L.capacity = newCapacity; 					   // 5
}
  1. newCapacity是新的容量,通常是原来的两倍,这里有点像vector的扩容机制。
  2. 创建一个新的动态数组newData
  3. 把原数组的内容复制到新数组newData中。
  4. 释放原数组的内存,并且把L.data指向新数组。
  5. 更新L.capacity

插入元素

bool ListInsert(SqList &L, int index, ElemType value)
{
    if (index < 1 || index > L.length + 1)  // 1
		return false;

    if (L.length >= L.capacity) 			// 2
        ExpandList(L);

	for (int i = L.length; i >= index; i--) // 3
		L.data[i] = L.data[i - 1];

	L.data[index - 1] = value; 				// 4
	L.length++;
	return true;
}
  1. 检查index是否有效,如果无效直接返回false
  2. 如果当前数组满了,调用ExpandList扩容。
  3. 将插入位置及其后面的元素向后移动一位。
  4. 将新元素插入指定位置,更新L.length

删除元素

bool DeleteElem(SqList &L, int index, ElemType &value)
{
	if (index < 1 || index > L.length) 		// 1
		return false;

	value = L.data[index - 1]; 				// 2
	for (int i = index; i < L.length; i++)  // 3
		L.data[i - 1] = L.data[i];

	L.length--; 							// 4
	return true;
}
  1. 检查index是否有效。
  2. 保存被删除的数据到value变量中。
  3. 将删除位置后到元素往前移动一位。
  4. 更新L.length

修改元素

bool ModifyList(SqList &L, int index, ElemType newValue)
{
    if (index < 1 || index > L.length) // 1
        return false;
    L.data[index - 1] = newValue; 	   // 2
    return true;
}
  1. 检查index是否有效。
  2. 更新指定位置的值为newValue

查看元素

void DisplayList(SqList L)
{
	for (int i = 0; i < L.length; i++)
		printf("%d ", L.data[i]);
	printf("\n");
}
  1. 打印顺序表中的所有元素

完整代码(可执行)

#include <iostream>
using namespace std;

typedef int ElemType;

typedef struct
{
    ElemType *data;
	int length;
    int capacity;
}SqList;

void InitList(SqList &L, int initalLength, int initalCapacity, int arr[])
{
    L.data = new ElemType[initalCapacity];
	L.length = initalLength;
    L.capacity = initalCapacity;
    for (int i = 0; i < L.length; i++)
        L.data[i] = arr[i];
}

void DestoryList(SqList& L)
{
    delete[] L.data;
    L.data = nullptr;
    L.length = 0;
    L.capacity = 0;
}

void ExpandList(SqList &L) // 扩容
{
    int newCapacity = L.capacity * 2;
    ElemType *newData = new ElemType[newCapacity];
    for (int i = 0; i < L.length; i++)
        newData[i] = L.data[i];
    delete[] L.data;
    L.data = newData;
    L.capacity = newCapacity;
}

bool ListInsert(SqList &L, int index, ElemType value)
{
    if (index < 1 || index > L.length + 1)
		return false;

    if (L.length >= L.capacity)
        ExpandList(L);

	for (int i = L.length; i >= index; i--)
		L.data[i] = L.data[i - 1];

	L.data[index - 1] = value;
	L.length++;
	return true;
}

bool DeleteElem(SqList &L, int index, ElemType &value)
{
	if (index < 1 || index > L.length)
		return false;

	value = L.data[index - 1];
	for (int i = index; i < L.length; i++)
		L.data[i - 1] = L.data[i];

	L.length--;
	return true;
}

bool ModifyList(SqList &L, int index, ElemType newValue)
{
    if (index < 1 || index > L.length)
        return false;
    L.data[index - 1] = newValue;
    return true;
}

void DisplayList(SqList L)
{
	for (int i = 0; i < L.length; i++)
		printf("%d ", L.data[i]);
	printf("\n");
}

int main()
{
    int arr[] = {2, 2, 0, 5, 2, 1, 8, 1};
    int arrSize = sizeof(arr) / sizeof(arr[0]);
	SqList list;
	InitList(list, arrSize, 10, arr);

	while (1)
	{
		cout << "==========================" << endl;
		cout << "1. 插入数据" << endl;
		cout << "2. 删除数据" << endl;
        cout << "3. 修改数据" << endl;
		cout << "4. 查看数据" << endl;
		cout << "5. 退出" << endl;
		cout << "==========================请输入:";

		int input = 0;
		cin >> input;

		if (input == 1)
		{
			int index = 0;
			int value = 0;

			cout << "=====请输入要插入的位置:";
			cin >> index;
			cout << "=====请输入要插入的数据元素:";
			cin >> value;

			if (ListInsert(list, index, value))
			{
				cout << "插入后的数据是:";
                DisplayList(list);
			}
			else cout << "插入失败" << endl;
		}

		if (input == 2)
		{
			int index = 0;
			int value = 0;
			cout << "=====请输入要删除的位置:";
			cin >> index;

			if (DeleteElem(list, index, value))
            {
                cout << "删除后的数据是:";
                DisplayList(list);
            }
			else cout << "删除失败" << endl;
		}
        if (input == 3)
        {
            int index = 0;
            int newvalue = 0;
            cout << "=====请输入要修改的位置:";
            cin >> index;
            cout << "=====请输入新的数据元素:";
            cin >> newvalue;

            if (ModifyList(list, index, newvalue))
            {
                cout << "修改后的数据是:";
                DisplayList(list);
            }
            else cout << "修改失败" << endl;
        }
		if (input == 4) DisplayList(list);
		if (input == 5) break;
	}
    DestoryList(list);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值