Filename : SortedList.cs
// List ADT - Array implementation of Sorted List
// Written by : Jun Y. Ercia
// Date created : 6 September 2016
using System;
namespace ListADT
{
class SortedList
{
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 SortedList(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 sorted list
public void insert(string item)
{
int p = 0;
if (isFull())
{
Console.WriteLine("Error! The List if full.");
}
else
{ while (item.CompareTo(list[p]) > 0 && p < n)
{
p++;
}
for (int i = n; i > p; i--)
list[i] = list[i - 1];
list[p] = item;
n++;
}
}
//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 SortedList Class
using System;
namespace ListADT
{
class Program
{
static void Main(string[] args)
{
SortedList fruits = new SortedList(5); //create new list of fruits
fruits.insert("Banana"); //insert Banana
fruits.insert("Apple"); //insert Apple
fruits.insert("Grape"); //insert Grape
fruits.print(); //print Apple, Banana, Grape
fruits.insert("Guava"); //insert Guava
fruits.insert("Orange"); //insert Orange
fruits.insert("Mango"); //Error! list is full
fruits.print(); //print Apple, Banana, Grape, Guava, Orange
Console.WriteLine(fruits.front()); //print "Apple"
Console.WriteLine(fruits.tail()); //print "Orange"
fruits.removeAt(0); //remove "Apple"
fruits.print(); //print Banana, Grape, Guava, Orange
fruits.remove("Grape"); //remove "Grape"
fruits.print(); //print Banana, Guava, Orange
fruits.makenull(); //make the list empty
fruits.print(); //print "The List is Empty"
Console.ReadKey();
}
}
}
No comments:
Post a Comment