#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;//存储空间基址
int length;
int listsize;//当前分配的存储容量
}SqList;
//构造空线性表
Status InitList_Sq(SqList& L)
{
L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (L.elem == 0)exit(OVERFLOW);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
//写入数据
void InputData(SqList& L)
{
int n, i;
printf("input n:");
scanf_s("%d", &n);
for (i = 0; i < n; i++)
{
printf("No.%d:", i + 1);
scanf_s("%d", &L.elem[i]);
}
L.length = n;
}
//遍历
void ListPrint(SqList L)
{
int i;
printf("The list is :");
for (i = 0; i < L.length; i++)
{
printf("%d ", L.elem[i]);
}
printf("\n");
}
//判空
Status ListEmpty(SqList L)
{
return L.length == 0 ? TRUE : FALSE;
}
//输出线性表元素个数
int ListLength(SqList L)
{
return L.length;
}
//清空线性表
Status ClearList(SqList& L)
{
L.length = 0;
return OK;
}
//销毁线性表
void DestroyList(SqList& L)
{
free(L.elem);//非free(L)
L.length = 0;
L.listsize = 0;//存储容量为0
L.elem = NULL;//数据为空
}
//从线性表中获取第i个元素
Status GetElem(SqList L, int i, ElemType* e)
{
if (i<1 || i>L.length)
return ERROR;
*e = L.elem[i - 1];
return OK;
}
//查找线性表中的数据在线性表中第一次出现的位置
int LocateElem_Sq(SqList L, ElemType e)
{
//输入一个数在线性表中找到他的位置
int i;
ElemType* p;
i = 0;
p = L.elem;
while (i <= L.length && *p != e)
{
++i;
p++;
}
if (i <= L.length)return i;
else
return 0;
}
//删除线性表中的数据
Status ListDelete_Sq(SqList& L, int i, ElemType& e)
{//删除第i个元素并返回其值e
ElemType* p;
ElemType* q;
if (i < 1 || (i > L.length))
return ERROR;
p = &(L.elem[i - 1]); //被删除元素的位置
e = *p;//找到需要删除的值传给*e
q = L.elem + L.length - 1;//表尾元素的位置
for (++p; p <= q; ++p)
*(p - 1) = *p; // 被删除之后的元素左移
--L.length; // 表长减一
return OK;
}
//线性表中插入数据
Status ListInsert_Sq(SqList &L, int i, ElemType e)
{
ElemType* q;
ElemType* p;
ElemType* newbase;
if (i<1 || i>L.length + 1)//插入位置不符合条件
return ERROR;
if (L.length >= L.listsize)//当前存储空间已满,需要增加分配
{
newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
if (newbase == 0)exit(OVERFLOW); //分配空间失败
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
// 注意:C语言中数组的下标从0开始,线性表中第i个元素是L.elem[i-1]
q = &(L.elem[i - 1]);//q为插入位置
for (p = &(L.elem[L.length - 1]); p >= q; --p) *(p + 1) = *p;//插入位置之后元素后移
*q = e;
++L.length;
return OK;
}
Status MergeList(SqList La, SqList Lb, SqList& Lc) {
int* pa, * pa_last, * pb, * pb_last, * pc;
pa = La.elem;
pb = Lb.elem;
Lc.listsize = Lc.length = La.length + Lb.length;//不用InitList()创建空表Lc
pc = Lc.elem = (int*)malloc(Lc.listsize * sizeof(int));
if (!Lc.elem) //存储分配失败
exit(0);
pa_last = La.elem + La.length - 1;
pb_last = Lb.elem + Lb.length - 1;
while (pa <= pa_last && pb <= pb_last) //表La和表Lb均非空
{ //归并
if (*pa < *pb)
*pc++ = *pa++;
else if (*pa > * pb)
*pc++ = *pb++;
else {
*pc++ = *pa++;
pb++;
Lc.length--;
}
}
while (pa <= pa_last) //表La非空且表Lb空
*pc++ = *pa++; //插入La的剩余元素
while (pb <= pb_last) //表Lb非空且表La空
*pc++ = *pb++; //插入Lb的剩余元素
return OK;
}
void main()
{
SqList L;
int choice = 1;
if (InitList_Sq(L) == OK)
printf("success!\n");
else
printf("fail!\n");
while (choice != 0)
{
//meau
system("cls");
printf("1.InputDate\t2.ListPrint\t3.ListEmpty\t4.ListLength\t5.ClearList\n");
printf("6.DestroyList\t7.GetElem\t8.LocateElem\t9.ListDelete\t10.ListInsert\t11.MergeList\n");
printf("0.Exit\n");
//input choice
printf("Input your choice:");
scanf_s("%d", &choice);
//action
switch (choice)
{
case 0:
printf("ByeBye!\n");
break;
case 1:
InputData(L);
break;
case 2:
ListPrint(L);
break;
case 3:
if (ListEmpty(L) == TRUE)
printf("The list is empty!\n");
else
printf("The list is not empty!\n");
break;
case 4:
{
int len;
len = ListLength(L);
printf("The length is %d.\n", len);
break;
}
case 5:
if (ClearList(L) == OK)
printf("Clear!\n");
break;
case 6:
DestroyList(L);
break;
case 7:
{
int pos;
ElemType e;
printf("Input the position:");
scanf_s("%d", &pos);
if (GetElem(L, pos, &e) == OK)
printf("The No.%d element is %d.", pos, e);
else
printf("The position is invalid.\n");
break;
}
case 8:
{
ElemType e;
printf("Input the number:");
scanf_s("%d", &e);
int pos = LocateElem_Sq(L, e);
if (pos > 0)
printf("Found! Tts position is %d.\n", pos + 1);
else
printf("Not found!\n");
break;
}
case 9:
{
ElemType e;
int i;
printf("Input the index of list:");
scanf_s("%d", &i);
if ((ListDelete_Sq(L, i, e)) == TRUE)
printf("Have deleted!\n");
else
printf("Have not deleted!\n");
break;
}
case 10:
{
int i;
ElemType e;
printf("Input the position:");
scanf_s("%d", &i);
printf("Input a number:");
scanf_s("%d", &e);
if (ListInsert_Sq(L, i, e) == OK)
printf("Have inserted!\n");
else
printf("Not inserted!\n");
break;
}
case 11:
{
SqList Lb;
SqList Lc;
if (InitList_Sq(Lb) == OK) { //初始化Lb
printf("Lb Init success!\n");
InputData(Lb);
MergeList(L, Lb, Lc);
ListPrint(Lc);
free(Lb.elem);
}
else
printf("Lb Init fail!\n");
break;
}
}
system("pause");
}
DestroyList(L);
}
《数据结构》C语言版 顺序表的基本操作实现
最新推荐文章于 2022-10-10 13:22:37 发布