#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
//顺序表的初始化
#include"header.h"
#include<malloc.h>
Status InitList(SqList *L){
L->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem)return OVERFLOW;
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
//在顺序表中删除第i个元素,并用e返回其值
#include"header.h"
#include<malloc.h>
Status ListDelete(SqList *L,int i,int *e)
{
ElemType *p,*q;
if(i<1||i>L->length)return ERROR;
p=&(L->elem[i-1]);
for(q=&(L->elem[i-1]);q<=&(L->elem[L->length-1]);++q)
*q=*(q+1);
--L->length;
return OK;
}
//顺序表L第i个位置前插入元素e
#include"header.h"
#include<malloc.h>
Status Listlnsert(SqList *L,int i,ElemType e){
ElemType *q,*p,*newbase;
if(i<1||i>L->length+1)return ERROR;
if(L->length>=L->listsize){
newbase=(ElemType*)realloc(L->elem,(LISTINCREMENT+L->listsize)*sizeof(ElemType));
if(!newbase)return OVERFLOW;
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
p=&(L->elem[i-1]);
for(q=&(L->elem[L->length-1]);p<=q;q--)
*(q+1)=*q;
*p=e;
++L->length;
return OK;
}
#include"InitList.h"
#include"ListDelete.h"
#include"Listlnsert.h"
int main()
{
SqList LL;
int i,j;
ElemType e;
if(InitList(&LL)==OK){ //SqList *类型实参与SqList *类型形参不兼容
for(i=1;i<=5;i++)
if(Listlnsert(&LL,i,i)!=OK) break;
printf("显示顺序表lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
printf("\n请输入要删除元素的序号:");
scanf_s("%d",&j);
if(ListDelete(&LL,j,&e)==OK){
printf("delete_elem=%d\n",e);
printf("\n删除元素后的顺序表Lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
}
else
printf("delete_elem is failed\n");
return 0;
}
1 个解决方案
#1
没问题的。但是你的{不匹配:
#include"InitList.h"
#include"ListDelete.h"
#include"Listlnsert.h"
int main()
{
SqList LL;
int i,j;
ElemType e;
if(InitList(&LL)==OK) { //SqList *类型实参与SqList *类型形参不兼容
for(i=1;i<=5;i++)
if(Listlnsert(&LL,i,i)!=OK) break;
printf("显示顺序表lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
printf("\n请输入要删除元素的序号:");
scanf_s("%d",&j);
if(ListDelete(&LL,j,&e)==OK) {
printf("delete_elem=%d\n",e);
printf("\n删除元素后的顺序表Lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
}
else
printf("delete_elem is failed\n");
return 0;
}
#include"InitList.h"
#include"ListDelete.h"
#include"Listlnsert.h"
int main()
{
SqList LL;
int i,j;
ElemType e;
if(InitList(&LL)==OK) { //SqList *类型实参与SqList *类型形参不兼容
for(i=1;i<=5;i++)
if(Listlnsert(&LL,i,i)!=OK) break;
printf("显示顺序表lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
printf("\n请输入要删除元素的序号:");
scanf_s("%d",&j);
if(ListDelete(&LL,j,&e)==OK) {
printf("delete_elem=%d\n",e);
printf("\n删除元素后的顺序表Lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
}
else
printf("delete_elem is failed\n");
return 0;
}
#1
没问题的。但是你的{不匹配:
#include"InitList.h"
#include"ListDelete.h"
#include"Listlnsert.h"
int main()
{
SqList LL;
int i,j;
ElemType e;
if(InitList(&LL)==OK) { //SqList *类型实参与SqList *类型形参不兼容
for(i=1;i<=5;i++)
if(Listlnsert(&LL,i,i)!=OK) break;
printf("显示顺序表lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
printf("\n请输入要删除元素的序号:");
scanf_s("%d",&j);
if(ListDelete(&LL,j,&e)==OK) {
printf("delete_elem=%d\n",e);
printf("\n删除元素后的顺序表Lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
}
else
printf("delete_elem is failed\n");
return 0;
}
#include"InitList.h"
#include"ListDelete.h"
#include"Listlnsert.h"
int main()
{
SqList LL;
int i,j;
ElemType e;
if(InitList(&LL)==OK) { //SqList *类型实参与SqList *类型形参不兼容
for(i=1;i<=5;i++)
if(Listlnsert(&LL,i,i)!=OK) break;
printf("显示顺序表lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
printf("\n请输入要删除元素的序号:");
scanf_s("%d",&j);
if(ListDelete(&LL,j,&e)==OK) {
printf("delete_elem=%d\n",e);
printf("\n删除元素后的顺序表Lst:\n");
for(i=1;i<=LL.length;i++)
printf("第%d个元素为:%d\n",i,LL.elem[i-1]);
}
else
printf("delete_elem is failed\n");
return 0;
}