顺序表的相关学习

功能实现

实现了顺序表的创建、插入、查找、修改和删除功能。

代码说明

代码由三部分组成,分别为main.c、Sqlist_function.c和Sqlist.h组成。
Sqlist.h

#ifndef __SQLIST__
#define __SQLIST__
#define SIZE 50
typedef int ElemType;
typedef struct
{
	ElemType elem[SIZE];
	ElemType length;
}Sqlist;
//数据插入
Sqlist	Insert_List(Sqlist List, ElemType location, ElemType data);
//数据查询
int Search(Sqlist List, ElemType data);
//数据删除
Sqlist Delete_location(Sqlist List, ElemType location);
Sqlist Delete_data(Sqlist List, ElemType data);
//数据修改
Sqlist Change_List(Sqlist List, ElemType  location, ElemType data);
//排序
Sqlist Sort_List(Sqlist List);
//命令
void ShowOrder();
//输出
void Cout_List(Sqlist List);
#endif

Sqlist_function.c

#include<stdio.h>
#include<stdlib.h>
#include "Sqlist.h"
Sqlist	Insert_List(Sqlist List, ElemType location, ElemType data)
{
	int k;
	if (location < 1 || location>List.length + 1)
	{
		printf("插入位置错误,请重新插入!!\n");
		exit(0);
	}
	else
	{
		for (k = List.length - 1; k >=location-1; k--)
		{
			List.elem[k + 1] = List.elem[k];
		}
		List.elem[++k] = data;
		List.length++;
	}
	return List;
}
int Search(Sqlist List, ElemType data)
{
	int  s;
	for (s = 0; s < List.length&&List.elem[s] != data; s++);
	if (s < List.length)
		return s + 1;
	else
		return 0;
}
Sqlist Delete_location(Sqlist List, ElemType location)
{
	if (location < 1 || location > List.length)
	{
		printf("位置输入错误,请重新输入!!\n");
		exit(0);
	}
	for (; location < List.length; location++)
	{
		List.elem[location - 1] = List.elem[location];
	}
	List.length--;
	return List;
}
Sqlist Delete_data(Sqlist List, ElemType data)
{
	int i = 0, v, j;
	for (j = 0; j < List.length; j++)
	{
		if (List.elem[j] == data)
		{
			for (v = j; v < List.length - 1; v++)
			{
				List.elem[v] = List.elem[v + 1];
			}
			List.length--;
		}
	}
	return List;
}
Sqlist Change_List(Sqlist List, ElemType  location, ElemType data)
{
	if (location < 1 || location > List.length)
	{
		printf("位置输入错误,请重新输入!!\n");
		exit(0);
	}
	List.elem[location - 1] = data;
	return List;
}
Sqlist Sort_List(Sqlist List)
{
	int i, j, temp;
	for (i = 0; i < List.length - 1; i++)
	{
		for (j = i + 1; j < List.length; j++)
		{
			if (List.elem[i] > List.elem[j])
			{
				temp = List.elem[i];
				List.elem[i] = List.elem[j];
				List.elem[j] = temp;
			}
		}
	}
	return List;
}
void Cout_List(Sqlist List)
{
	int i;
	for (i = 0; i<List.length; i++)
	{
			printf("%d\t", List.elem[i]);
	}
	printf("\n");
}
void ShowOrder()
{
    printf("\t*************顺序表管理系统**************\n");
	printf("\t*********  ^请先创建一个顺序表^  ********\n");
	printf("\t************  1.创建顺序表   ************\n");
	printf("\t************  2.插入数据   ************\n");
	printf("\t************  3.查找数据   ************\n");
	printf("\t************  4.删除数据   ************\n");
	printf("\t************  5.修改数据   ************\n");
	printf("\t************  6.退出系统   ************\n");
}

main.c

#include<stdio.h>
#include<stdlib.h>
#include"Sqlist.h"
#pragma warning(disable:4996)
int main(int argc, const char* argv[])
{
	Sqlist List;			
   	List.length = 0;
	int i,m;
	int location, insert;    //插入定义
	int search, search2;	//查找定义
	int del,mmp,temp;			//删除方法定义
	int revamp,change;      //修改定义
	int fo;					//排序定义
	int nOrder = -1;
	int flag=1;
	ShowOrder();
	while (flag)
	{
		printf("请输入指令(1-5)查看指令:");
		scanf("%d", &nOrder);
		switch (nOrder)
		{
		case 1:
			printf("请输入数组元素个数m:");
			scanf("%d", &m);
			printf("请输入顺序表元素%d个(空格注意):", m);
			for ( i = 0; i< m; i++)
			{				
				scanf("%d", &List.elem[i]);
				List.length++;
			}
			break;
		case 2:
			printf("请输入要插入的位置location:");
			scanf("%d", &location);
			printf("输入要插入的数值insert:");
			scanf("%d", &insert);
			List = Insert_List(List, location, insert);
			printf("插入后的顺序表是:\n");		
			Cout_List(List);
			printf("^^^是否进行排序(1/0)^^^:\n\n");
			scanf("%d", &fo);
			if (fo == 1)
			{
				List= Sort_List(List);
				Cout_List(List);
			}
			else
			{
			printf("原数组:\n");
				Cout_List(List);
			}
			break;
		case 3:
			printf("请输入你要查找的数据search:");
			scanf_s("%d", &search);
			search2 = Search(List, search);
			printf("查找后的结果:\n");
			if (search2 == 0)
			{
				printf("该顺序表中没有你索要查找的值!\n");
			}
			else
			{
				printf("已找到!\n  位置在第%d位\n", search2);
			}
			break;
		case 4:

		    printf("\t请输入使用删除哪一个方法temp:\n");
			printf("\t***方法一(删除第几个位置)***\n");
			printf("\t***方法二(删除已知确定数值)***\n");
			scanf_s("%d", &temp);
			switch (temp)
			{
			case 1:
				printf("请输入要删除第几个位置的数mmp:");
  			   scanf_s("%d", &mmp);
				List = Delete_location(List, mmp);
				printf("删除后的数组为:\n");
				Cout_List(List);
				break;
			case 2:
				printf("请输入要删除数del:");
				scanf_s("%d", &del);
				List = Delete_data(List,del);
				printf("删除后的数组为:\n");
				Cout_List(List);
				break;
			default:
				printf("输入命令有误,请重新输入!\n");
				break;
			}
			break;
		case 5:
			printf("请输入你想要修改第几个元素revamp:");
			scanf_s("%d", &revamp);
			printf("请输入你改变后的数值change:");
			scanf_s("%d", &change);
			List = Change_List(List, revamp, change);
			printf("修改后的顺序表是:\n");
			Cout_List(List);
			break;
		case 6:
			exit(0);
			break;
		default:
			printf("输入命令有误,请重新输入!\n");
			break;
		}
	}
	system("pause");
	return 0;
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值