关于一个简单的顺序表代码

1.首先是头文件SeqList.h的代码:

cs 复制代码
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int SXBint;
typedef struct SL
{
	SXBint* a;
	int size;
	int capacity;
}SLnode;
//初始化
void SeqLsitInit(SLnode* ps);
//尾插
void SeqPushback(SLnode* ps, SXBint x);
//头插
void SeqPushFront(SLnode* ps, SXBint x);
//打印
void Seq_dayin(SLnode ps);
//尾删
void SeqListPopbank(SLnode* ps);
//头删
void SeqListPopFront(SLnode* ps);
//指定插入
void SeqlList_charu(SLnode* ps, int pos, SXBint x);
//指定删除
void SeqList_shan(SLnode* ps, int pos);
//销毁
void SeqList_xiaohui(SLnode* ps);
//查找
int SeqList_chazhao(SLnode* ps, SXBint x);
//修改
void SeqList_xiugai(SLnode* ps, int pos, SXBint x);

2.源文件SeqList.c的实现方法函数的代码

cs 复制代码
#include"SeqList.h"
//初始化
void SeqLsitInit(SLnode* ps)
{
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
//申请空间
void kuorong(SLnode* ps)
{
	if (ps->capacity == ps->size)
	{
		int Newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SXBint* temp = (SXBint*)realloc(ps->a, sizeof(SLnode) * Newcapacity);
		if (temp == NULL)
		{
			perror("error:");
			exit(1);
		}
		ps->a = temp;
		ps->capacity = Newcapacity;
	}
}
//尾插
void SeqPushback(SLnode* ps, SXBint x)
{
	assert(ps);
	kuorong(ps);
	ps->a[ps->size++] = x;
}
//打印数据
void Seq_dayin(SLnode ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d->", ps.a[i]);
	}
	printf("NULL\n");
}
//头插
void SeqPushFront(SLnode* ps, SXBint x)
{
	assert(ps);
	kuorong(ps);
	for (int i = ps->size; i >= 0; i--)
	{
		ps->a[i+1] = ps->a[i];
	}
	ps->a[0] = x;
	ps->size++;
}
//尾删
void SeqListPopbank(SLnode* ps)
{
	assert(ps);
	ps->size--;
}
//头删
void SeqListPopFront(SLnode* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i+1];
	}
	ps->size--;
}
//指定插入
void SeqlList_charu(SLnode* ps, int pos, SXBint x)
{
	assert(ps);
	assert(pos < ps->size);
	kuorong(ps);
	for (int i = ps->size; i >= pos; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[pos] = x;
	ps->size++;
}
//指定删除
void SeqList_shan(SLnode* ps, int pos)
{
	assert(pos < ps->size);
	int start = pos + 1;
	while (start < ps->size)
	{
		ps->a[start - 1] = ps->a[start];
		++start;
	} 
	ps->size--;
} 
//销毁
void SeqList_xiaohui(SLnode* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
//查找
int SeqList_chazhao(SLnode* ps, SXBint x)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}
//修改
void SeqList_xiugai(SLnode* ps, int pos, SXBint x)
{
	assert(pos < ps->size);
	assert(ps);
	ps->a[pos] = x;
}

3.测试的源文件test.c

cs 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void menu()
{

	printf("\n*********************\n");
	printf("1.尾插数据 2.头插入数据\n");
	printf("3.尾删数据 4.头删数据\n");
	printf("5.打印数据 6.修改数据\n");
	printf("7.指定插入 8.指定删除\n ");
	printf("9.查找     10.退出   \n");
	printf("*******************\n");
	printf("\n请输入你的操作\n");
}
int main()
{
	SLnode S;
	SeqLsitInit(&S);
	int x = 0, pos;
	int input = 0;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("请输入x的值,输入-1结束尾插数据\n");
			do
			{
				scanf("%d", &x);
				if (x!=-1)
				{
					SeqPushback(&S, x);
				}
			} while (x!=-1);
			break;
		case 2:
			printf("请输入x的值,输入-1结束头插\n");
			do
			{
				scanf("%d", &x);
				if (x != -1)
				{
					SeqPushFront(&S, x);
				}
			} while (x != -1);
			break;
		case 3:
			SeqListPopbank(&S);
			printf("尾删成功\n");
			break;
		case 4:
			SeqListPopFront(&S);
			printf("头删成功\n");
			break;
		case 5:
			Seq_dayin(S);
			break;
		case 6:
			printf("请输入你要修改的数据位置\n");
			scanf("%d", &pos);
			printf("请输入你要修改的数\n");
			scanf("%d", &x);
			SeqList_xiugai(&S, pos, x);
			printf("修改成功\n");
			break;
		case 7:
			printf("请输入你要指定插入的位置\n");
			scanf("%d", &pos);
			printf("请输入你要插入的数\n");
			scanf("%d", &x);
			SeqlList_charu(&S, pos, x);
			printf("插入成功\n");
			break;
		case 8:
			printf("请输入你要删除的位置下标\n");
			scanf("%d", &x);
			SeqList_shan(&S, x);
			break;
		case 9:
			printf("请输入你要查找的数\n");
			scanf("%d", &x);
			int ret = SeqList_chazhao(&S, x);
			if (ret != -1)
			{
				printf("找到了,下标为%d", ret);
			}
			else {
				printf("找不到\n");
			}
			break;
		case 10:
			input = -1;
			printf("退出中....");
			break;
		default:
			printf("请输入有效数字\n");
			break;
		}
	} while (input != -1);
	return 0;
}
相关推荐
minji...3 小时前
数据结构 堆(4)---TOP-K问题
java·数据结构·算法
落羽的落羽4 小时前
【C++】论如何封装红黑树模拟实现set和map
数据结构·c++·学习
一百天成为python专家5 小时前
K-近邻算法
数据结构·python·算法·pandas·近邻算法·ipython·python3.11
小新学习屋5 小时前
《剑指offer》-数据结构篇-哈希表/数组/矩阵/字符串
数据结构·leetcode·哈希表
爱装代码的小瓶子16 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
aramae19 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
cccc来财20 小时前
Java实现大根堆与小根堆详解
数据结构·算法·leetcode
刚入坑的新人编程1 天前
暑期算法训练.9
数据结构·c++·算法·leetcode·面试·排序算法
找不到、了1 天前
Java排序算法之<选择排序>
数据结构·算法·排序算法
小徐不徐说1 天前
动态规划:从入门到精通
数据结构·c++·算法·leetcode·动态规划·代理模式