head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define MAXSIZE 5
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int data[MAXSIZE];
int len;
}seqlist_t;
void menue();
seqlist_t *get_space();
int insert_rear(seqlist_t *list,int e);
int output(seqlist_t *list);
int delete_rear(seqlist_t *list);
int find_sub(seqlist_t *list, int sub);
int xiugai(seqlist_t *list,int sub,int n);
void exist_list(seqlist_t *list);
int charu(seqlist_t *list,int sub,int n);
int delete_sub(seqlist_t *list,int sub);
int insert_element(seqlist_t *list,int n,int z);
int delete_element(seqlist_t *list,int n);
int modify_element(seqlist_t *list,int n,int z);
int find_element(seqlist_t *list,int n);
int list_sort(seqlist_t *list,int sign);
seqlist_t *free_space(seqlist_t *list);
int remove_dumplicate(seqlist_t *list);
void input_element(seqlist_t *list);
void list_merge(seqlist_t *list,seqlist_t *list1,seqlist_t *list2);
void merge();
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
menue();
seqlist_t *list=get_space();
int number;
while(1){
printf("请输入您的选择:");
scanf("%d", &number);
switch(number){
case 1://表尾部插入
{
int n,i;
printf("请输入插入个数\n");
scanf("%d", &n);
for(i=0;i<n;i++){
int e;
printf("请输入插入数值:");
scanf("%d", &e);
insert_rear(list,e);
}
break;
}
case 2://表尾部删除
{
delete_rear(list);
break;
}
case 3://顺序表遍历
{
output(list);
break;
}
case 4://按下标查找
{
int n;
printf("请输入要查找的下标\n");
scanf("%d", &n);
find_sub(list,n);
break;
}
case 5://按下标修改
{
int sub,n;
printf("要修改的下标和值:");
scanf("%d %d", &sub,&n);
xiugai(list,sub,n);
break;
}
case 6://按下标插入
{
int sub,n;
printf("请输入插入的下标和值:");
scanf("%d %d", &sub,&n);
charu(list,sub,n);
break;
}
case 7:
{
int sub;
printf("请输入要删除值的下标:");
scanf("%d", &sub);
delete_sub(list,sub);
break;
}
case 8:
{
int n,z;
printf("请输入要在哪个元素后面插入和值:");
scanf("%d %d", &n,&z);
insert_element(list,n,z);
break;
}
case 9:
{
int n;
printf("请输入要删除的元素:");
scanf("%d", &n);
delete_element(list,n);
break;
}
case 10:
{
int n,z;
printf("请输入要修改的元素和值:");
scanf("%d %d", &n,&z);
modify_element(list,n,z);
break;
}
case 11:
{
int n;
printf("请输入要查找的元素:");
scanf("%d", &n);
find_element(list,n);
break;
}
case 12:
{
int sign;
printf("请输入排序方式:");
printf("冒泡输入:1,选择输入任意值:");
scanf("%d", &sign);
list_sort(list,sign);
break;
}
case 13:
{
list=free_space(list);
if(NULL==list){
printf("空间释放成功\n");
}
break;
}
case 14:
{
remove_dumplicate(list);
printf("去重成功\n");
break;
}
case 15:
{
merge();
break;
}
default:printf("输入错误,请重新输入\n");break;
case 0:printf("退出程序\n");exit(0);
}
}
return 0;
}
head.c
#include "head.h"
void menue(){
puts("1,表尾部插入");
puts("2,表尾部删除");
puts("3,顺序表遍历");
puts("4,按下表查找");
puts("5,按下标修改");
puts("6,按下标插入");
puts("7,按下标删除");
puts("8,按元素插入");
puts("9,按元素删除");
puts("10,按元素修改");
puts("11,按元素查找");
puts("12,顺序表排序");
puts("13,释放空间");
puts("14,顺序表去重");
puts("15,顺序表有序合并");
puts("0,退出程序");
}
//申请空间
seqlist_t *get_space(){
seqlist_t *list=(seqlist_t *)malloc(sizeof(seqlist_t));
if(NULL==list){
printf("申请失败\n");
}
list->len=0;//初始化
return list;
}
//表尾插入
int insert_rear(seqlist_t *list,int e){
if(NULL==list||MAXSIZE==list->len){//判断表满
printf("插入表失败\n");
return -1;
}
list->data[list->len]=e;
list->len++;
return 0;
}
//顺序表遍历
int output(seqlist_t *list){
if(NULL==list||0==list->len){//判断表空
printf("表为空");
return -1;
}
int i;
for(i=0;i<list->len;i++){
printf("%d\t", list->data[i]);
}
puts("");
return 0;
}
//尾部删除
int delete_rear(seqlist_t *list){
if(NULL==list||0==list->len){
printf("删除失败\n");
return -1;
}
list->len--;
printf("删除成功\n");
return 0;
}
//按下标查找
int find_sub(seqlist_t *list, int sub){
if(NULL==list||0==list->len||sub<0||sub>=list->len){
printf("查找失败\n");
return -1;
}
printf("查找的下标值:%d\n", list->data[sub]);
return 0;
}
//按下标修改
int xiugai(seqlist_t *list,int sub,int n){
if(NULL==list||0==list->len||sub<0||sub>=list->len){
printf("修改失败\n");
return -1;
}
list->data[sub]=n;
printf("修改成功\n");
return 0;
}
//判断表是否存在
void exist_list(seqlist_t *list){
if(NULL==list){
printf("表不存在\n");
}
if(0==list->len){
printf("表为空,请输入数据再操作\n");
}
}
//按下标插入
int charu(seqlist_t *list,int sub,int n){
if(NULL==list||MAXSIZE==list->len||sub<0||sub>list->len){
printf("插入失败\n");
return -1;
}
int i;
for(i=list->len-1;i>=sub;i--){
list->data[i+1]=list->data[i];
}
list->data[sub]=n;
list->len++;
return 0;
}
//按下标删除
int delete_sub(seqlist_t *list,int sub){
if(NULL==list||0==list->len||sub<0||sub>=list->len){
printf("删除失败\n");
return -1;
}
int i;
for(i=sub;i<list->len-1;i++){
list->data[i]=list->data[i+1];
}
list->len--;
return 0;
}
//按元素插入
int insert_element(seqlist_t *list,int n,int z){//表,要搜索的元素,要插入的元素
if(NULL==list||MAXSIZE==list->len){
printf("插入失败\n");
return -1;
}
int i;
for(i=0;i<list->len;i++){
if(n==list->data[i]){
charu(list,i+1,z);
return 0;
}
}
printf("搜索元素不存在\n");
return -1;
}
//按元素删除
int delete_element(seqlist_t *list,int n){//表,要删除的元素
if(NULL==list||0==list->len){
printf("删除失败\n");
return -1;
}
int i;
for(i=0;i<list->len;i++){
if(n==list->data[i]){
delete_sub(list,i);
return 0;
}
}
printf("要删除的元素不存在\n");
return -1;
}
//按元素修改
int modify_element(seqlist_t *list,int n,int z){//表,被修改的元素,待写入的元素
if(NULL==list||0==list->len){
printf("修改元素失败\n");
return -1;
}
int i;
for(i=0;i<list->len;i++){
if(n==list->data[i]){
list->data[i]=z;
return 0;
}
}
printf("被修改的元素不存在\n");
return -1;
}
//按元素查找,输出元素的下标
int find_element(seqlist_t *list,int n){//表,要查找的元素
if(NULL==list||0==list->len){
printf("查找元素失败\n");
return -1;
}
int i;
for(i=0;i<list->len;i++){
if(n==list->data[i]){
printf("查找元素的下标:%d\n", i);
return 0;
}
}
printf("查找的元素不存在\n");
return -1;
}
//顺序表排序
int list_sort(seqlist_t *list,int sign){//表,排序方式
if(NULL==list||0==list->len){
printf("排序失败\n");
return -1;
}
if(1==sign){//选择冒泡排序
int i,j,temp;
for(i=0;i<list->len-1;i++){
for(j=0;j<list->len-1-i;j++){
if(list->data[j]<list->data[j+1]){
temp=list->data[j];
list->data[j]=list->data[j+1];
list->data[j+1]=temp;
}
}
}
}else{//选择选择排序
int i,j,temp;
for(i=0;i<list->len-1;i++){
for(j=i;j<list->len;j++){
if(list->data[i]<list->data[j]){
temp=list->data[i];
list->data[i]=list->data[j];
list->data[j]=temp;
}
}
}
}
}
//顺序表释放空间
seqlist_t *free_space(seqlist_t *list){
free(list);
list=NULL;
return list;
}
//顺序表去重
int remove_dumplicate(seqlist_t *list){
if(NULL==list||0==list->len){
printf("去重失败\n");
return -1;
}
int i,j;
for(i=0;i<list->len-1;i++){
for(j=i+1;j<list->len;j++){
if(list->data[i]==list->data[j]){
delete_sub(list,j);
}
}
}
return 0;
}
//顺序表有序合并
void merge(){
seqlist_t *list1=get_space();
seqlist_t *list2=get_space();
seqlist_t *list=get_space();
printf("请给list1输入:");
input_element(list1);
printf("请给list2输入:");
input_element(list2);
//两表分别排序
int sign;
printf("请输入排序方式:");
printf("冒泡输入:1,选择输入任意值:");
scanf("%d", &sign);
list_sort(list1,sign);
list_sort(list2,sign);
//表遍历
output(list1);
output(list2);
//顺序合并
list_merge(list,list1,list2);
//表遍历
output(list);
}
//数值输入
void input_element(seqlist_t *list){//表,插入数量
int i,n;
printf("请输入插入表数值个数,数量不超过4个:\n");
scanf("%d", &n);
for(i=0;i<n;i++){
int e;
printf("请输入插入数值:");
scanf("%d", &e);
insert_rear(list,e);
}
}
//两表顺序合并
void list_merge(seqlist_t *list,seqlist_t *list1,seqlist_t *list2){
int i=0,j=0;
while(i<list1->len&&j<list2->len){
if(list1->data[i]>list2->data[j]){
list->data[list->len]=list1->data[i];
i++;
list->len++;
}else{
list->data[list->len]=list2->data[j];
j++;
list->len++;
}
}
if(i==list1->len){
while(j<list2->len){
list->data[list->len]=list2->data[j];
list->len++;
j++;
}
}else if(j==list2->len){
while(i<list1->len){
list->data[list->len]=list1->data[i];
list->len++;
i++;
}
}
}