C实现顺序表

本文介绍了线性表中的顺序表数据结构,包括静态和动态顺序表的概念,并提供了C语言实现顺序表的基本操作,如初始化、销毁、打印、插入、删除、查找等。示例代码展示了顺序表的创建、增删查改以及销毁的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…

一、顺序表

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。

顺序表一般可以分为:

  1. 静态顺序表:使用定长数据存储元素
  2. 动态顺序表:使用动态开辟的数组存储

1.1 头文件

#pragma once

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

typedef int SeqListDataType ;

//定义一个线性表
typedef struct SeqList
{
	SeqListDataType* a;  //线性空间
	int size;           //数组内的有效数字的个数
	int capacity;       //容量
}SeqList;

//顺序表初始化
void SeqListInit(SeqList* ps);

//顺序的销毁
void SeqListDestory(SeqList* ps);

//顺序表打印
void SeqListPrint(SeqList* ps);

//顺序表尾插
void SeqListPushBack(SeqList* ps, SeqListDataType x);

//顺序表头插
void SeqListPushFront(SeqList* ps, SeqListDataType x);

//顺序表头删
void SeqListPopFront(SeqList* ps);

//顺序表尾删
void SeqListPopBack(SeqList* ps);

// 顺序表查找
int SeqListFind(SeqList* ps, SeqListDataType x);

// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SeqListDataType x);

// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);

1.2 接口实现

#include "SeqList.h"

void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = (SeqListDataType*)malloc(sizeof(SeqListDataType) * 4);
	if (ps == NULL)
	{
		perror("malloc fail\n");
		exit(-1);
	}
	ps->size = 0;
	ps->capacity = 4;
}

void SeqListDestory(SeqList* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

void SeqListPrint(SeqList* ps)
{
	assert(ps);	
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

void SeqListPushBack(SeqList* ps, SeqListDataType x)
{
	if (ps->capacity == ps->size)
	{
		SeqListDataType* newlist = realloc(ps->a, sizeof(SeqListDataType) * ps->capacity * 2);
		if (newlist == NULL)
		{
			perror("realloc fail\n");
		}
		else
		{
			ps->a = newlist;
			ps->capacity *= 2;
		}
	}
	ps->a[ps->size] = x;
	ps->size++;
}

void SeqListPushFront(SeqList* ps, SeqListDataType x)
{
	if (ps->capacity == ps->size)
	{
		SeqListDataType* newlist = realloc(ps->a, sizeof(SeqListDataType) * (ps->capacity * 2));
		if (newlist == NULL)
		{
			perror("realloc fail\n");
		}
		else
		{
			ps->a = newlist;
			ps->capacity *= 2;
		}
	}
	for (int i = ps->size; i >= 1; i--)
	{
		ps->a[i] = ps->a[i-1];
	}
	ps->a[0] = x;
	ps->size++;
}

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	assert(ps->size > 0);
	for (int i = 0; i < ps->size-1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	assert(ps->size > 0);
	ps->size--;
}

// 顺序表查找
int SeqListFind(SeqList* ps, SeqListDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (x == ps->a[i])
			return i;
	}
	return -1;
}

// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SeqListDataType x)
{
	assert(ps);
	if (ps->capacity == ps->size)
	{
		SeqListDataType* newlist = realloc(ps->a, sizeof(SeqListDataType) * ps->capacity * 2);
		if (newlist == NULL)
		{
			perror("realloc fail\n");
		}
		else
		{
			ps->a = newlist;
			ps->capacity *= 2;
		}
	}
	for (int i = ps->size - 1; i >= (int)pos; i--)
	{
		ps->a[i+1] = ps->a[i];
	}
	ps->a[pos] = x;
	ps->size++;
}

// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos)
{
	assert(ps);
	assert(ps->size > 0);
	for (int i = (int)pos;i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

1.3 运行结果

#include "SeqList.h"

void SeqListDisplay()
{
	SeqList s;
	SeqListInit(&s);
	SeqListPushBack(&s, 1);
	SeqListPushBack(&s, 2);
	SeqListPushBack(&s, 3);
	SeqListPushFront(&s, 4);
	SeqListPushFront(&s, 5);
	SeqListPushFront(&s, 6);
	SeqListPrint(&s);

	SeqListPopFront(&s);
	SeqListPrint(&s);
	
	SeqListPopBack(&s);
	SeqListPrint(&s);
	
	int ret = SeqListFind(&s,1);
	SeqListInsert(&s, ret,10);
	SeqListPrint(&s);

	SeqListErase(&s, ret);
	SeqListPrint(&s);

	SeqListDestory(&s);
}

int main()
{
	SeqListDisplay();
	return 0;
}

在这里插入图片描述

二、例题

在这里插入图片描述思路:
在这里插入图片描述

具体代码实现:

int removeElement(int* nums, int numsSize, int val){
    int src = 0,dest = 0;
    while(src < numsSize)
    {
        if(nums[src] == val)
        {
            src++;
        }
        else
        {
            nums[dest] = nums[src];
            src++;
            dest++;
        }
    }
    return dest;
}

在这里插入图片描述

思路:在这里插入图片描述具体代码实现:

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
    int end1 = m -1,end2 = n - 1;
    int end = m+n-1;
    while(end1>=0 && end2>=0)
    {
        if(nums2[end2] > nums1[end1])
        {
            nums1[end--] = nums2[end2];
            end2--;
        }
        else
        {
            nums1[end--] = nums1[end1];
            end1--;
        }
    }
    //如果end2还没有结束,还有数据,还需要挪过去
    while(end2>=0)
    {
        nums1[end--] = nums2[end2];
        end2--;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值