GOT IT WORKING!
I tried to go through the loop by hand and figured out this code which now works. But I don't know if its efficient, but something is better than nothing. Check it out:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// --- FUNCTION PROTOTYPES ---
void create_node();
void remove_newline();
char append();
void display();
// --- GLOBAL VARIABLES ---
int x = 1;
int i;
// --- LIST ---
struct numbers
{
int num;
struct numbers *next;
};
void bubble_sort(struct numbers *a);
struct numbers *first_node;
struct numbers *current_node;
struct numbers *new_node;
struct numbers *temp;
struct numbers *array_node;
int main()
{
printf("Hello!, input the numbers below: \n\n");
first_node=malloc(sizeof(struct numbers));
if(first_node== NULL)
{
puts("Memory allocation failed!");
return(0);
}
current_node=first_node;
printf("Input the #%d value: ", x);
scanf("%d", ¤t_node->num);
remove_newline();
char choice=append();
while(choice == 'y' || choice== 'Y')
{
x++;
create_node();
remove_newline();
choice = append();
}
if( choice == 'n' || choice == 'N')
{
current_node->next=NULL;
new_node->next=NULL;
printf("Okay, then. Bye\n\n");
}
else
{
printf("Invalid Input!");
return(0);
}
display();
printf("\n\nTOTAL NUMBER OF NODES: %d\n\n",x);
struct numbers *a;
a=first_node;
bubble_sort(a);
a=first_node;
for(i=0;i<x;i++)
{
printf("%d\t",a->num);
a=a->next;
}
return(0);
}
//--- FUNCTIONS ---
void create_node()
{
new_node=malloc(sizeof(struct numbers));
current_node->next=new_node;
if(new_node==NULL)
{
puts("Memory allocation failed");
}
current_node=new_node;
printf("Input #%d number: \n",x);
scanf("%d",¤t_node->num);
}
void remove_newline()
{
while(getchar() != '\n')
;
}
char append()
{
printf("\nDo you want to add more numbers in the list? Y/N: ");
char choice;
scanf("%c", &choice);
putchar('\n');
return(choice);
}
void display()
{
printf("Here is the unsorted list: \n");
temp=first_node;
while(temp!= NULL)
{
printf("%d\t",temp->num);
temp=temp->next;
}
}
void bubble_sort(struct numbers *a)
{
struct numbers *b;
int temp;
b=a->next;
for(i=0;i<x;i++)
{
while( b != NULL )
{
if(a->num > (b->num))
{
temp=a->num; // SWAPPING NUMBERS
a->num=(b->num);
(b->num)=temp;
}
a=a->next; // INCREMENT POSITIONS OF a & b
b=a->next;
}
a=first_node; // RESET THE POSITION OF a & b
b=a->next;
}
}
Thank you guys for mulling over the problem for me