1

I came across some problems while doing my assignment. Part of this assignment involves creating a linked-list with the data given and printing them out. The data stored in the list is of datatype 'Atom', which can be an integer, a string, a "pair"(self-defined datatype), or a list made of Nodes.

If the input is (the first integer indicates the number of data)

4
1 2 3 4

it prints

1 2 3 4

What makes me feel strange is that, if I change the function read_list_helper(int n) like this

    Node* read_list_helper(int n)
{
    Atom *atom;

    if (n == 0) return NULL;

    return combine_atom_and_list(get_atom(), read_list_helper(n - 1));
}

the output becomes

4 3 2 1

Why would this happen? I have tried several keywords to search for the answer, but none of them works.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define STRING 0
#define NUM 1
#define PAIR 2
#define LIST 3

struct Atom;

typedef struct Pair
{
    struct Atom *left;
    struct Atom *right;
} Pair;

typedef struct Node
{
    struct Atom *data;
    struct Node *next;
} Node;

typedef struct Atom
{
    int dtype;
    int val = 0;
    char str[21] = {'\0'};
    Pair *pair = NULL;
    Node *Node = NULL;
} Atom;

Node* read_list();
Node* read_list_helper(int);
Atom* get_atom();
Node* combine_atom_and_list(Atom*, Node*);

void print_atom(Atom*);
void print_pair(Pair*);
void print_list(Node*);

int main()
{
    Node *head1;

    head1 = read_list();
    print_list(head1);


    system("pause");
    return 0;
}

Node* read_list()
{
    int n;

    scanf("%d", &n);

    return read_list_helper(n);
}

Node* read_list_helper(int n)
{
    Atom *atom;

    if (n == 0) return NULL;

    atom = get_atom();

    return combine_atom_and_list(atom, read_list_helper(n - 1));
}

Atom* get_atom()
{
    int num;
    char str[21];
    int i;

    Atom* res = (Atom*)malloc(sizeof(Atom));

    if (scanf("%d", &num) == 1)
    {
        res->dtype = NUM;
        res->val = num;
    }
    else
    {
        scanf(" %20s", str);
        res->dtype = STRING;
        strncpy(res->str, str, 20);
    }
    return res;
}

Node* combine_atom_and_list(Atom* atom, Node* node)
{
    Node *res = (Node*)malloc(sizeof(Node));
    res->data = atom;
    res->next = node;
    return res;
}



void print_atom(Atom* atom)
{
    if (atom == NULL) return;

    switch (atom->dtype)
    {
    case NUM:
        printf("%d", atom->val);
        break;
    case STRING:
        printf("%s", atom->str);
        break;
    case PAIR:
        print_pair(atom->pair);
        break;
    case LIST:
        print_list(atom->Node);
        break;
    default:
        break;
    }
}

void print_pair(Pair* pair)
{
    print_atom(pair->left);
    print_atom(pair->right);
}

void print_list(Node* node)
{
    print_atom(node->data);
    if (node->next != NULL)
    {
        printf(" ");
        print_list(node->next);
    }
}
Sienna Liu
  • 13
  • 3
  • This question is probably still a duplicate of something, but more likely because the linked list is being assembled backwards than because of indeterminacy in the order of evaluation of parameters. – Quinn Mortimer Jan 27 '18 at 15:53

1 Answers1

0

Function arguments are not evaluated in any particular order. Since one of your arguments does I/O, the order of evaluation affects the order of the inputs.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76