PTA:Cコースデザイン (6)

機能に関する質問

6-6-1 単一リンクリストの長さを求める

先頭ノード Interface を持つ単一リンク リストのテーブル長を見つける関数を実装します

int Length ( LinkList L );

LinkList 構造は次のように定義されます。

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

達成:

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

6-6-2 単一リンクリスト内の要素のシリアル番号を検索する

インターフェース:

int Locate ( LinkList L, ElemType e);

Lは先頭ノードを持つ単連結リストの先頭ポインタ、eは検索対象の要素値です。e が単一リンク リストに存在する場合、関数 Locate はそのシリアル番号 (シリアル番号は 1 から始まります) を返し、それ以外の場合は 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 単一リンクリストのノードの階乗和を求める

この質問では、単一リンク リストLのノードの階乗和を求める関数の実装が必要です。デフォルトでは、すべてのノードの値は負ではなく、タイトルは結果がint範囲内にあることを保証します。
インターフェース:

int FactorialSum( List L );

単一リンク リストはList次のように定義されます。

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

達成:

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 逆引きデータを含むリンクリストの作成

入力データの逆順にリンクリストを構築する関数を実装します。
インターフェース:

struct ListNode *createlist();

この関数は入力から一連の正の整数を取得するためにcreatelist使用され、-1 を読み取ると、入力の終了を意味します。scanf入力データの逆順に連結リストを作成し、連結リストの先頭ポインタを返します。リンク リストのノード構造は次のように定義されます。

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

ここでは、追加の逆リンクリストを作成するために、意図的にテールプラグを作成してから反転しました

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

プログラミングの質問

6-7-1 単一リンクリストの作成と走査

ここに画像の説明を挿入

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

おすすめ

転載: blog.csdn.net/m0_74195626/article/details/130176369
おすすめ