Tuesday, August 19, 2014

Priority Queues Demo - An application of Heap ADT


#include <cstdlib>
#include <conio.h>
#include <iostream>

using namespace std;

char getChoice(); 
char menu(string menu_item[], int menu_size);

#define HEAP_SIZE 21
#define MENU_SIZE 8

int heap[HEAP_SIZE];
int n = 0;

string menu_item[] = {"[1] Insert an element", 
                      "[2] Delete the maximum element",
                      "[3] Print all Elements",
                      "[4] Print the maximum element",
                      "[5] Examine the left child",
                      "[6] Examine the right child",
                      "[7] Examine the parent",
                      "[8] Exit"};

char getChoice();
void insert(int heap[], int &n, int x);
char menu(string menu_item[], int menu_size);
int deletemax(int heap[], int &n);
void heapify(int heap, int r);
void print_elements();
bool find(string s, char c);
void heapify(int heap[], int r);
void swap(int &n1, int &n2); 
int max(int heap[]);
int search(int heap[], int number);


int main(int argc, char *argv[])
{
    char choice;
    int number;
    do
    {
       system("CLS") ;
       choice = menu(menu_item, MENU_SIZE);
       switch (choice) {
           case '1' :
                cout << "What number would you like to insert into the heap  ? ";
                cin >> number;
                insert(heap, n, number); 
                break;
           case '2' :             
                deletemax(heap, n);
                cout << "The maximum value has been deleted." << endl;
                break;                
           case '3' :
                print_elements();
                break;                    
           case '4' :
                if (n != 0)
                   cout << "\nThe maximum element is " << max(heap) << endl;
                else
                   cout << "\nHeap is empty!" << endl;   
                break;                     
           case '5' :
                if (n != 0) {
                   cout << "What number would you like to get the left child ? ";
                   cin >> number;
                   int index = search(heap, number);
                   if (index <= n) {
                      if ((index * 2) <= n) 
                         cout << "\nThe left child of " << number << " is " << heap[index * 2] << endl;
                      else
                         cout << endl << number << " has no left child." << endl;    
                   }   
                   else
                      cout << number << " not found!" << endl;                          
                }       
                else
                   cout << "\nHeap is empty!" << endl;   
                break;  
           case '6' :
                 if (n != 0) {
                   cout << "What number would you like to get the right child ? ";
                   cin >> number;
                   int index = search(heap, number);
                   if (index <= n) {
                      if ((index * 2 + 1) <= n) 
                         cout << "\nThe right child of " << number << " is " << heap[index * 2 + 1] << endl;
                      else
                         cout << endl << number << " has no right child." << endl;    
                   } 
                   else
                      cout << number << " not found!" << endl;     
                }       
                else
                   cout << "\nHeap is empty!" << endl;  
                break;
           case '7' :
                 if (n != 0) {
                   cout << "What number would you like to get the parent ? ";
                   cin >> number;
                   int index = search(heap, number);
                   if (index <= n) {
                     if ((index / 2) >= 1) 
                         cout << "\nThe parent of " << number << " is " << heap[index / 2] << endl;
                     else
                         cout << endl << number << " has no parent." << endl;    
                   }      
                   else
                      cout << number << " not found!" << endl;
                 } 
                 else
                    cout << "\nHeap is empty!" << endl;  
                break;     
                    
       }
       
       system("PAUSE");
    }  while (choice != '8');
    
    return EXIT_SUCCESS;
}
                     
char getChoice() 
{
     string choices = "12345678";
     char choice;
     do 
        choice =  getch();
     while (!find(choices,choice));   
     return choice;
}

bool find(string s, char c)
{
     bool found = false;
     int i = 0;
     while ((!found) && i < s.length())
     {
           if (c == s[i]) {
               found = true;
               break;
           }    
           i++;
     }
     return found;
} 

char menu(string menu_item[], int menu_size)
{
     char choice = 0;
     cout << "=============================" << endl; 
     cout << "  Hello! I'm a Heap Manager  " << endl;
     cout << "=============================" << endl; 
     for (int i = 0; i < menu_size; i++)
        cout << menu_item[i] << endl;
     cout << "=============================" << endl; 
     cout << "Enter your choice : ";
     choice = getChoice();
     cout << choice << endl;
     return choice;
}


void insert(int heap[], int &n, int x)
{
     int pnew = n + 1;
     heap[pnew] = x;
     while(pnew > 1 && heap[pnew /2] < x) {
       heap[pnew] = heap[pnew / 2];
       pnew = pnew / 2;           
     }
     heap[pnew] = x;
     n++;
}

int deletemax(int heap[], int &n)
{
    int max = heap[1];
    heap[1] = heap[n];
    n--;
    heapify(heap, 1);
    return max;
}    

int max(int heap[]) 
{
    return heap[1];
}

void heapify(int heap[], int r)
{
     int largest;
     int left = 2 * r;
     int right = 2 * r +1;
     if(left <= n && heap[left] > heap[r])
        largest = left;
     else
        largest = r;
        
     if (right <= n && heap[right] > heap[largest])
        largest = right;
        
     if (largest != r) {
        swap(heap[r], heap[largest]);
        heapify(heap, largest);  
     }                 
}   

