C语言: 顺序表的实现

实现功能:

创建顺序表

插入元素

删除元素

遍历元素

查找元素
 

实现的代码:

sqlist.h

#ifndef    aaa
#define    aaa
#define N 10
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct xx{

	int numb;


}Data;

typedef struct sq{

	Data  s[N];
	int count;

}sqlist;

//创建顺序表函数
sqlist *creat_sq();  

//第二种创建顺序表方式(地址更改值)
/*int creat_sq2(sqlist **z);  */    

//尾插法插入数字
int tail_insert(sqlist * z,int x);

//遍历顺序表(为了验证代码正确性)
int print_list(sqlist * z);

//任意按位置插入数字
int pos_insert(sqlist *z, int pos, int x);

//尾删法删除
int tail_delets(sqlist *z);

//任意按位置删除顺序表
int pos_delete(sqlist *z , int pos , int x);

//修改元素数据-按位置修改
int modify_pos(sqlist *z , int pos,int x);

//修改元素数据-按值修改
int modify_value(sqlist *z, int oldx, int x);

//按位置查找元素
int search(sqlist *z, int pos);

//将顺序表降序排序 
int sort(sqlist *z);

//顺序表的删除重复
int chong(sqlist *z);

//顺序表的清空
int clean(sqlist *z);

//顺序表的销毁
int distory(sqlist **z);

#endif

sqlist.c

#include"a.h"

//创建顺序表函数
sqlist *creat_sq(){

	sqlist * p =(sqlist *)malloc(sizeof(sqlist));
	if(p==NULL){

		printf("内存分配失败\n");
		return NULL;
	}
	memset(p,0,sizeof(sqlist));

	return p;
}  

//第二种创建顺序表方式
/*
   int creat_sq2(sqlist ** z){

   if(z==NULL){
   printf("传入的地址为空,请您检查...\n");
   return -1;
   }

 *z = (sqlist * )malloc(sizeof(sqlist));

 if(*z==NULL){
 printf("内存分配失败\n");
 return -1;
 }
 return 0;


 }
 */


//尾插法:
int tail_insert(sqlist *z,int x){
	if(z==NULL){
		printf("传入的参数为空指针\n");
		return -1;
	}

	if(z->count == N){
		printf("您的表满咯,插入失败\n");
		return -1;
	}


	z->s[z->count].numb = x;
	z->count++;

}


//遍历顺序表(为了验证代码正确性)
int print_list(sqlist * z){
	if(z==NULL){
		printf("传入的参数为空指针\n");
		return -1;
	}

	for(int i = 0 ; i < z->count ; i++){

		printf("%d ",z->s[i].numb);

	}
	puts("");

}


//任意按位置插入数字
int pos_insert(sqlist *z, int pos, int x){
	if(z==NULL){
		printf("传入的参数为空指针\n");
		return -1;
	}
	if(pos < 0 || pos > z->count){
		printf("输入的位置不合理\n");
		return -1;
	}


	for(int i =z->count ; i > pos; i--){

		z->s[i] = z->s[i-1];


	}
	z->s[pos].numb = x;
	z->count++;


}


//尾删法删除
int tail_delets(sqlist *z){
	if(z==NULL){
		printf("传入的参数为空指针\n");
		return -1;
	}


	z->count--;
}


//任意按位置删除顺序表
int pos_delete(sqlist *z , int pos , int x){
	if(z==NULL){
		printf("传入的参数为空指针\n");
		return -1;
	}
	if(pos < 0 || pos > z->count){
		printf("输入的位置不合理\n");
		return -1;
	}

	for(int i = pos; i < z->count-1;i++ ){

		z->s[i] = z->s[i+1];

	}
	z->count--;

}


//修改元素数据-按位置修改
int modify_pos(sqlist *z , int pos,int x){
	if(pos < 0 || pos > z->count){

		printf("输入的位置不合理\n");
		return -1;

	}
	z->s[pos].numb = x;

}


//修改元素数据-按值修改
int modify_value(sqlist *z, int oldx, int x){
	int c = 0;
	for(int i = 0 ; i < z->count; i++){

		if(z->s[i].numb == oldx){
			z->s[i].numb = x;
			c++;
		}
	}
	if(c==0){
		printf("没有修改的元素\n");
	}
}


//按位置查找元素
int search(sqlist *z, int pos){
	if(pos < 0 || pos > z->count){

		printf("输入的位置不合理\n");
		return -1;

	}
	printf("查找%d的元素为%d\n",pos,z->s[pos].numb);



}


int sort(sqlist *z){
	for(int i = 0 ; i < z->count-1; i++){
		for(int j = 0 ; j < z->count-1-i;j++ ){
			if(z->s[j+1].numb > z->s[j].numb){
				Data temp;
				temp = z->s[j];
				z->s[j] = z->s[j+1];
				z->s[j+1] = temp;
			}
		}
	}
}


//顺序表的删除重复
int chong(sqlist *z){
	if(z==NULL){
		printf("传入的参数为空指针\n");
		return -1;
	}
	for(int i = 0 ; i < z->count;i++){
		for(int j = i+1 ; j < z->count;j++){

			if(z->s[i].numb == z->s[j].numb){
				for(int w = j; w < z->count-1;w++){

					z->s[w] = z->s[w+1];


				}
				j--;
				z->count--;
			}
		}
	}
}


//顺序表的清空
int clean(sqlist * z){
	if(z==NULL){
		printf("传入的参数为空指针\n");
		return -1;
	}

	z->count = 0;

}

//顺序表的销毁
int distory(sqlist **z){
	if(*z == NULL || z == NULL){
		printf("失败!\n");
	}
	free(*z);
	*z=NULL;

}

main.c

#include"a.h"





int main(int argc, const char *argv[])
{
	sqlist * ymzs = creat_sq();

	/*  sqlist *sws=NULL;
		creat_sq2(&sws);*/

	tail_insert(ymzs,1);
	tail_insert(ymzs,2);
	tail_insert(ymzs,3);
	tail_insert(ymzs,4);
	tail_insert(ymzs,5);
	tail_insert(ymzs,6);                   
	print_list(ymzs); // 1 2 3 4 5 6

	pos_insert(ymzs,2,1);
	pos_insert(ymzs,2,1);
	print_list(ymzs); // 1 2 1 1 3 4 5 6

	tail_delets(ymzs);
	print_list(ymzs); // 1 2 1 1 3 4 5

	pos_delete(ymzs,2,1);
	pos_delete(ymzs,2,1);
	print_list(ymzs); // 1 2  3 4 5

	modify_pos(ymzs,0,5);
	modify_pos(ymzs,1,1);
	print_list(ymzs); // 5 1 3 4 5

	modify_value(ymzs,5,6);
	print_list(ymzs); // 6 1 3 4 6


	search(ymzs,0); // 6 1 3 4 6

	sort(ymzs);
	print_list(ymzs);// 6 6 4 3 1  

	chong(ymzs);
	print_list(ymzs);// 6  4 3 1 

	clean(ymzs);
	distory(&ymzs);


	return 0;
}

实现结果:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值