创建一个结构体用于存放顺序表相关数据
#define SEQTYPE int
typedef struct SeqList
{
SEQTYPE* data;
int size;
int capacity;
}SeqList;
初始化顺序表
void SeqListInit(SeqList* pq)
{
CheckNull(pq);
pq->data = NULL;
pq->capacity = 0;
pq->size = 0;
}
插入元素
- 插入到表头;
- 插入到指定位置;
- 插入到尾部;
先检查容量是否够用
void CheckCapacity(SeqList* pq)
{
CheckNull(pq);
if (pq->size >= pq->capacity)
{
int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2;
SEQTYPE* new = (SEQTYPE*)realloc(pq->data, sizeof(SEQTYPE) * newcapacity);
if (new == NULL)
{
perror("realloc");
exit(-1);
}
pq->data = new;
pq->capacity = newcapacity;
}
puts("增容成功");
}
void SeqListInsert(SeqList* pq, int pos)
{
CheckNull(pq);
assert(pos <= pq->size);
SEQTYPE InsertVal;
if (pos == -1)
{
printf("请分别输入添加的数据和位置,空格隔开:>");
scanf("%d %d", &InsertVal, &pos);
if (pos > pq->size)
{
printf("请正确输入\n");
return;
}
}
else
{
printf("请输入添加的数据:>");
scanf("%d", &InsertVal);
}
CheckCapacity(pq);
int end = pq->size;
int begin = pos;
while (begin < end)
{
pq->data[end] = pq->data[end - 1];
--end;
}
pq->data[pos] = InsertVal;
++pq->size;
printf("添加成功\n");
}
void SeqListPushBack(SeqList* pq)
{
CheckNull(pq);
SeqListInsert(pq, pq->size);
}
void SeqListPushFront(SeqList* pq)
{
CheckNull(pq);
SeqListInsert(pq, 0);
}
删除元素
- 删除首元素;
- 删除指定位置元素;
- 删除尾部元素;
void SeqListErase(SeqList* pq, int pos)
{
CheckNull(pq);
if (pos == -1)
{
printf("请输入要删除数据的位置:>");
scanf("%d", &pos);
if (pos < 0 || pos >= pq->size)
{
printf("请正确输入\n");
return;
}
}
int begin = pos;
int end = pq->size - 1;
while (begin < end)
{
pq->data[begin] = pq->data[begin + 1];
++begin;
}
--pq->size;
puts("删除成功");
}
void SeqListPophBack(SeqList* pq)
{
CheckNull(pq);
SeqListErase(pq, pq->size - 1);
}
void SeqListPophFront(SeqList* pq)
{
CheckNull(pq);
SeqListErase(pq, 0);
}
元素修改
- 找到目标元素;
- 直接修改该元素的值;
void SeqListModify(SeqList* pq)
{
CheckNull(pq);
int pos;
SEQTYPE x;
printf("请输入修改的位置和新的数据,空格隔开:>");
scanf("%d %d", &pos, &x);
if (pos < 0 && pos >= pq->size)
{
printf("请正确输入\n");
return;
}
pq->data[pos] = x;
puts("修改成功");
}
查找元素
- 查找目标元素,算法多种,比如二分,插值等等,这里使用顺序查找算法,具体代码如下:
void SeqListFindData(SeqList* pq)
{
CheckNull(pq);
SEQTYPE x;
printf("请输入要查找的数据:>");
scanf("%d", &x);
for (int i = 0; i < pq->size; i++)
{
if (pq->data[i] == x)
{
printf("所需查询数据存在,下标为:>%d\n", i);
return;
}
}
printf("找不到\n");
}
排序元素
void SeqListSort(SeqList* pq)
{
CheckNull(pq);
int option = 0;
printf("输入0为升序,1为降序:>");
scanf("%d", &option);
for (int i = 0; i < pq->size - 1; i++)
{
for (int j = 0; j < pq->size - i - 1; j++)
{
if (pq->data[j] > pq->data[j + 1])
{
SEQTYPE tmp = pq->data[j];
pq->data[j] = pq->data[j + 1];
pq->data[j + 1] = tmp;
}
}
}
if (option)
{
SeqListReverse(pq);
return;
}
}
元素反转
void SeqListReverse(SeqList* pq)
{
CheckNull(pq);
int left = 0;
int right = pq->size - 1;
while (left < right)
{
SEQTYPE tmp = pq->data[left];
pq->data[left] = pq->data[right];
pq->data[right] = tmp;
++left;
--right;
}
}
源码
- 以上是顺序表常用的功能操作,下面附上完整代码,VS2019环境
SeqList.c
#include "SeqList.h"
void CheckNull(SeqList* pq)
{
if (pq == NULL)
{
perror("pq::");
exit(-1);
}
}
void SeqListInit(SeqList* pq)
{
CheckNull(pq);
pq->data = NULL;
pq->capacity = 0;
pq->size = 0;
}
void SeqListDestory(SeqList* pq)
{
CheckNull(pq);
free(pq->data);
pq->data = NULL;
pq->size = 0;
pq->capacity = 0;
}
void CheckCapacity(SeqList* pq)
{
CheckNull(pq);
if (pq->size >= pq->capacity)
{
int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2;
SEQTYPE* new = (SEQTYPE*)realloc(pq->data, sizeof(SEQTYPE) * newcapacity);
if (new == NULL)
{
perror("realloc");
exit(-1);
}
pq->data = new;
pq->capacity = newcapacity;
}
puts("增容成功");
}
void SeqListPrint(SeqList* pq)
{
CheckNull(pq);
if (pq->size == 0)
printf("\n");
else
{
for (int i = 0; i < pq->size; i++)
{
printf("%d ", pq->data[i]);
}
puts("\n--------------------------------------");
}
}
void SeqListPushBack(SeqList* pq)
{
CheckNull(pq);
SeqListInsert(pq, pq->size);
}
void SeqListPushFront(SeqList* pq)
{
CheckNull(pq);
SeqListInsert(pq, 0);
}
void SeqListInsert(SeqList* pq, int pos)
{
CheckNull(pq);
assert(pos <= pq->size);
SEQTYPE InsertVal;
if (pos == -1)
{
printf("请分别输入添加的数据和位置,空格隔开:>");
scanf("%d %d", &InsertVal, &pos);
if (pos > pq->size)
{
printf("请正确输入\n");
return;
}
}
else
{
printf("请输入添加的数据:>");
scanf("%d", &InsertVal);
}
CheckCapacity(pq);
int end = pq->size;
int begin = pos;
while (begin < end)
{
pq->data[end] = pq->data[end - 1];
--end;
}
pq->data[pos] = InsertVal;
++pq->size;
printf("添加成功\n");
}
void SeqListErase(SeqList* pq, int pos)
{
CheckNull(pq);
if (pos == -1)
{
printf("请输入要删除数据的位置:>");
scanf("%d", &pos);
if (pos < 0 || pos >= pq->size)
{
printf("请正确输入\n");
return;
}
}
int begin = pos;
int end = pq->size - 1;
while (begin < end)
{
pq->data[begin] = pq->data[begin + 1];
++begin;
}
--pq->size;
puts("删除成功");
}
void SeqListPophBack(SeqList* pq)
{
CheckNull(pq);
SeqListErase(pq, pq->size - 1);
}
void SeqListPophFront(SeqList* pq)
{
CheckNull(pq);
SeqListErase(pq, 0);
}
void SeqListModify(SeqList* pq)
{
CheckNull(pq);
int pos;
SEQTYPE x;
printf("请输入修改的位置和新的数据,空格隔开:>");
scanf("%d %d", &pos, &x);
if (pos < 0 && pos >= pq->size)
{
printf("请正确输入\n");
return;
}
pq->data[pos] = x;
puts("修改成功");
}
void SeqListFindPos(SeqList* pq)
{
CheckNull(pq);
int pos;
printf("请输入要查找数据的位置:>");
scanf("%d", &pos);
if (pos < 0 && pos >= pq->size)
{
printf("请正确输入\n");
return;
}
for (int i = 0; i < pq->size; i++)
{
if (pq->data[i] == pq->data[pos])
{
printf("查找位置的数据为:>%d\n", pq->data[pos]);
break;
}
}
}
void SeqListFindData(SeqList* pq)
{
CheckNull(pq);
SEQTYPE x;
printf("请输入要查找的数据:>");
scanf("%d", &x);
for (int i = 0; i < pq->size; i++)
{
if (pq->data[i] == x)
{
printf("所需查询数据存在,下标为:>%d\n", i);
return;
}
}
printf("找不到\n");
}
void SeqListSort(SeqList* pq)
{
CheckNull(pq);
int option = 0;
printf("输入0为升序,1为降序:>");
scanf("%d", &option);
for (int i = 0; i < pq->size - 1; i++)
{
for (int j = 0; j < pq->size - i - 1; j++)
{
if (pq->data[j] > pq->data[j + 1])
{
SEQTYPE tmp = pq->data[j];
pq->data[j] = pq->data[j + 1];
pq->data[j + 1] = tmp;
}
}
}
if (option)
{
SeqListReverse(pq);
return;
}
}
void SeqListReverse(SeqList* pq)
{
CheckNull(pq);
int left = 0;
int right = pq->size - 1;
while (left < right)
{
SEQTYPE tmp = pq->data[left];
pq->data[left] = pq->data[right];
pq->data[right] = tmp;
++left;
--right;
}
}
test.c
#include "SeqList.h"
void menu()
{
printf("######################################################\n");
printf("##### 1. Print 2. Insert #####\n");
printf("##### 3. PushFront 4. PushBack #####\n");
printf("##### 5. PopFront 6. PopBack #####\n");
printf("##### 7. FindPos 8. FindData #####\n");
printf("##### 9. Modify 10. Erase #####\n");
printf("##### 11. EMPTY 12. Sort #####\n");
printf("##### 13. Reverse 0. Exit #####\n");
printf("######################################################\n");
}
enum Option
{
EXIT,
PRINT,
INSERT,
PUSHFRONT,
PUSHBACK,
POPFRONT,
POPBACK,
FINDPOS,
FINDDATA,
MODIFY,
ERASE,
EMPTY,
SORT,
REVERSE,
};
int main()
{
int option = 0;
int posi = -1;
SeqList s;
SeqListInit(&s);
do
{
menu();
printf("请选择:>");
scanf("%d", &option);
switch (option)
{
case PRINT:
SeqListPrint(&s);
break;
case INSERT:
SeqListInsert(&s, posi);
break;
case PUSHFRONT:
SeqListPushFront(&s);
break;
case PUSHBACK:
SeqListPushBack(&s);
break;
case POPFRONT:
SeqListPophFront(&s);
break;
case POPBACK:
SeqListPophBack(&s);
break;
case FINDPOS:
SeqListFindPos(&s);
break;
case FINDDATA:
SeqListFindData(&s);
break;
case MODIFY:
SeqListModify(&s);
break;
case ERASE:
SeqListErase(&s, posi);
break;
case EMPTY:
SeqListDestory(&s);
break;
case SORT:
SeqListSort(&s);
break;
case REVERSE:
SeqListReverse(&s);
break;
case EXIT:
SeqListDestory(&s);
printf("退出\n");
break;
default:
printf("请正确输入\n");
break;
}
} while (option);
return 0;
}
SeqList.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#define SEQTYPE int
typedef struct SeqList
{
SEQTYPE* data;
int size;
int capacity;
}SeqList;
void SeqListInit(SeqList* pq);
void SeqListDestory(SeqList* pq);
void SeqListPrint(SeqList* pq);
void SeqListInsert(SeqList* pq, int pos);
void SeqListPushBack(SeqList* pq);
void SeqListPushFront(SeqList* pq);
void SeqListErase(SeqList* pq, int pos);
void SeqListPophBack(SeqList* pq);
void SeqListPophFront(SeqList* pq);
void SeqListModify(SeqList* pq);
void SeqListFindPos(SeqList* pq);
void SeqListFindData(SeqList* pq);
void SeqListSort(SeqList* pq);
void SeqListReverse(SeqList* pq);
|