`
Dev|il
  • 浏览: 122152 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

顺序表的增删改查

 
阅读更多
//功能:顺序表的实现
//思路:初始化一个顺序表,如果n > INIT_LIST_SIZE 则重新分配内存空间
//反之,将元素存入顺序表
#include <stdio.h>
#include <malloc.h>

#define INIT_LIST_SIZE 2
#define ERROR 0
#define OK 1
#define LISTINCREMENT 10
#define SIZE sizeof(int)
typedef int ElemType;
typedef int Status;

//顺序表定义
typedef struct{
   int length;
   int listsize;
   ElemType *elem;
}sqList;

//初始化顺序表
sqList initSqList()
{
	sqList L;
	L.elem = (ElemType *)malloc(sizeof(ElemType) * INIT_LIST_SIZE);
	if(!L.elem)
	{
		printf("顺序表初始化失败!");
	}
	L.length = 0;
	L.listsize = INIT_LIST_SIZE;
	return L;
}
//创建顺序表
sqList createSqList(sqList L)
{
	int i =0, e;
	int *newBase;
	printf("请输入顺序表元素(以Ctrl + Z结束):");
	while(scanf("%d", &e) != EOF)
	{
		if(i >= L.listsize)
		{
			newBase = (ElemType *)realloc(L.elem, SIZE * (L.listsize + LISTINCREMENT));
			L.elem = newBase;
			L.listsize += LISTINCREMENT;
		}
		L.elem[i++] = e;
	}
	L.length = i;
	printf("输入的顺序表元素为:");
	for(i = 0; i < L.length; i++)
	{
		printf("%d ", L.elem[i]);
	}
	printf("\n");
	return L;
}
//插入顺序表的值:
sqList InsertValue(sqList L, int location, ElemType e)
{
	ElemType *newBase;
	//当顺序表已存满时
	if(L.length >= L.listsize)
	{
		newBase = (ElemType *)realloc(L.elem, SIZE * (L.listsize + LISTINCREMENT));
		L.elem = newBase;
		L.listsize += LISTINCREMENT;
	}
	ElemType * q = &L.elem[location - 1];
	ElemType *p = &L.elem[L.length];
	for(; p > q; p--)
	{
		*p = *(p - 1);
	}
	*q = e;
	++L.length;
	return L;
}
//按位置查找值
ElemType GetElem(sqList L, int location)
{
	ElemType *e;
	e = L.elem + location - 1;
	return *e;
}
//定位元素,返回第一个匹配的位置
int LocateElem(sqList L, ElemType e)
{
	for(int i = 0; i < L.length; i++)
	{
		if(L.elem[i] == e)
			return i+1;
	}
	return 0;
}
//删除顺序表的元素,按位置删除
sqList deleteElemLocation(sqList L, int location)
{
	ElemType *p = &L.elem[location - 1];
	for(; p < &L.elem[L.length - 1]; p++)
	{
		*p = *(p + 1);
	}
	L.length--;
	return L;
}
int main()
{
	int len;
	sqList L = initSqList();
	L = createSqList(L);
	//插入值
	printf("请输入插入值的位置:");
	int location;
	int element;
	scanf("%d", &location);
	while(location < 1 || location > L.length)
	{
		printf("输入位置有误,请重新输入!\n");
		scanf("%d", &location);
	}
	printf("请输入插入的元素:");
	scanf("%d", &element);
	L = InsertValue(L, location, element);
	printf("插入元素后的顺序表为:\n");
	for(int i = 0; i < L.length; i++)
	{
		printf("%d ", L.elem[i]);
	}
	printf("\n");

	//查找元素
	printf("请输入查找的位置:");
	scanf("%d", &location);
	while(location < 1 || location > L.length)
	{
		printf("输入位置有误,请重新输入!\n");
		scanf("%d", &location);
	}
	printf("第%d个元素为%d\n", location, GetElem(L, location));
	printf("请输入查找的元素值:");
	scanf("%d", &element);
	if(!LocateElem(L, element))
	{
		printf("不存在此元素!");
	}else
	{
		printf("元素%d在顺序表第%d个位置!\n", element, LocateElem(L, element));
	}
	//删除元素
	printf("请输入删除元素的位置:");
	scanf("%d", &location);
	while(location < 1 || location > L.length)
	{
		printf("输入位置有误,请重新输入!\n");
		scanf("%d", &location);
	}
	L = deleteElemLocation(L, location);
	printf("删除后的顺序表元素为:\n");
	for(i = 0; i < L.length; i++)
	{
		printf("%d ", L.elem[i]);
	}
	printf("\n一共有%d个元素\n", L.length);
	return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics