#include<stdio.h>
#include<malloc.h>
#include<string.h>
const int mx = 100;
typedef struct{
int *arr;
int length;
int size;
}Sqlist;
void view(Sqlist *L)
{
int i;
for(i = 1; i <= L->length; i++)
printf("%d ", *(L->arr+i));
}
int Init_List(Sqlist *L)
{
printf("Pleas input arr length\n");
int n;
int a;
int i;
scanf("%d", &n);
L->arr = (int*)malloc(sizeof(int)*mx);
L->length = 0;
L->size = mx;
printf("Please input your numbers\n");
for(i = 1; i <= n; i++)
{
scanf("%d", &a);
*(L->arr + i) = a ;
L->length++;
}
view(L);
}
int Get(Sqlist *L,int i)
{
if(i<1||i>L->length)
{
printf("error\n");
return -1;
}
printf("%d\n",*(L->arr+i));
view(L);
}
void Len(Sqlist *L)
{
printf("%d", L->length);
}
int LInsert(Sqlist *L,int i,int a)
{
if(i<1||i>L->length+1)
{
return -1;
}
int *p = L->arr + i;
if(L->length+1>=L->size)
{
int *newbase = (int *)malloc(sizeof(int)*(L->length+mx));
if(!newbase)
{
return -1;
}
else L->arr = newbase;
}
int *tmp = L->arr + L->length;
for(;tmp>=p;tmp--)
{
*(p+1) = *(p);
}
*p = a;
L->length++;
return 1;
}
int Delete(Sqlist *L,int i)
{
if(i<1||i>L->length)
{
printf("error\n");
return -1;
}
int *tmp = L->arr+i;
for(;tmp<L->arr+L->length;tmp++)
{
*tmp = *(tmp+1);
}
L->length--;
view(L);
}
int Insert(Sqlist *L,int i,int a)
{
if(i<1||i>L->length+1)
{
printf("error\n");
return -1;
}
int *p = L->arr + i;
if(L->length+1>=L->size)
{
int *newbase = (int *)malloc(sizeof(int)*(L->length+mx));
if(!newbase)
{
return -1;
}
else L->arr = newbase;
}
int *tmp = L->arr+L->length;
for(;tmp>=p;tmp--)
{
*(p+1) = *(p);
}
*p = a;
L->length++;
view(L);
}
void Uni(Sqlist *A, Sqlist *B, Sqlist *C)
{
C->arr = (int *) malloc (sizeof(int) * mx);
C->length = 0;
C->size = mx;
int i = 1, j = 1, k = 1;
while(i <= A->length && j <= B->length)
if(*(A->arr+i) <= *(B->arr+j))
LInsert(C, k++, *(A->arr + i++));
else
LInsert(C, k++, *(B->arr + j++));
while(i <= A->length) LInsert(C, k++, *(A->arr + i++));
while(j <= B->length) LInsert(C, k++, *(B->arr + j++));
printf("执行合并线性表La、Lb完成,生成的新线性表Lc为:\n");
view(C);
}
Sqlist L;
Sqlist A,B,C;
int llm;
int main(){
printf("Pleas input arr length\n");
Init_List(&L);
int pos;
int op = 100;
printf("\n请输入您要执行的操作代码:\n");
printf("1删除线性表的第i个位置元素\n");
printf("2获得线性表中第i个位置的元素\n");
printf("3在线性表的第i个位置插入元素e\n");
printf("4获得线性表的长度\n");
printf("5将有序表LA、LB合并到线性表Lc中\n");
while(op)
{
scanf("%d", &op);
if(op == 1)
{
printf("Delete\n");
scanf("%d", &llm);
Delete(&L,llm);
}
else if(op == 2)
{
printf("Get\n");
scanf("%d", &llm);
Get(&L,llm);
}
else if(op == 3)
{
printf("Insert\n");
scanf("%d", &llm);
int a;
scanf("%d", &a);
Insert(&L,llm,a);
}
else if(op == 4)
{
Len(&L);
}
else if(op == 5)
{
Init_List(&A);
Init_List(&B);
Uni(&A,&B,&C);
}
}
return 0;
}
|