数据结构 - 20230516

练习

顺序表

head.h

#ifndef __HEAD_H__

#define __HEAD_H__

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

#define MAXSIZE 10

typedef int DataType; 
typedef struct {
	DataType data[MAXSIZE];
	int len;
}Seqlist;

void menu();

Seqlist *create();
int isEmpty(Seqlist *list);
int isFull(Seqlist *list);

int insert_rear(DataType e, Seqlist *list);
int delete_rear(Seqlist *list);
void output(Seqlist *list);

int searchWithSub(int sub, int *result, Seqlist *list);
int changeWithSub(int sub, DataType e, Seqlist *list);
int insertWithSub(DataType e, int index, Seqlist *list);
int deleteWithSub(int index, Seqlist *list);

int searchWithData(DataType e, Seqlist *list);
int changeWithData(DataType oldValue, DataType newValue, Seqlist *list);
int insertWithData(DataType targetValue, DataType e, Seqlist *list);
int deleteWithData(DataType e, Seqlist *list);

int bubbleSort(Seqlist *list);
int selectSort(Seqlist *list);

int removeSameValue(Seqlist *list);



#endif

main.c

#include "head.h"

int main(int argc, const char *argv[]) {
	Seqlist *list = create();

	menu();
	int number;
	while(1) {
		printf(" --------------------------- \n");
		printf("请输入您的选择:");
		scanf("%d", &number);
		switch(number) {
		case 1: 
			//表尾部插入
			{
				int n;
				printf("请输入要输入的数量:");
				scanf("%d", &n);
				for (int i=0; i<n; i++) {
					printf("请输入数:");
					int data;
					scanf("%d", &data);
					int result = insert_rear(data, list);
					if (-1 == result) {
						break;
					}
				}
			}
			break;
		case 2:
			//表尾删除
			{
				if (delete_rear(list) == 0) {
					printf("删除成功\n");
				}
			}
			break;
		case 3:
			//顺序表遍历
			{
				output(list);
			}
			break;
		case 4:
			//按下标查找
			{
				int sub;
				printf("请输入要查找的下标:");
				scanf("%d", &sub);
				int result;
				if (searchWithSub(sub, &result, list) == 0) {
					printf("查找结果是:%d\n", result);
				}
			}

			break;
		case 5:
			//按下标修改
			{
				int sub;
				printf("请输入要修改的下标:");
				scanf("%d", &sub);
				int e;
				printf("请输入修改后的值:");
				scanf("%d", &e);
				if(changeWithSub(sub, e, list) == 0) {
					printf("修改成功\n");
				}
			}
			break;
		case 6:
			//按下标插入
			{
				printf("请输入要插入的数:");
				int e;
				scanf("%d", &e);
				printf("请输入要插入的位置:");
				int index;
				scanf("%d", &index);
				
				if(insertWithSub(e, index, list) == 0) {
					printf("插入成功\n");
				}	
			}
			break;
		case 7:
			//按下标删除
			{
				printf("请输入要删除的下标:");
				int sub;
				scanf("%d", &sub);
				if(deleteWithSub(sub, list) == 0) {
					printf("删除成功\n");
				}
			}
			break;
		case 8:
			//按元素查找
			{
				printf("请输入要查找的元素:");
				DataType e;
				scanf("%d", &e);
				DataType result = searchWithData(e, list);
				if (result != -1) {
					printf("查找的下标是:%d\n", result);
				}
			}
			break;
		case 9:
			//按元素修改
			{
				printf("请输入要修改的元素:");
				DataType oldValue;
				scanf("%d", &oldValue);
				printf("请输入新的值:");
				DataType newValue;
				scanf("%d", &newValue);
				if (changeWithData(oldValue, newValue, list) == 0) {
					printf("修改成功\n");
				}
			}
			break;
		case 10:
			//按元素插入
			{
				printf("请输入要目标元素:");
				DataType targetValue;
				scanf("%d", &targetValue);
				printf("请输入插入的值:");
				DataType e;
				scanf("%d", &e);
				if (insertWithData(targetValue, e, list) == 0) {
					printf("插入成功\n");
				}
			}
			break;
		case 11:
			//按元素删除
			{
				printf("请输入要删除的元素:");
				DataType e;
				scanf("%d", &e);
				if(deleteWithData(e, list) == 0) {
					printf("删除成功\n");
				}
			}
			break;
		case 12:
			//冒泡排序
			{
				if(bubbleSort(list) == 0) {
					printf("冒泡排序成功\n");
				}
			}
			break;
		case 13:
			//选择排序
			{
				if(selectSort(list) == 0) {
					printf("选择排序成功\n");
				}
			}
			break;
		case 14:
			//去除重复元素
			{
				if(removeSameValue(list) == 0) {
					printf("去重成功\n");
				}
			}
			break;
		default:
			printf("输入错误,请重新输入您的选择\n");
			break;
		case 0:
			printf("退出程序\n");
			free(list);
			list = NULL;
			exit(0);
		}
	}

	return 0;
}

