#include<stdlib.h>
#include<stdbool.h>
#include<stdio.h>
struct ARRAY
{
int *parr;
int len;
int cnt;
};
void swap(struct ARRAY *p,int n1,int n2);
bool is_empty(struct ARRAY *p);
bool is_full(struct ARRAY *p);
void init_seq_list(struct ARRAY p,int l)
{
(p).parr = malloc(lsizeof(int));
//p->parr = malloc(lsizeof(int));
if(p->parr == NULL)
{
printf(“创建顺序表失败!\n”);
exit(-1);
}
p->len = l;
p->cnt = 0;
}
bool is_full(struct ARRAY *p)
{
if(p->cnt == p->len)
return true;
else
return false;
}
bool insert_node(struct ARRAY *p,int pos,int data)
{
if(is_full§)
return false;
if(pos < 1 || pos > p->cnt+1)
return false;
for(int i=p->cnt-1;i >= pos-1;i--)
{
p->parr[i+1]=p->parr[i];
}
p->parr[pos-1]=data;
p->cnt++;
return true;
}
void show_seq(struct ARRAY *p)
{
if(is_empty§)
{
return ;
}
for(int i=0;icnt;i++)
{
printf("%d\t",p->parr[i]);
}
printf("\n");
}
bool is_empty(struct ARRAY *p)
{
if(p->cnt==0)
return true;
else
return false;
}
void delete_node(struct ARRAY *p,int delete_node)
{
int sign=p->cnt-delete_node;
int i = delete_node-1;
for(int j=0;sign >= 0;sign–,j++)
{
p->parr[i+j]=p->parr[i+j+1];
}
p->cnt–;
}
void inversion_list(struct ARRAY *p)
{
int i;
int a = p->cnt;
for(i=0;a > (p->cnt)/2;i++,a–)
{
int b=a-1;
swap(p,b,i);
}
}
void sort_list(struct ARRAY *p)
{
int i,j;
for(i=0;i < (p->cnt);i++)
{
for(j=0;j < (p->cnt-1);j++)
{
if(p->parr[j] > p->parr[j+1])
{
swap(p,j,j+1);
}
}
}
}
void swap(struct ARRAY *p,int n1,int n2)
{
int tem;
tem = p->parr[n1];
p->parr[n1] = p->parr[n2];
p->parr[n2] = tem;
}
int main(void)
{
struct ARRAY a;
int choice_f,delete_node_of_num,n1,n2;
init_seq_list(&a,10);
while(1)
{
printf("*****************************************************************\n");
printf("1、添加节点\n");
printf("2、删除节点\n");
printf("3、倒置节点\n");
printf("4、排序节点\n");
printf("5、退出\n");
printf("选择:");
scanf("%d",&choice_f);
switch(choice_f)
{
case 1:
printf("请输入你的数值(格式:第几个节点添加 数值/1 数值...):\n");
scanf("%d %d",&n1,&n2);
insert_node(&a,n1,n2);
show_seq(&a);
break;
case 2:
printf("你要删除哪个节点:\n");
scanf("%d",&delete_node_of_num);
delete_node(&a,delete_node_of_num);
show_seq(&a);
break;
case 3:
printf("倒置如下:\n");
inversion_list(&a);
show_seq(&a);
break;
case 4:
printf("排序如下:\n");
sort_list(&a);
show_seq(&a);
break;
case 5:
printf("BYEBYE!\n");
goto end;
}
}
end:
return 0;
}