顺序表_增删改查等基本操作

本代码主要为了创建顺序表,并且完成对顺序表的创建,检索,插入,删除

#include "stdio.h"
#include "stdlib.h"
#define MAXSIZE 100

typedef struct node{

		int data[MAXSIZE];
		int length;

}SeqList,*PSeqList;          //定义一个顺序表

PSeqList Init_SeqList() //顺序表初始化
{
	PSeqList PL=(PSeqList)malloc(sizeof(SeqList));
	if(PL)
	{
	PL->length=0;
	return PL;
	
	}
	printf("create error!");
	return NULL;

}

int isNUllList_seq(PSeqList PL)  //判断是否空线性表,1:是,0:不是
{

	return (PL->length==0);

}

int Location_SeqList (PSeqList PL,int x) //顺序表的查找 若没有,返回0,若有返回序号(从0开始数)
{
	
	int i=0;
	while(i<PL->length&&PL->data[i]!=x)
		i++;
	if(i>=PL->length)
	{ 
		printf("NOT EXIST\n");
		return 0 ;
	}
	else 
	{	
		printf("the number is at %dth\n",i);
		return (i);
	}

}

int Insert_SeqList(PSeqList PL,int i,int x)    //顺序表的插入 其中 i 为第几个位置(从0开始) 
											   //-2:表不存在,-1:溢出,0:插入位置不合法,1插入成功
{
	int j;

	if(!PL)
	{
	printf("the List does not exist\n");
	return -2;
	
	}

	if(PL->length>=MAXSIZE)
	
	{

	printf("out of space\n");
	return -1;

	}

	if(i<0||i>PL->length)
	{
	 
	printf("the location you chose is illegall!\n");
	return 0;
	
	}

	for(j=PL->length-1;j>=i;j--)
		PL->data[j+1]=PL->data[j];
		PL->data[i]=x;
		PL->length++;
		return 1;

}

int Delete_SeqList(PSeqList PL,int i) //顺序表的删除,i为删除的元素所在位置(从0开始),-2:表不存在
									  //0:删除位置不合法,1:成功
{
	int j;

	if(!PL)
	{
	printf("the List does not exist");
	return -2;
	
	}



	if(i<0||i>PL->length)
	{
	 
	printf("the location you chose is illegall!");
	return 0;
	
	}

	for(j=i+1;j<PL->length;j++)
		PL->data[j-1]=PL->data[j];
	PL->length--;
	return 1;
	

}

main()
{
	int i,n,x,l;
    PSeqList list;
	list=Init_SeqList();
	printf("please chose the operation you want\n 0:creat \n 1:location\n 2:insert\n 3:delete \n");
	scanf("%d",&n);
	switch(n)
	{
	case 0: printf("enter 10 numbers:\n");
			for (i=0;i<10;i++)
			{
			list->length++;
			scanf("%d",&list->data[i]);
			printf("%d ",list->data[i]);
			}
			break;

	case 1:printf("enter 10 numbers:\n");
			for (i=0;i<10;i++)
			{
			list->length++;
			scanf("%d",&list->data[i]);
			printf("%d ",list->data[i]);
			}
			printf("\n enter the number you are looking for:\n");
		   scanf("%d",&x);
		   Location_SeqList(list,x);
		   break;
	case 2:printf("enter 10 numbers:\n");
			for (i=0;i<10;i++)
			{
			list->length++;
			scanf("%d",&list->data[i]);
			printf("%d ",list->data[i]);
			}
			printf("\n enter the  location and the number  you want to insert:\n");
			scanf("%d",&l);
			scanf("%d",&x);
			Insert_SeqList(list,l,x);
			for(i=0;i<list->length;i++)
			printf("%d " , list->data[i]);
			printf("\n");
			break;
	case 3:printf("enter 10 numbers:\n");
			for (i=0;i<10;i++)
			{
			list->length++;
			scanf("%d",&list->data[i]);
			printf("%d ",list->data[i]);
			}
			printf("\n enter the  the location of the number  you want to delete:\n");
			scanf("%d",&l);
			Delete_SeqList(list,l);
			for(i=0;i<list->length;i++)
			printf("%d " , list->data[i]);
			printf("\n");
			break;
	default:printf("error\n ");
		    break;
			
	}

}

猜你喜欢

转载自blog.csdn.net/qqGHJ/article/details/82730941