数据结构——顺序表(文字+代码+带图详细讲解)

在这里插入图片描述

在 C 语言中,顺序表可以使用数组来实现。顺序表是一种线性表,其中的元素在物理上连续存储,可以通过下标访问任意元素。
顺序表的基本操作包括插入、删除、查找、遍历和初始化等。
在这里插入图片描述

这段代码定义了一个顺序表的结构体,其中包括三个成员变量:
SLDataType* array:指向动态开辟的数组,即顺序表的存储空间。
size_t size:有效数据个数,即当前顺序表中实际存储的元素个数。
size_t capicity:空间容量的大小,即当前顺序表的存储空间的大小。

typedef int SLDataType;
//定义了一个顺序表的结构体
typedef struct SeqList
{
	SLDataType* array;//指向动态开辟的数组
	size_t size;	//有效数据个数
	size_t capicity;	//空间容量的大小
}SeqList;

这段代码实现了顺序表的初始化操作。该函数接受一个指向顺序表结构体的指针 psl,并在内存中动态分配了一个大小为4的数组来存储元素。

具体来说,该函数完成了以下操作:
使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。
使用 malloc()函数动态分配了一个大小为4的数组来存储元素,并将其赋值给 psl->array,即顺序表的存储空间。
判断数组是否分配成功,如果分配失败则输出错误信息并返回。
将 psl->size 和 psl->capicity初始化为0和4,分别表示当前顺序表中实际存储的元素个数和存储空间的容量大小。

