其他分享
首页 > 其他分享> > C语言顺序表增删查改

C语言顺序表增删查改

作者:互联网

list.h

#pragma once
#include<stdlib.h>
#include <stdio.h>
#include<assert.h>
#include<string.h>
#include<errno.h>
typedef int SLDataType;
#define N 10
//struct SeqList
//{
//	SLDataType a[N];
//	int size;
//
//};
typedef struct SeqList
{
	SLDataType* a;
	int size;
	int capactity;

}SL,SeqList;
void SeqListPushBack(SL* ps, SLDataType x);
void SeqListPophBack(struct SeqList* ps);
void SeqListPushFront(struct SeqList* ps, SLDataType x);
void SeqListPophFront(struct SeqList* ps);
void SeqListInsert(struct SeqList* ps,int pos,SLDataType x);
void SeqListErase(struct SeqList* ps,int pos); 
void SeqListInit(SL* ps);
void testSeqList1();
void SeqListPrint(SL* ps);
void SeqListCheckCapacity(SL* ps);
void SeqListDestroy(SL* ps);
void SeqListFind(SL* ps, SLDataType x);
void SeqListChange(struct SeqList* ps, int pos, SLDataType x);

seqlist.c

#include"list.h"
void SeqListPushBack(SL* ps, SLDataType x);
void SeqListPophBack(struct SeqList* ps);
void SeqListPushFront(struct SeqList* ps, SLDataType x);
void SeqListInsert(struct SeqList* ps, int pos, SLDataType x);
//void SeqListErase(struct SeqList* ps, int pos, SLDataType x);


int main()
{
	//SeqList s;
	testSeqList1();
	return 0;
}

void testSeqList1()
{
	SeqList s;
	SeqListInit(&s);
	SeqListPushBack(&s,1);
	SeqListPushBack(&s, 3);
	SeqListPushBack(&s, 2);
	SeqListPushBack(&s, 1);
	SeqListPushBack(&s, 3);
	SeqListPushBack(&s, 2);
	SeqListPrint(&s);
	SeqListPophBack(&s);
	SeqListPophBack(&s);
	SeqListPushFront(&s, -2);
	SeqListPrint(&s);
	SeqListPophFront(&s);
	SeqListInsert(&s,1, 6);
	SeqListPrint(&s);
	/*SeqListErase(&s, 1, 6);
	SeqListPrint(&s);*/
	SeqListInsert(&s, 2, 6);
	SeqListPrint(&s);
	SeqListErase(&s, 2);
	SeqListPrint(&s);
	SeqListFind(&s, 6);
	SeqListChange(&s, 3, 9);
	SeqListPrint(&s);



	



	SeqListPrint(&s);
	SeqListDestroy(&s);
}
void SeqListInit(SL* ps)
{
	ps->a =(SLDataType*)malloc(sizeof(SLDataType)*4) ;
	if (ps->a==NULL)
	{
		printf("失败");
		exit(-1);
	}
	ps->size = 0;
	ps->capactity=4;
}
void SeqListPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	//if (ps->size>=ps->capactity)
	//{
	//	ps->capactity *= 2;
	//	assert(ps->a);
	//	ps->a = realloc(ps->a, sizeof(SLDataType) * (ps->capactity));
	//	//assert(ps->a);
	//	if (ps->a==NULL)
	//	{
	//		printf("扩容失败");
	//		exit(-1);

	//	}
	//}
	SeqListCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
	//SeqListInsert(ps, ps->size, x);
}
void SeqListPophBack(struct SeqList* ps)
{
	assert(ps);
	ps->a[ps->size - 1] = 0;
	ps->size--;
}
void SeqListPrint(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d", ps->a[i]);
	}
	printf("\n");

}
void SeqListPushFront(struct SeqList* ps, SLDataType x)
{
	assert(ps);
	SeqListCheckCapacity(ps);
	int end = ps->size - 1;
	while (end>=0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}
void SeqListCheckCapacity(SL* ps)
{
	if (ps->size >= ps->capactity)
	{
		ps->capactity *= 2;
		assert(ps->a);
		ps->a = realloc(ps->a, sizeof(SLDataType) * (ps->capactity));
		//assert(ps->a);
		if (ps->a == NULL)
		{
			printf("扩容失败");
			exit(-1);

		}
	}

}
void SeqListPophFront(struct SeqList* ps)
{
	assert(ps);
	int start = 0;
	while (start<ps->size-1)
	{
		ps->a[start] = ps->a[start + 1];
		start++;
	}
	ps->size--;
}
void SeqListInsert(struct SeqList* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos < ps->size&&pos>0);
	SeqListCheckCapacity(ps);
	//int postion = ps->a[pos];
	int end = ps->size - 1;

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

	
}
void SeqListErase(struct SeqList* ps, int pos)
{
	assert(ps);
	assert(pos <= ps->size&& pos> 0);
	int end = ps->size - 1;
	int start = pos;
	//ps->a[pos] = 0;
	while (start<end)
	{
		ps->a[pos]=ps->a[pos+1];
		start++;
	}
	ps->size--;
}
void SeqListFind(SL* ps, SLDataType x)
{
	assert(ps);
	int i = 0;
	for ( i = 0; i < ps->size; i++)
	{
		if (ps->a[i]==x)
		{
			return printf("%d\n",i);
		}
	}
	return -1;
}
void SeqListChange(struct SeqList* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos <= ps->size && pos > 0);
	ps->a[pos] = x;
}
void SeqListDestroy(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capactity = ps->size = 0;
}
//时间复杂度o(N)
//浪费空间
//缓存命中率高 把一段数据加载到缓存
//随机访问

标签:ps,int,void,pos,C语言,查改,增删,SeqList,size
来源: https://blog.csdn.net/m0_55733945/article/details/121890951