#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAXSIZE 65535
void insert(int *, int, int);
bool search(int *, int, int);
void printlist(int *, int);
int main()
{
int sqlist[MAXSIZE] = {1, 2, 3, 5, 8, 12, 16, 18, 22, 25, 29, 77, 89, 92};
int len = 14, ins;
printf("输入要插入的数字(输入ctrl + Z退出):");
while(~scanf("%d", &ins))
{
insert(sqlist, ins, len);
len++;
printf("输入要删除的数字:");
scanf("%d", &ins);
bool delete_success = search(sqlist, ins, len);
printf(delete_success ? "Delete Success!\n" : "Delete Failed!\n");
len = delete_success ? len - 1 : len;
printf("输出结果如下:\n");
printlist(sqlist, len);
printf("输入要插入的数字(输入ctrl + Z退出):");
}
return 0;
}
void insert(int sq[], int x, int n)
{
int loc = n;
for(int i = 0; i < n; i++)
{
if(sq[i] > x)
{
loc = i;
break;
}
}
for(int i = n - 1; i >= loc; i--)
sq[i + 1] = sq[i];
sq[loc] = x;
}
bool search(int sq[], int x, int n)
{
for(int i = 0; i < n; i++)
if(sq[i] == x)
{
for(int j = i; j < n; j++)
sq[j] = sq[j + 1];
return true;
}
return false;
}
void printlist(int sq[], int n)
{
for(int i = 0; i < n; i++)
printf(i ? " %d" : "%d", sq[i]);
putchar(10);
}
|