顺序表
main.c
#include "list.h"
int main()
{
int number;
menue();
SeqlistTyp *List=create();
while(1)
{
printf("请输入您的选择:");
scanf("%d",&number);
switch (number)
{
case 1:
{
int n,e;
printf("请输入尾插的个数:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("请输入尾插的数据元素:");
scanf("%d",&e);
Insert_rear(e,List);
}
}
break;
case 2:
{
Delete_rear(List);
}
break;
case 3:
{
output(List);
}
break;
case 4:
{
int sub;
scanf("请选择要查找的下标:%d",&sub);
Find_sub(List,sub);
}
break;
case 5:
{
int sub,e;
printf("请选择要修改的下标:");
scanf("%d",&sub);
printf("请输入要修改的内容:");
scanf("%d",&e);
Updata_sub(List,sub,e);
}
break;
case 6:
{
int sub,e;
printf("请选择要插入的下标:");
scanf("%d",&sub);
printf("请输入要插入的内容:");
scanf("%d",&e);
Middle_Insert(List,sub,e);
}
break;
case 7:
{
int sub;
printf("请选择要删除元素的下标:");
scanf("%d",&sub);
Middle_Delete(List,sub,1);
}
break;
case 8:
{
int Num;
printf("请输入要查找的元素:");
scanf("%d",&Num);
Find_Num(List,Num);
}
break;
case 9:
{
int Num,e;
printf("请输入要查找的元素:");
scanf("%d",&Num);
printf("请输入要插入的内容:");
scanf("%d",&e);
Add_Num(List,Num,e);
}
break;
case 10:
{
int Num;
printf("请输入要删除的元素:");
scanf("%d",&Num);
Delete_Num(List,Num);
}
break;
case 11:
{
int Num,e;
printf("请输入要修改的元素:");
scanf("%d",&Num);
printf("请输入要修改的内容:");
scanf("%d",&e);
Updata_Num(List,Num,e);
}
break;
case 12:
{
int a,b;
printf("请选择排序方式:1、冒泡|2、选择:");
scanf("%d",&a);
printf("请选择:1、降序|2、升序:");
scanf("%d",&b);
Sort_Num(List,a,b);
}
break;
case 13:
{
Merge(List);
}
break;
case 14:
{
SeqlistTyp la={11,11,12,14,16};
la.len=5;
SeqlistTyp lb={10,13,15,18,19,20};
lb.len=6;
SeqlistTyp lc;
lc.len=0;
Combine(&la,&lb,&lc);
output(&lc);
}
break;
case 15:
{
free(List);
List=NULL;
printf("列表空间已被释放!\n");
}
break;
default:printf("输入错误,请重新输入\n");break;
case 0:free(List);printf("退出程序\n");exit(0);
}
}
return 0;
}
list.c
#include "list.h"
void menue()
{
puts("\t************");
puts("\t*1、表尾部插入");
puts("\t*2、表尾删除");
puts("\t*3、顺序表遍历");
puts("\t*4、按下标查找");
puts("\t*5、按下标修改");
puts("\t*6、按下标插入");
puts("\t*7、按下标删除");
puts("\t*8、按元素查找");
puts("\t*9、按元素插入");
puts("\t*10、按元素删除");
puts("\t*11、按元素修改");
puts("\t*12、按元素大小排序");
puts("\t*13、删除列表中所有重复的元素");
puts("\t*14、有序合并两组数");
puts("\t*15、释放列表空间");
puts("\t*0、退出整个程序");
puts("\t************");
}
SeqlistTyp *create()
{
SeqlistTyp *List=(SeqlistTyp *)malloc(sizeof(SeqlistTyp));
if(List==NULL)
{
return NULL;
}
List->len=0;
return List;
}
int Empty_Seqlist(SeqlistTyp *List)
{
return List->len==0?-1:0;
}
int Insert_rear(int e,SeqlistTyp *List)
{
if(List==NULL || List->len==Max_size)
{
printf("插入失败!");
return -1;
}
List->data[List->len]=e;
List->len++;
return 0;
}
int Delete_rear(SeqlistTyp *List)
{
if(List==NULL || Empty_Seqlist(List))
{
printf("删除失败!\n");
return -1;
}
List->len--;
printf("尾删成功!\n");
return 0;
}
int output(SeqlistTyp *List)
{
if(List==NULL||List->len==0)
{
puts("顺序表为空\n");
return -1;
}
printf("列表:");
for(int i=0;i<List->len;i++)
{
printf("%d ",List->data[i]);
}
puts("");
}
int Find_sub(SeqlistTyp *List,int sub)
{
if(List==NULL||List->len==0||sub<0||sub>=List->len)
{
puts("查找失败!\n");
return -1;
}
printf("下标为%d的元素为:%d\n",sub,List->data[sub]);
return List->data[sub];
}
int Updata_sub(SeqlistTyp *List,int sub,int e)
{
if(List==NULL||List->len==0||sub<0||sub>=List->len)
{
puts("修改失败!\n");
return -1;
}
List->data[sub]=e;
printf("修改成功!\n");
output(List);
return 0;
}
int Middle_Insert(SeqlistTyp *List,int sub,int e)
{
int i;
if(List==NULL||sub<0||sub>List->len|| List->len==Max_size)
{
puts("插入失败!\n");
return -1;
}
for(i=List->len-1;i>=sub;i--)
{
List->data[i+1]=List->data[i];
}
List->len++;
List->data[sub]=e;
puts("插入成功!\n");
output(List);
return 0;
}
int Middle_Delete(SeqlistTyp *List,int sub,char fg)
{
int i;
if(List==NULL||sub<0||sub>List->len)
{
if(fg==1)
{
puts("删除失败!\n");
}
return -1;
}
for(i=sub;i<List->len-1;i++)
{
List->data[i]=List->data[i+1];
}
List->len--;
if(fg==1)
{
puts("删除成功!\n");
output(List);
}
return 0;
}
int Find_Num(SeqlistTyp *List,int Num)
{
int i;
if(List==NULL||List->len==0)
{
puts("查找失败!\n");
return -1;
}
for(i=0;i<List->len;i++)
{
if(List->data[i]==Num)
{
printf("该元素的下标为%d\n",i);
return i;
}
}
puts("没有此元素!\n");
return -2;
}
int Add_Num(SeqlistTyp *List,int Num,int e)
{
int flag;
flag=Find_Num(List,Num);
if(flag==-1)
{
return -1;
}
else
{
Middle_Insert(List,flag+1,e);
}
return 0;
}
int Delete_Num(SeqlistTyp *List,int Num)
{
int flag;
flag=Find_Num(List,Num);
if(flag==-1)
{
return -1;
}
else
{
Middle_Delete(List,flag,1);
}
return 0;
}
int Updata_Num(SeqlistTyp *List,int Num,int e)
{
int flag;
flag=Find_Num(List,Num);
if(flag==-1)
{
return -1;
}
else
{
Updata_sub(List,flag,e);
}
return 0;
}
int Sort_Num(SeqlistTyp *List,int a,int b)
{
int temp;
if(List==NULL||List->len==0)
{
puts("顺序表为空\n");
return -1;
}
if(a==1)
{
for(int i=0;i<List->len;i++)
{
for(int j=i+1;j<List->len;j++)
{
if(List->data[i]<List->data[j] && b==1)
{
temp=List->data[i];
List->data[i]=List->data[j];
List->data[j]=temp;
}
else if(List->data[i]>List->data[j] && b!=1)
{
temp=List->data[i];
List->data[i]=List->data[j];
List->data[j]=temp;
}
}
}
}else
{
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]&& b==1)
{
min=j;
}else if(List->data[min] > List->data[j]&& b!=1)
{
min=j;
}
}
if(min!=i)
{
int temp=List->data[min];
List->data[min]=List->data[i];
List->data[i]=temp;
}
}
}
if(b==1)
{
printf("列表元素降序排列:");
}
else
{
printf("列表元素升序排列:");
}
output(List);
}
int Merge(SeqlistTyp *List)
{
if(List==NULL || Empty_Seqlist(List))
{
printf("去重失败\n");
}
for(int i=0;i<List->len-1;i++)
{
for(int j=i+1;j<List->len;j++)
{
if(List->data[i]==List->data[j])
{
Middle_Delete(List,j,0);
j--;
}
}
}
printf("去重成功\n");
}
void Combine(SeqlistTyp *la,SeqlistTyp *lb,SeqlistTyp *lc)
{
int p=0,q=0;
while(p<la->len && q<lb->len)
{
if(la->data[p] <= lb->data[q])
{
lc->data[lc->len++]=la->data[p++];
}
else
{
lc->data[lc->len++]=lb->data[q++];
}
}
while(p<la->len)
{
lc->data[lc->len++]=la->data[p++];
}
while(q<lb->len)
{
lc->data[lc->len++]=lb->data[q++];
}
}
list.h
#ifndef __LIST_
#define __LIST_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Max_size 20
typedef struct
{
int data[Max_size];
int len;
}SeqlistTyp;
SeqlistTyp *create();
void menue();
int Insert_rear(int e,SeqlistTyp *List);
int output(SeqlistTyp *List);
int Empty_Seqlist(SeqlistTyp *List);
int Insert_rear(int e,SeqlistTyp *List);
int Delete_rear(SeqlistTyp *List);
int Find_sub(SeqlistTyp *List,int sub);
int Updata_sub(SeqlistTyp *List,int sub,int e);
int Middle_Insert(SeqlistTyp *List,int sub,int e);
int Middle_Delete(SeqlistTyp *List,int sub,char fg);
int Find_Num(SeqlistTyp *List,int Num);
int Add_Num(SeqlistTyp *List,int Num,int e);
int Delete_Num(SeqlistTyp *List,int Num);
int Updata_Num(SeqlistTyp *List,int Num,int e);
int Sort_Num(SeqlistTyp *List,int a,int b);
int Merge(SeqlistTyp *List);
void Combine(SeqlistTyp *la,SeqlistTyp *lb,SeqlistTyp *lc);
#endif