PTA: C Course Design (6)

function questions

6-6-1 Find the length of the singly linked list

Implement a function to find the table length of the singly linked list with the leading node Interface
:

int Length ( LinkList L );

The LinkList structure is defined as follows:

typedef struct LNode
{
    
    
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;

accomplish:

int Length ( LinkList L )
{
    
    
    int n = 0;
    LinkList cur = L;
    while(cur->next)
    {
    
    
        cur = cur->next;
        n++;
    }
    return n;
}

6-6-2 Find the serial number of the element in the singly linked list

interface:

int Locate ( LinkList L, ElemType e);

L is the head pointer of the singly linked list with the head node, and e is the element value to be searched. If e exists in the singly linked list, the function Locate returns its serial number (the serial number starts from 1); otherwise, it returns 0.

int Locate ( LinkList L, ElemType e)
{
    
    
    int n = 1;
    LinkList cur = L->next;
    while(cur)
    {
    
    
        if(cur->data == e)
            return n;
        n++;
        cur = cur->next;
    }
    return 0;
}

6-6-3 Find the factorial sum of the nodes of the singly linked list

LThis question requires the implementation of a function to find the factorial sum of the nodes of a singly linked list . By default, the values ​​of all nodes are non-negative, and the title guarantees that the result is within intthe range.
interface:

int FactorialSum( List L );

The single linked list Listis defined as follows:

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

accomplish:

int factorial(int data)
{
    
    
    if (data == 0)
        return 1;
    else
    {
    
    
        int ret = data;
        while (--data)
        {
    
    
            ret *= data;
        }
        return ret;
    }
}
int FactorialSum( List L )
{
    
    
    if(L==NULL)
        return 0;
    int ret = 0;
    List cur = L;
    while(cur)
    {
    
    
        ret += factorial(cur->Data);
        cur = cur->Next;
    }
    return ret;
}

6-6-4 Create a linked list with reverse data

Implement a function to build a linked list in reverse order of the input data.
interface:

struct ListNode *createlist();

The function createlistuses scanfto get a series of positive integers from the input, and when it reads −1, it means the end of the input. Create a linked list in the reverse order of the input data, and return the linked list head pointer. The linked list node structure is defined as follows:

struct ListNode {
    
    
    int data;
    struct ListNode *next;
};

It can be directly plugged in. Here, in order to write an extra reverse linked list, I deliberately made a tail plug and then reversed.
Realization:

struct ListNode* reverseList(struct ListNode* head)
{
    
    
    struct ListNode* newhead = NULL, * cur = head;
    while (cur)
    {
    
    
        struct ListNode* next = cur->next;
        cur->next = newhead;
        newhead = cur;
        cur = next;
    }
    return newhead;
}
struct ListNode* BuySLTNode(int x)
{
    
    
    struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
    if (newnode == NULL)
    {
    
    
        perror("malloc fail");
        return;
    }
    newnode->data = x;
    newnode->next = NULL;

    return newnode;
}
void SLTPushBack(struct ListNode** pphead, int x)
{
    
    
    struct ListNode* newnode = BuySLTNode(x);
    if (*pphead == NULL)
    {
    
    
        *pphead = newnode;
    }
    else
    {
    
    
        struct ListNode* tail = *pphead;
        while (tail->next != NULL)
        {
    
    
            tail = tail->next;
        }
        tail->next = newnode;
    }
}
struct ListNode* createlist()
{
    
    
    struct ListNode* head = NULL;
    int n = 0;
    while (1)
    {
    
    
        scanf("%d", &n);
        if (n == -1)
            break;
        SLTPushBack(&head, n);
    }
    struct ListNode* ret = reverseList(head);
    return ret;
}

programming questions

6-7-1 Creation and traversal of singly linked list

insert image description here

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

typedef struct ListNode
{
    
    
    int data;
    struct ListNode* next;
}ListNode;

ListNode* BuySLTNode(int x)
{
    
    
    ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
    if (newnode == NULL)
    {
    
    
        perror("malloc fail");
        return;
    }
    newnode->data = x;
    newnode->next = NULL;

    return newnode;
}
void SLTPushBack(ListNode** pphead, int x)
{
    
    
    ListNode* newnode = BuySLTNode(x);
    if (*pphead == NULL)
    {
    
    
        *pphead = newnode;
    }
    else
    {
    
    
        ListNode* tail = *pphead;
        while (tail->next != NULL)
        {
    
    
            tail = tail->next;
        }
        tail->next = newnode;
    }
}
int main()
{
    
    
    int n = 0;
    scanf("%d", &n);
    if(n == 0)
        return 0;
    ListNode* head = NULL;
    while (n--)
    {
    
    
        int c = 0;
        scanf("%d", &c);
        SLTPushBack(&head, c);
    }
    ListNode* cur = head;
    printf("%d", cur->data);
    cur = cur->next;
    while (cur)
    {
    
    
        printf(" %d", cur->data);
        cur = cur->next;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_74195626/article/details/130176369