Preguntas sobre la expansión de la estructura de datos

Inversión de lista enlazada única

Esta pregunta requiere la implementación de una función para invertir una lista unidireccional determinada.
Definición de interfaz de función:

List Reverse( List L );

La estructura de la Lista se define de la siguiente manera:

typedef struct Node *PtrToNode;
struct Node {
    ElementType Data; /* 存储结点数据 */
    PtrToNode   Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */

L es una lista enlazada individualmente, y la función Inversa debería devolver la lista enlazada invertida.
Ejemplo de procedimiento de prueba de árbitro:

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表 */

List Reverse( List L );

int main()
{
    List L1, L2;
    L1 = Read();
    L2 = Reverse(L1);
    Print(L1);
    Print(L2);
    return 0;
}

/* 你的代码将被嵌在这里 */

Ejemplo de entrada:

5
1 3 4 5 2

Muestra de salida:

1
2 5 4 3 1

Código:

List Reverse( List L )
{
    List p, q;
    p = L;
    L = NULL;
    while (p)
    {
        q = p;
        p = p->Next;
        q->Next = L;   
        L = q;
    }
    return L;
}

explicar:
Guarde la tabla original como tabla p y deje la tabla original L vacía. Recorra la lista enlazada individualmente, siempre que p no esté vacía, use q para registrar la posición actual y luego mueva el puntero p hacia atrás. Primero asigne L con un valor NULL a q->next, luego el siguiente valor señalado por la ubicación del primer q es NULL, y luego asigne q a L, luego el puntero principal de esta lista vinculada es L.
Por analogía, q->next siempre se coloca delante de L, y luego L = q se usa para intercambiar el puntero principal hacia atrás.

Conjunto de operaciones de tabla de secuencia

Esta pregunta requiere la implementación del conjunto de operaciones de la tabla de secuencia.
Definición de interfaz de función:

List MakeEmpty(); 
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );

La estructura de la Lista se define de la siguiente manera:

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

La definición de cada función de operación es:

List MakeEmpty(): crea y devuelve una lista lineal vacía;

Posición Buscar (Lista L, Tipo de elemento X): Devuelve la posición de X en la lista lineal. Si no se encuentra, devuelve ERROR;

bool Insertar (Lista L, Tipo de elemento X, Posición P): inserta X en la posición P y devuelve verdadero. Si el espacio está lleno, imprima "COMPLETO" y devuelva falso; si el parámetro P apunta a una posición ilegal, imprima "POSICIÓN ILEGAL" y devuelva falso;

bool Eliminar (Lista L, Posición P): elimina el elemento en la posición P y devuelve verdadero. Si el parámetro P apunta a una posición ilegal, imprima "POSICIÓN P VACÍA" (donde P es el valor del parámetro) y devuelva falso.
Ejemplo de procedimiento de prueba de árbitro:

#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; /* 保存线性表中最后一个元素的位置 */
};

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;
}

/* 你的代码将被嵌在这里 */

Ejemplo de entrada:

6
1 2 3 4 5 6
3
6 5 1
2
-1 6

Muestra de salida:

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.

Código:

//创建并返回一个空的线性表
List MakeEmpty()
{
    List L;
    L = (List)malloc(sizeof(struct LNode)); //(1)注意结构体的名字并未重新定义,所以必须使用原始名称;(2)只开辟一个节点空间就行
    L->Last = -1;  //注意赋值是-1
    return L;
}

//返回线性表中X的位置。若找不到则返回ERROR
Position Find( List L, ElementType X )
{
    int flag = 0;
    int i;
    for (i=0;i<=L->Last;i++)
    {
        if (X == L->Data[i])
        {
            flag++;
            return i;
        }
    }
    if (!flag) return ERROR;
}


//将X插入在位置P并返回true。若空间已满,则打印“FULL”并返回false;如果参数P指向非法位置,则打印“ILLEGAL POSITION”并返回false
bool Insert( List L, ElementType X, Position P )
{
    if (L->Last == MAXSIZE-1) //因为要插入元素,所以必须比MAXSIZE少一位
    {
        printf("FULL");
        return false;
    }
    if (P > L->Last+1 || P < 0)  //P插入的位置可以是Last位置,但是不能是Last的下一位
    {
        printf("ILLEGAL POSITION");
        return false;
    }
    int i;
    for (i=L->Last+1;i>P;i--)
        L->Data[i] = L->Data[i-1];
    L->Data[i] = X;
    L->Last++;
    return true;
}


//将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”(其中P是参数值)并返回false。
bool Delete( List L, Position P )
{
    if (P < 0 || P > L->Last)
    {
        printf("POSITION %d EMPTY",P);
        return false;
    }
    int i;
    for (i=P;i<L->Last;i++)
        L->Data[i] = L->Data[i+1];
    L->Last--;
    return true;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_44236278/article/details/102466473
Recomendado
Clasificación