线性表分为顺序表和单链表
线性表的操作主要是查询、插入、删除
1、顺序表
- 首先,定义一个顺序表的结构体
#define MAX_SIZE 10
typedef struct {
int data[MAX_SIZE];
int length;
}SqList, * PsqList;
- 创建一个线性表
void createSqList(PsqList pSqList) {
pSqList->length = 0;
for (int i = 0; i < MAX_SIZE - 5; i++) {
pSqList->data[i] = (i + 1) * 3;
pSqList->length++;
}
}
- 遍历线性表
void displaySqList(PsqList pSqList) {
printf("\tSqList Length: %d\n", pSqList->length);
for (int i = 0; i < pSqList->length; i++) {
printf("\t%d", pSqList->data[i]);
}
printf("\n");
}
1.1、查询
- 顺序查询
#include <string.h>
//根据数值查询位置
const char* queryPositionByValue(PsqList pSqList, int value) {
const char* result = "表中无此数据";
for (int i = 0; i < pSqList->length; i++) {
if (pSqList->data[i] == value) {
char buffer[100];
sprintf_s(buffer, "数据 %d 在表中的位置 %d\n", value, i);
result = _strdup(buffer);
return result;
}
}
return result;
}
1.2、插入
- 在position处插入元素
- 位置position在表中对应的序号 i = position-1
- 思路:将v(length-1)至vi依次后移一位,然后将新数据插入vi
//在指定位置插入元素
void insertElementByPosition(PsqList pSqList, int position, int element) {
if (position > MAX_SIZE || position < 1) {
return;
}
if (pSqList->length >= MAX_SIZE) {
return;
}
if (position == pSqList->length + 1) {
pSqList->data[pSqList->length] = element;
pSqList->length++;
return;
}
int i = position - 1;
for (int j = pSqList->length - 1; j >= i; j--) {
pSqList->data[j + 1] = pSqList->data[j];
}
pSqList->data[i] = element;
pSqList->length++;
}
1.3、删除
- 删除position处的元素
- 位置position在表中对应的序号 i = position-1
- 思路:将v(i+1)至v(length-1)依次向前移动一位
//删除指定位置的元素
void deleteElementByPosition(PsqList pSqList, int position) {
if (position < 1 || position > MAX_SIZE) {
return;
}
if (position > pSqList->length) {
return;
}
int i = position - 1;
for (int j = i; j < pSqList->length - 1; j++) {
pSqList->data[j] = pSqList->data[j + 1];
}
pSqList->length--;
}
1.4、运行结果
int main() {
SqList sqList;
PsqList pSqList = &sqList;
createSqList(pSqList);
displaySqList(pSqList);
printf("\n\t查找数据 12\n");
const char* result = queryPositionByValue(pSqList, 12);
printf("\t%s", result);
printf("\n\t在第3个位置插入数据 67\n");
insertElementByPosition(pSqList, 3, 67);
displaySqList(pSqList);
printf("\n\t删除第4个位置的数据\n");
deleteElementByPosition(pSqList, 4);
displaySqList(pSqList);
return 0;
}
2、单链表
- 首先创建一个链表结构体
typedef struct LNode {
int data;
struct LNode* next;
}LNode, * LinkList;
- 遍历链表
//遍历链表
void displayLinkList(LinkList p) {
printf("\n");
while (p != NULL) {
printf("\t%d", p->data);
p = p->next;
}
printf("\n");
}
2.1、创建单链表
(1)头插法
- 创建链表
//头插法
void createLinkByHead(LinkList& linkList) {
linkList = (LinkList)malloc(sizeof(LNode));
linkList->data = 100;
linkList->next = NULL;
for (int i = 0; i < 5; i++) {
LinkList p = (LinkList)malloc(sizeof(LNode));
p->data = (i + 2) * 100;
p->next = linkList->next;
linkList->next = p;
}
}
- 输出结果
int main() {
LinkList L1;
createLinkByHead(L1);
displayLinkList(L1);
displayLinkList(L1);
return 0;
}
(2)尾插法
- 创建链表
//尾插法
void createLinkByTail(LinkList& linkList) {
linkList = (LinkList)malloc(sizeof(LNode));
linkList->data = 100;
linkList->next = NULL;
LinkList pre = linkList;
for (int i = 0; i < 5; i++) {
LinkList p = (LinkList)malloc(sizeof(LNode));
p->data = (i + 2) * 100;
pre->next = p;
p->next = NULL;
pre = p;
}
}
- 输出结果
int main() {
LinkList L2;
createLinkByTail(L2);
displayLinkList(L2);
displayLinkList(L2);
return 0;
}
2.2、通过序号查找
//通过序号查找
LinkList queryByIndex(LinkList linkList, int index) {
LinkList p = linkList;
int i = 0;
while (i < index) {
p = p->next;
i++;
}
return p;
}
- 输出结果
int main() {
LinkList L;
createLinkByTail(L);
displayLinkList(L);
int position = 3;
LinkList p= queryByIndex(L, position-1);
printf("\n\t第 %d 个元素的值是 %d\n", position, p->data);
return 0;
}
2.3、通过序号插入
//通过序号插入
LinkList insertByIndex(LinkList linkList, int index, int data) {
LinkList pEle = (LinkList)malloc(sizeof(LNode));
pEle->data = data;
pEle->next = NULL;
LinkList pre = linkList;
LinkList p = pre;
if (index == 0){
pEle->next = pre;
linkList = pEle;
}
else {
for (int i = 0; i < index; i++) {
pre = p;
p = p->next;
}
pre->next = pEle;
pEle->next = p;
}
return linkList;
}
- 输出结果
int main() {
LinkList L;
createLinkByTail(L);
displayLinkList(L);
int positionInsert1 = 3;
printf("\n\t在第 %d 个位置插入\n", positionInsert1);
LinkList LInsert1 = insertByIndex(L, positionInsert1 - 1, 333);
displayLinkList(LInsert1);
int positionInsert2 = 1;
printf("\n\t在第 %d 个位置插入\n", positionInsert2);
LinkList LInsert2 = insertByIndex(LInsert1, positionInsert2 - 1, 111);
displayLinkList(LInsert2);
int positionInsert3 = 8;
printf("\n\t在第 %d 个位置插入\n", positionInsert3);
LinkList LInsert3 = insertByIndex(LInsert2, positionInsert3 - 1, 888);
displayLinkList(LInsert3);
int positionInsert4 = 10;
printf("\n\t在第 %d 个位置插入\n", positionInsert4);
LinkList LInsert4 = insertByIndex(LInsert3, positionInsert4 - 1, 101010);
displayLinkList(LInsert4);
return 0;
}
2.4、通过序号删除
//通过序号删除
LinkList deleteByIndex(LinkList linkList, int index) {
LinkList pre = linkList;
if (index == 0) {
linkList = linkList->next;
pre->next = NULL;
free(pre);
}
else {
LinkList p = pre;
for (int i = 0; i < index; i++) {
pre = p;
p = p->next;
}
pre->next = p->next;
p->next = NULL;
free(p);
}
return linkList;
}
- 输出结果
int main() {
LinkList L;
createLinkByTail(L);
displayLinkList(L);
int positionDel1 = 3;
printf("\n\t删除第 %d 个元素的值\n", positionDel1);
LinkList LDel1= deleteByIndex(L, positionDel1 - 1);
displayLinkList(LDel1);
int positionDel2 = 1;
printf("\n\t删除第 %d 个元素的值\n", positionDel2);
LinkList LDel2 = deleteByIndex(LDel1, positionDel2 - 1);
displayLinkList(LDel2);
int positionDel3 = 4;
printf("\n\t删除第 %d 个元素的值\n", positionDel3);
LinkList LDel3 = deleteByIndex(LDel2, positionDel3 - 1);
displayLinkList(LDel3);
return 0;
}