You are on page 1of 4

#include <iostream> #include <conio.

h> #include <string> #include <iomanip> using namespace std; typedef struct node { int DATA; struct node *NEXT; }; node *HEAD = NULL; node *start=NULL; int Create(int data); int Display(); int Display1(); int SortAsc(); int SortDesc(); int main() { int numElem, num; char ch; int nobreak=1; cout << "Enter number of elements: "; cin >> numElem; cout << "Enter " << numElem << " numbers: "; for(int i = 0 ; i < numElem ; i++) { cin >> num; Create(num); } while (nobreak){ cout<<"---CHOOSE AN OPTION---"; cout<<" ASCENDING/DESCENDING order? A/D (0 to exit)"<<endl; cout<<"Your choice: "; cin>> ch; switch(ch) { case 'a': case 'A': if (ch == 'a'||ch=='A'){ cout << "Ascending Order"<<endl; SortAsc(); Display(); } break; case 'd': case 'D': if (ch == 'd' ||ch== 'D'){ cout << "Descending Order"<<endl; SortDesc(); Display1(); } break;

case '0':{ cout<<"goodbye!"<<endl; nobreak=0; break; } default:{ cout<<"Unknown option, menu loop will continue"; } } } } int SortAsc() { node* curr = HEAD; int count = 0; while(curr!=NULL) { count++; curr = curr->NEXT; } for(int i = count ; i > 1 ; i-- ) { node *temp, *swap1; swap1 = HEAD; for(int j = 0 ; j < count-1 ; j++ ) { if(swap1->DATA > swap1->NEXT->DATA) { node *swap2 = swap1->NEXT; swap1->NEXT = swap2->NEXT; swap2->NEXT = swap1; if(swap1 == HEAD) { HEAD = swap2; swap1 = swap2; } else { swap1 = swap2; temp->NEXT = swap2; } } temp = swap1; swap1 = swap1->NEXT; } } } int SortDesc() { node* curr = HEAD; int count = 0; while(curr!=NULL) { count++; curr = curr->NEXT; }

for(int i = count ; i > 1 ; i-- ) { node *temp, *swap1; swap1 = HEAD; for(int j = 0 ; j < count-1 ; j++ ) { if(swap1->DATA < swap1->NEXT->DATA) { node *swap2 = swap1->NEXT; swap1->NEXT = swap2->NEXT; swap2->NEXT = swap1; if(swap1 == HEAD) { HEAD = swap2; swap1 = swap2; } else { swap1 = swap2; temp->NEXT = swap2; } } temp = swap1; swap1 = swap1->NEXT; } } } int Create(int data) { node *front, *tail; tail = new node; tail->DATA = data; tail->NEXT = NULL; if(HEAD == NULL) HEAD = tail; else { front = HEAD; while(front->NEXT!=NULL) front = front->NEXT; front->NEXT = tail; } } int Display() { node *curr; curr = HEAD; while(curr!=NULL) { cout <<curr->DATA << endl; curr = curr->NEXT; } } int Display1()

node *curr; curr = HEAD; while(curr!=NULL) { cout <<curr->DATA << endl; curr = curr->NEXT; } return 0; system("pause"); }

You might also like