I have this code (below) and it should be spitting out correct0 1 1 2 1 2 3 1 2
ID: 3569508 • Letter: I
Question
I have this code (below) and it should be spitting out correct0
1
1 2
1 2 3
1 2 3 4
but it's spitting out
1
1
1 2
1 2
1 3 2
1 3 2
1 4 3 2
1 4 3 2
when input0 is is entered using ./main input0 output0 What is wrong with the code that it's spitting out the numbers backwards?
i 1
i 2
i 3
i 4
q
#include "orderedList.h"
#include
#include
Node *orderedInsert(Node *p, int newval)
/* Allocates a new Node with data value newval
and inserts into the ordered list with
first node pointer p in such a way that the
data values in the modified list are in
nondecreasing order as the list is traversed.
*/
{
Node *q = NULL;
Node *tmp = NULL;
tmp = p;
q = malloc(sizeof(Node));
q->data = newval;
if (p == NULL)
{
q->next = p;
return q;
}
if (newval <= p->data)
{
q->next = p;
return q;
}
else
{
while (tmp != NULL)
{
if (tmp->data <= newval)
{
q->next = tmp->next;
tmp->next = q;
return p;
}
tmp = tmp->next;
}
return p;
}
}
void printList(FILE *outfile, Node *p)
/* Prints the data values in the list with
first node pointer p from first to last,
with a space between successive values.
Prints a newline at the end of the list.
*/
{
Node *tmp = p;
while (tmp != NULL)
{
printf ("%d ", tmp->data);
tmp = tmp->next;
}
puts ("");
}
void clearList(Node **p)
/* Deletes all the nodes in the list with
first node pointer *p, resulting in *p
having value NULL. Note that we are passing
a pointer by address so we can modify that
pointer.
*/
{
Node *tmp;
while (*p != NULL)
{
tmp = *p;
*p = (*p)->next;
free (tmp);
}
}
#include "orderedList.h"
#include
#include
Node *orderedInsert(Node *p, int newval)
/* Allocates a new Node with data value newval
and inserts into the ordered list with
first node pointer p in such a way that the
data values in the modified list are in
nondecreasing order as the list is traversed.
*/
{
Node *q = NULL;
Node *tmp = NULL;
tmp = p;
q = malloc(sizeof(Node));
q->data = newval;
if (p == NULL)
{
q->next = p;
return q;
}
if (newval <= p->data)
{
q->next = p;
return q;
}
else
{
while (tmp != NULL)
{
if (tmp->data <= newval)
{
q->next = tmp->next;
tmp->next = q;
return p;
}
tmp = tmp->next;
}
return p;
}
}
void printList(FILE *outfile, Node *p)
/* Prints the data values in the list with
first node pointer p from first to last,
with a space between successive values.
Prints a newline at the end of the list.
*/
{
Node *tmp = p;
while (tmp != NULL)
{
printf ("%d ", tmp->data);
tmp = tmp->next;
}
puts ("");
}
void clearList(Node **p)
/* Deletes all the nodes in the list with
first node pointer *p, resulting in *p
having value NULL. Note that we are passing
a pointer by address so we can modify that
pointer.
*/
{
Node *tmp;
while (*p != NULL)
{
tmp = *p;
*p = (*p)->next;
free (tmp);
}
}
#include "orderedList.h"
#include
#include
Node *orderedInsert(Node *p, int newval)
/* Allocates a new Node with data value newval
and inserts into the ordered list with
first node pointer p in such a way that the
data values in the modified list are in
nondecreasing order as the list is traversed.
*/
{
Node *q = NULL;
Node *tmp = NULL;
tmp = p;
q = malloc(sizeof(Node));
q->data = newval;
if (p == NULL)
{
q->next = p;
return q;
}
if (newval <= p->data)
{
q->next = p;
return q;
}
else
{
while (tmp != NULL)
{
if (tmp->data <= newval)
{
q->next = tmp->next;
tmp->next = q;
return p;
}
tmp = tmp->next;
}
return p;
}
}
void printList(FILE *outfile, Node *p)
/* Prints the data values in the list with
first node pointer p from first to last,
with a space between successive values.
Prints a newline at the end of the list.
*/
{
Node *tmp = p;
while (tmp != NULL)
{
printf ("%d ", tmp->data);
tmp = tmp->next;
}
puts ("");
}
void clearList(Node **p)
/* Deletes all the nodes in the list with
first node pointer *p, resulting in *p
having value NULL. Note that we are passing
a pointer by address so we can modify that
pointer.
*/
{
Node *tmp;
while (*p != NULL)
{
tmp = *p;
*p = (*p)->next;
free (tmp);
}
}
Explanation / Answer
Correct answer to print the above correct pyramid is very simple as written below: