C语言动态顺序表

   顺序表是将表中的节点依次存放在计算机内存中一组地址连续的存储单元中,表可以动态增长,尾插元素,尾删元素,头插元素,头删元素,打印元素,查找元素,插入指定位置的元素,删除指定元素,删除所有指定的元素,逆序顺序表,排序顺序表,二分查找。

 ☆☆☆代码实现

seqlist.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#define DEFAULT_MAX 2  //默认大小
#define INC 1  //默认增容大小

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

typedef int DataType;

typedef struct Seqlist
{
	DataType* data;
	int sz;
	int capacity;
}Seqlist, *pSeqlist;

void InitSeqlist(pSeqlist pList);
void PushBack(pSeqlist pList, DataType d);
void PopBack(pSeqlist pList);
void PushFront(pSeqlist pList, DataType d);
void PopFront(pSeqlist pList);
int Find(pSeqlist pList, DataType d);
void Display(const pSeqlist pList);
void Insert(pSeqlist pList, DataType d, int pos);
void Remove(pSeqlist pList, DataType d);
void RemoveAll(pSeqlist pList, DataType d);
void Reverse(pSeqlist pList);
void Sort(pSeqlist pList);
int BinarySearch(pSeqlist pList, DataType d);
void Release(pSeqlist pList);

#endif //__SEQLIST_H__
seqlist.c

#include "seqlist.h"

void InitSeqlist(pSeqlist pList)  //顺序表初始化
{
	assert(pList != NULL);
	pList->sz = 0;
	pList->data = (DataType*)malloc(DEFAULT_MAX*sizeof(DataType));
	if (pList->data == NULL)
	{
		perror("malloc");
		exit(EXIT_FAILURE);
	}
	memset(pList->data, 0x00, DEFAULT_MAX*sizeof(DataType));
	pList->capacity = DEFAULT_MAX;
}

void CheckCapacity(pSeqlist pList)  //检查容量
{
	assert(pList != NULL);
	if (pList->sz == pList->capacity)    
	{
		DataType *ptr = realloc(pList->data,(pList->capacity + INC)*sizeof(DataType));
		if (ptr == NULL)
		{
			perror("realloc");
			exit(EXIT_FAILURE);
		}
		pList->data = ptr;
		pList->capacity += INC;    //满了进行增容。
	}
}

void PushBack(pSeqlist pList, DataType d)  //尾插元素
{
	assert(pList != NULL);
	CheckCapacity(pList);
	pList->data[pList->sz] = d;
	pList->sz++;
}

void PopBack(pSeqlist pList)  //后进先出,尾删元素
{
	int i = 0;
	assert(pList != NULL);
	if (pList->sz == 0)
		return;
	pList->sz--;
}

void PushFront(pSeqlist pList, DataType d)  //头插元素
{
	assert(pList != NULL);
	CheckCapacity(pList);
	memmove(pList->data + 1, pList->data, pList->sz*sizeof(DataType));  //pdest,psrc,字节数
	pList->data[0] = d;
	pList->sz++;
}

void PopFront(pSeqlist pList)  尾删元素
{
	assert(pList != NULL);
	if (pList->sz == 0)
	{
		return;
	}
	memmove(pList->data, pList->data + 1, pList->sz*sizeof(DataType));
	pList->sz--;
}

void Display(const pSeqlist pList)  //打印元素
{
	int i = 0;
	assert(pList != NULL);
	for (i = 0; i < pList->sz; i++)
	{
		printf("%d ", pList->data[i]);
	}
	printf("\n");


}

int Find(pSeqlist pList, DataType d)  //查找元素
{
	int i = 0;
	assert(pList != NULL);
	for (i = 0; i < pList->sz; i++)
	{
		if (pList->data[i] == d)
		{
			return i;
		}
	}
	return -1;
}

void Insert(pSeqlist pList, DataType d, int pos)  //指定位置插入元素
{
	CheckCapacity(pList);
	assert(pList != NULL);
	int len = pList->sz;
	memmove(pList->data + pos, pList->data + pos - 1, (len - pos + 1)*sizeof(DataType)); // 不允许越界访问
	pList->data[pos - 1] = d;
	pList->sz++;
}

