蒟蒻的我,今天折腾了3个小时,终于把数据结构的线性表源码,用C语言写出来了,qwq
数据结构线性表源码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define cmax 30
struct Sqlist
{
int data[cmax];
int length;
};
void AscendingSequence(struct Sqlist *L)
{
int tmp;
if(L->length<1||L->length>cmax)
{
printf("ascending sequence fail
");
return ;
}
else
{
int i=0,j=0;
for(i=0;i<L->length-1;i++)
{
for(j=0;j<L->length-(i+1);j++)
if(L->data[j]>L->data[j+1])
{
tmp=L->data[j+1];
L->data[j+1]=L->data[j];
L->data[j]=tmp;
}
}
printf("Celebrate You,ascending sequence OK!
");
}
}
void DescendingOrder(struct Sqlist *L)
{
int tmp;
if(L->length<1||L->length>cmax)
{
printf("Descending Order fail
");
return ;
}
else
{
int i=0,j=0;
for(i=0;i<L->length-1;i++)
{
for(j=0;j<L->length-(i+1);j++)
if(L->data[j]<L->data[j+1])
{
tmp=L->data[j+1];
L->data[j+1]=L->data[j];
L->data[j]=tmp;
}
}
printf("Celebrate You,Descending Order OK!
");
}
}
void Initlist(struct Sqlist *L)
{
L->length=0;
memset(L->data,0,sizeof(L->data));
}
int InsertSqlist(struct Sqlist *L,int i,int val)
{
if(i<1||i>L->length+1||L->length==cmax)
{
printf("insert fail
");
return -1;
}
else
{
int j=L->length-1;
for(;j>=i-1;j--)
{
L->data[j+1]=L->data[j];
}
L->data[i-1]=val;
L->length++;
return 1;
}
}
void ShowSqlist(struct Sqlist L)
{
int j=0;
for(;j<L.length;j++)
printf("%d ",L.data[j]);
printf("
");
return ;
}
int DeleteSqlist(struct Sqlist *L,int i)
{
if(i<1||i>L->length||L->length==0)
{
printf("delete fail
");
return -1;
}
else
{
int j=i-1;
for(;j<L->length-1;j++)
L->data[j]=L->data[j+1];
L->length--;
printf("Celebrate You,delete OK!
");
return 1;
}
}
int GetSqlist(struct Sqlist L,int i)
{
return L.data[i-1];
}
int main()
{
struct Sqlist L;
Initlist(&L);
char s[50];
int t,flag=0;
printf("请输入一些数进行初始化线性表,最大长度不得超过30,输入-1代表输入结束:
");
while(~scanf("%d",&t))
{
if(t==-1)
break;
flag++;
InsertSqlist(&L,flag,t);
}
printf("初始化之后的线性表的元素为:
");
ShowSqlist(L);
printf("请输入你要进行的相关指令(输入end代表结束):insert,show,delete,get,AscendingSequence,DescendingOrder.
");
while(~scanf("%s",s))
{
if(strcmp(s,"end")==0)
break;
if(strcmp(s,"insert")==0)
{
int tmp,val,k;
printf("请输入要插入的位置和值;中间以空格隔开:");
scanf("%d%d",&tmp,&val);
k=InsertSqlist(&L,tmp,val);
if(k==1)
{
printf("Celebrate You,insert OK!
");
printf("插入%d后,线性表元素为:
",val);
ShowSqlist(L);
}
}
if(strcmp(s,"AscendingSequence")==0)
{
AscendingSequence(&L);
printf("升序后的线性表元素为:
");
ShowSqlist(L);
}
if(strcmp(s,"DescendingOrder")==0)
{
DescendingOrder(&L);
printf("降序后的线性表元素为:
");
ShowSqlist(L);
}
if(strcmp(s,"delete")==0)
{
int tmp,k;
printf("请输入要删除的位置:");
scanf("%d",&tmp);
k=DeleteSqlist(&L,tmp);
if(k==1)
{
printf("删除%d位置元素之后,线性表的元素为:
",tmp);
ShowSqlist(L);
}
}
if(strcmp(s,"get")==0)
{
int tmp;
printf("请输入要获取元素的位置:");
scanf("%d",&tmp);
if(L.length<tmp||tmp<1)
printf("Location Error!
");
else
{
int val=GetSqlist(L,tmp);
printf("Celebrate You,get OK!
");
printf("位置为%d的元素其值为%d
",tmp,val);
}
}
if(strcmp(s,"show")==0)
{
printf("线性表的元素为:
");
ShowSqlist(L);
printf("Celebrate You,show OK!
");
}
memset(s,' ',sizeof(s));
}
return 0;
}