C language implements the basic functions of sequence table initialization, addition, deletion, query and modification

C language implements the basic functions of sequence table initialization, addition, deletion, query and modification

introduction

definition

Sequence table is a sequential storage structure of linear table. For a table shaped like A_1, A_2....A_n, the size of this table is N; and, we call a table with a size of 0 an empty table.

principle

<1> In fact, the implementation of the sequence table is a simple array implementation, and all operations on the table are realized by using the array.
<2> The first element in the sequence table has only one successor node, the last element has only one predecessor node, and the middle elements have only one predecessor node and one successor node.

Implementation process and results

C language realizes all function operations of sequence tables.

the code

#include<stdio.h>
#include<stdlib.h>

//宏定义
#define MAXSIZE 50
#define ERROR 0

typedef int Elemtype; 

typedef struct 
{
    
    
	Elemtype x[MAXSIZE];
	int length;
}sqlist;

//顺序表的初始化、增删查改以及输出操作函数的声明
void Initlist(sqlist *L);
void Outlist(sqlist *L);
void Insertlist(sqlist *L,int i,Elemtype e);
void Deletelist(sqlist *L,int i);
void Foundlist(sqlist *L,Elemtype e);
void Amendlist(sqlist *L,int i,Elemtype e);

//主函数
void main()
{
    
    
	Elemtype e;
	int i;
	char ch;
	sqlist a;
	int t;
	printf("		《 Welcome to use the founction 》\n");
	printf("	    The founction can offer following service\n");
	printf("		*******Please first Initlist*******\n\n");
	printf("		1、Initlist   ->初始化线性表\n");
	//初始条件:顺序表已存在
	printf("		2、Insertlist ->插入操作\n");
	printf("		3、Deletelist ->删除操作\n");
	printf("		4、Foundlist  ->查找操作\n");
	printf("		5、Amendlist  ->更改操作\n");
	printf("		0、Exitlist   ->退出操作\n\n\n");
	
	do
	{
    
    
		printf("Enter your choice:");
		scanf("%d",&t);

		switch(t)
		{
    
    
		case 1:
			{
    
    
				Initlist(&a);
				Outlist(&a);
			}break;
		case 2:
			{
    
    
				printf("Enter Insert data:");
				scanf("%d",&e);
				printf("Enter the position of Insert:");
				scanf("%d",&i);
				Insertlist(&a,i,e);
				Outlist(&a);
			}break;
		case 3:
			{
    
    
				printf("Enter the position of Delete:");
				scanf("%d",&i);
				Deletelist(&a,i);
				Outlist(&a);
			}break;
		case 4:
			{
    
    
				printf("Enter Found data:");
				scanf("%d",&e);
				Foundlist(&a,e);
			}break;
		case 5:
			{
    
    
				printf("Enter the position of amend:");
				scanf("%d",&i);
				printf("Enter data of amending:");
				scanf("%d",&e);
				Amendlist(&a,i,e);
				Outlist(&a);
			}break;
		case 0:
			{
    
    
				ch = getchar();
			}break;
		}
	}while(t!=0);
	ch = getchar();
}
//初始化操作
void Initlist(sqlist *L)
{
    
    
	int i;
	printf("Enter the length of the List: ");
	scanf("%d",&L->length);
	printf("\n");
	for(i=0;i<L->length;i++)
	{
    
    
		printf("Data %d:",i+1);
		scanf("%d",&L->x[i]);
		printf("\n");
	}
}
//输出操作
void Outlist(sqlist *L)
{
    
    
	int i;
	printf("Output current List:");
	for(i=0;i<L->length;i++)
	{
    
    
		if(i<L->length-1)
		{
    
    
			printf("%d ->",L->x[i]);
		}	
		else
		{
    
    
			printf("%d ",L->x[i]);
		}
	}
	printf("\n");
}
//插入操作
void Insertlist(sqlist *L,int i,Elemtype e)
{
    
    
	int j;
	if(L->length==MAXSIZE)
	{
    
    
		printf("List full!!!!!!\n");
		exit(0);
	}
	if(i<0||i>L->length+1)
	{
    
    
		printf("The position is illegal!!!!!!\n");
		exit(0);
	}
	for(j = L->length-1;j>=i;j--)
	{
    
    
		L->x[j+1]=L->x[j];
	}
	L->x[i]=e;
	L->length++;
}
//删除操作
void Deletelist(sqlist *L,int i)
{
    
    
	int j;
	if(L->length==0)
	{
    
    
		printf("List empty!!!!!!\n");
		exit(0);
	}
	if(i<0||i>L->length+1)
	{
    
    
		printf("The position is illegal!!!!!!\n");
		exit(0);
	}
	for(j = i+1;j<L->length;j++)
	{
    
    
		L->x[j-1]=L->x[j];
	}
	L->length--;
}
//查找操作
void Foundlist(sqlist *L,Elemtype e)
{
    
    
	int j;
	for(j=0;j<L->length;j++)
	{
    
    
		if(L->x[j] == e)
			printf("The element you're looking for is in the %d position\n",j+1);
	}
}
//修改操作
void Amendlist(sqlist *L,int i,Elemtype e)
{
    
    
	if(L->length==0)
	{
    
    
		printf("List empty!!!!!!\n");
		exit(0);
	}
	if(i<0||i>L->length+1)
	{
    
    
		printf("The position is illegal!!!!!!\n");
		exit(0);
	}
	L->x[i] = e;
}

operation result

insert image description here

Summarize

<1> Although the array is dynamically specified, it is still necessary to estimate the maximum size of the table. Estimated and actual deviation, that is, the resulting waste of space.
<2> If the data in the table is too large, the time for inserting and deleting operations is a bit expensive.

The above-mentioned so-called space and time overhead can be avoided by the linked storage structure of the linear table.

Guess you like

Origin blog.csdn.net/MZYYZT/article/details/113106289