顺序表
- 顺序表的定义和初始化
顺序表定义有两种,一种是把里面的data定义为数组,还有一种是把data定义为数组指针,数组类型操作更为简单,但是如果涉及到顺序表的扩容就会显得很不方便,数组指针的扩容更为方便。
typedef struct SqList
{
int *data;
int maxsize;
int length;
}SqList;
//initialize a sequential list
bool InitSqList(SqList* list)
{
list->length = 0;
list->maxsize = MAX_SIZE;
list->data = (int*)malloc(MAX_SIZE * sizeof(int));
return true;
}
- 顺序表的两种插入操作,一种是在顺序表尾部插入元素,一种是在指定位置插入元素
指定位置插入元素,只需要把需要插入位置的元素开始,之后每一个元素向后移一位,这里有一个问题是,移动元素时只能从末尾开始移动,如果正向移动那么后面的值会出现错误。
//insert the new element at the end of the list
bool InsertTail(SqList* list,int num)
{
if (list->length >= list->maxsize)
{
return false;
}
list->data[list->length] = num;
list->length++;
return true;
}
//insert the new element at the target position
bool Insert(SqList* list,int local,int num)
{
if (list->length >= list->maxsize)
{
return false;
}
for (int i = list->length + 1; i >= local; i--)
{
list->data[i] = list->data[i - 1];
}
list->data[local-1] = num;
list->length++;
return true;
}
- 顺序表的删除操作
. 删除顺序表指定位置元素
//deleta an element at a specified position
bool Delete(SqList* list,int local)
{
if (local >= list->length)
{
return false;
}
for (int i = local - 1; i < list->length; i++)
{
list->data[i] = list->data[i + 1];
}
list->length--;
return true;
}
. 删除顺序表中指定值的元素
//delete all elements that are equal to this value
int DeleteElement(SqList* list,int num)
{
for (int i = 0; i < list->length; i++)
{
if (list->data[i] == num)
{
for (int j = i; j < list->length; j++)
{
list->data[j] = list->data[j + 1];
}
i--;
list->length--;
}
}
return 1;
}
- 顺序表的扩容
//extend squential list
void Extend(SqList* list)
{
list->maxsize *= 2;
int* L = (int*)malloc(list->maxsize * sizeof(int));
for (int i = 0; i < list->length; i++)
{
L[i] = list->data[i];
}
list->data = L;
}
完整代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
#define MAX_SIZE 10
typedef struct SqList
{
int *data;
int maxsize;
int length;
}SqList;
//initialize a sequential list
bool InitSqList(SqList* list)
{
list->length = 0;
list->maxsize = MAX_SIZE;
list->data = (int*)malloc(MAX_SIZE * sizeof(int));
return true;
}
//insert the new element at the end of the list
bool InsertTail(SqList* list,int num)
{
if (list->length >= list->maxsize)
{
return false;
}
list->data[list->length] = num;
list->length++;
return true;
}
//insert the new element at the target position
bool Insert(SqList* list,int local,int num)
{
if (list->length >= list->maxsize)
{
return false;
}
for (int i = list->length + 1; i >= local; i--)
{
list->data[i] = list->data[i - 1];
}
list->data[local-1] = num;
list->length++;
return true;
}
//deleta an element at a specified position
bool Delete(SqList* list,int local)
{
if (local >= list->length)
{
return false;
}
for (int i = local - 1; i < list->length; i++)
{
list->data[i] = list->data[i + 1];
}
list->length--;
return true;
}
//delete all elements that are equal to this value
int DeleteElement(SqList* list,int num)
{
for (int i = 0; i < list->length; i++)
{
if (list->data[i] == num)
{
for (int j = i; j < list->length; j++)
{
list->data[j] = list->data[j + 1];
}
i--;
list->length--;
}
}
return 1;
}
//extend squential list
void Extend(SqList* list)
{
list->maxsize *= 2;
int* L = (int*)malloc(list->maxsize * sizeof(int));
for (int i = 0; i < list->length; i++)
{
L[i] = list->data[i];
}
list->data = L;
}
//print squential list
void Print(SqList* list)
{
for (int i = 0; i < list->length; i++)
{
printf("%d ", list->data[i]);
}
printf("\n");
}
void test()
{
SqList list;
InitSqList(&list);
InsertTail(&list, 1);
InsertTail(&list, 2);
InsertTail(&list, 3);
InsertTail(&list, 4);
InsertTail(&list, 5);
Insert(&list, 2, 9);
Insert(&list, 2, 9);
Insert(&list, 2, 8);
Insert(&list, 2, 9);
DeleteElement(&list, 9);
Print(&list);
printf("%d\n",list.maxsize);
Extend(&list);
printf("%d\n", list.maxsize);
}
int main()
{
test();
}