数据结构:线性表(代码 最详细最全 纯手写 纯C语言)ing


一、顺序表

静态分配

#include "stdio.h"
#define MAXSIZE 30
typedef int Type;
typedef struct 
{
	Type elem[MAXSIZE];
	Type last;
}Lnode,*List;

void InitList(List L){
	L->last=0;
}

Type ListInsert(List L,int i,int e){
	int j;

	if (L->last>30)
		return -1;

	if (i<0||i>L->last+1)
		return -2;

	for (j=L->last; j >= i; --j)
		L->elem[j]=L->elem[j-1];

	L->elem[i-1]=e;	

	L->last++;

	return 1;
}

Type ListPrint(List L){
	int i;

	i=ListEmpty(L);

	if (i==-1){
		printf("the table is empty\n");

		return 1;
	}
	printf("Elements in the table\n");
	for (i = 0; i < L->last; ++i)
		printf("%d->", L->elem[i]);

	printf("\n");

	return 1;
}

Type ListEmpty(List L){
	if (L->last==0)
		return -1;
}

Type ListDelete(List L,int i){
	int temp;
	temp=ListEmpty(L);
	if (temp==-1)
		return -1;

	if (i<0||i>L->last)
		return -2;

	for (; i <L->last ; ++i)
		L->elem[i-1]=L->elem[i];

	L->last--;
}

Type LocateElem(List L,int e){
	int i=0;

	while(i<=L->last&&L->elem[i]!=e)
		i++;

	if (i>L->last)
		return -1;
	else
		return i+1;
}

void ListLenth(List L){
	printf("Table length is%d\n", L->last);
}

Type GetElem(List L,int i){
	int temp;

	temp=ListEmpty(L);

	if (temp==-1)
		return -2;

	if (i<0||i>L->last)
		return -1;

	return L->elem[i-1];
}

Type ClearList(List L){
	if (L->last==0)
		printf("Table was empty\n");
	L->last=0;
}

Type PriorElem(List L,int cur_e){
	int temp;

	temp=ListEmpty(L);

	if (temp==-1)
		return -2;

	if (L->elem[0]==cur_e)
		return -1;

	temp=LocateElem(L,cur_e);

	if (temp==-1)
		return -3;

	return temp-2;
}

Type NextElem(List L,int cur_e){
	int temp;

	temp=ListEmpty(L);

	if (temp==-1)
		return -2;

	if (L->elem[L->last-1]==cur_e)
		return -1;

	temp=LocateElem(L,cur_e);

	if (temp==-1)
		return -3;

	return temp;
}

int main()
{
	int n,value,temp,loc,i=1,k=1;

	Lnode L;

	InitList(&L);
	printf("This code is the static allocation method of sequence table\n");

	printf("Please enter the sequence table you want to create (-1 ends,Space separated)\n");

	scanf("%d",&n);

	while(n!=-1){

		ListInsert(&L,i,n);

		i++;

		scanf("%d",&n);

	}
	while(k!=-1){
		printf("Please select an action\n");

		printf("1.Show all elements\n");

		printf("2.Insert an element\n");

		printf("3.Delete an element\n");

		printf("4.Find an element (by value)\n");

		printf("5.Find an element (by location)\n");

		printf("6.Length of table\n");

		printf("7.Clear table\n");

		printf("8.Find Prev\n");

		printf("9.Find next\n");

		printf("10.exit\n");

		scanf("%d",&n);

		switch(n){

			case 1:ListPrint(&L);

			break;

			case 2:{
				printf("Please enter the location to insert and the element to insert\n");

				printf("Format: position element; Example: 3 10\n");

				scanf("%d %d",&loc,&value);

				temp=ListInsert(&L,loc,value);

				if (temp==-1)
					printf("Insert failed,Table full\n");

				else if(temp==-2)
					printf("Insert failed,Illegal insertion\n");

				else{
					printf("Insert successful\n");

					ListPrint(&L);
				}
				break;
			}
			case 3:{
				printf("Please enter the location to delete\n");

				scanf("%d",&loc);

				temp=ListDelete(&L,loc);

				if (temp==-1)
					printf("Deletion failed, the table is empty\n");

				else if (temp==-2)
					printf("Deletion failed,Illegal deletion\n");

				else{
					printf("Deletion successful\n");

					ListPrint(&L);
				}
				break;
			}
			case 4:{
				printf("Please enter the element to find\n");

				scanf("%d",&value);

				loc=LocateElem(&L,value);

				if (loc==-1)
					printf("Find failed,The %d is not in the table\n",value);
				else{
					printf("Find successful\n");

					printf("The position of the element is:%d\n",loc);
				}
				break;
			}
			case 5:{
				printf("Please enter the location you want to find\n");

				scanf("%d",&loc);

				value=GetElem(&L,loc);

				if (value==-1)
					printf("Find failed,Illegal search\n");

				else if (value==-2)
					printf("Find failed,the table is empty\n");

				else{
					printf("Find successful\n");
					printf("The element of the location to find is %d\n",value);
				}

			break;
			}
			case 6:ListLenth(&L);break;

			case 7:ClearList(&L);break;

			case 8:{	
				printf("The precursor of which element do you want to find\n");

				scanf("%d",&value);

				temp=PriorElem(&L,value);

				if (temp==-1)
					printf("Find failed,The current element has no precursor because it is the first\n");

				else if (temp==-2)
					printf("Find failed,the table is empty\n");

				else if (temp==-3)
					printf("Find failed,The %d is not in the table\n",value);

				else
				printf("%d\n",L.elem[temp]);

				break;
			}
			case 9:{
				printf("Which element do you want to find the successor of\n");

				scanf("%d",&value);

				temp=NextElem(&L,value);

				if (temp==-1)
					printf("Find failed,The current element has no precursor because it is the last\n");

				else if (temp==-2)
					printf("Find failed,the table is empty\n");

				else if (temp==-3)
					printf("Find failed,The %d is not in the table\n",value);

				else
				printf("%d\n",L.elem[temp]);
				break;}

			default :k=-1; break;
		}
	}
	return 0;
}

动态分配


二、链表


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X在学了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值