Data structure expansion questions

Single linked list reversal

This question requires the implementation of a function to reverse a given singly linked list.
Function interface definition:

List Reverse( List L );

The List structure is defined as follows:

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

L is a given singly linked list, and the function Reverse should return the reversed linked list.
Sample Referee Test Procedure:

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

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

Input example:

5
1 3 4 5 2

Output sample:

1
2 5 4 3 1

Code:

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

explain:
Store the original table as p table, and leave the original table L empty. Traverse the singly linked list, as long as p is not empty, use q to record the current position, and then move the p pointer backward. First assign L with a value of NULL to q->next, then the next value pointed to by the location of the first q is NULL, and then assign q to L, then the head pointer of this linked list is L.
By analogy, q->next is always placed in front of L, and then L = q is used to exchange the head pointer back.

Sequence table operation set

This question 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 );

The List structure is defined as follows:

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

The definition of each operation function is:

List MakeEmpty(): Create and return an empty linear list;

Position Find(List L, ElementType X): Returns the position of X in the linear list. If not found, return ERROR;

bool Insert(List L, ElementType X, Position P): Insert X at position P and return true. If the space is full, print "FULL" and return false; if parameter P points to an illegal position, print "ILLEGAL POSITION" and return false;

bool Delete(List L, Position P): Delete the element at position P and return true. If parameter P points to an illegal position, print "POSITION P EMPTY" (where P is the parameter value) and return 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; /* 保存线性表中最后一个元素的位置 */
};

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

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

Input example:

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

Output sample:

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()
{
    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;
}

Guess you like

Origin blog.csdn.net/weixin_44236278/article/details/102466473