顺序表的初始化,插入,删除,遍历,倒序,排序

#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(l
sizeof(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;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值