test.c

ubuntu@ubuntu:seqlist$ 
ubuntu@ubuntu:seqlist$ cat test.c
#include "head.h"

void menu() {
	puts("****************************");
	puts("1.表尾部插入");
	puts("2.表尾删除");
	puts("3.顺序表遍历");
	puts("4.按下标查找");
	puts("5.按下标修改");
	puts("6.按下标插入");
	puts("7.按下标删除");
	puts("8.按元素查找");
	puts("9.按元素修改");
	puts("10.按元素插入");
	puts("11.按元素删除");
	puts("12.冒泡排序");
	puts("13.选择排序");
	puts("14.去除重复元素");
	puts("0.退出整个程序");
	puts("****************************");
}

//创建
Seqlist *create() {
	Seqlist *list = (Seqlist *)malloc(sizeof(Seqlist));
	if (NULL == list) {
		return NULL;
	}
	list->len = 0;
	return list;
}

//判断满
int isFull(Seqlist *list) {
	if (list && list->len >= MAXSIZE) {
		return 1;
	}
	return 0;
}

//判断空
int isEmpty(Seqlist *list) {
	if (list && list->len == 0) {
		return 1;
	}
	return 0;
}

//顺序表整体输出
void output(Seqlist *list) {
	if (NULL == list) {
		return;
	}
	if (isEmpty(list)) {
		printf("没有元素\n");
		return;
	}

	for (int i=0; i<list->len; i++) {
		printf("%d ", list->data[i]);
	}
	printf("\n");
}

//尾部插入
int insert_rear(DataType e, Seqlist *list) {
	if (isFull(list)) {
		printf("插入失败\n");
		return -1;
	}

	list->data[list->len] = e;
	(list->len)++;
	return 0;
}

//尾部删除
int delete_rear(Seqlist *list) {
	if (NULL == list) {
		printf("删除失败,表为空\n");
		return -1;
	}
	if (isEmpty(list)){
		printf("删除失败,失败原因:没有元素\n");
		return -1;
	}

	list->len--;
	return 0;
}

//按下标查找
int searchWithSub(int sub, int *result, Seqlist *list) {
	if (NULL == list || isEmpty(list) || sub < 0 || sub >= list->len) {
		printf("查找失败 \n");
		return -1;
	}

	*result = list->data[sub];
	return 0;
}

//按下标修改
int changeWithSub(int sub, DataType e, Seqlist *list) {
	if (NULL == list || isEmpty(list) || sub < 0 || sub >= list->len) {
		printf("修改失败 \n");
		return -1;
	}
	list->data[sub] = e;
	return 0;
}

