本人菜鸟,运行时不知道咋回事,输入上一步出下一步结果,如何改啊觉得小弟我是伸手就飘过吧…
本人初学者,运行时不知道怎么回事,输入下一步出上一步结果,怎么改啊!觉得我是伸手就飘过吧……
/* 顺序表的基本操作*/
#include <stdio.h>
#include <malloc.h>
#define true 1
#define false 0
#define ok 1
#define error 0
#define overflow -2
#define List_Init_Size 20
#define ListIncrement 5
typedef int status;
typedef char Elemtype;
typedef struct
{ Elemtype *elem;
int length;
int Listsize;
} SqList;
status InitList_sq(SqList &L)
{
L.elem=(Elemtype *)malloc(List_Init_Size *sizeof(Elemtype));
if (L.elem==NULL) return error;
L.length=0;
L.Listsize= List_Init_Size;
return ok;
}
void DestroyList_sq(SqList &L)
{
free(L.elem);
}
status ListEmpty_sq(SqList L)
{
if (L.length==0) return true;
else return false;
}
int ListLength_sq(SqList L)
{
return(L.length);
}
void PrintList_sq(SqList L)
{
int i;
if (ListEmpty_sq(L)) return;
for (i=0;i<L.length;i++)
printf("%c",L.elem[i]);
printf("\n");
}
status GetElem_sq(SqList L,int i,Elemtype &e)
{
if (i<1 || i>L.length)
return error;
e=L.elem[i-1];
return ok;
}
int LocateElem_sq(SqList L, Elemtype e)
{
int i=0;
while (i<L.length && L.elem[i]!=e) i++;
if (i>=L.length)
return 0;
else
return i+1;
}
status ListInsert_sq(SqList &L,int i,Elemtype e)
{
int j;
if (i<1 || i>L.length+1)
return error;
if (L.length== L.Listsize)
{ Elemtype *newbase;
newbase=(Elemtype *)realloc(L.elem ,( L.Listsize+ListIncrement) *sizeof(Elemtype));
if (newbase==NULL) return error;
L.elem = newbase;
L.Listsize= L.Listsize+ListIncrement;
}
for (j=L.length-1;j>=i-1;j--) /*将elem[i]及后面元素后移一个位置*/
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
L.length++; /*顺序表长度增1*/
return ok;
}
status ListDelete_sq(SqList &L,int i,Elemtype &e)
{
int j;
if (i<1 || i>L.length)
return error;
e=L.elem[i-1];
for (j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
L.length--;
return ok;
}
void main()
{
SqList L;
Elemtype ch;
int m;
printf("(1)初始化顺序表L\n");
printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
printf("(3)输出顺序表L:");
printf("(4)顺序表L长度");
printf("(5)顺序表L是否为空表\n");
printf("(6)顺序表L的第3个元素\n");
printf("(7)元素a的位置\n");
printf("(8)在第4个元素位置上插入f元素\n");
printf("(9)输出顺序表L\n ");
printf("(10)删除L的第3个元素\n");
printf("(11)输出顺序表L:");
printf("(12)释放顺序表L\n");
printf("(13)退出\n");
printf("请输入选项\n");
scanf("%d\n",&m);
while (m<=12)
{switch(m)
{
case 1:
InitList_sq(L);
break;
case 2:
ListInsert_sq(L,1,'a');
ListInsert_sq (L,2,'b');
ListInsert_sq (L,3,'c');
ListInsert_sq (L,4,'d');
ListInsert_sq (L,5,'e');
break;
case 3:
PrintList_sq(L);
break;
case 4:
printf("顺序表L长度=%d\n",ListLength_sq(L));
break;
case 5:
printf("顺序表L为%s\n",(ListEmpty_sq(L)?"空":"非空"));
break;
case 6:
printf("顺序表L的第3个元素=%c\n",ch);
GetElem_sq(L,3,ch);
break;
case 7:
printf("元素a的位置=%d\n",LocateElem_sq(L,'a'));
break;
case 8:
ListInsert_sq(L,4,'f');
break;
case 9:
PrintList_sq (L);
break;
case 10:
ListDelete_sq(L,3,ch);
break;
case 11:
PrintList_sq (L);
/* 顺序表的基本操作*/
#include <stdio.h>
#include <malloc.h>
#define true 1
#define false 0
#define ok 1
#define error 0
#define overflow -2
#define List_Init_Size 20
#define ListIncrement 5
typedef int status;
typedef char Elemtype;
typedef struct
{ Elemtype *elem;
int length;
int Listsize;
} SqList;
status InitList_sq(SqList &L)
{
L.elem=(Elemtype *)malloc(List_Init_Size *sizeof(Elemtype));
if (L.elem==NULL) return error;
L.length=0;
L.Listsize= List_Init_Size;
return ok;
}
void DestroyList_sq(SqList &L)
{
free(L.elem);
}
status ListEmpty_sq(SqList L)
{
if (L.length==0) return true;
else return false;
}
int ListLength_sq(SqList L)
{
return(L.length);
}
void PrintList_sq(SqList L)
{
int i;
if (ListEmpty_sq(L)) return;
for (i=0;i<L.length;i++)
printf("%c",L.elem[i]);
printf("\n");
}
status GetElem_sq(SqList L,int i,Elemtype &e)
{
if (i<1 || i>L.length)
return error;
e=L.elem[i-1];
return ok;
}
int LocateElem_sq(SqList L, Elemtype e)
{
int i=0;
while (i<L.length && L.elem[i]!=e) i++;
if (i>=L.length)
return 0;
else
return i+1;
}
status ListInsert_sq(SqList &L,int i,Elemtype e)
{
int j;
if (i<1 || i>L.length+1)
return error;
if (L.length== L.Listsize)
{ Elemtype *newbase;
newbase=(Elemtype *)realloc(L.elem ,( L.Listsize+ListIncrement) *sizeof(Elemtype));
if (newbase==NULL) return error;
L.elem = newbase;
L.Listsize= L.Listsize+ListIncrement;
}
for (j=L.length-1;j>=i-1;j--) /*将elem[i]及后面元素后移一个位置*/
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
L.length++; /*顺序表长度增1*/
return ok;
}
status ListDelete_sq(SqList &L,int i,Elemtype &e)
{
int j;
if (i<1 || i>L.length)
return error;
e=L.elem[i-1];
for (j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
L.length--;
return ok;
}
void main()
{
SqList L;
Elemtype ch;
int m;
printf("(1)初始化顺序表L\n");
printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
printf("(3)输出顺序表L:");
printf("(4)顺序表L长度");
printf("(5)顺序表L是否为空表\n");
printf("(6)顺序表L的第3个元素\n");
printf("(7)元素a的位置\n");
printf("(8)在第4个元素位置上插入f元素\n");
printf("(9)输出顺序表L\n ");
printf("(10)删除L的第3个元素\n");
printf("(11)输出顺序表L:");
printf("(12)释放顺序表L\n");
printf("(13)退出\n");
printf("请输入选项\n");
scanf("%d\n",&m);
while (m<=12)
{switch(m)
{
case 1:
InitList_sq(L);
break;
case 2:
ListInsert_sq(L,1,'a');
ListInsert_sq (L,2,'b');
ListInsert_sq (L,3,'c');
ListInsert_sq (L,4,'d');
ListInsert_sq (L,5,'e');
break;
case 3:
PrintList_sq(L);
break;
case 4:
printf("顺序表L长度=%d\n",ListLength_sq(L));
break;
case 5:
printf("顺序表L为%s\n",(ListEmpty_sq(L)?"空":"非空"));
break;
case 6:
printf("顺序表L的第3个元素=%c\n",ch);
GetElem_sq(L,3,ch);
break;
case 7:
printf("元素a的位置=%d\n",LocateElem_sq(L,'a'));
break;
case 8:
ListInsert_sq(L,4,'f');
break;
case 9:
PrintList_sq (L);
break;
case 10:
ListDelete_sq(L,3,ch);
break;
case 11:
PrintList_sq (L);