当前位置:网站首页>6-2 sequence table operation set
6-2 sequence table operation set
2022-07-05 06:36:00 【timingzj】
6-2 Sequence table operation set
This problem requires the implementation of the operation set of the sequence table .
Function interface definition :
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
among List The structure is defined as follows :
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* Save the position of the last element in the linear table */
};
Each operation function is defined as :
List MakeEmpty(): Create and return an empty linear table ;
Position Find( List L, ElementType X ): Return to the linear table X The location of . If not, return ERROR;
bool Insert( List L, ElementType X, Position P ): take X Insert in position P And back to true. If the space is full , Then print “FULL” And back to false; If parameters P Point to an illegal location , Then print “ILLEGAL POSITION” And back to false;
bool Delete( List L, Position P ): Place P Delete the element and return true. If parameter P Point to an illegal location , Then print “POSITION P EMPTY”( among P Is the parameter value ) And back to false.
Sample referee test procedure :
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
#define ERROR -1
typedef enum {
false, true} bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* Save the position of the last element in the linear table */
};
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P;
int N;
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);
else
printf("%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");
}
return 0;
}
/* Your code will be embedded here */
sample input :
6
1 2 3 4 5 6
3
6 5 1
2
-1 6
sample output :
FULL Insertion Error: 6 is not in.
Finding Error: 6 is not in.
5 is at position 0.
1 is at position 4.
POSITION -1 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
POSITION 6 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
Code :
//List MakeEmpty(): Create and return an empty linear table ;
List MakeEmpty()
{
List L = (List)malloc(sizeof(struct LNode));
L->Last = -1;
return L;
}
//Position Find( List L, ElementType X ): Return to the linear table X The location of .
// If not, return ERROR;
Position Find( List L, ElementType X )
{
int i = 0;
for(i = 0; i <= L->Last; i++)
{
if(L->Data[i] == X)
return i;
}
return ERROR;
}
//bool Insert( List L, ElementType X, Position P ):
// take X Insert in position P And back to true.
// If the space is full , Then print “FULL” And back to false;
// If parameters P Point to an illegal location , Then print “ILLEGAL POSITION” And back to false;
bool Insert( List L, ElementType X, Position P )
{
// Space full
if(L->Last == MAXSIZE-1)
{
printf("FULL");
return false;
}
//P Point to an illegal location ,P The range pointed :[0, L->Last],P Insert range of :[0, L->Last+1]
if(P < 0 || P > (L->Last+1))
{
printf("ILLEGAL POSITION");
return false;
}
// Insert ,P The following elements are collectively shifted to the right
int end = L->Last;
while(end >= P)
{
L->Data[end+1] = L->Data[end];
end--;
}
L->Data[P] = X;
L->Last++;
return true;
}
//bool Delete( List L, Position P ):
// Place P Delete the element and return true.
// If parameter P Point to an illegal location , Then print “POSITION P EMPTY”( among P Is the parameter value )
// And back to false.
bool Delete( List L, Position P )
{
//P Scope of deletion :[0, L->Last]
if(P < 0 || P > L->Last)
{
printf("POSITION %d EMPTY", P);
return false;
}
// Delete
int start = P;
while(P < L->Last)
{
L->Data[start] = L->Data[start+1];
start++;
}
L->Last--;
return true;
}
边栏推荐
- Some classic recursion problems
- 2022/6/29-日报
- Vscode configures the typera editor for MD
- [Chongqing Guangdong education] 1185t administrative leadership reference test of National Open University in autumn 2018
- 求组合数 AcWing 887. 求组合数 III
- PR automatically moves forward after deleting clips
- LSA Type Explanation - lsa-5 (type 5 LSA - autonomous system external LSA) and lsa-4 (type 4 LSA - ASBR summary LSA) explanation
- 【高德地图POI踩坑】AMap.PlaceSearch无法使用
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- 4.Oracle-重做日志文件管理
猜你喜欢

Game theory acwing 892 Steps Nim game

Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter

博弈论 AcWing 891. Nim游戏

NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar

LeetCode-54

求组合数 AcWing 888. 求组合数 IV

ollvm编译出现的问题纪录

背包问题 AcWing 9. 分组背包问题

VLAN experiment

Package webapp or H5 pages into apps
随机推荐
ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC
时间很快,请多做有意义的事情
11-gorm-v2-03-basic query
MySQL (UDF authorization)
P2575 master fight
Chart. JS - Format Y axis - chart js - Formatting Y axis
The “mode“ argument must be integer. Received an instance of Object
Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
高斯消元 AcWing 884. 高斯消元解异或線性方程組
1. Create Oracle database manually
Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
Configuration method and configuration file of SolidWorks GB profile library
达梦数据库全部
[wustctf2020] plain_ WP
Day 2 document
[moviepy] unable to find a solution for exe
4.Oracle-重做日志文件管理
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
Vant Weapp SwipeCell设置多个按钮
Getting started with typescript