简体   繁体   中英

Getting different results from PowerShell and CMD running an executable written in C

Following is a program I got as an assignment:

Given as input, a linked list and several pairs of elements. Process these pairs in such a way that the node with the first element of the pair starts pointing to the node with the second element of the pair. Repeat this process for each pair. Print the resultant linked list.

  1. The pairs should be processed sequentially in the given order.
  1. If while processing a pair, the linked list breaks, you can discard the disconnected nodes. Thereafter, if a pair contains an element from the disconnected nodes, print “error'.
  1. If there are duplicate entries in the linked list, consider the first occurrence (while traversing the linked list (in its current form) starting from head till the end).
  1. It is so possible that after processing all the pairs, the linked list develops cycles . In that case, print “error”. It is so possible that after processing a pair, the linked list becomes invalid, however while processing a later pair, it again becomes valid. Hence, check the validity of the linked list only after processing all the pairs.
  1. A '#' as the first element in a pair represents that the node with this element should be made the head “# 5” means that the node with the element 5 is now the head of the list.
  1. A '#' as the second element in a pair represents null, ie, the entry “5 #” means that the node with the element 5 will now have “null” in its “next” field.

Input format: ./a.out [number of elements in the linked list] [Elements of the linked list separated with space] [Pair 1] [Pair 2]... [Pair k]

Output format: Elements of the modified linked list separated by space

Test Case 1: ./a.out 7 6 78 67 12 56 67 80 12 67 67 78 67 6 # 67 78 #

67 6 78

Following is the explanation for the changes made to the linked list after every operation. 6 -> 78 -> 67 -> 12 -> 56 -> 67 -> 80

After processing (12,67) : 6 -> 78 -> 67 <-> 12 ( A cycle develops between 12 and 67)

After processing (67,78) : 6 -> 78 <-> 67 ( 12 becomes disconnected from the linked list)

After processing (67,6) : 6 -> 78 -> 67 -> 6 ->... (back link from 67 to 6. Same node is represented by the same color)

After processing (#, 67) : 67 -> 6 -> 78 -> 67 ->... ( 67 becomes head)

After processing (78, #) : 67 -> 6 -> 78 ( 78 becomes the last node)

Test Case 2: ./a.out 4 12 20 21 40 20 12 21 # (Processing the first pair, 20 12 breaks 21 and 40 from the linked list, hence error while processing the second pair, 21 #)

error

Test Case 3: ./a.out 2 -3 4 1 2 (Elements given in the pair do not exist in the linked list)

error

Test Case 4: ./a.out 6 1 2 3 4 5 6 6 5 5 4 4 3 3 2 2 1 # 6 (Cycle)

error

This the the solution I came up with and it satisfies all the test cases so far:

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


struct Node
{
    int value;
    struct Node* next;
}*head = NULL, *temp = NULL;

void Link(const char *left, const char *right, int sizeOfLL)
{
    short counter = 0;
    struct Node* leftPtr = NULL, * rightPtr = NULL;
    temp = head;

    if (atoi(left) == head->value)
        leftPtr = head;
    if (atoi(right) == head->value)
        rightPtr = head;
    if (strcmp(left, "#") == 0 && leftPtr == NULL)
        counter++;
    if (strcmp(right, "#") == 0 && rightPtr == NULL)
        counter++;
    for (int i = 2; i <= sizeOfLL && (leftPtr == NULL || rightPtr == NULL); i++)
    {
        temp = temp->next;
        if (temp == NULL)
            break;
        if (strcmp(left, "#") == 0 && leftPtr == NULL)
            counter++;
        if (strcmp(right, "#") == 0 && rightPtr == NULL)
            counter++;
        if (atoi(left) == temp->value && leftPtr == NULL)
        {
            leftPtr = temp;
            counter++;
        }
        if (atoi(right) == temp->value && rightPtr == NULL)
        {
            rightPtr = temp;
            counter++;
        }
    }

    if (counter == 0)
    {
        printf("error1");
        exit(0);
    }
    if (leftPtr == NULL)
        head = rightPtr;
    else
        leftPtr->next = rightPtr;
}

int main(int argc, char* argv[])
{
    struct Node* newNode = (struct Node*)malloc(sizeof(int));

    newNode->value = atoi(argv[2]);
    head = newNode;
    temp = newNode;

    for (int i = 2; i <= atoi(argv[1]); i++)
    {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        if (newNode == NULL)
        {
            printf("error2");
            return 0;
        }
        newNode->value = atoi(argv[i + 1]);
        temp->next = newNode;
        temp = newNode;
    }
    temp->next = NULL;

    for (int i = atoi(argv[1]) + 2; i < argc; i = i + 2)
    {
        Link(argv[i], argv[i + 1], atoi(argv[1]));
    }

    temp = head;
    for (int i = 2; i <= atoi(argv[1]) + 1; i++)
    {
        if (temp == NULL)
            break;
        temp = temp->next;
        
        if (i == atoi(argv[1]) + 1)
        {
            printf("error3");
            return 0;
        }
    }

    printf("%d ", head->value);
    for (int i = 2; i <= atoi(argv[1]); i++)
    {
        head = head->next;
        if (head == NULL)
            break;
        printf("%d ", head->value);
    }

    return 0;
}

On compiling this C program using gcc, when I pass 4 1 2 3 4 1 # as command line arguments in CMD, it shows the expected output, 1 , but passing 4 1 2 3 4 1 \\# in PowerShell gives error1 , coming from the Link() function. Why is it? Is it anyhow related to writing \\ before # in PowerShell as the escape character? PowerShell output CMD output

Silly mistake by me. Thank you @vonPryz, PowerShell escape character is ` not \\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM