C语言实现顺序表的基本操作(创建,增,删,改,查)

C语言实现顺序表的基本操作

#include <stdio.h>
#include <stdlib.h>
#define SIZE 2
typedef struct Array
{
	int count;
	int isize;
	int *p;
}List;
List *initArray();//初始化并置空
int creatList(List *list);//创建顺序表(非空)
void insert(List *list);//在顺序表中插入元素
void dele(List *list);//删除顺序表中的元素
void look(List *list);//查找顺序表中的元素
void print(List *list);//打印顺序表中的元素
void destroy(List *list);//销毁顺序表
int get(List *list,int i);//取元素
void inserte(List *list,int i,int b);//在第i个元素前面插入元素
void sort(List *list);//将顺序表中元素按非递减排序
void merge(List *La,List *Lb,List *Lc);//合并两个顺序表
List *initArray()//初始化并置空的实现
{
	List *list;
	list=(List *)malloc(sizeof(List));
	if(!list)
	{
		printf("\t\t\t\t\t划分空间失败!");
		return 0;
	}
	list->p=malloc(SIZE*sizeof(int));
	if(list->p!=NULL)
	{
		list->count=0;
		list->isize=SIZE;
	}
	else
	{
		printf("\t\t\t\t\t划分空间失败!\n");
		return 0;
	}
	return list;
}
int creatList(List *list)//创建顺序表的实现
{
	int num,i;
	printf("\t\t\t\t\t请输入顺序表中元素的个数:");
	scanf("%d",&num);
	printf("\t\t\t\t\t请输入%d个数:",num);
	if(num>SIZE)
	{
	list->p=realloc(list->p,num*sizeof(int));
	list->isize=num;
	}
	for(i=0;i<num;i++)
		scanf("%d",&list->p[i]);
	list->count=num;
	return 1;
}
void insert(List *list)//在顺序表中插入元素的实现
{
	int elem;
	if(list->count<list->isize)
	{
		printf("\t\t\t\t\t请输入要插入的元素(整型):");
		scanf("%d",&elem);
		list->p[list->count++]=elem;
		printf("\t\t\t\t\t插入成功!");
	}
	else
	{
		list->p=realloc(list->p,list->count*4+4);
		if(list->p!=NULL)
		{
		printf("\t\t\t\t\t请输入要插入的元素(整型):");
		scanf("%d",&elem);
		list->p[list->count++]=elem;
		printf("\t\t\t\t\t插入成功!");
		}
		else
		{
			printf("\t\t\t\t\t数组扩建失败\n");
		}
	}
}
void dele(List *list)//删除顺序表中的某一元素的实现
{
	int elem,i,j,b=0;
	printf("\n\t\t\t\t\t请输入要删除的元素:");
	scanf("%d",&elem);
	for(i=0;i<list->count;i++)
	{
		if(list->p[i]==elem)
		{
			if(i==list->count-1)
				list->count--;
			else
			{
				for(j=i;j<list->count-1;j++)
				list->p[j]=list->p[j+1];
				list->count--;
			}
			b++;
			printf("\t\t\t\t\t删除成功!\n");
			break;
		}
	}
	if(b==0)
	printf("\t\t\t\t\t未找到该元素,删除失败!\n");
}
void look(List *list)//查找表中某一元素的实现
{
	int elem,i,a=0;
	printf("\t\t\t\t\t请输入要查找的元素(整型):");
	scanf("%d",&elem);
	for(i=0;i<list->count;i++)
	{
		if(list->p[i]==elem)
		{
			a++;
			printf("\t\t\t\t\t找到该元素,第一次出现的位置(下标):%d\n",i);
			break;
		}
	}
	if(a==0)
		printf("\t\t\t\t\t未找到元素%d\n",elem);
}
void print(List *list)//输出顺序表中元素的实现
{
	int i;
	printf("\t\t\t\t\t数组中的各元素如下:\n");
	printf("\t\t\t\t\t");
	for(i=0;i<list->count;i++)
		printf("%d ",list->p[i]);
	printf("\n");
}
void destroy(List *list)//销毁顺序表的实现
{
	free(list->p);
}



int get(List *list,int i)//在顺序表中取元素
{
	if(0<i&&i<=list->count)
	return list->p[i-1];
	return 0;
}
void inserte(List *list,int i,int b)//在第i个位置前面插入数
{
	int *newlist,*p1,*p2;
	if(i<1||i>list->count)
	{
		printf("\t\t\t\t\ti值有误!");
		exit(0);
	}
	if(list->count>=list->isize)
	{
		newlist=realloc(list->p,(list->count)*sizeof(int));
		if(!newlist)
			exit(0);
		list->p=newlist;
	//	list->isize+=10;
	}
	p1=&(list->p[i-1]);
	p2=&(list->p[list->count-1]);
	for(;p2>=p1;p2--)
		*(p2+i)=*p2;
	*p1=b;
	//list->count++;
}
void sort(List *list)//冒泡排序
{
	int i,a;
	for(i=0;i<list->count-1;i++)
	{
		if(list->p[i]>list->p[i+1])
		{
			a=list->p[i];
			list->p[i]=list->p[i+1];
			list->p[i+1]=a;
		}
	}
}
void merge(List *La,List *Lb,List *Lc)//合并两个顺序表
{
	int ia=1,jb=1,k=0,a,b;
	while(ia<=La->count&&jb<=Lb->count)
	{
		a=get(La,ia);
		b=get(Lb,jb);
		if(a<=b)
		{
			inserte(Lc,k+1,a);
			k++;
			ia++;
		}
		else
		{
			inserte(Lc,k+1,get(Lb,jb));
			k++;
			jb++;
		}
	}
	while(ia<=La->count)
	{
		inserte(Lc,k+1,get(La,ia));
		k++;
		ia++;
	}
	while(jb<=Lb->count)
	{
		inserte(Lc,k+1,get(Lb,jb));
		k++;
		jb++;
	}
}
void menu()//菜单
{
	List *list,*La,*Lb,*Lc;
	int size=10,ia=1,jb=1,k=0;
	int choice;
	printf("\t\t\t\t         菜单选项         ");
	printf("\n\t\t\t\t\t**************************");
	printf("\n\t\t\t\t\t*输入1:查看第一题");
	printf("\n\t\t\t\t\t*输入2:查看第二题");
	printf("\n\t\t\t\t\t*输入0:退出菜单!");
	printf("\n\t\t\t\t\t**************************");
	printf("\n\t\t\t\t\t请输入你的选项:");
	scanf("%d",&choice);
	while(choice>0&&choice<=2)
	{
	switch(choice)
	{
	case 1:printf("\t\t\t\t\t第一题:\n");
		list=initArray();
	if(!list)
	{
		printf("\t\t\t\t\t初始化失败\n");
		exit(0);
	}
	if(!creatList(list))
	{
		printf("\t\t\t\t\t创建顺序表失败\n");
		exit(0);
	}
	insert(list);
	dele(list);
	look(list);
	print(list);
	printf("\n");
	destroy(list);break;
	case 2:printf("\n\n\t\t\t\t\t第二题:\n");
	Lc=initArray();
	La=initArray();
	Lb=initArray();
	La->p=malloc(SIZE*sizeof(int));
	Lb->p=malloc(SIZE*sizeof(int));
	Lc->p=malloc(SIZE*sizeof(int));
	if(!La||!Lb||!Lc)
	{
		printf("初始化失败\n");
		exit(0);
	}
	if(!creatList(La)||!creatList(Lb))
	{
		printf("创建顺序表失败\n");
		exit(0);
	}
	if(Lc->isize<(La->count+Lb->count))
		Lc->p=realloc(Lc->p,(La->count+Lb->count)*sizeof(int));
	Lc->count=(La->count+Lb->count);
	Lc->isize=La->count+Lb->count;
	sort(La);
	sort(Lb);
	merge(La,Lb,Lc);
	print(Lc);break;
	case 0:printf("\t\t\t\t\t感谢你使用该菜单系统,已退出!");break;
	}
	printf("\n\t\t\t\t\t请输入你的选项:");
	scanf("%d",&choice);
	}
}
int main()
{
	menu();
	return 0;
}
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页