数据结构--顺序表

本文详细介绍了顺序表的概念和结构,包括静态顺序表和动态顺序表,并提供了增删查改等基本操作的接口函数代码实现。通过示例展示了如何在C语言中实现顺序表的初始化、插入、删除、查找和修改等功能,同时讨论了动态扩容的策略。此外,还提供了一个测试案例来演示这些操作的实际应用。
摘要由CSDN通过智能技术生成

顺序表的实现

概念以及结构

顺序表是用一段物理地址的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表可以分为: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;
}

通讯录的底层其实就是顺序表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值