|
|
#2
浮烟醉2019-09-27 13:13
|
程序代码:#include<iostream>
using namespace std;
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 50
#define ERROR -1
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last;
};
/* 初始化 */
List MakeEmpty()
{
List L;
L = (List)malloc(sizeof(struct LNode));
L->Last = -1;
return L;
}
/* 查找 */
Position Find( List L, ElementType X )
{
Position i = 0;
while( i <= L->Last && L->Data[i]!= X )
i++;
if ( i > L->Last ) return ERROR; /* 如果没找到,返回错误信息 */
else return i; /* 找到后返回的是存储位置 */
}
/* 插入 */
bool Insert( List L, ElementType X, int i )
{
Position j;
if ( L->Last == MAXSIZE-1) {
/* 表空间已满,不能插入 */
return false;
}
if ( i<1 || i>L->Last+2 ) { /* 检查插入位置的合法性 */
return false;
}
for( j=L->Last;j>=i-1;j-- )
L->Data[j+1] = L->Data[j];
L->Data[i-1] = X; /* 新元素插入 */
L->Last++; /* Last仍指向最后元素 */
return true;
}
/* 删除 */
bool Delete( List L, int i )
{
Position j;
if( i<1 || i>L->Last+1 ) { /* 检查空表及删除位置的合法性 */
return false;
}
for( j=i; j<=L->Last; j++ )
L->Data[j-1] = L->Data[j];
L->Last--; /* Last仍指向最后元素 */
return true;
}
int length(List L)
{
return L->Last+1;
}
int main(int argc,char** argv){
List L=MakeEmpty();
int n,op;
Position i;
ElementType x;
scanf("%d",&n);
while(n--)
{
scanf("%d",&op);
switch(op)
{
case 1:scanf("%d %d",&x,&i);
Insert(L,x,i);
break;
case 2:scanf("%d",&i);
Delete(L,i);
break;
case 3:scanf("%d",&x);
printf("%d\n",Find(L,x));
break;
}
}
for(int j=0;j<=L->Last;j++)
{
printf("%d ",L->Data[j]);
}
return 0;
}
using namespace std;
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 50
#define ERROR -1
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last;
};
/* 初始化 */
List MakeEmpty()
{
List L;
L = (List)malloc(sizeof(struct LNode));
L->Last = -1;
return L;
}
/* 查找 */
Position Find( List L, ElementType X )
{
Position i = 0;
while( i <= L->Last && L->Data[i]!= X )
i++;
if ( i > L->Last ) return ERROR; /* 如果没找到,返回错误信息 */
else return i; /* 找到后返回的是存储位置 */
}
/* 插入 */
bool Insert( List L, ElementType X, int i )
{
Position j;
if ( L->Last == MAXSIZE-1) {
/* 表空间已满,不能插入 */
return false;
}
if ( i<1 || i>L->Last+2 ) { /* 检查插入位置的合法性 */
return false;
}
for( j=L->Last;j>=i-1;j-- )
L->Data[j+1] = L->Data[j];
L->Data[i-1] = X; /* 新元素插入 */
L->Last++; /* Last仍指向最后元素 */
return true;
}
/* 删除 */
bool Delete( List L, int i )
{
Position j;
if( i<1 || i>L->Last+1 ) { /* 检查空表及删除位置的合法性 */
return false;
}
for( j=i; j<=L->Last; j++ )
L->Data[j-1] = L->Data[j];
L->Last--; /* Last仍指向最后元素 */
return true;
}
int length(List L)
{
return L->Last+1;
}
int main(int argc,char** argv){
List L=MakeEmpty();
int n,op;
Position i;
ElementType x;
scanf("%d",&n);
while(n--)
{
scanf("%d",&op);
switch(op)
{
case 1:scanf("%d %d",&x,&i);
Insert(L,x,i);
break;
case 2:scanf("%d",&i);
Delete(L,i);
break;
case 3:scanf("%d",&x);
printf("%d\n",Find(L,x));
break;
}
}
for(int j=0;j<=L->Last;j++)
{
printf("%d ",L->Data[j]);
}
return 0;
}