void swap(int &n1, int &n2) 
{
     int temp;
     
     temp = n1;
     n1 = n2;
     n2 = temp;
     
}       

void print_elements()
{
     for (int i= 1; i <= n; i++) {
         cout << heap[i] << " ";
     }
}

int search(int heap[], int number)
{
    int i = 1;
    while (i <= n) {
       if (heap[i] == number) 
           break;
       i++;   
    }
    return i;
}    

Wednesday, August 13, 2014

List ADT using array implementation

// ---------------------------------------------------------------------------------
// Project Name        : ListADTArrayImplementation.dev
// Program Description : This program manage list of persons' name. It implements
//                       LIST ADT using array.
// Programmed          : Jun Y. Ercia
// Date Created        : July 9, 2014 
// Date Modified       : July 9, 2014
// Language            : C++
// IDE                 : Dev C++
// ---------------------------------------------------------------------------------
#include <cstdlib>
#include <conio.h>
#include <iostream>
using namespace std;
bool isempty();
void makenull();
string tail(string list[], int n);
string front(string list[]);
int search(string list[], string element);
void insert(string list[], string element, int p);
void Delete(string list[], int p);
void print_list(string list[], int n);
bool find(string s, char c);
char getChoice();
char menu(string menu_item[], int menu_size);
#define LIST_SIZE 10
#define MENU_SIZE 7
string list[LIST_SIZE];
int n = 0;
string menu_item[] = {"[1] Insert an Element",
                      "[2] Delete an Element",
                      "[3] Search an Element",
                      "[4] Examine the Front Element",
                      "[5] Examine the Tail Element",
                      "[6] Print all Elements",
                      "[7] Exit"};
int main(int argc, char *argv[])
{
    char choice;
    string name;
    int position;
    do
    {
       system("CLS") ;
       choice = menu(menu_item, MENU_SIZE);
       switch (choice) {
           case '1' :
                cout << "What name would you like to insert into the list  ? ";
                cin >> name;
                do {
                  cout << "In what position would you like to insert " << name
                       << "-[0-" << n <<"] ? ";
                  cin >> position;                 
                }  while (!(position >= 0 && position <= n));
                insert(list, name, position);
                break;
           case '2' :
                cout << "What name would you like to delete ? ";
                cin >> name;
                if ((position = search(list, name)) < n) {
                   Delete(list, position);              
                   cout << name << " has been deleted." << endl;
                }                
                else {
                   cout << "Cannot perform deletion. " << name << " is not in the list." << endl; 
                }
                break;               
           case '3' :
                cout << "What name would you like to search ? ";
                cin >> name;
                if (search(list, name) < n) {
                   cout << name << " is in the list." << endl;            
                }                
                else {
                   cout << name << " is not in the list." << endl; 
                }
                break;                   
           case '4' :
                if (isempty())
                    cout << "The List is empty." << endl;             
                else
                     cout << "The element at the front is " << front(list) << endl; 
                break;                    
           case '5' :
                if (isempty())
                    cout << "The List is empty." << endl;            
                else
                     cout << "The element at the Tail is " << tail(list, n) << endl; 
                break;                    
           case '6' :
                if (isempty())
                    cout << "The List is empty." << endl;           
                else
                    print_list(list, n);
                break;                   
                   
       }
       system("PAUSE");
    }  while (choice != '7');
   
    return EXIT_SUCCESS;
}

bool isempty()
{
     return (n == 0);
}    

void makenull()
{
     n = 0;
}    
string tail(string list[], int n)
{
   if (!isempty())
      return list[n-1];   
   else
      cout << "List is empty..." << endl;  

string front(string list[])
{
   if (!isempty())
      return list[0];   
   else
      cout << "List is empty..." << endl;  
}      

int search(string list[], string element)
{
    bool found =  false;
    int p = 0;
    while (!found && p < n) {
       if (list[p] == element) {
          found = true;
          break;
       }              
       p++;
    }     
    return p;
}
void insert(string list[], string element, int p)
{
     for (int i = n; i > p ; i--)
        list[i] = list[i-1];
     list[p] = element;
     n++;  
}    
void Delete(string list[], int p)
{
     for (int i = p; i < (n-1) ; i++)
        list[i] = list[i+1];
     n--;  

void print_list(string list[], int n)
{
     for (int i=0; i < n; i++)
        cout << (i+1) << ". " << list[i] << endl;
}    
                     
bool find(string s, char c)
{
     bool found = false;
     int i = 0;
     while ((!found) && i < s.length())
     {
           if (c == s[i]) {
               found = true;
               break;
           }   
           i++;
     }
     return found;
}    
                     
char getChoice()
{
     string choices = "1234567";
     char choice;
     do
        choice =  getch();
     while (!find(choices,choice));  
     return choice;
}
char menu(string menu_item[], int menu_size)
{
     char choice = 0;
     cout << "=============================" << endl;
     cout << "  Hello! I'm a List Manager  " << endl;
     cout << "=============================" << endl;
     for (int i = 0; i < menu_size; i++)
        cout << menu_item[i] << endl;
     cout << "=============================" << endl;
     cout << "Enter your choice : ";
     choice = getChoice();
     cout << choice << endl;
     return choice;
}