单链表的相关功能的实现

#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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值