#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSIZE 100
typedef int ElemType;
typedef struct
{
ElemType* Elem;
int length;
}SqList;
Status InitList(SqList &L)
{
L.Elem = new ElemType[MAXSIZE];
L.length = 0;
return OK;
}
Status CreateList(SqList& L) {
ElemType e;
int i = 0;
L.Elem = new ElemType[MAXSIZE];
if (!L.Elem)
exit(OVERFLOW);
L.length = 0;
printf("please input the end with 0\n");
for (i = 0;; i++)
{
scanf("%d", &L.Elem[i]);
L.length++;
if (L.Elem[i] == 0)
break;
}
return OK;
}
Status GetElem(SqList& L, int i,ElemType& e)
{
if (i<1 || i>L.length)
return ERROR;
e = L.Elem[i - 1];
return OK;
}
Status LocateElem(SqList& L, ElemType& e)
{
for (int i = 0;i<L.length; i++)
{
if (L.Elem[i] == e)
return i + 1;
}
}
Status ListInsert(SqList& L, int i, ElemType& e)
{
if (i<1 || i>L.length)
return ERROR;
if (L.length == MAXSIZE)
return ERROR;
for (int j = i-1; j <L.length-1; j++)
{
L.Elem[j + 1] = L.Elem[j];
}
L.Elem[i - 1] = e;
++L.length;
return OK;
}
Status Listdelete(SqList& L, int i, ElemType e)
{
if (i<1 || i>L.length)
return ERROR;
for (int j = i; j < L.length; j++)
{
L.Elem[j] = L.Elem[j++];
}
return OK;
}
Status PrintList(SqList& L)
{
for (int i = 0; i < L.length; i++)
{
printf("%d ", L.Elem[i]);
}
return OK;
}
int main()
{
int i;
int choose;
int place;
ElemType e;
SqList L;
printf("InitList\n");
printf("CreateList\n");
printf("GetElement\n");
printf("LocateElement\n");
printf("ListInsert\n");
printf("Listdelete\n");
printf("PrintList\n");
printf("Out\n");
choose = -1;
while (choose != 0)
{
printf("plaese input the choose\n");
scanf("%d", &choose);
switch (choose)
{
case 1:
InitList(L);
if (InitList(L))
{
printf("InitList success\n");
}
else
{
printf("InitList fail\n");
}
break;
case 2:
{
CreateList(L);
}
break;
case 3:
printf("please input a place\n");
scanf("%d", &place);
GetElem(L, place, e);
printf("%d\n", GetElem(L, place, e));
if (e != 0)
{
printf("Get Element success\n");
printf("the %d Element is %d\n", place, e);
}
else
{
printf("Get Element fail");
}
break;
case 4:
printf("please input the Element\n");
scanf("%d", &e);
LocateElem(L, e);
if (e != 0)
{
printf("find success\n");
printf("This Element Location at %d\n", e);
}
else
{
printf("find fail");
}
break;
case 5:
printf("please input the place\n");
scanf("%d", &place);
printf("please input the element\n");
scanf("%d", &e);
ListInsert(L, place, e);
if (e != 0)
{
printf("Insert success\n");
for (int j = 0; j < L.length; j++)
{
printf("%d ", L.Elem[j]);
}
printf("\n");
}
else
{
printf("Insert fail\n");
}
break;
case 6:
printf("please input the place\n");
scanf("%d", &place);
Listdelete(L, place, e);
if (L.Elem != 0)
{
printf("Delete success\n");
for (int j = 0; j < L.length; j++)
{
printf("%d ", L.Elem[j]);
}
printf("\n");
}
else
{
printf("Delete fail\n");
}
break;
case 7:
printf("print this List\n");
PrintList(L);
for (int i = 0; i < L.length; i++)
{
printf("%d ", L.Elem[i]);
}
break;
}
}
return 0;
}
单链表的相关功能的实现
C++与链表相关知识
于 2024-09-30 10:04:36 首次发布
229

被折叠的 条评论
为什么被折叠?



