C语言:顺序表

顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构,下面我们简单实现顺序表。

test.c中

#define _CRT_SECURE_NO_WARNINGS 1
#include "seqlist.h"

void menu()
{
	printf("****************************\n");
	printf("1.PushBack       2.PopBack\n");
	printf("3.PushFront      4.PopFront\n");
	printf("5.Show           6.Remove  \n");
	printf("7.RemoveAll      8.Sort    \n");
	printf("9.BinarySearch   10.Find   \n");
	printf("0.Exit                     \n");
	printf("****************************\n");
}

int main()
{
	Seqlist mylist;
	int input = 0;
	InitSeqlist(&mylist);
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
			DateTyPe data = 0;
		case 1:
			printf("请输入要插入的元素:>");
			scanf("%d", &data);
			PushBack(&mylist, data);
			break;
		case 2:
			PopBack(&mylist);
			break;
		case 3:
			printf("请输入要插入的元素:>");
			scanf("%d", &data);
			PushFront(&mylist, data);
			break;
		case 4:
			PopFront(&mylist);
			break;
		case 5:
			Show(&mylist);
			break;
		case 6:
			printf("请输入要查找的元素:>");
			scanf("%d", data);
			Remove(&mylist, data);
			break;
		case 7:
			printf("请输入要查找的元素:>");
			scanf("%d", data);
			RemoveAll(&mylist, data);
			break;
		case 8:
			Sort(&mylist);
			break;
		case 9:
		{
				  int ret = 0;
				  printf("请输入要查找的元素:>");
				  scanf("%d", data);
				  ret = BinarySearch(&mylist, data);
				  if (ret == -1)
				  {
					  printf("查找的元素不存在\n");
				  }
				  else
				  {
					  printf("元素下标为%d\n", ret);
				  } 
					  break;
		}
			
		case 10:
		{
				   int ret2 = 0;
				   ret2 = Find(&mylist, data);
				   if (ret2 == -1)
				   {
					   printf("指定元素不存在\n");
				   }
				   else
				   {
					   printf("元素下标是%d\n", ret2);
				   }
		}
			break;
			
		case 0:
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
	DestroySeqlist(&mylist);
	system("pause\n");
	return 0;
}

seplist.h中

#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#endif //__DEQLIST_H__

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

#define MAX 100
#define DEFAULT_SZ 3
#define INC_SZ 2
typedef int DateTyPe;

typedef struct Seqlist
{
	int capacity;//容量
	int count;//有效
	DateTyPe *PData;//指向堆上的空间
}Seqlist,*PSeqlist;

void InitSeqlist(PSeqlist p);
void DestroySeqlist(PSeqlist p);
void CheckCapacity(PSeqlist p);
void PushBack(PSeqlist p, DateTyPe d);//尾插
void Show(PSeqlist p);
void PopBack(PSeqlist);//尾删
void PopFront(PSeqlist p);//头删
void PushFront(PSeqlist p, DateTyPe d);//头插
int Find(PSeqlist p,DateTyPe d);
void Remove(PSeqlist p, DateTyPe d);
void RemoveAll(PSeqlist p, DateTyPe d);
int BinarySearch(PSeqlist p, DateTyPe d);
void Destroy(PSeqlist p);
void Sort(PSeqlist p);


seqlist.c中

#include"seqlist.h"

void InitSeqlist(PSeqlist p)//初始化
{
	p->PData = (DateTyPe *)malloc(DEFAULT_SZ  * sizeof(DateTyPe)*MAX);
	if (p->PData == NULL)
	{
		perror("malloc");
		return;
	}
	p->count = 0;
	p->capacity = DEFAULT_SZ;
	memset(p->PData, 0, sizeof(DateTyPe)*DEFAULT_SZ);
}

void CheckCapacity(PSeqlist p)//检查容量是否满
{
	assert(p);
	DateTyPe *tmp = NULL;
	if (p->capacity == p->count)
	{
		tmp = (DateTyPe *)realloc(p->PData, (p->capacity + INC_SZ)*sizeof(DateTyPe));
		if (tmp != NULL)//判断申请内存是否成功
		{
			p->PData = tmp;
		}
	}
	p->capacity += INC_SZ;
}

void DestroySeqlist(PSeqlist p)//销毁
{
	assert(p != NULL);
	free(p->PData);
}

void Show(PSeqlist p)//展示
{
	assert(p != NULL);
	int i = 0;
	for (i = 0; i < p->count; i++)
	{
		printf("%d ", p->PData[i]);
	}
	printf("\n");
}

void PopBack(PSeqlist p)//后删
{
	assert(p != NULL);
	if (p->count == 0)
	{
		printf("顺序表为空\n");
		return;
	}
	p->count--;
}

void PushBack(PSeqlist p, DateTyPe d)//后插
{
	CheckCapacity(p);
	p->PData[p->count] = d;
	p->count++;
}

void PushFront(PSeqlist p, DateTyPe d)//头插
{
	int i = 0;
	assert(p != NULL);
	CheckCapacity(p);
	if (p->count == 0)
	{
		p->PData = d;
		p->count++;
	}
	else
	{
		for (i = p->count; i > 0; i--)
		{
			p->PData[i] = p->PData[i-1];
		}
		p->PData[0] = d;
		p->count++;
	}

}

void PopFront(PSeqlist p)//头删
{
	int i = 0;
	assert(p != NULL);
	if (p->count == 0)
	{
		printf("顺序表为空\n");
		return;
	}
	for (i = 0; i < p->PData; i++)
	{
		p->PData[i] = p->PData[i+1];
	}
	p->count--;
}

int Find(PSeqlist p, DateTyPe d)//查找指定元素
{
	int i = 0;
	assert(p != NULL);
	for (i = 0; i < p->count; i++)
	{
		if (p->PData[i] == d)
		{
			return i;
		}
	}
	return -1;
}

void Remove(PSeqlist p, DateTyPe d)//删除指定元素
{
	int pos = 0;
	int i = 0;
	assert(p != NULL);
	if (p->count == 0)
	{
		printf("顺序表为空\n");
		return;
	}
	if ((pos = Find(p, d) == -1))
	{
		printf("指定元素不存在\n");
		return;
	}
	for (i = pos; i < p->count - 1; i++)
	{
		p->PData[i] = p->PData[i+1];
	}
	p->count--;
}

void RemoveAll(PSeqlist p, DateTyPe d)//删除所有的某个元素
{
	int pos = 0;
	int i = 0;
	assert(p != NULL);
	while ((pos = Find(p, d)) != -1)
	{
		for (i = pos; i < p->count - 1; i++)
		{
			p->PData[i] = p->PData[i+1];
		}
		p->count--;
	}
}

void Sort(PSeqlist p)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < p->count - 1; i++)
	{
		for (j = 0; j < p->count - 1 - i; j++)
		{
			if (p->PData[j] > p->PData[j + 1])
			{
				DateTyPe tmp = p->PData[j];
				p->PData[j] = p->PData[j + 1];
				p->PData[j + 1] = tmp;
			}
		}
	}
}

int BinarySearch(PSeqlist p, DateTyPe d)
{
	int i = 0;
	int mid = 0;
	int left = 0;
	int right = p->count - 1;
	while (left <= right)
	{
		mid = left - (left - right) / 2;
		if (p->PData[mid] > d)
		{
			right = mid - 1;
		}
		else if (p->PData[mid] < d)
		{
			left = mid + 1;
		}
		else
			return mid;
	}
	return -1;
}
部分结果显示如下:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值