//按下标插入
int insertWithSub(DataType e, int sub, Seqlist *list) {
	if (NULL == list) {
		printf("插入失败,表为空\n");
		return -1;
	}
	if (isFull(list)) {
		printf("插入失败, 顺序表已满\n");
		return -1;
	}
	if (sub > list->len) {
		printf("插入失败, 顺序表必须连续\n");
		return -1;
	}
	for (int i=list->len-1; i>=sub; i--) {
		(list->data)[i+1] = (list->data)[i];
	}
	list->data[sub] = e;
	list->len++;
	return 0;
}

//按下标删除
int deleteWithSub(int index, Seqlist *list) {
	if (NULL == list) {
		printf("删除失败,表为空\n");
		return -1;
	}
	if (isEmpty(list)) {
		printf("删除失败, 空顺序表\n");
		return -1;
	}
	if (index<0 || index>=(list->len)) {
		printf("删除失败 index = %d, len = %d\n", index, list->len);
		return -1;
	}

	for (int i=index+1; i<list->len; i++) {
		list->data[i-1] = list->data[i];
	}
	list->len--;
	return 0;
}

//按元素查找下标
int searchWithData(DataType e, Seqlist *list) {
	if (NULL == list || isEmpty(list)) {
		printf("查找失败\n");
		return -1;
	}

	for (int i=0; i<list->len; i++) {
		if (list->data[i] == e) {
			return i;
		}
	}

	return -1;
}

//按元素修改
int changeWithData(DataType oldValue, DataType newValue, Seqlist *list) {
	int searchResult = searchWithData(oldValue, list);
	if (searchResult == -1) {
		printf("查找元素失败\n");
		return -1;
	}

	list->data[searchResult] = newValue;
	return 0;
}

//按元素插入
int insertWithData(DataType targetValue, DataType e, Seqlist *list) {
	if (NULL == list || isFull(list)) {
		printf("插入失败\n");
		return -1;
	}
	int searchResult = searchWithData(targetValue, list);
	if (searchResult == -1) {
		printf("查找元素失败\n");
		return -1;
	}

	return insertWithSub(e, searchResult+1, list);
}

//按元素删除
int deleteWithData(DataType e, Seqlist *list) {
	if (NULL == list || isFull(list)) {
		printf("删除失败\n");
		return -1;
	}
	int searchResult = searchWithData(e, list);
	if (searchResult == -1) {
		printf("查找元素失败\n");
		return -1;
	}

	return deleteWithSub(searchResult, list);
}

//排序,冒泡
int bubbleSort(Seqlist *list) {
	if (NULL == list || isEmpty(list)) {
		printf("排序失败\n");
		return -1;
	}
	int len = list->len;
	DataType *data = list->data;
	for (int i=0; i<len-1; i++) {
		for	(int j=0; j<len-i-1; j++) {
			if (data[j]>data[j+1]) {
				DataType temp = data[j];
				data[j] = data[j+1];
				data[j+1] = temp;
			}
		}
	}
	data = NULL;
	return 0;
}

//排序,选择排序
int selectSort(Seqlist *list) {
	if (NULL == list || isEmpty(list)) {
		printf("排序失败\n");
		return -1;
	}

	int len = list->len;
	DataType *data = list->data;
	for (int i=0; i<len-1; i++) {
		int maxIndex = 0;
		for (int j=1; j<len-i; j++) {
			if (data[j] > data[maxIndex]) {
				maxIndex = j;
			}
		}
		int temp = data[maxIndex];
		data[maxIndex] = data[len-i-1];
		data[len-i-1] = temp;
	}
	data = NULL;
	return 0;
}

//去重
int removeSameValue(Seqlist *list) {
	if (NULL == list || isEmpty(list)) {
		printf("去重失败\n");
		return -1;
	}
	for (int i=0; i<list->len; i++) {
		DataType curValue = list->data[i];
		int j = i+1;
		while(j<list->len) {
			if (list->data[j] == curValue) {
				deleteWithSub(j, list);
			} else {
				j++;
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值