顺序表的实现
概念以及结构
顺序表是用一段物理地址的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表可以分为:1.静态顺序表:使用定长数组存储
2.动态顺序表:使用动态开辟的数组存储
增删查改等接口函数
void SeqListInit(SL*ps);//初始化
void SeqListPushBack(SL*ps,SQDataType x);//尾插
void SeqListFront(SL*ps,SQDataType x);//头插
void SeqListPopBack(SL*ps);//尾删
void SeqListPOpFront(SL*ps);//头删
代码实现
seqlist.h
#pragma once
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <assert.h>
typedef int SQDATEType;
typedef struct SeqList
{
SQDATEType* a;
int size;
int capacity;
}SL;
void SeqListInit(SL *s);//初始化
void SeqListPushBack(SL* s, SQDATEType x);//尾插
void SeqListPushFront(SL* s, SQDATEType x);//头插
void SeqListPrint(SL* s);//打印
void SeqListCheck(SL* s);//检查容量
void SeqListPopBack(SL* s);//尾删
void SeqListPopFront(SL* s);//头删
void SeqListInsert(SL* s, int pos, SQDATEType x);//中间插入
void SeqListErase(SL* s, int pos);//中间删除
void SeqListDestory(SL*s);
int SeqListFind(SL* s, SQDATEType x);
void SeqListModity(SL* s, int pos, SQDATEType x);
seqlist.c
#include"Seqlist.h"
静态
//void SeqListInit(SL *s)
//{
// memset(s->a, 0, sizeof(SQDATEType) * MAX_SIZE);
// s->size = 0;
//}
//
//动态
void SeqListInit(SL* s)
{
s->a = NULL;
s->size = 0;
s->capacity = 0;
}
void SeqListPushBack(SL* s, SQDATEType x)
{
SeqListCheck(s);
s->a[s->size] = x;
s->size++;
}
void SeqListPrint(SL* s)
{
for (int i = 0;i < s->size;i++)
{
printf("%d ", s->a[i]);
}
}
void SeqListCheck(SL* s)
{
if (s->capacity == s->size)//扩容
{
int newcapacity = s->capacity == 0 ? 4 : s->capacity * 2;
SQDATEType* p = (SQDATEType*)realloc(s->a, sizeof(SQDATEType) * newcapacity);
if (p == NULL)
{
printf("出错!");
return;
}
s->a = p;
s->capacity = newcapacity;
}
}
void SeqListPushFront(SL* s, SQDATEType x)
{
SeqListCheck(s);
int end = s->size - 1;
while (end>=0)
{
s->a[end + 1] = s->a[end];
--end;
}
s->a[0] = x;
s->size++;
}
void SeqListPopBack(SL* s)
{
assert(s->size > 0);
s->size--;
}
void SeqListPopFront(SL* s)
{
assert(s->size > 0);
int start = 1;
while (start < s->size)
{
s->a[start - 1] = s->a[start];
start++;
}
s->size--;
}
void SeqListInsert(SL* s,int pos, SQDATEType x)
{
SeqListCheck(s);
int end = s->size - 1;//固定
while (end >=pos)
{
s->a[end + 1] = s->a[end];
end--;
}
s->a[pos] = x;
s->size++;
}
void SeqListErase(SL* s, int pos)
{
assert(s->size > 0);
int start = pos + 1;
while (start < s->size)//下标为size-1;
{
s->a[start - 1] = s->a[start];
start++;
}
s->size--;
}
void SeqListDestory(SL*s)
{
free(s->a);
s->a = NULL;
s->size = 0;
s->capacity = 0;
}
int SeqListFind(SL* s, SQDATEType x)
{
for (int i = 0;i < s->size;i++)
{
if (s->a[i] == x)
{
return i;
}
}
return -1;
}
void SeqListModity(SL* s, int pos, SQDATEType x)
{
assert(pos < s->size);
s->a[pos] = x;
}
test.c
#include"Seqlist.h"
void TestSqList1()//相当于第一个顺序表
{
SL s1;
SeqListInit(&s1);//初始化
SeqListPushBack(&s1, 1);
SeqListPushBack(&s1, 2);
SeqListPushBack(&s1, 3);
SeqListPushBack(&s1, 4);
SeqListPushBack(&s1, 5);
SeqListPushBack(&s1, 5);
SeqListPushFront(&s1, 8);
SeqListPushFront(&s1, 7);
SeqListPushFront(&s1, 6);
SeqListPopFront(&s1);
SeqListPopBack(&s1);
SeqListPrint(&s1);
printf("\n");
SeqListErase(&s1,2);
SeqListPrint(&s1);
printf("\n");
SeqListInsert(&s1, 5, 6);
SeqListPrint(&s1);
printf("\n");
printf("%d", SeqListFind(&s1, 6));
}
void menu()
{
printf("***********************************************\n");
printf("*******1.尾插 2.头插 **************************\n");
printf("*******3.尾删 4.头删 **************************\n");
printf("*******5.中插 6.中删 **************************\n");
printf("*******7.打印 8.退出 **************************\n");
printf("******* 请选择功能 *****************************\n");
}
int main()
{
SL p;
SeqListInit(&p);
int s = 0;
while (s!=8)
{
menu();
int x = 0;
int pos = 0;
scanf_s("%d", &s);
switch (s)
{
case 1:
printf("请输入你要尾插入的数据,以-1结束\n");
while(x!=-1)
{
scanf_s("%d", &x);
if(x!=-1)
SeqListPushBack(&p, x);
}
break;
case 2:
printf("请输入你要头插入的数据,以-1结束\n");
do {
scanf_s("%d", &x);
if(x!=-1)
SeqListPushFront(&p, x);
} while (x != -1);
break;
case 3:
SeqListPopBack(&p);
break;
case 4:
SeqListPopFront(&p);
break;
case 5:
printf("请分别输入插入的位置以及数字:\n");
scanf_s("%d%d", &pos, &x);
SeqListInsert(&p, pos, x);
break;
case 6:
printf("请输入要删除的位置:\n");
scanf_s("%d", &pos);
SeqListErase(&p, pos);
break;
case 7:
SeqListPrint(&p);
break;
case 8:
break;
default:
printf("选择错误,重新选择!:\n");
break;
}
}
return 0;
}
通讯录的底层其实就是顺序表