main.c
#include "head.h"
int main(int argc, const char *argv[])
{
/*
Seqlist *list=create_seqlist();
datatype e;//定义输入数据元素
int n;
printf("输入个数:");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("输入插入表:\n");
scanf("%d",&e);
if(insert_rear(e,list)){
printf("错误!\n");
break;
}
}
output(list);
*/
/*
//删除
delete_Seqlist(list);
output(list);
//查找
printf("输入要查找的下标:");
int sub;
scanf("%d",&sub);
printf("值为:%d\n",search_sub(sub,list));
//修改值
datatype num=0;
printf("修改的值和下标");
scanf("%d %d",&num,&sub);
chang_Seqlist(sub,num,list);
output(list);
//按下标插入
printf("输入下要插入的值和下标");
scanf("%d %d",&num,&sub);
insert_sub(sub,num,list);
output(list);
//按下标删除
printf("输入想要删除的值的下标\n");
int del_sub;
scanf("%d",&del_sub);
delete_Seqlist_sub(del_sub,list);
output(list);
*/
//按元素查找
/*
datatype num;
datatype key;
printf("请输入元素");
scanf("%d",&key);
scanf("%d",&num);
int sub=search_data(list,num);
if(sub==-1)
printf("失败\n");
else
printf("在%d下标\n",sub);
*/
//element_replase(list,key,num);
//element_delete(list,key);
//element_insert(list,key,num);
/*
bubble_Seqlist(list);
output(list);
ss(list);
output(list);
//释放
list=free_sp(list);
*/
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXs 9//定义线性表长度
typedef int datatype;//自定义数据元素类型
//定义顺序表
typedef struct{
datatype data[MAXs];//数据元素
int len;//顺序表长度
}Seqlist;
Seqlist *create_seqlist();
int insert_rear(datatype e,Seqlist *list);
void output(Seqlist *list);
Seqlist *free_sp(Seqlist *list);
int delete_Seqlist(Seqlist *list);
int search_sub(int sub,Seqlist *list);
int chang_Seqlist(int sub,datatype num,Seqlist *list);
int insert_sub(int sub,datatype num,Seqlist *list);
int delete_Seqlist_sub(int sub,Seqlist *list);
int search_data(Seqlist *list,datatype num);
int element_replase(Seqlist *list,datatype key,datatype num);
int element_delete(Seqlist *list,datatype key);
int element_insert(Seqlist *list,datatype key,datatype num);
int bubble_Seqlist(Seqlist *list);
int ss(Seqlist *list);
void combine(Seqlist *la,Seqlist *lb,Seqlist *lc);
#endif
test.c:
#include "head.h"
/*
* function: 在堆区申请空间
* @param [ in] 无
* @param [out]
* @return 如果成功返回地址,失败返回NULL
*/
Seqlist *create_seqlist(){
Seqlist *list=(Seqlist *)malloc(sizeof(Seqlist));
if(NULL==list)return NULL;
//初始化数据元素,可以不写
memset(list->data,0,sizeof(list->data));
//初始化顺序表长度,必须写
list->len=0;
return list;
}
/*
* function: 判断线序表是否为满
* @param [ in]
* @param [out]
* @return 成功返回0,否则-1
*/
int full_Seqlist(Seqlist *list){
return list->len==MAXs?-1:0;
}
/*
* function: 尾部插入
* @param [ in]
* @param [out] 顺序表 个数
* @return 如果成功返回0,不成功返回-1
*/
int insert_rear(datatype e,Seqlist *list){
if(NULL==list || full_Seqlist(list))return -1;
list->data[list->len]=e;
list->len++;
printf("len=%d\n",list->len);
return 0;
}
/*
* function: 判断len是否为空
* @param [ in]
* @param [out]
* @return 是返回-1,不是返回0
*/
int empty(Seqlist *list){
return list->len==0?-1:0;
}
/*
* function: 输出
* @param [ in]
* @param [out] 顺序表
* @return 无
*/
void output(Seqlist *list){
if(NULL==list || empty(list))return;
for(int i=0;i<list->len;i++){
printf("%d\t",list->data[i]);
}
puts("");
}
/*
* function: 尾删
* @param [ in]
* @param [out]
* @return 成功为0,否则-1
*/
int delete_Seqlist(Seqlist *list){
if(NULL==list || empty(list))return -1;
list->len--;
return 0;
}
/*
* function: 查找
* @param [ in] 输入下标和list
* @param [out]
* @return 返回值。失败返回-1;
*/
int search_sub(int sub,Seqlist *list){
if(NULL==list||empty(list)||sub<0 ||sub>=list->len){
return -1;
}
return list->data[sub];
}
/*
* function: 任意位置修改值
* @param [ in]
* @param [out]
* @return
*/
int chang_Seqlist(int sub,datatype num,Seqlist *list){
if(NULL==list||empty(list)||sub<0 ||sub>=list->len){
return -1;
}
list->data[sub]=num;
return 0;
}
/*
* function: 按下标插入
* @param [ in]
* @param [out] 顺序表 下标 值
* @return 成功0 失败-1
*/
int insert_sub(int sub,datatype num,Seqlist *list){
if(NULL==list||full_Seqlist(list)||sub<0 ||sub>=list->len){
printf("失败!\n");
return -1;
}
for(int i=list->len-1;i>=sub;i--){
list->data[i+1]=list->data[i];
}
list->data[sub]=num;
list->len++;
return 0;
}
/*
* function: 按下标删除
* @param [ in]
* @param [out]
* @return
*/
int delete_Seqlist_sub(int sub,Seqlist *list){
if(NULL==list||empty(list)||sub<0 ||sub>=list->len){
printf("失败!\n");
return -1;
}
for(int i=sub;i<list->len;i++){
list->data[i]=list->data[i+1];
}
list->len--;
return 0;
}
/*
* function: 按元素查找
* @param [ in]
* @param [out]
* @return 返回下标
*/
int search_data(Seqlist *list,datatype num){
if(NULL==list || empty(list))return -1;
for(int i=0;i<list->len;i++){
if(num==list->data[i])
return i;
}
return -1;
}
/*
* function: 按元素替换
* @param [ in]
* @param [out] 顺序表,要修改的值,修改后的值
* @return 成功0;失败-1
*/
int element_replase(Seqlist *list,datatype key,datatype num){
int sub=search_data(list,key);
if(sub==-1)return -1;
chang_Seqlist(sub,num,list);
return 0;
}
/*
* function: 按元素删除
* @param [ in]
* @param [out] list key
* @return
*/
int element_delete(Seqlist *list,datatype key){
int sub=search_data(list,key);
if(sub==-1)return -1;
delete_Seqlist_sub(sub,list);
return 0;
}
/*
* function: 按元素插入
* @param [ in]
* @param [out] list key num
* @return
*/
int element_insert(Seqlist *list,datatype key,datatype num){
int sub=search_data(list,key);
if(sub==-1)return -1;
insert_sub(sub,num,list);
return 0;
}
/*
* function: 顺序表排序-冒泡升序
* @param [ in]
* @param [out] Seqlist *list
* @return 成功0;失败-1
*/
int bubble_Seqlist(Seqlist *list){
if(NULL==list || empty(list))return -1;
for(int i=1;i<list->len;i++){
int count=0;
for(int j=0;j<list->len-i;j++){
if(list->data[j]<list->data[j+1]){
int t=list->data[j];
list->data[j]=list->data[j+1];
list->data[j+1]=t;
count++;
}
}
if(count==0)return 0;
}
return 0;
}
/*
* function: 顺序表排序-简单选择降序
* @param [ in]
* @param [out] Seqlist *list
* @return 成功0;失败-1;
*/
int ss(Seqlist *list){
if(NULL==list || empty(list))return -1;
for(int i=0;i<list->len-1;i++){
int min=i;
for(int j=i+1;j<list->len;j++){
if(list->data[min]>list->data[j])
min=j;
}
if(min!=i){
int t=list->data[min];
list->data[min]=list->data[i];
list->data[i]=t;
}
}
return 0;
}
/*
* function: 顺序表合并
* @param [ in]
* @param [out] 三个顺序表地址
* @return
*/
void combine(Seqlist *la,Seqlist *lb,Seqlist *lc){
int pa=0;//la下标
int pb=0;//lb下标
while(pa<la->len && pb<lb->len){
if(la->data[pa]<=lb->data[pb])
lc->data[lc->len++]=la->data[pa++];
else
lc->data[lc->len++]=lb->data[pb++];
}
while(pa<la->len)
lc->data[lc->len++]=la->data[pa++];
while(pb<lb->len)
lc->data[lc->len++]=lb->data[pb++];
}
/*
* function: 释放堆区空间
* @param [ in]
* @param [out]
* @return
*/
Seqlist *free_sp(Seqlist *list){
if(NULL==list){
return NULL;
}
free(list);
list=NULL;
return list;
}