线性表结构之顺序表的基本操作

内容包括顺序表的初始化,创建,增加元素,删除元素,输出等

  • 顺序表的定义:
typedef struct Sqlist
{
	ElemType *elem_array;//表中元素(指针数组)
	int length;//当前表所用长度
	int listsize;	//表的存储数据量
}Sqlist;
  • 顺序表的初始化:
Status Init_Sqlist(Sqlist *L)
{
	L->listsize=MAX_SIZE;
	L->elem_array=(ElemType *)malloc(MAX_SIZE*sizeof(ElemType));
	if(!L->elem_array)
	{
		L->elem_array=NULL;
		return ERROR;
	}
	else
	{
		L->length=0;
		return OK;
	}
}
  • 顺序表的创建:(创建n个元素的顺序表)
Status Create_Sqlist(Sqlist *L,int n)//创建具有n个元素的顺序表
{
	int m;
	L->length=1;
	printf("input:\n");
	for(int i=0;i<n;i++)
	{
		scanf("%d",&m);
		L->elem_array[i]=m;//指针数组的赋值
		L->length++;
	}
	return OK;
}
  • 顺序表的插入:(在创建好的表中的第i个位置插入e这个元素)
Status Insert_Sqlist(Sqlist *L,int i,ElemType e)//在创建好的表中的第i个位置插入e这个元素
{
	int j;
	if(i<0||i>L->length-1)   printf("ERROR");
	else
	{
		for(j=L->length-1;j>=i-1;j--)//判断插入位置
		{
			L->elem_array[j+1]=L->elem_array[j];//将所插位置之后的元素向后移动
		}
		L->elem_array[i-1]=e;//插入元素
		L->length++;//表加长
		return OK;
	}
}
  • 线性表的删除:(在顺序表中删除所处i位置的元素)
Status Delete_Sqlist(Sqlist *L,int i)
{
	int k;
	ElemType x;
	if(L->length==0)
	{
		printf("list is NULL!\n");
		return ERROR;
	}
	if(i<1||i>L->length)   return ERROR;
	else
	{
		x=L->elem_array[i-1];
		for(k=i;k<L->length;k++)
		{
			L->elem_array[k-1]=L->elem_array[k];//将删除元素后的元素向前移动
		}
		L->length--;
		return x;
	}
}
  • 顺序表的输出:
Status Print_Sqlist(Sqlist *L)
{
	for(int j=0;j<L->length;j++)
	{
		printf("E[%d]=%d   ",j,L->elem_array[j]);
	}
	return OK;
}
  • 完整代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
#define OK 1
#define ERROR -1

typedef int ElemType;
typedef int Status;

typedef struct Sqlist
{
	ElemType *elem_array;
	int length;
	int listsize;	
}Sqlist;

Status Init_Sqlist(Sqlist *L)
{
	L->listsize=MAX_SIZE;
	L->elem_array=(ElemType *)malloc(MAX_SIZE*sizeof(ElemType));
	if(!L->elem_array)
	{
		L->elem_array=NULL;
		return ERROR;
	}
	else
	{
		L->length=0;
		return OK;
	}
}

Status Create_Sqlist(Sqlist *L,int n)
{
	int m;
	L->length=1;
	printf("input:\n");
	for(int i=0;i<n;i++)
	{
		scanf("%d",&m);
		L->elem_array[i]=m;
		L->length++;
	}
	return OK;
}

Status Insert_Sqlist(Sqlist *L,int i,ElemType e)
{
	int j;
	if(i<0||i>L->length-1)   printf("ERROR");
	else
	{
		for(j=L->length-1;j>=i-1;j--)
		{
			L->elem_array[j+1]=L->elem_array[j];
		}
		L->elem_array[i-1]=e;
		L->length++;
		return OK;
	}
}

Status Delete_Sqlist(Sqlist *L,int i)
{
	int k;
	ElemType x;
	if(L->length==0)
	{
		printf("list is NULL!\n");
		return ERROR;
	}
	if(i<1||i>L->length)   return ERROR;
	else
	{
		x=L->elem_array[i-1];
		for(k=i;k<L->length;k++)
		{
			L->elem_array[k-1]=L->elem_array[k];
		}
		L->length--;
		return x;
	}
}

Status Print_Sqlist(Sqlist *L)
{
	for(int j=0;j<L->length;j++)
	{
		printf("E[%d]=%d   ",j,L->elem_array[j]);
	}
	return OK;
}

int main()
{
	Sqlist *L;
	int j;
	L=(Sqlist *)malloc(sizeof(Sqlist));
	Init_Sqlist(L);
	Create_Sqlist(L,8);//创建具有8个元素的顺序表
	Print_Sqlist(L);
	Insert_Sqlist(L,5,-2);在第5这个位置插入-2
	printf("\n");
	printf("after insert:\n");
	Print_Sqlist(L);
	Delete_Sqlist(L,4);删除第4这个位置上的元素
	printf("\n");
	printf("after delete:\n");
	Print_Sqlist(L);
	return 0;
}
  • 代码运行结果如下:

猜你喜欢

转载自blog.csdn.net/qq_42735631/article/details/82766455