Okay, here is the finished code, use laserlight's guidance and his code and modified as per my needs.:
Code:
#include<stdio.h>
#include<stdlib.h>
int menu(void);
void create(int num2);
int flag = 0; // NODE COUNTER
struct number
{
int num;
struct number *next;
};
struct number *first=NULL;
struct number *current=NULL;
struct number *new_node=NULL;
struct number *sort_list=NULL;
struct number *list=NULL;
struct number *prev=NULL;
void display(struct number *start);
struct number *smallest_no(struct number *spy);
int main(void)
{
int choice,num2,x;
printf("Welcome!\n\n");
choice = menu(); // PROMPT FOR CHOICE
while(choice != 3)
{
switch(choice)
{
case 1:
{
printf("Number to stuff in the list: ");
scanf("%d", &num2);
create(num2);
choice = menu();
break;
}
case 2:
{
display(first);
putchar('\n');
choice = menu();
break;
}
case 4:
{
for(x=0;x<flag;x++)
{
sort_list = smallest_no(first);
if(list)
{
prev->next=sort_list;
prev=sort_list;
}
else
{
list = sort_list;
prev=list;
}
}
display(list);
list=NULL;
choice = menu();
break;
}
default:
printf("Invalid Choice!\n\n");
choice = menu();
break;
}
}
printf("Bye!\n\a");
exit(0);
}
int menu(void)
{
int choice;
printf("=== Menu === \n"); // MENU FOR CHOOSING
printf("1 - Create Linked List OR append node in the end of current list.\n\
2 - Display the list.\n\
3 - Quit.\n4 - Sort the current list.\n===========\n\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
putchar('\n');
return(choice);
}
void create(int num2)
{
if(first==NULL) // IF THERE IS NO FIRST NODE IN THE LINKED LIST
{ // AND PUT THE NUMBER INPUT INTO THE NODE
first=malloc(sizeof(struct number));
if(first==NULL)
{
printf("ERROR");
exit(0);
}
first->num = num2;
first->next=NULL;
current=first;
flag = 1;
}
else
{
current=first; // TRAVERSE THE LINKED LIST FROM FIRST
while(current->next != NULL) // NODE TO THE LAST NODE
{
current=current->next;
}
new_node=malloc(sizeof(struct number));
if(new_node==NULL)
{
printf("ERROR");
exit(0);
}
current->next = new_node; // LAST NODE IS NOW NEWLY CREATED NODE
current=new_node; // AND PUT THE NUMBER INPUT INTO THE NODE
current->num= num2;
current->next=NULL;
flag++; // NODE NUMBER INCREASED
}
}
void display(struct number *start)
{
if(start == NULL)
{
printf("No List to display!!!\n"); // LIST IS EMPTY
}
else
{
printf("Here is the list: \n");
current=start;
while(current != NULL)
{
printf("%d\t",current->num);
current=current->next;
}
putchar('\n');
putchar('\n');
printf("TOTAL NUMBER OF NODES: %d\n", flag);
putchar('\n');
}
}
struct number *smallest_no(struct number *spy)
{
struct number *smallest = spy;
if(smallest)
{
struct number *before_smallest=NULL;
struct number *previous = smallest;
struct number *with= smallest->next;
while(with)
{
if(with->num < smallest->num)
{
smallest= with;
before_smallest=previous;
}
previous=with;
with=with->next;
}
if(before_smallest)
{
before_smallest->next = smallest->next;
}
else
{
first=smallest->next;
}
if(smallest->next)
{
smallest->next=NULL;
}
else
{
current=before_smallest;
}
}
return(smallest);
}
laserlight could you please tell me, in your code (last post on the first page of this thread),how are you supposed to get the tail of the original list. I couldn't get that so I took the original list's tail and head (named current and first in the code,respectively).
Thanks.