关于顺序表的增删查改,头删尾删头插尾插
实现顺序表的四种基础操作
//这个是头文件 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;
}