主题:大家帮忙看一下这个顺序表
大家帮忙看一下这个顺序表:
有一个warning 好象是说检查是否为空的时候palist没有初始化,怎么把值传递进去谢谢
还有运行的时候,选2、3等项目会说内存不能read,不知道错误在哪里请求指教
程序可能有点长但COPY在VC下,运行后就很明显了,谢谢大家帮忙!
//*************************************
// 顺序表示线性表
//*************************************
#include <stdio.h>
#include <malloc.h>
#define DataType int
//定义线性表属性
typedef struct Seqlist
{
int n;
int MAXNUM;
DataType *element;
}*PSeqlist;
//创建空顺序表
PSeqlist CreateNullList_seq(int m)
{
PSeqlist palist = (PSeqlist) malloc(sizeof(struct Seqlist));
if(palist != NULL)
{
palist->element = (DataType *) malloc(sizeof(DataType) * m);
if(palist->element)
{
palist->n = 0; //表长为0
palist->MAXNUM = m;
return(palist);
}
else free (palist);
}
printf("Out of space! \n");
return NULL;
}
//判断顺序表是否为空
int isNullList_seq(PSeqlist palist)
{
if(palist->n == 0)
{
printf("The list is Null! \n");
return 1;
}
else
return 0;
}
//在顺序表中寻找一个值为x的下标,如果不存在返回 -1
int locate_seq(PSeqlist palist, DataType x)
{
if(palist->n == 0)
{
printf("The list has no member! \n");
return -1;
}
else
{
int i;
for(i = 0; i < palist->n; i++)
if(palist->element[i] == x) return i;
return -1;
}
}
//在下标为P之前,插入x
int insertPre_seq(PSeqlist palist,int p,DataType x)
{
int q;
//溢出判断
if(palist->n == palist->MAXNUM){
printf("over flow! \n");
return 0;
}
//空顺序表插入
if(isNullList_seq(palist))
{
palist->n = 1;
palist->element[0] = x;
return 1;
}
//不存在下标
if(p < 0 || p > palist -> n)
{
printf("The P is not a correct number! \n ");
return 0;
}
for(q = palist->n -1; q > p; q--)
palist->element[q+1] = palist->element[q];
palist->element[q] = x;
palist->n = palist->n+1;
return 1;
}
//在下标为P之后,插入x
int insertPost_seq(PSeqlist palist,int p,DataType x)
{
int q;
//溢出判断
if(palist->n == palist->MAXNUM){
printf("over flow! \n");
return 0;
}
//空顺序表插入
if(isNullList_seq(palist))
{
palist->n = 1;
palist->element[0] = x;
return 1;
}
//不存在下标
if(p < 0 || p > palist -> n)
{
printf("The P is not a correct number! \n ");
return 0;
}
for(q = palist->n -1; q > p; q--)
palist->element[q+1] = palist->element[q];
palist->element[q+1] = x;
palist->n = palist->n+1;
return 1;
}
//删除下标为p的元素
int deleteP_seq(PSeqlist palist, int p)
{
int q;
if(palist->n == 0) { printf("The list is NULL \n"); return 0;}
if(p < 0 ||p > palist->n)
{
printf("The number p is not correct! \n");
return 0;
}
palist->n -= 1;
for(q = p; q < palist->n-1; q++)
palist->element[p] = palist->element[p+1];
return 1;
}
// 删除一个值为x的元素
int deleteV_seq(PSeqlist palist, DataType x)
{
if(isNullList_seq(palist))
{
printf("The list is NULL! \n");
return 0;
}
deleteP_seq(palist, locate_seq(palist, x));
return 1;
}
int main()
{
int m;
int i;
DataType x;
int p;
int a = 1;
PSeqlist palist;
printf("1.创建空顺序表 \n");
printf("2.判断顺序表是否为空 \n");
printf("3.在顺序表中寻找一个值为x的下标,如果不存在返回 -1 \n");
printf("4.在下标为P之前,插入x \n");
printf("5.在下标为P之后,插入x \n");
printf("6.删除下标为p的元素 \n");
printf("7.删除值为x的元素 \n");
printf("8.退出 \n");
while(a == 1){
printf("Input a number between 1~7 to choose the menu: \n");
scanf("%d",&i);
switch(i)
{
case 1:
printf("Please input the list size! \n");
scanf("%d",&m);
if(CreateNullList_seq(m)){
printf("The list has done which size is :");
printf("%d \n",m);
printf("completed \n");
CreateNullList_seq(m);
}
else
printf("The list create lost! \n");
break;
case 2:
if(isNullList_seq(palist))
printf("The list is NULL! \n");
else
printf("The list is not NULL! \n");
break;
case 3:
printf("Please input a number \n");
scanf("%d",&x);
if(locate_seq(palist, x))
{
printf("The position of 'x' in the list is :");
printf("%d \n",locate_seq(palist, x));
}
else
printf("the member x is not in the list! \n");
break;
case 4:
printf("input a number \n");
scanf("%d",&x);
printf("input the position of the new element! \n");
scanf("%d",&p);
if(insertPre_seq(palist,p, x))
printf("insert before p successed! \n");
else
printf("Insert fault!\n");
break;
case 5:
printf("input a number \n");
scanf("%d",&x);
printf("input the position of the new element! \n");
scanf("%d",&p);
if(insertPost_seq(palist,p,x))
printf("insert after p successed! \n");
else
printf("Insert fault! \n");
break;
case 6:
printf("Please input the position of the element which you want to delete! \n");
scanf("%d",&p);
if(deleteP_seq(palist,p))
printf("Delete successed! \n");
else
printf("Delete fault \n");
break;
case 7:
printf("input the element you want to delete! \n");
scanf("%d",&x);
if(deleteV_seq(palist, x))
printf("Delete the element successed! \n");
else
printf("Delete fault! \n");
break;
case 8:
a =0;
break;
default:
printf("Please input a correct number! \n");
break;
}
}
return 0;
}
有一个warning 好象是说检查是否为空的时候palist没有初始化,怎么把值传递进去谢谢
还有运行的时候,选2、3等项目会说内存不能read,不知道错误在哪里请求指教
程序可能有点长但COPY在VC下,运行后就很明显了,谢谢大家帮忙!
//*************************************
// 顺序表示线性表
//*************************************
#include <stdio.h>
#include <malloc.h>
#define DataType int
//定义线性表属性
typedef struct Seqlist
{
int n;
int MAXNUM;
DataType *element;
}*PSeqlist;
//创建空顺序表
PSeqlist CreateNullList_seq(int m)
{
PSeqlist palist = (PSeqlist) malloc(sizeof(struct Seqlist));
if(palist != NULL)
{
palist->element = (DataType *) malloc(sizeof(DataType) * m);
if(palist->element)
{
palist->n = 0; //表长为0
palist->MAXNUM = m;
return(palist);
}
else free (palist);
}
printf("Out of space! \n");
return NULL;
}
//判断顺序表是否为空
int isNullList_seq(PSeqlist palist)
{
if(palist->n == 0)
{
printf("The list is Null! \n");
return 1;
}
else
return 0;
}
//在顺序表中寻找一个值为x的下标,如果不存在返回 -1
int locate_seq(PSeqlist palist, DataType x)
{
if(palist->n == 0)
{
printf("The list has no member! \n");
return -1;
}
else
{
int i;
for(i = 0; i < palist->n; i++)
if(palist->element[i] == x) return i;
return -1;
}
}
//在下标为P之前,插入x
int insertPre_seq(PSeqlist palist,int p,DataType x)
{
int q;
//溢出判断
if(palist->n == palist->MAXNUM){
printf("over flow! \n");
return 0;
}
//空顺序表插入
if(isNullList_seq(palist))
{
palist->n = 1;
palist->element[0] = x;
return 1;
}
//不存在下标
if(p < 0 || p > palist -> n)
{
printf("The P is not a correct number! \n ");
return 0;
}
for(q = palist->n -1; q > p; q--)
palist->element[q+1] = palist->element[q];
palist->element[q] = x;
palist->n = palist->n+1;
return 1;
}
//在下标为P之后,插入x
int insertPost_seq(PSeqlist palist,int p,DataType x)
{
int q;
//溢出判断
if(palist->n == palist->MAXNUM){
printf("over flow! \n");
return 0;
}
//空顺序表插入
if(isNullList_seq(palist))
{
palist->n = 1;
palist->element[0] = x;
return 1;
}
//不存在下标
if(p < 0 || p > palist -> n)
{
printf("The P is not a correct number! \n ");
return 0;
}
for(q = palist->n -1; q > p; q--)
palist->element[q+1] = palist->element[q];
palist->element[q+1] = x;
palist->n = palist->n+1;
return 1;
}
//删除下标为p的元素
int deleteP_seq(PSeqlist palist, int p)
{
int q;
if(palist->n == 0) { printf("The list is NULL \n"); return 0;}
if(p < 0 ||p > palist->n)
{
printf("The number p is not correct! \n");
return 0;
}
palist->n -= 1;
for(q = p; q < palist->n-1; q++)
palist->element[p] = palist->element[p+1];
return 1;
}
// 删除一个值为x的元素
int deleteV_seq(PSeqlist palist, DataType x)
{
if(isNullList_seq(palist))
{
printf("The list is NULL! \n");
return 0;
}
deleteP_seq(palist, locate_seq(palist, x));
return 1;
}
int main()
{
int m;
int i;
DataType x;
int p;
int a = 1;
PSeqlist palist;
printf("1.创建空顺序表 \n");
printf("2.判断顺序表是否为空 \n");
printf("3.在顺序表中寻找一个值为x的下标,如果不存在返回 -1 \n");
printf("4.在下标为P之前,插入x \n");
printf("5.在下标为P之后,插入x \n");
printf("6.删除下标为p的元素 \n");
printf("7.删除值为x的元素 \n");
printf("8.退出 \n");
while(a == 1){
printf("Input a number between 1~7 to choose the menu: \n");
scanf("%d",&i);
switch(i)
{
case 1:
printf("Please input the list size! \n");
scanf("%d",&m);
if(CreateNullList_seq(m)){
printf("The list has done which size is :");
printf("%d \n",m);
printf("completed \n");
CreateNullList_seq(m);
}
else
printf("The list create lost! \n");
break;
case 2:
if(isNullList_seq(palist))
printf("The list is NULL! \n");
else
printf("The list is not NULL! \n");
break;
case 3:
printf("Please input a number \n");
scanf("%d",&x);
if(locate_seq(palist, x))
{
printf("The position of 'x' in the list is :");
printf("%d \n",locate_seq(palist, x));
}
else
printf("the member x is not in the list! \n");
break;
case 4:
printf("input a number \n");
scanf("%d",&x);
printf("input the position of the new element! \n");
scanf("%d",&p);
if(insertPre_seq(palist,p, x))
printf("insert before p successed! \n");
else
printf("Insert fault!\n");
break;
case 5:
printf("input a number \n");
scanf("%d",&x);
printf("input the position of the new element! \n");
scanf("%d",&p);
if(insertPost_seq(palist,p,x))
printf("insert after p successed! \n");
else
printf("Insert fault! \n");
break;
case 6:
printf("Please input the position of the element which you want to delete! \n");
scanf("%d",&p);
if(deleteP_seq(palist,p))
printf("Delete successed! \n");
else
printf("Delete fault \n");
break;
case 7:
printf("input the element you want to delete! \n");
scanf("%d",&x);
if(deleteV_seq(palist, x))
printf("Delete the element successed! \n");
else
printf("Delete fault! \n");
break;
case 8:
a =0;
break;
default:
printf("Please input a correct number! \n");
break;
}
}
return 0;
}