数据结构C语言实现顺序表——实现增删查改操作完整代码

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include

typedef int SEQ;

typedef struct SeqLisrt
{
	SEQ* a;
	int size;
	int capaciti;
}SL;


//初始化
void Init(SL* ps)
{
	ps->a = NULL;
	ps->size = 0;
	ps->capaciti = 0;
}

//销毁
void Destory(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capaciti = 0;
}

//打印
void Print(SL* ps)
{
	int i = 0;
	for (i=0;isize;i++)
	{
		printf("%d ",ps->a[i]);
	}
}

//开辟空间
void CheckCapacity(SL* ps)
{
	if (ps->size == ps->capaciti)
	{
		int newcapaciti = ps->capaciti == 0 ? 4 : ps->capaciti * 2;
		SEQ* tmp = (SEQ*)realloc(ps->a, newcapaciti * sizeof(SEQ)*2);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capaciti = newcapaciti;
		}
	}
}

//尾插
void PushBack(SL* ps, SEQ x)
{
	CheckCapacity(ps);
	assert(ps);

	ps->a[ps->size] = x;
	ps->size++;
}

//头插
void PushFront(SL* ps, SEQ x)
{
	CheckCapacity(ps);
	assert(ps);

	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end+1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

//尾删
void PopBack(SL* ps)
{
	CheckCapacity(ps);
	assert(ps->size > 0);

	ps->size--;
}


//头删
void PopFront(SL* ps)
{
	CheckCapacity(ps);
	assert(ps->size > 0);

	int start = 1;
	while (start < ps->size)
	{
		ps->a[start - 1] = ps->a[start];
		start++;
	}
	ps->size--;
}


//任意位置插入
void Insert(SL* ps, int pos, SEQ x)
{
	assert(pos <= ps->size);

	int end = ps->size - 1;
	while (end > +pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}


//任意位置删除
void Erase(SL* ps, int pos)
{
	assert(pos <= ps->size);

	int start = pos;
	while (start < ps->size - 1)
	{
		ps->a[start] = ps->a[start + 1];
		start++;
	}
	ps->size--;
}


//修改
void At(SL* ps, int pos, SEQ x)
{
	assert(pos <= ps->size);

	ps->a[pos] = x;
}


int Find(SL* ps, int pos)
{
	assert(pos <= ps->size);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == pos)
		{
			return i;
		}
	}
	return -1;
}

void menu()
{
	printf("********************************\n");
	printf("1.尾插数据            2.头插数据\n");
	printf("3.尾删数据            4.头删数据\n");
	printf("5.任意插入            6.任意删除\n");
	printf("7.修改数据            8.查找数据\n");
	printf("9.打印数据           -1.退出\n");
	printf("********************************\n");
	printf("请输入你操作的选项>:");
}

int main()
{
	int option = 0;
	int x = 0;
	int i = 0;
	int pos = 0;
	SL s;
	Init(&s);     //使用顺序表前先初始化

	while (option != -1)
	{
		menu();
		scanf("%d", &option);
		switch (option)
		{
		case 1:
			printf("请输入你要尾插的数据,以-1结束\n");
			do
			{
				scanf("%d", &x);
				if (x != -1)
				{
					PushBack(&s, x);
				}
			} while (x != -1);
			break;
		case 2:
			printf("请输入你要头插的数据,以-1结束\n");
			do
			{
				scanf("%d", &x);
				if (x != -1)
				{
					PushFront(&s, x);
				}
			} while (x != -1);
			break;
		case 3:
			printf("请输入你要尾删的元素个数\n");
			scanf("%d", &x);
			for (i = 0; i < x; i++)
			{
				PopBack(&s);
			}
			break;
		case 4:
			printf("请输入你要头删的元素个数\n");
			scanf("%d", &x);
			for (i = 0; i < x; i++)
			{
				PopFront(&s);
			}
			break;
		case 5:
			printf("请输入要插入的位置以及插入的数据\n");
			scanf("%d%d", &pos, &x);
			Insert(&s, pos, x);
			break;
		case 6:
			printf("请输入要删除的位置\n");
			scanf("%d", &pos);
			Erase(&s, pos);
			break;
		case 7:
			printf("请输入位置以及修改的数据\n");
			scanf("%d%d", &pos, &x);
			At(&s, pos, x);
			break;
		case 8:
			printf("请输入需要查找的数据\n");
			scanf("%d", &x);
			int ret = Find(&s, x);
			if (ret != -1)
			{
				printf("找到了下标为:>%d\n", ret);
			}
			else
				printf("找不到\n");
			break;
		case 9:
			Print(&s);
			printf("\n");
			break;
		default:
			break;
		}
	}
	Destory(&s);//使用完后销毁
	return 0;
}

你可能感兴趣的:(顺序表,C语言,数据结构,c语言,ruby)