顺序表的插入、删除和查找算法(C语言)

头文件

后续的各种顺序表运算都要用,亲测没问题~

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

#define OK 1
#define ERROR -1
#define OVERFLOW 0
#define LIST_INIT_SIZE 100	//初始分配长度为100
#define LISTINCREMENT 10	//存储空间的分配增加量

/*定义顺序表*/
typedef struct
{
	int *elem;		//存储空间基地址
	int length;		//表长度初值为0
	int listsize;	//表空间初始尺寸
}SeqList;

/*创建顺序表*/
int CreateList_Seq(SeqList *L)
{
	int i;
	printf("Input the datas of the SeqList:");
	for(i=0;i<L->length;i++)
		scanf("%d",&L->elem[i]);
	return OK;
}
/*顺序表的初始化*/
int InitList_Seq(SeqList *L)
{
	L->elem = (int*)malloc(LIST_INIT_SIZE*sizeof(int));//动态分配空间
	if(!L->elem)
	{
		printf("Out of space!!\n");
		exit(OVERFLOW);
	}
	L->length = 0;
	L->listsize = LIST_INIT_SIZE;
	return OK;
}

int Output_SeqList(SeqList L)//输出
{
	int i;
	for(i=0;i<L.length;i++)
		printf("%4d",L.elem[i]);
	printf("\n");
	return OK;
}

插入算法

#include "SeqList.h"
/*插入算法*/
int ListInsert_Seq( SeqList *L, int i, int e) 
{		
	int *p, *q, *new1;
	if((i<1)||(i>L->length+1))  //插入元素的参数不合理
		return ERROR;
	if(L->length>=L->listsize)  //开辟新空间
	{
		new1 = (int*)realloc(L->elem,(L->length+LISTINCREMENT)*sizeof(int));
		if(!new1)
			exit(OVERFLOW);
		L->elem = new1;
		L->listsize += LISTINCREMENT;//增加存储容量
	}
	q = &L->elem[i-1];	//指针q指向插入位置
	for(p=&L->elem[L->length-1];p>=q;--p) 
	{
		*(p+1) = *p;	//插入位置及其后的元素右移
	}
		*q = e;			//在L的第i个位置插入元素e
		L->length++;	//线性表长度自增
		return OK;
}

void main()
	{
		int i, n, e;
		SeqList L;		//定义一个线性表
		InitList_Seq(&L);	//线性表初始化
		printf("\n Input the length of the SeqList L: n=");
		scanf("%d",&n);
		L.length = n;
		CreateList_Seq(&L);	//创建线性表
		printf("Input the insert data: e = ");
		scanf("%d",&e);
		printf("Input the insert location: i = ");
		scanf("%d",&i);
		if(ListInsert_Seq(&L,i,e))
		{
			printf("output the data of the Seqlist: ");
			Output_SeqList(L);
		}
		else
			printf("Can't insert the data!\n");
		system("pause");
	}

删除算法

#include "SeqList.h"
/*顺序表的删除算法*/
int ListDelete_Seq(SeqList *L, int i, int *e)
{
	int *p,*q;
	if(i<1||i>L->length)
		return ERROR;	 //i值不合法
	p = &L->elem[i-1];	//被删除元素的位置
	*e = *p;			//被删除元素的值赋给e
	q = L->elem + L->length-1;//表尾元素的位置
	for(++p;p<=q;p++)	//++p为移动的第一个元素
		*(p-1) = *p;	//元素左移
	L->length--;		//顺序表长度减一
	return OK;
}

void main()
{
	int i,n,e;
	SeqList L;	//定义一个顺序表
	InitList_Seq(&L);//初始化
	printf("\n Input the length of the Seqlist L: n=");
	scanf("%d",&n);
	L.length = n;
	CreateList_Seq(&L);//创建顺序表
	printf("Input the delete location: i=");
	scanf("%d",&i);
	if(ListDelete_Seq(&L,i,&e))
	{
		printf("Delete the data %d:\n",e);
		printf("Output the data of the Seqlist: ");
		Output_SeqList(L);
	}
	else
		printf("Can't find the delete data!\n");
	system("pause");
}

查找算法

#include "SeqList.h"

/*按内容查找算法*/
int ListSearch_Seq( SeqList *L,int n, int e)
{
	int i=1;
	while(i<=L->elem[i]&&L->elem[i]!=e)
		i++;
	if(i>L->length) return ERROR;
	else return i;
}
void main()
{
		int i, n, e;
		SeqList L;		//定义一个线性表
		InitList_Seq(&L);	//线性表初始化
		printf("\n Input the length of the SeqList L: n=");
		scanf("%d",&n);
		L.length = n;
		CreateList_Seq(&L);	//创建线性表
		printf("Input the insert data: e = ");
		scanf("%d",&e);
		if(ListSearch_Seq(&L,n,e)!=-1)
			printf("\n%d在顺序表的第%d位\nn",e,ListSearch_Seq(&L,n,e)+1);
		system("pause");
	}


  • 7
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
顺序表的简单插入排序代码如下: ```c #include <stdio.h> #define MAXSIZE 10 typedef struct { int r[MAXSIZE+1]; // 存储排序关键字,r[0]不用 int length; // 顺序表长度 } SqList; void InsertSort(SqList *L) { int i, j; for (i = 2; i <= L->length; ++i) { if (L->r[i] < L->r[i-1]) { // 需将L->r[i]插入有序子表 L->r[0] = L->r[i]; // 设置哨兵 for (j = i-1; L->r[j] > L->r[0]; --j) { L->r[j+1] = L->r[j]; // 记录后移 } L->r[j+1] = L->r[0]; // 插入到正确位置 } } } int BinarySearch(SqList L, int key) { int low = 1, high = L.length, mid; while (low <= high) { mid = (low + high) / 2; if (L.r[mid] == key) { return mid; } else if (L.r[mid] > key) { high = mid - 1; } else { low = mid + 1; } } return 0; } int main() { SqList L = {{0, 49, 38, 65, 97, 76, 13, 27, 49}, 8}; InsertSort(&L); int key = 49; int pos = BinarySearch(L, key); if (pos) { printf("%d is found at position %d\n", key, pos); } else { printf("%d is not found\n", key); } return 0; } ``` 这里定义了一个 `SqList` 结构体,包含一个数组 `r` 和一个整型变量 `length`,数组 `r` 存储了排序关键字。`InsertSort` 函数实现了简单插入排序算法,`BinarySearch` 函数实现了二分查找算法,其中 `low` 和 `high` 分别表示查找范围的起始位置和结束位置,`mid` 表示中间位置,如果找到了关键字 `key`,则返回其位置,否则返回 0。 本例中,先将一个顺序表排序,然后查找关键字为 49 的元素。程序输出: ``` 49 is found at position 2 ``` 以上代码仅供参考,实际使用中需要根据具体情况进行适当修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值