#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);
L.length = 0;
L.listsize = 0;
L.elem = NULL;
}
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)
{
ElemType* p;
ElemType* q;
if (i < 1 || (i > L.length))
return ERROR;
p = &(L.elem[i - 1]);
e = *p;
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;
}
q = &(L.elem[i - 1]);
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;
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)
{
if (*pa < *pb)
*pc++ = *pa++;
else if (*pa > * pb)
*pc++ = *pb++;
else {
*pc++ = *pa++;
pb++;
Lc.length--;
}
}
while (pa <= pa_last)
*pc++ = *pa++;
while (pb <= pb_last)
*pc++ = *pb++;
return OK;
}
void main()
{
SqList L;
int choice = 1;
if (InitList_Sq(L) == OK)
printf("success!\n");
else
printf("fail!\n");
while (choice != 0)
{
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");
printf("Input your choice:");
scanf_s("%d", &choice);
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) {
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);
}