-4

I am trying to edit a program I made a year ago, but it seems I am failing somewhere because I can't get the result I want. I want to make the program to sort the numbers from low to high and the user should enter numbers until 0 is pressed. Would love ot get some help from someone advanced!

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

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

struct node* List;

void Add(struct node* p, int d)
{
    struct node* q;
    q = malloc(sizeof(struct node));
    if (q == NULL)
        printf("Not enaugh memory!");
    else{
        q->data = d;
        if (List == NULL || List->data < d)
        {
            q->next = List;
            List = q;
        } else {
            struct node *ptr = List;
            while ((ptr->next != NULL) && (ptr->next->data>d)){
                ptr = ptr->next;
            }
            q->next = ptr->next;
            ptr->next = q;
        }
    }
}

int main()
{
    int n, i, a;
    printf("How many numbers are you going to enter? ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        printf("\nEnter a number: ");
        scanf("%d", &a);
        Add(List, a);
    }
    printf("\nEntered and sorted numbers are: ");
    struct node *ptr = List;
    while (ptr != NULL)
    {
        printf("%d ", ptr->data);
        ptr = ptr->next;
    }
    printf("\n\n");
    system("PAUSE");
    return 0;
}
MikeMB
  • 20,029
  • 9
  • 57
  • 102
Daniela Gocheva
  • 63
  • 2
  • 11
  • 2
    Please indent your code. – Sourav Ghosh Mar 31 '15 at 18:09
  • 2
    Please explain, in as much detail as you can stand to write, exactly what you think this program *should* do, and what it did instead, and why that behavior baffles you. – zwol Mar 31 '15 at 18:12
  • `until 0 is pressed`..where's the magic? – Sourav Ghosh Mar 31 '15 at 18:13
  • Also, [never use `scanf` for anything](https://stackoverflow.com/questions/24302160/scanf-on-an-istream-object/24318630#24318630). Just looking at your code, I see at least two places where it's probably doing something that makes no sense to you because `scanf` is terrible. If you use `fgets` and `strtol` instead those problems will disappear. – zwol Mar 31 '15 at 18:15
  • 1. Righ now it asks you how many numbers you are going to enter. Instead of that it should let you enter numbers until '0' is entered. 2. Right now the numbers are sorted in high to low order and it should be the other way - low to high order. – Daniela Gocheva Mar 31 '15 at 18:16
  • Please add `List = NULL;` somewhere at the start of this program. – Hogan Mar 31 '15 at 18:16

1 Answers1

1

Just change to line

if (List == NULL || List->data < d)

to

if (List == NULL || List->data > d)

And change the line

while ((ptr->next != NULL) && (ptr->next->data>d)){

to

while ((ptr->next != NULL) && (ptr->next->data<d)){

And you have not added 0 check.Please add that, rest is fine. For this modify the for loop with this one

//printf("How many numbers are you going to enter? ");
//scanf("%d", &n);
for (;;)
{
    printf("\nEnter a number: ");
    scanf("%d", &a);
    if(a == 0)
       break;
    Add(List, a);
}
Yasir Majeed
  • 739
  • 3
  • 12