void Remove(pSeqlist pList, DataType d)  //删除指定元素
{
	assert(pList != NULL);
	int ret=Find(pList,d);
	memmove(pList->data + ret, pList->data + ret + 1, (pList->sz - ret - 1)*sizeof(DataType));
	if (ret == -1)
	{
		return;
	}
	pList->sz--;
}

void RemoveAll(pSeqlist pList, DataType d)  //删除所有指定的元素
{
	int i = 0;
	assert(pList != NULL);
	for (i = 0; i < pList->sz; i++)
	{
		if (pList->data[i] == d)
		{
			memmove(pList->data + i, pList->data + i + 1, (pList->sz - i - 1)*sizeof(DataType));
			pList->sz--;
		}
	}
}

void Reverse(pSeqlist pList)  //逆序顺序表
{
	int left = 0;
	assert(pList != NULL);
	int right = pList->sz - 1;
	while (left <= right)
	{
		DataType tmp = pList->data[left];
		pList->data[left] = pList->data[right];
		pList->data[right] = tmp;
		left++;
		right--;
	}
}

void Sort(pSeqlist pList)  //排序顺序表,采用了冒泡排序
{
	int i = 0;
	int j = 0;
	assert(pList != NULL);
	for (i = 0; i < pList->sz - 1; i++)
	{
		for (j = 0; j < pList->sz - 1 - i; j++)
		{
			if (pList->data[j]>pList->data[j + 1])
			{
				DataType tmp = pList->data[j];
				pList->data[j] = pList->data[j + 1];
				pList->data[j + 1] = tmp;
			}
		}
	}
}

int BinarySearch(pSeqlist pList, DataType d)   //二分查找,前提是顺序表必须是有序表
{
	int left = 0;
	int right = pList->sz - 1;
	while (left <= right)
	{
		DataType mid = left + ((right-left) >> 1); //加比右移位优先级高
		if (pList->data[mid] > d)
		{
			right=mid-1;
		}
		else if (pList->data[mid] < d)
		{
			left=mid+1;
		}
		else
		{
			return mid;
		}
	}
	return -1;
}

void Release(pSeqlist pList)
{
	free(pList->data);
	pList->data = NULL;
	pList->capacity = 0;
	pList->sz = 0;
}

test.c

#include "seqlist.h"

void test()
{
	Seqlist my_pcon;
	InitSeqlist(&my_pcon);
	PushBack(&my_pcon, 1);
	PushBack(&my_pcon, 2);
	PushBack(&my_pcon, 3);
	PushBack(&my_pcon, 4);
	Display(&my_pcon);
	PopBack(&my_pcon);
	PopBack(&my_pcon);
	PopBack(&my_pcon);
	Display(&my_pcon);
	PushFront(&my_pcon, 5);
	Display(&my_pcon);
	PushFront(&my_pcon, 4);
	Display(&my_pcon);
	PopFront(&my_pcon);
	Display(&my_pcon);
	PushFront(&my_pcon, 4);
	Display(&my_pcon);
	PushFront(&my_pcon, 3);
	Display(&my_pcon);
	PushFront(&my_pcon, 6);
	Display(&my_pcon);
	int ret=Find(&my_pcon, 5);
	printf("%d\n", ret);
	Insert(&my_pcon, 2, 3);
	Display(&my_pcon);
	Insert(&my_pcon, 8, 4);
	Display(&my_pcon);
	Remove(&my_pcon, 2);
	Display(&my_pcon);
	PushFront(&my_pcon, 5);
	Display(&my_pcon);
	RemoveAll(&my_pcon, 5);
	Display(&my_pcon);
	RemoveAll(&my_pcon, 6);
	Display(&my_pcon);
	Reverse(&my_pcon);
	Display(&my_pcon);
	Sort(&my_pcon);
	Display(&my_pcon);
	int ret1=BinarySearch(&my_pcon, 6);
	printf("%d", ret1);
	Release(&my_pcon);  //程序最后应该释放动态开辟的内存空间,防止内存泄漏
}

int main()
{
	test();
	getchar();
	return 0;
}
程序运行图



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值