练习-有一个顺序表 ,以第一个数值为基准,将所有大于它的数值都放到他的右边,反之放于左边
今天336行
代码如下:
#include<stdio.h>
#include<string.h>
#include<Windows.h>
#include<malloc.h>
#define MAXSIZE 5
typedef struct ONE {
int a[MAXSIZE];
int Length;
}one;
//建立顺序表/2为测试程序
void CreatList(one*& L, int c);
//初始化顺序表
void InitList(one*& L);
//销毁
void DestroyList(one* &L);
//判空
bool ListEmpty(one*& L);
//表长
int ListLength(one*& L);
//输出
void DisplayList(one*& L);
//求表中某个元素
int GetList(one*& L, int e);
//插入元素
bool ListInsert(one*& L,int e, int p);
//删除元素
bool DeleteList(one*& L, int e);
void CreatList(one* &L, int c) {
int i = 0,m;
//L = (one * )malloc(sizeof(one));
while (i < c) {
m = i + 1;
printf("请输入%d个中第%d个元素:",c,m);
scanf("%d",&L->a[i]);
++i;
}
L->Length = i;
}
void CreatList2(one*& L, int b[], int c) {
//测试程序
int i = 0, m;
//L = (one * )malloc(sizeof(one));
while (i < c) {
//printf("请输入%d个中第%d个元素:", c, m);
L->a[i] = b[i];m = ++i;
}
L->Length = i;
}
void InitList(one* &L) {
L = (one*)malloc(sizeof(one));
L->Length = 0;
}
void DestroyList(one*& L) {
free(L);
}
bool ListEmpty(one* &L) {
if (L->Length == 0) {
return true;
}
else {
return false;
}
}
int ListLength(one*& L) {
return L->Length;
}
void DisplayList(one*& L) {
int i;
for (i = 0; i < L->Length; ++i) {
printf("第%d个--------%d\n",i+1, L->a[i]);
}
}
int GetList(one*& L, int e) {
int i = 0;
for (i = 0; i < L->Length; ++i) {
if (L->a[i] == e) {
return i;
}
}
return -1;
}
bool ListInsert(one*& L, int e, int p) {
if (p<0 || p>L->Length) {
return false;
}
int i;
int m = p - 1;
for (i=L->Length;i>m;--i) {
L->a[L->Length] = L->a[L->Length - 1];
}
L->a[m] = e;
L->Length++;
return true;
}
bool DeleteList(one*& L, int e) {
int i,j,k=L->Length;
for (i = 0; i < L->Length; ++i) {
if(L->a[i]==e){
for (j = i; j < L->Length; j++) {
L->a[j] = L->a[j + 1];
}
L->Length--;
}
}
if (L->Length < k) {
return true;
}
else {
return false;
}
}
//测试(数值的交换)
void swap(int *a, int *b) {
int temp;
temp = *a;
//*a =* b;
*a = *b;
*b = temp;
printf("%d\t%d\n", *a, *b);
/*
main函数体内容测试
int* a, * b;
int i, j;
i = 1; j = 4;
a = &i; b = &j;
swap(a, b);
printf("%d\t%d\n", *a, *b);
*/
}
//练习-有一个顺序表 ,以第一个数值为基准,将所有大于它的数值都放到他的右边,反之放于左边
//练习方法一
void partitionalOne(one*& L) {
int temp = L->a[0]; int k;
int i = 0,j = 0;;
do {
if (temp>= L->a[j + 1]) {
i = j;
for (k = L->a[i + 1]; i+1 != 0; i--) {
//int tp = L->a[i + 1];
L->a[i + 1] = L->a[i];
}
L->a[0] = k;
}
j++;
} while (j < L->Length-1);
}
//练习方法二
void partitionalTwo(one*& L) {
int i = 0, j = L->Length - 1;
int num = L->a[0];
while (i < j) {
while (i<j && L->a[j]>num) {
j--;
}
while (i < j && L->a[j] <= num) {
i++;
}
if (i < j) {
swap(&L->a[i], &L->a[j]);
}
}
swap(&L->a[0], &L->a[i]);
}
//练习方法三
void partitionalThree(one*& L) {
int i = 0, j = L->Length - 1;
int num = L->a[0];
while (i < j) {
while (i<j && L->a[j]>num) {
j--;
}
L->a[i] = L->a[j];
while (i < j && L->a[j] <= num) {
i++;
}
L->a[j] = L->a[i];
}
L->a[i] = num;
}
int main() {
int choose,num6,num7,p7,num8;
//one* L;
one *L=NULL;
int a[] = { 0,1,2,3,4};
while (1) {
//printf("\n");
printf("\n\t\t\t 菜 单 \n");
printf("\n\t\t\t\t\t***************************************\n");
printf("\n\t\t\t\t\t1-创 建 表 2-销 毁 表 \n");
printf("\n\t\t\t\t\t3-检测表是否为空 4-求 表 的 长 度 \n");
printf("\n\t\t\t\t\t5-输 出 线 性 表 6-求表中的‘e’元素 \n");
printf("\n\t\t\t\t\t7-插 入‘e’元素 8-删 除 ‘e’ 元 素 \n");
printf("\n\t\t\t\t\t*************** 9-练习 ****************\n");
printf("\n\t\t\t\t\t*************** 0-退出 ****************\n");
printf("\n\t\t\t请选择:");
scanf("%d", &choose);
switch (choose){
case 1:
{
printf("请先设定要输入的数据数量\n\n\t\t\t\t\t!!!!!(注意:数据最多可储存%d个)!!!!!\n",MAXSIZE);
int num1;
scanf("%d", &num1);
printf("\n");
InitList(L);
CreatList(L, num1);
printf("\n");
system("pause");
break;
}
case 2: {
DestroyList(L); system("pause"); break;
}
case 3: {
if (ListEmpty(L) == false) {
printf("该表不为空\n");
}
else {
printf("该表是空表\n");
}
printf("\n");
system("pause");
break;
}
case 4:{
printf("表长为:%d\n",ListLength(L));
printf("\n");
system("pause");
break;
}
case 5: {
//InitList(L);
//CreatList2(L,a, MAXSIZE);
DisplayList(L);
printf("\n");
system("pause");
break;
}
case 6: {
int CA6;
printf("请输入你想要查询的元素\n");
scanf("%d", &num6);
CA6=GetList(L,num6);
if (CA6 == -1) {
printf("表中无该元素:%d\n", num6);
}
else {
printf("表中有'%d'元素\n%d是表中第%d个元素\n",num6,num6,CA6+1);
}
printf("\n");
system("pause");
break;
}
case 7: {
printf("请选择你要插入的元素和位置\n");
DisplayList(L);
scanf("%d\n%d", &num7,&p7);
bool CA7 = ListInsert(L, num7, p7);
if (CA7 != true) {
printf("插入失败\n");
}
//DisplayList(L);
printf("\n");
system("pause");
break;
}
case 8: {
printf("请选择你要删除的元素:\n");
DisplayList(L);
scanf("%d", &num8);
bool CA8 = DeleteList(L, num8);
if (CA8 != true) {
printf("删除失败,请你选择正确的删除项\n");
}
//DisplayList(L);
printf("\n");
system("pause");
break;
}
case 9: {
//partitionalOne(L);
//partitionalTwo(L);
partitionalThree(L);
DisplayList(L);
printf("\n");
system("pause");
break;
}
case 0:
return 0;
default: {
printf("请输入正确的选项");
printf("\n");
system("pause");
break;
}
}
}
return 0;
}