[Data structure exercises] Find the mth element from the bottom of the linked list (PTA)

Require:

Please design an algorithm that is as efficient as possible in terms of time and space, without changing the linked list, to find the last m (>0) element of the linear list stored in the linked list.

Function interface definition:

ElementType Find( List L, int m );

where the Liststructure is defined as follows:

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

Lis a singly linked list of the given head node; the function is Findto return Lthe penultimate melement without changing the original linked list. If such an element does not exist, an error flag is returned ERROR.

Example of the referee test procedure:

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

#define ERROR -1

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

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

ElementType Find( List L, int m );

int main()
{
    
    
    List L;
    int m;
    L = Read();
    scanf("%d", &m);
    printf("%d\n", Find(L,m));
    Print(L);
    return 0;
}

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

Input sample:

5
1 2 4 5 6
3

Sample output:

4
1 2 4 5 6 

full code

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

#define ERROR -1

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

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

ElementType Find( List L, int m );

int main()
{
    
    
    List L;
    int m;
    L = Read();
    scanf("%d", &m);
    printf("%d\n", Find(L,m));
    Print(L);
    return 0;
}

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

/*
 * @Description : 创建一个用户定义的链表
 * @param       : 无
 * @return 		: 新的的链表
 */
List Read()
{
    
    
    int n;
    scanf("%d", &n);
    //为头节点申请空间
    List Head = (List)malloc(sizeof(PtrToNode));
    //防止野指针的出现
    Head->Next = NULL;
    //当n不是0的时候
    if (n != 0){
    
    
        //创建一个指针
        List p = Head;
        for (int i = 0; i < n; i++){
    
    
            //创建一个中间节点
            List Var = (List)malloc(sizeof(PtrToNode));
            //尾插法
            scanf("%d", &(Var->Data));
            //p的指针指向新的元素
            p->Next = Var;
            //p后移动
            p = Var;
        }
        //安全起见,尾指针为空
        p->Next = NULL;
    }
    return Head;
}

/*
 * @Description : 打印链表
 * @param  -L   : 需要打印的链表
 * @return 		: 无
 */
void Print( List L )
{
    
    
    //先找到第一个结点,判断是不是空
    L = L->Next;
    if (L == NULL){
    
    
        printf("NULL");
    }
    else{
    
    
        while (L){
    
    
            printf("%d ",L->Data);
            L = L->Next;
        }
    }
    printf("\n");
}

/*
 * @Description : 查找函数
 * @param  -L   : 需要查找的链表
 * @param  -m   : 第m个元素
 * @return 		: 返回找到的元素
 *                不存在返回错误
 */
ElementType Find( List L, int m )
{
    
    
    int Len = 0;
    int i = 0;
    List p = L;
    //计算链表长度
    while (p->Next){
    
    
        Len++;
        p = p->Next;
    }
    if (m > Len){
    
    
        return ERROR;
    }
    else{
    
    
        p = L;
        while (p->Next && i < Len - m ){
    
    
            p = p->Next;
            i++;
        }
        return p->Next->Data;
    }
}


At last

This method is not the optimal solution and takes 26ms. If the boss has a better method, please advise!

Guess you like

Origin blog.csdn.net/qq_45628620/article/details/114800376