//顺序表初始化
void SeqListInit(SeqList* psl)
{
	assert(psl);
	psl->array= (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (psl->array == NULL)
	{
		perror("malloc fail");
		return;
	}
	psl->size = 0;
	psl->capicity = 4;
}

在这里插入图片描述

这段代码实现了顺序表的动态增容操作。该函数接受一个指向顺序表结构体的指针 psl,并在顺序表中的元素个数等于容量大小时进行扩容操作。
具体来说,该函数完成了以下操作:

使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。
判断当前顺序表中实际存储的元素个数 psl->size是否等于存储空间的容量大小 psl->capicity,如果相等则说明顺序表已满,需要进行扩容操作。
使用 realloc()函数对顺序表存储空间进行扩容,将原来的空间大小 psl-capicity 扩大一倍,即 psl->capicity * 2,并将扩容后的空间大小赋值给 psl->capicity。
判断扩容是否成功,如果扩容失败则输出错误信息并返回。
将扩容后的新空间地址赋值给 psl->array,即顺序表的存储空间。

// 检查空间,如果满了,进行增容
void CheckCapacity(SeqList* psl)
{
	assert(psl);
	if (psl->size == psl->capicity)
	{
		SLDataType* tmp = (SLDataType*)realloc(psl->array, sizeof(SLDataType) * psl->capicity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		psl->array = tmp;
		psl->capicity *= 2;
	}
}

在这里插入图片描述
在这里插入图片描述

这段代码实现了顺序表尾插操作。该函数接受一个指向顺序表结构体的指针 psl 和要插入的元素值 x,并将元素插入到顺序表的末尾。

具体来说,该函数完成了以下操作:
使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。
调用 CheckCapacity(),函数检查顺序表的存储空间是否已满,如果已满则进行动态增容操作。
将要插入的元素值 x 赋值给顺序表存储空间中下一个可用的位置,即
psl->array[psl->size]。 将顺序表中实际存储的元素个数 psl->size 加1,表示顺序表中又插入了一个元素。

// 顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x)
{
	assert(psl);
	CheckCapacity(psl);
	psl->array[psl->size] = x;
	psl->size++;
}

在这里插入图片描述

这段代码实现了顺序表尾删操作。该函数接受一个指向顺序表结构体的指针 psl,并将顺序表中的最后一个元素删除。

具体来说,该函数完成了以下操作:

使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。
使用 assert() 宏判断顺序表中实际存储的元素个数psl->size 是否大于0,如果不大于0则说明顺序表为空,无法进行删除操作。
将顺序表存储空间中最后一个元素位置 psl->array[psl->size-1] 的值清空,即置为0。
将顺序表中实际存储的元素个数 psl->size减1,表示顺序表中又删除了一个元素。

// 顺序表尾删
void SeqListPopBack(SeqList* psl)
{
	assert(psl);
	assert(psl->size > 0);
	psl->array[psl->size-1] = 0;
	psl->size--;
}

在这里插入图片描述
在这里插入图片描述

这段代码实现了顺序表头插操作。该函数接受一个指向顺序表结构体的指针 psl 和要插入的元素值 x,并将元素插入到顺序表的头部。

具体来说,该函数完成了以下操作:

使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。
调用 CheckCapacity()函数检查顺序表的存储空间是否已满,如果已满则进行动态增容操作。
从顺序表存储空间中最后一个元素位置开始,向后逐个将每个元素向后移动一个位置,直到顺序表存储空间中的所有元素都向后移动了一个位置。
将要插入的元素值 x 赋值给顺序表存储空间中第一个位置,即 psl->array[0]。 将顺序表中实际存储的元素个数 psl->size加1,表示顺序表中又插入了一个元素。

// 顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x)
{
	assert(psl);
	CheckCapacity(psl);
	int end = psl->size - 1;
	while (end >= 0)
	{
		psl->array[end + 1] = psl->array[end];
		--end;
	}
	psl->array[0] = x;
	psl->size++;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这段代码实现了顺序表头删操作。该函数接受一个指向顺序表结构体的指针 psl,并将顺序表中的第一个元素删除。

具体来说,该函数完成了以下操作:

使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。
使用 assert() 宏判断顺序表中实际存储的元素个数 psl->size 是否大于0,如果不大于0则说明顺序表为空,无法进行删除操作。
从顺序表存储空间中第二个元素位置开始,向前逐个将每个元素向前移动一个位置,直到顺序表存储空间中的所有元素都向前移动了一个位置。
将顺序表中实际存储的元素个数 psl->size 减1,表示顺序表中又删除了一个元素。

// 顺序表头删
void SeqListPopFront(SeqList* psl)
{
	assert(psl);
	assert(psl->size > 0);
	int begin = 1;
	while (begin < psl->size)
	{
		psl->array[begin - 1] = psl->array[begin];
		++begin;
	}
	psl->size--;
}

在这里插入图片描述
在这里插入图片描述

这段代码实现了顺序表的查找操作。该函数接受一个指向顺序表结构体的指针 psl 和要查找的元素值 x,并在顺序表中查找该元素。

具体来说,该函数完成了以下操作:

使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。
在顺序表存储空间中逐个查找元素,如果找到了元素值等于 x的元素,则返回该元素在顺序表中的下标。
如果在顺序表中没有找到元素值等于 x 的元素,则返回-1,表示查找失败。

// 顺序表查找
int SeqListFind(SeqList* psl, SLDataType x)
{
	assert(psl);
	for (int i = 0; i < psl->size; i++)
	{
		if (psl->array[i] == x)
		{
			return i;
		}
	}
	return -1;
}

这段代码实现了顺序表在指定位置插入元素的操作。该函数接受一个指向顺序表结构体的指针 psl,要插入的位置 pos,以及要插入的元素值 x。

具体来说,该函数完成了以下操作:

使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。
使用 assert() 宏判断要插入的位置 pos 是否合法,即在顺序表的范围内。
调用 CheckCapacity() 函数检查顺序表的存储空间是否已满,如果已满则进行动态增容操作。
从顺序表存储空间中最后一个元素位置开始,向后逐个将每个元素向后移动一个位置,直到顺序表存储空间中的要插入位置 pos 的元素位置。
将要插入的元素值 x 赋值给顺序表存储空间中要插入位置 pos 的元素位置,即 psl->array[pos]。
将顺序表中实际存储的元素个数 psl->size 加1,表示顺序表中又插入了一个元素。

// 顺序表在pos位置插入x
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
{
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);
	CheckCapacity(psl);
	int end = psl->size - 1;
	while (end >= pos)
	{
		psl->array[end + 1] = psl->array[end];
		--end;
	}
	psl->array[pos] = x;
	psl->size++;
}

在这里插入图片描述

在这里插入图片描述

这段代码实现了顺序表删除指定位置的元素操作。该函数接受一个指向顺序表结构体的指针 psl 和要删除的位置 pos。

具体来说,该函数完成了以下操作:

使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。
使用 assert() 宏判断要删除的位置 pos是否合法,即在顺序表的范围内。
从顺序表存储空间中要删除位置 pos的下一个元素位置开始,向前逐个将每个元素向前移动一个位置,直到顺序表存储空间中的所有元素都向前移动了一个位置。
将顺序表中实际存储的元素个数psl->size 减1,表示顺序表中又删除了一个元素。

// 顺序表删除pos位置的值
void SeqListErase(SeqList* psl, size_t pos)
{
assert(psl);
assert(pos >= 0 && pos <= psl->size);
int begin = pos + 1;
while (begin < psl->size)
{
psl->array[begin - 1] = psl->array[begin];
++begin;
}
psl->size--;
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

这段代码实现了顺序表的销毁操作。该函数接受一个指向顺序表结构体的指针 psl,并释放掉顺序表所占用的内存空间。

具体来说,该函数完成了以下操作:

使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。 调用 free() 函数释放顺序表存储空间所占用的内存空间。
将顺序表存储空间指针 psl->array 置为NULL,表示该存储空间已被释放。
将顺序表的容量 psl->capicity和实际存储的元素个数 psl->size 都置为0,表示顺序表已被销毁。
需要注意的是,在使用完顺序表后,需要及时调用该函数来释放顺序表所占用的内存空间,以避免内存泄漏。

// 顺序表销毁
void SeqListDestory(SeqList* psl)
{
	assert(psl);
	free(psl->array);
	psl->array == NULL;
	psl->capicity = 0;
	psl->size = 0;
}

这段代码实现了顺序表的打印操作。该函数接受一个指向顺序表结构体的指针 psl,并将顺序表中存储的所有元素打印输出。

具体来说,该函数完成了以下操作:

使用 assert() 宏判断指针 psl 不为空,如果为空则程序会中止。 使用 for循环逐个遍历顺序表中实际存储的元素,将每个元素打印输出。
在打印完所有元素后,使用 printf()函数输出一个换行符,以便于显示下一行输出。

// 顺序表打印
void SeqListPrint(SeqList* psl)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
printf("%d ", psl->array[i]);
}
printf("\n");

}

完整程序

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
//定义了一个顺序表的结构体
typedef struct SeqList
{
	SLDataType* array;//指向动态开辟的数组
	size_t size;	//有效数据个数
	size_t capicity;	//空间容量的大小
}SeqList;

//顺序表初始化
void SeqListInit(SeqList* psl);
// 检查空间,如果满了,进行增容
void CheckCapacity(SeqList* psl);
// 顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x);
// 顺序表尾删
void SeqListPopBack(SeqList* psl);
// 顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x);
// 顺序表头删
void SeqListPopFront(SeqList* psl);
// 顺序表查找
int SeqListFind(SeqList* psl, SLDataType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* psl, size_t pos);
// 顺序表销毁
void SeqListDestory(SeqList* psl);
// 顺序表打印
void SeqListPrint(SeqList* psl);

//顺序表初始化
void SeqListInit(SeqList* psl)
{
	assert(psl);
	psl->array= (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (psl->array == NULL)
	{
		perror("malloc fail");
		return;
	}
	psl->size = 0;
	psl->capicity = 4;
}

// 检查空间,如果满了,进行增容
void CheckCapacity(SeqList* psl)
{
	assert(psl);
	if (psl->size == psl->capicity)
	{
		SLDataType* tmp = (SLDataType*)realloc(psl->array, sizeof(SLDataType) * psl->capicity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		psl->array = tmp;
		psl->capicity *= 2;
	}
}

// 顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x)
{
	assert(psl);
	CheckCapacity(psl);
	psl->array[psl->size] = x;
	psl->size++;
}

// 顺序表尾删
void SeqListPopBack(SeqList* psl)
{
	assert(psl);
	assert(psl->size > 0);
	psl->array[psl->size-1] = 0;
	psl->size--;
}

// 顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x)
{
	assert(psl);
	CheckCapacity(psl);
	int end = psl->size - 1;
	while (end >= 0)
	{
		psl->array[end + 1] = psl->array[end];
		--end;
	}
	psl->array[0] = x;
	psl->size++;
}

// 顺序表头删
void SeqListPopFront(SeqList* psl)
{
	assert(psl);
	assert(psl->size > 0);
	int begin = 1;
	while (begin < psl->size)
	{
		psl->array[begin - 1] = psl->array[begin];
		++begin;
	}
	psl->size--;
}

// 顺序表查找
int SeqListFind(SeqList* psl, SLDataType x)
{
	assert(psl);
	for (int i = 0; i < psl->size; i++)
	{
		if (psl->array[i] == x)
		{
			return i;
		}
	}
	return -1;
}

// 顺序表在pos位置插入x
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
{
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);
	CheckCapacity(psl);
	int end = psl->size - 1;
	while (end >= pos)
	{
		psl->array[end + 1] = psl->array[end];
		--end;
	}
	psl->array[pos] = x;
	psl->size++;
}

// 顺序表删除pos位置的值
void SeqListErase(SeqList* psl, size_t pos)
{
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);
	int begin = pos + 1;
	while (begin < psl->size)
	{
		psl->array[begin - 1] = psl->array[begin];
		++begin;
	}
	psl->size--;
}
// 顺序表销毁
void SeqListDestory(SeqList* psl)
{
	assert(psl);
	free(psl->array);
	psl->array == NULL;
	psl->capicity = 0;
	psl->size = 0;
}

// 顺序表打印
void SeqListPrint(SeqList* psl)
{
	assert(psl);
	for (int i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->array[i]);
	}
	printf("\n");

}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用Java顺序表实现简易通讯录的代码: 首先,在Java中需要定义一个类来表示通讯录中的每一个联系人,这个类包含联系人的姓名和电话号码两个属性: ```java public class Contact { private String name; private String phoneNumber; public Contact(String name, String phoneNumber) { this.name = name; this.phoneNumber = phoneNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } } ``` 接下来,我们用一个顺序表来存储所有的联系人信息: ```java public class ContactList { private Contact[] contacts; private int size; public ContactList(int capacity) { contacts = new Contact[capacity]; size = 0; } public boolean add(Contact contact) { if (size >= contacts.length) { return false; } contacts[size++] = contact; return true; } public boolean remove(int index) { if (index < 0 || index >= size) { return false; } for (int i = index; i < size - 1; i++) { contacts[i] = contacts[i + 1]; } contacts[--size] = null; return true; } public Contact get(int index) { if (index < 0 || index >= size) { return null; } return contacts[index]; } public int size() { return size; } } ``` 在这个类中,我们定义了一个Contact类型的数组来存储所有的联系人信息,同时还有一个size变量来记录当前已经存储的联系人数目。在这个类中,我们提供了以下几个方法: - add(Contact contact):向通讯录中添加一个联系人,如果当前已经存储的联系人数目已经达到了数组的容量,则添加失败。 - remove(int index):根据给定的下标从通讯录中删除一个联系人,如果给定的下标越界,则删除失败。 - get(int index):根据给定的下标获取通讯录中指定位置的联系人,如果给定的下标越界,则返回null。 - size():获取当前通讯录中已经存储的联系人数目。 我们可以使用这个类来实现一个简单的通讯录应用程序,这个程序可以让用户输入联系人的姓名和电话号码,然后将其添加到通讯录中,也可以让用户删除指定位置的联系人,还可以列出所有已经存储的联系人信息。下面是这个应用程序的代码: ```java import java.util.Scanner; public class ContactApp { public static void main(String[] args) { ContactList contactList = new ContactList(10); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:"); System.out.println("1. 添加联系人"); System.out.println("2. 删除联系人"); System.out.println("3. 列出所有联系人"); System.out.println("4. 退出"); int choice = scanner.nextInt(); scanner.nextLine(); switch (choice) { case 1: System.out.println("请输入联系人姓名:"); String name = scanner.nextLine(); System.out.println("请输入联系人电话号码:"); String phoneNumber = scanner.nextLine(); Contact contact = new Contact(name, phoneNumber); if (contactList.add(contact)) { System.out.println("添加成功!"); } else { System.out.println("添加失败,通讯录已满!"); } break; case 2: System.out.println("请输入要删除联系人的下标:"); int index = scanner.nextInt(); if (contactList.remove(index)) { System.out.println("删除成功!"); } else { System.out.println("删除失败,下标越界!"); } break; case 3: System.out.println("所有联系人信息:"); for (int i = 0; i < contactList.size(); i++) { Contact c = contactList.get(i); System.out.println((i + 1) + ". " + c.getName() + " " + c.getPhoneNumber()); } break; case 4: System.out.println("谢谢使用!"); return; default: System.out.println("输入错误,请重新输入!"); break; } } } } ``` 这个应用程序首先创建了一个ContactList对象来存储所有的联系人信息。然后,它通过一个无限循环来等待用户输入选择,并根据用户的选择执行相应的操作。当用户选择退出程序时,程序会结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值