#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX_SIZE 8//顺序表初始大小
#define INC_SIZE 5//每次 扩容大小
void menu()
{
printf("*********************************\n");
printf("**********顺序表基本操作*********\n");
printf("**********1,按位置查找***********\n");
printf("**********2,按值查找*************\n");
printf("**********3,插入*****************\n");
printf("**********4,删除*****************\n");
printf("**********5,输出*****************\n");
printf("**********6,合并有序表***********\n");
printf("**********7,清屏*****************\n");
printf("**********8,退出*****************\n");
}
//顺序表框架
typedef int ElemType;//数据类型
typedef struct SqList
{
ElemType* elem;//指向首地址的指针
int length;//数组最大容量
int list_size;//当前元素的数量,也可做当前元素的下标
}*LPSqList, List;
//函数声明
void initList(LPSqList list);
bool inc(LPSqList list);
int findByPos(LPSqList list, int pos);
int findByData(LPSqList list, ElemType data);
void deleteByPos(LPSqList list, int data);
void insertByTail(LPSqList list, ElemType data);
void print(LPSqList list);
bool empty_(LPSqList list);
//初始化顺序表
void initList(LPSqList list)
{
list->length = MAX_SIZE;
list->elem = (LPSqList)malloc(sizeof(List) * list->length);
list->list_size = 0;
if (list->elem == NULL)
{
return;
}
}
//扩容顺序表
bool inc(LPSqList list)
{
ElemType In = realloc(list->elem, sizeof(ElemType) * (list->length + INC_SIZE));
if (In == NULL)
{
return false;
}
list->elem = In;
list->length += INC_SIZE;
return true;
}
//判空
bool empty_(LPSqList list)
{
return list->list_size == 0;
}
//1,按位置查找
int findByPos(LPSqList list, int pos)
{
for (int i = 0; i < list->list_size; i++)
{
if (list->elem[i] == list->elem[pos])
{
return list->elem[i];
}
}
return -1;
}
//2,按值查找
int findByData(LPSqList list, ElemType data)
{
for (int i = 0; i < list->list_size; i++)
{
if (list->elem[i] == data)
{
return i;
}
}
return -1;
}
//3,插入
void insertByTail(LPSqList list, ElemType data)
{
if (list->list_size >= list->length && !inc(list))
{
printf("满了");
return 0;
}
list->elem[list->list_size] = data;//在当前(尾部)位置插入数据;
list->list_size++;//移动到下一个位置
}
//4,删除
void deleteByPos(LPSqList list, int data)
{
if (empty_(list))
{
return;
}
int pos = findByData(list, data);
if (pos != -1)
{
for (int i = pos; i < list->list_size - 1; i++)
{
list->elem[i] = list->elem[i + 1];//从删除的点开始往前移
}
}
list->list_size--;
}
//5,输出
void print(LPSqList list)
{
for (int i = 1; i < list->list_size + 1; i++)
{
printf("第%d个学生的成绩为:%d;\n", i, list->elem[i - 1]);
}
}
//6, 合并有序表
void merge(LPSqList LA, LPSqList LB, LPSqList LC)
{
int i, j, k;
i = 0; j = 0; k = 0;
while (i < LA->list_size && j < LB->list_size)
{
//将小的值赋给LC
if (LA->elem[i] <= LB->elem[j])
{
LC->elem[k] = LA->elem[i];
i++; k++;
}
else
{
LC->elem[k] = LB->elem[j];
j++; k++;
}
}
//当表LA比表LB长时,将表LA剩下的元素赋给表LC
while (i < LA->list_size)
{
LC->elem[k] = LA->elem[i];
i++; k++;
}
//当表LB比表LA长时,将表LB剩下的元素赋给表LC
while (j < LB->list_size)
{
LC->elem[k] = LB->elem[j];
j++; k++;
}
LC->list_size = LA->list_size + LB->list_size;
}
int main()
{
List list, list1, list2;
initList(&list);
initList(&list1);
initList(&list2);
menu();
int select = -1, item = -1, pos = -1;
while (1)
{
printf("请输入你的选择:");
scanf("%d", &select);
switch (select)
{
case 1:
printf("请输入你想查找的位置:");
scanf("%d", &pos);
findByPos(&list, pos);
printf("第%d个位置的值为:%d\n", pos, list.elem[pos]);
break;
case 2:
printf("请输入你想查找的数据:");
scanf("%d", &item);
int ret = findByData(&list, item);
printf("数据%d的位置为%d\n:", ret + 1, item);
break;
case 3:
printf("请输入你想插入的数据:");
scanf("%d", &item);
insertByTail(&list, item);
break;
case 4:
printf("请输入你想删除的数据:");
scanf("%d", &item);
deleteByPos(&list, item);
break;
case 5:
print(&list);
break;
case 6:
printf("请向list1中插入数据:");
scanf("%d", &item);
insertByTail(&list1, item);
merge(&list, &list1, &list2);
printf("合并的顺序表数据为:\n");
print(&list2);
break;
case 7:
system("cls");
menu();
break;
case 8:
exit(0);
}
}
return 0;
}
顺序表基本操作--学生成绩管理系统2.0
最新推荐文章于 2023-11-05 19:23:58 发布