Tuesday, September 6, 2016

List ADT - Array Implementation of Unsorted List

Filename : List.cs

// List ADT - Array implementation of Unsorted List
// Written by    : Jun Y. Ercia
// Date created : 6 September 2016

using System;

namespace ListADT
{
     class List
     {
          private int size;           // maximum capacity of list
          private string [] list;    // container of list items
          private int n;               // position of the next free space
                                              //    in the container
           
          //create a new list with maximum capacity of "size"
          //default capacity is 10 
          public List(int size = 10)
          {
               this.size = size;
               list = new string[size];
               n = 0;
          }

          //check if the list is empty or not
          public bool isEmpty()
          {
               return n == 0;
          }

          //check if the list is full or not
          public bool isFull()
          {
               return n == size;
          }

          //make the list empty
          public void makenull()
          {
               n = 0;
          }

          //return the number of items of the list
          public int length()
          {
               return n;
          }

          //return the first item of the list
          public string front()
          {
               if (!isEmpty())
                    return list[0];
               else
               {
                    Console.WriteLine("Error! The List is empty.");
                    return "";
               }
          }

          //return the last item of the list
          public string tail()
          {
               if (!isEmpty())
                    return list[n-1];
               else
               {
                    Console.WriteLine("Error! The List is empty.");
                    return "";
               }
          }

          //remove an item at position p of the list
          public void removeAt(int p)
          {
               if (p < n)
               {
                    for (int i = p; i < n - 1; i++)
                         list[i] = list[i + 1];
                    n--;
               }
               else
               {
                    Console.WriteLine("Error! No item at position {0}.", p);
               }
          }

          //remove an item from the list
          public void remove(string item)
          {
               int p;
               if (search(item, out p))
               {
                    for (int i = p; i < n - 1; i++)
                         list[i] = list[i + 1];
                    n--;
               }
               else
               {
                    Console.WriteLine("Error! item not found.");
               }
          }

          public override string ToString()
          {
               string items = "";
               if (!isEmpty())
               {
                    for (int i = 0; i < n; i++)
                    {
                         items += list[i] + (i < n - 1 ? ", " : "");
                    }
               }
               else
               {
                    items = "The List is Empty.";
               }
               return items;
          }

          //print all items
          public void print()
          {
               if (!isEmpty())
               {
                  Console.WriteLine(ToString());
               }
               else
               {
                    Console.WriteLine("Error! The List is empty.");
               }
          }

          //insert an item into the list at position p
          public void insert(int p, string item)
          {
               if (isFull())
               {
                    Console.WriteLine("List Overflow.");
               }
               else
               {
                    for (int i = n; i > p; i--)
                         list[i] = list[i - 1];
                    list[p] = item;
                    n++;

               }
          }

          //insert an item at the front of the list
          public void insertAtFront(string item)
          {
               insert(0, item);
          }

          //insert an item at the end of the list
          public void insertAtEnd(string item)
          {
               insert(n, item);
          }

          //search an item from the list
          //return true and pass back the position of the item
          //   via parameter if found. Otherwise,  return false
          //   and pass back -1.
          public bool search(string item, out int p)
          {
               bool found = false;
               int i = 0;
               p = -1;

               while (!found && i < n)
               {
                    if (item.Equals(list[i++]))
                         found = true;
               }
               p = --i;
               return found;
          }
     }
}

-------------------------------------------------------------------------------------------------------------------------

Driver Program Using List Class

Filename : Program.cs


using System;

namespace ListADT
{
     class Program
     {
          static void Main(string[] args)
          {
               List fruits = new List(5);      //create new list of fruits

               fruits.insert(0, "Banana");    //insert at position 0
               fruits.insert(0, "Apple");      //insert at position 0
               fruits.insert(1, "Grape");      //insert at position 1
               fruits.print();                        //print Apple, Grape, Banana
              
               fruits.insertAtFront("Guava"); //insert at the front of the list
               fruits.insertAtEnd("Orange");  //insert at the end of the list
               fruits.insert(0,"Mango");          //Error! list is full
               fruits.print();                             //print Guava, Apple, Grape, Banana, Orange

               Console.WriteLine(fruits.front());  //print "Guava"
               Console.WriteLine(fruits.tail());    //print "Orange"
              
               fruits.removeAt(0);                        //remove "Guava"
               fruits.print();                                  //print  Apple, Grape, Banana, Orange

               fruits.remove("Grape");            //remove "Grape"
               fruits.print();                                  //print Apple, Banana, Orange

               fruits.makenull();                           //make the list empty
               fruits.print();                                  //print "The List is Empty"
               Console.ReadKey();
          }
     }
}



No comments:

Post a Comment