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;
}

No comments:

Post a Comment