用C写一个顺序表的简单操作

该程序有一个选项菜单

#include "stdio.h"
#include "stdlib.h"
#define ERROR 0;
#define SUCCESS 1;
typedef struct{
	int list[100];
	int length;
}MyList;

/*清除scanf()缓存函数*/
void safe_flush(FILE *fp)
{
	int ch;
	while ((ch = fgetc(fp)) != EOF && ch != '\n');
}
void Menu(){
	printf_s("--------The Menu---------\n");
	printf_s("0 : HELP\n");
	printf_s("1 : PrintList\n");
	printf_s("2 : FindList\n");
	printf_s("3 : InsertList\n");
	printf_s("4 : DeleteList\n");
	printf_s("5 : EXIT\n\n\n");
}
void CreateList(MyList *l, int n){           //形参为: MyList *l 的结构体指针 ,指向 结构体 L 。
	int i;
	printf_s("Please enter the data(must be integer): ");
	for (i = 0; i < n; i++){
		while (scanf_s("%d", &l->list[i])==0)
		{
			printf_s("Input is wrong,Please enter again: ");
			safe_flush(stdin);
		}
		l->length = n;                       // l->length  相当于 (*l).length  即L.length 。
	}
}
void PrintList(MyList *l, int n){
	int i;
	if (l->length != 0){
		printf_s("MyList is: ");
		for (i = 0; i < n; i++){
			printf_s("%d ", l->list[i]);
		}
	}
	else{
		printf_s("MyList is empty! ");
	}
}
void FindElem(MyList *l, int n){
/*	int i;
	printf_s("\nPlease enter the number you want to find: ");
	scanf_s("%d", &n);
	for (i = 1; i <= l->length; i++){
		if (n == l->list[i-1]){
			printf_s("The number of position is %d", i);
			break;
		}
		if (i == l->length){
			printf_s("The number you want to find is not here.");
		}
	}
*/
	int i = 1, *p;
	p = l->list;
	printf_s("Please enter the number you want to find: ");
	scanf_s("%d", &n);
	while ((*p++) != n && i <= l->length)  i++;
	if (i <= l->length)  printf_s("The number of position is %d", i);
	else  printf_s("The number you want to find is not here.");
}
int InsertList(MyList *l, int i, int n){
	int j;
	printf_s("The insert position is: ");
	scanf_s("%d", &i);
	printf_s("The insert number is: ");
	scanf_s("%d", &n);
	if (l->length == 0){
		l->list[0] = n;
		l->length++;
		return SUCCESS;
	}
	else if (i > l->length || i <= 0){
		printf_s("The position is wrong,");
		return ERROR;
	}
	else{
		for (j = (l->length - 1); j >= i - 1; j--){
			l->list[j + 1] = l->list[j];
		}
		l->list[i - 1] = n;
		l->length++;
		return SUCCESS;
	}
}
int DeleteList(MyList *l, int i){
	printf_s("Please enter the position you want to delete: ");
	while (scanf_s("%d", &i) == 0){
		printf_s("Input is wrong,Please enter again: ");
		safe_flush(stdin);
	}
	if (i<1 || i>l->length){
		printf_s("The position is wrong,");
		return ERROR;
	}
	int j;
	for (j = i; j < l->length; j++){
		l->list[j-1] = l-> list[j];
	}
	l->length--;
	return SUCCESS;
}

void main(){
	void CreateList(MyList *l, int n);
	void PrintList(MyList *l, int n);

	int command;
	int i = 0;
	int n = 0;
	MyList L;
	Menu();
	printf_s("Please enter the list size: ");
	while (scanf_s("%d", &n) == 0 || n<=0)
	{
		printf_s("Input is wrong,Please enter again: ");
		safe_flush(stdin);
	}
	CreateList(&L, n);
	while (1)
	{
		printf_s("\nPlease enter your command: ");
		while (scanf_s("%d", &command) == 0)
		{
			printf_s("Input is wrong,Please enter again: ");
			safe_flush(stdin);
		}
		switch (command)
		{
		case 0:
			Menu();
			break;
		case 1:
			PrintList(&L, n);
			break;
		case 2:
			FindElem(&L, n);
			break;
		case 3:
			if (InsertList(&L, i, n)){
				printf_s("Insert success!\n");
				PrintList(&L, L.length);
			}
			else{
				printf_s("Insert error!");
			}
			break;
		case 4:
			if (DeleteList(&L, i)){
				printf_s("Delete success!\n");
				PrintList(&L, L.length);
			}
			else{
				printf_s("Delete error!");
			}
			break;
		case 5:
			printf_s("exit\n");
			break;
		default:
			printf_s("ERROR : Without this option! ");
			break;
		}
		if (command == 5) break;
	}
	system("pause");
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值