ListMakeEmpty(){ListL=(List)malloc(sizeof(struct LNode));L->Last=-1;returnL;}PositionFind(ListL,ElementTypeX){for(int i =0; i <=L->Last; i++){if(L->Data[i]==X){return i;}}returnERROR;}
bool Insert(ListL,ElementTypeX,PositionP){if(L->Last==MAXSIZE-1){printf("FULL");returnfalse;}if(P<0||P>L->Last+1){printf("ILLEGAL POSITION");returnfalse;}for(int i =L->Last; i >=P; i--){L->Data[i +1]=L->Data[i];}L->Data[P]=X;L->Last++;returntrue;}
bool Delete(ListL,PositionP){if(P<0||P>L->Last){printf("POSITION %d EMPTY",P);returnfalse;}for(int i =P+1; i <=L->Last; i++){L->Data[i -1]=L->Data[i];}L->Last--;returntrue;}
全部代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE5
#define ERROR-1
typedef intElementType;
typedef intPosition;
typedef struct LNode*List;
struct LNode{ElementTypeData[MAXSIZE];PositionLast;/* 保存线性表中最后一个元素的位置 */};ListMakeEmpty();PositionFind(ListL,ElementTypeX);
bool Insert(ListL,ElementTypeX,PositionP);
bool Delete(ListL,PositionP);intmain(){ListL;ElementTypeX;PositionP;intN;L=MakeEmpty();scanf("%d",&N);while(N--){scanf("%d",&X);if(Insert(L,X,0)==false)printf(" Insertion Error: %d is not in.\n",X);}scanf("%d",&N);while(N--){scanf("%d",&X);P=Find(L,X);if(P==ERROR)printf("Finding Error: %d is not in.\n",X);elseprintf("%d is at position %d.\n",X,P);}scanf("%d",&N);while(N--){scanf("%d",&P);if(Delete(L,P)==false)printf(" Deletion Error.\n");if(Insert(L,0,P)==false)printf(" Insertion Error: 0 is not in.\n");}return0;}/* 你的代码将被嵌在这里 */ListMakeEmpty(){ListL=(List)malloc(sizeof(struct LNode));L->Last=-1;returnL;}PositionFind(ListL,ElementTypeX){for(int i =0; i <=L->Last; i++){if(L->Data[i]==X){return i;}}returnERROR;}
bool Insert(ListL,ElementTypeX,PositionP){if(L->Last==MAXSIZE-1){printf("FULL");returnfalse;}if(P<0||P>L->Last+1){printf("ILLEGAL POSITION");returnfalse;}for(int i =L->Last; i >=P; i--){L->Data[i +1]=L->Data[i];}L->Data[P]=X;L->Last++;returntrue;}
bool Delete(ListL,PositionP){if(P<0||P>L->Last){printf("POSITION %d EMPTY",P);returnfalse;}for(int i =P+1; i <=L->Last; i++){L->Data[i -1]=L->Data[i];}L->Last--;returntrue;}