有关顺序表的四种基础操作,头删尾删头插尾插

关于顺序表的增删查改,头删尾删头插尾插

实现顺序表的四种基础操作

//这个是头文件  SeqList.h
#include<assert.h>
#include<string.h>
#include<assert.h>
#include<windows.h>
#pragma warning(disable:4996)
typedef int hh;
typedef int Datatype;
//建立一个顺序表
typedef struct Seqlist
{
	Datatype* a;
	int size;//有效数据的个数
	int capecity;//有效数据的个数
}SL;

void SeqListInit();// 顺序表初始化

void SeqListPrint(SL* p);//顺序表打印

void SeqListPushBack(SL* psl, Datatype x);//顺序表尾插

void SeqListPopBack(SL* psl);// 顺序表尾删

void SeqListPushFront(SL* psl, Datatype x);//顺序表头插

void SeqListPopFront(SL* psl);// 顺序表头删


//这个是装函数的源文件   SeqList.c
#include"Seqlist.h"
#define N 4

void SeqListInit(SL* p)// 顺序表初始化
{
	p->a = (Datatype*)malloc(sizeof(Datatype)* N);
	if ((p->a == NULL))
	{

		printf("this pragram is error!");
		exit(-1);
	}
	p->size = 0;
	p->capecity = N;
}

void SeqListPushBack(SL* p, Datatype x)//顺序表尾插
{
	assert(p);//判断这个p是不是空指针,如果是的话直接结束
	//如果满了需要增容
	if (p->size >= p->capecity)
	{
		p->capecity *= 2;
		p->a = (Datatype*)realloc(p->a, sizeof(Datatype)*p->capecity);
	}
 	p->a[p->size] = x;
	p->size++;
}

void SeqListPrint(SL* p)//顺序表打印
{
	int end = p->size - 1;
	for (int i = 0; i <= end; i++)
		printf(" %d", p->a[i]);
	printf("\n");
}
void SeqListPopBack(SL* p)// 顺序表尾删
{
	assert(p);
	p->size--;
}

void SeqListPushFront(SL* p, Datatype x)//顺序表头插
{
	assert(p);
	int end = p->size-1;
	for (int i = end; i >=0; i--)
	{
		p->a[i+1] = p->a[i];
	}
	p->a[0] = x;
	p->size++;
}

void SeqListPopFront(SL* p)// 顺序表头删
{
	assert(p);
	int end = p->size - 1;
	for (int i = 1; i <= end;i++)
	{
		p->a[i-1] = p->a[i];
	}
	p->size--;

}




//输出都在这里了 text.c
#include"Seqlist.h"

void SeqListText1()//顺序表尾插的测试
{
	SL p;
	SeqListInit(&p);
	SeqListPushBack(&p, 1); 
	SeqListPushBack(&p, 2);
	SeqListPopBack(&p);
	SeqListPushFront(&p, 4);
	SeqListPopFront(&p);
	SeqListPrint(&p);
}
int main()
{
	SeqListText1();
	system("pause");
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值