1.首先是头文件SeqList.h的代码:
#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的实现方法函数的代码
#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
#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;
}