【问题标题】:How to prevent the program to display the converted input, I'm wanting the program to display the original input如何防止程序显示转换后的输入,我希望程序显示原始输入
【发布时间】:2021-12-11 01:53:57
【问题描述】:

我希望我的代码显示原始输入 3 -> 2-> 1-> 而不是显示反转的输入。 输出显示 1-> 2-> 3-> 。我想在不破坏代码的情况下查看原始输入。我怎样才能做到这一点?我们的教授教我们链表的概念,在这个程序中它是连接输入一个数字并检查它是否是一个palendrome

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

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

void insertNum(struct node** head, int number) {
    struct node* temp = malloc(sizeof(struct node));

    temp -> data = number;
    temp -> next = *head;
    *head = temp;
}

void display(struct node* head) {
    struct node* printNode = head;
    printf("displaying list1...\n");
    printf("displaying the converted list..\n");
    while (printNode != NULL) {
        if (printNode->next)
            printf("%d->",printNode->data);
        else
            printf("%d->NULL\n",printNode->data);
    
        printNode=printNode->next;
    }
}

struct node* reverseLL(struct node* head) {
    struct node* reverseNode = NULL, *temp;

    if (head == NULL) {
        printf("\nThe list is empty.\n\n");
        exit(0);
    }

    while (head != NULL) {
        temp = (struct node*) malloc(sizeof(struct node));
        temp -> data = head -> data;
        temp -> next = reverseNode;
        reverseNode = temp;
        head = head -> next;
    }

    return reverseNode;
}

int check(struct node* LLOne, struct node* LLTwo) {
    while (LLOne != NULL && LLTwo != NULL) {
        if (LLOne->data != LLTwo->data)
            return 0;

        LLOne = LLOne->next;
        LLTwo = LLTwo->next;
    }

    return (LLOne == NULL && LLTwo == NULL);
}

void deleteList(struct node** display) {
    struct node* temp = *display;

    while (temp != NULL) {
        temp = temp->next;
        free(*display);
        *display = temp;
    }
}

int main() {
    int inputNum, countRun = 0, loop;
    char choice;
    struct node* reverseList;
    struct node* head = NULL;
    do {
        printf("%Run number : %d\n", ++countRun);
        printf("Enter 0 to stop building the list, else enter any integer\n");
        printf("Enter list to check whether it is a palindrome... \n");
        do {
            scanf("%d", &inputNum);
            if (inputNum == 0)
                break;

            insertNum(&head, inputNum);
        } while (inputNum != 0);

        display(head);
        reverseList = reverseLL(head);

        if ((check(head, reverseList)) == 1)
            printf("\nPalindrome list.\n\n");
        else
            printf("\nNot palindrome.\n\n");

        deleteList(&head);
    } while (countRun != 2);

    system("pause");

    return 0;
}

【问题讨论】:

标签: c algorithm input struct queue


【解决方案1】:

您的insertNum 插入到列表的前面

您需要一个附加在列表后面的版本:

void
appendNum(struct node **head, int number)
{
    struct node *temp = malloc(sizeof(struct node));
    struct node *cur;
    struct node *prev;

    temp->data = number;

    // find tail of the list
    prev = NULL;
    for (cur = *head;  cur != NULL;  cur = cur->next)
        prev = cur;

    // append after the tail
    if (prev != NULL)
        prev->next = temp;

    // insert at front (first time only)
    else
        *head = temp;
}

这里是完整的代码:

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

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

void
appendNum(struct node **head, int number)
{
    struct node *temp = malloc(sizeof(struct node));
    struct node *cur;
    struct node *prev;

    temp->data = number;

    // find tail of the list
    prev = NULL;
    for (cur = *head;  cur != NULL;  cur = cur->next)
        prev = cur;

    // append after the tail
    if (prev != NULL)
        prev->next = temp;

    // insert at front (first time only)
    else
        *head = temp;
}

void
insertNum(struct node **head, int number)
{
    struct node *temp = malloc(sizeof(struct node));

    temp->data = number;

    temp->next = *head;
    *head = temp;
}

void
display(struct node *head)
{
    struct node *printNode = head;

    printf("displaying list1...\n");
    printf("displaying the converted list..\n");
    while (printNode != NULL) {
        if (printNode->next)
            printf("%d->", printNode->data);
        else
            printf("%d->NULL\n", printNode->data);
        printNode = printNode->next;
    }
}

struct node *
reverseLL(struct node *head)
{
    struct node *reverseNode = NULL,
        *temp;

    if (head == NULL) {
        printf("\nThe list is empty.\n\n");
        exit(0);
    }

    while (head != NULL) {
        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = head->data;
        temp->next = reverseNode;
        reverseNode = temp;
        head = head->next;
    }

    return reverseNode;

}

int
check(struct node *LLOne, struct node *LLTwo)
{
    while (LLOne != NULL && LLTwo != NULL) {
        if (LLOne->data != LLTwo->data)
            return 0;

        LLOne = LLOne->next;
        LLTwo = LLTwo->next;
    }

    return (LLOne == NULL && LLTwo == NULL);
}

void
deleteList(struct node **display)
{
    struct node *temp = *display;

    while (temp != NULL) {
        temp = temp->next;
        free(*display);
        *display = temp;
    }
}

int
main()
{
    int inputNum, countRun = 0, loop;
    char choice;
    struct node *reverseList;
    struct node *head = NULL;

    do {
        printf("%Run number : %d\n", ++countRun);
        printf("Enter 0 to stop building the list, else enter any integer\n");
        printf("Enter list to check whether it is a palindrome... \n");
        do {
            scanf("%d", &inputNum);
            if (inputNum == 0)
                break;

#if 0
            insertNum(&head, inputNum);
#else
            appendNum(&head, inputNum);
#endif
        } while (inputNum != 0);

        display(head);
        reverseList = reverseLL(head);

        if ((check(head, reverseList)) == 1)
            printf("\nPalindrome list.\n\n");
        else
            printf("\nNot palindrome.\n\n");

        deleteList(&head);

    } while (countRun != 2);

    system("pause");

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    相关资源
    最近更新 更多