using System;
namespace Queue_Linked_List_Implementation
{
class Queue
{
public class Node
{
public object item;
public Node next;
public Node() {
}
public Node(object item) {
this.item = item;
}
}
private int length;
private Node rear, front;
public Queue()
{
front = rear = null;
length = 0;
}
public int Length
{
get { return length; }
}
public bool isEmpty()
{
return front == null;
}
public void enqueue(Object item)
{
length++;
Node newptr;
newptr = new Node()
{
next = null,
item = item
};
if (isEmpty()) {
front = rear = newptr;
}
else
{
rear.next = newptr;
rear = newptr;
}
}
public Object dequeue()
{
Object item = "";
if (!isEmpty())
{
item = front.item;
front = front.next;
length--;
}
else
{
Console.WriteLine("Error! Queue Underflow");
}
return item;
}
public Object Front()
{
Object item = "";
if (!isEmpty())
{
item = front.item;
}
else
{
Console.WriteLine("Error! Queue Underflow");
}
return item;
}
}
}
------------------------------------------------------------------------------------------------------------------------------
Driver Program Using Queue Class
using System;
namespace Queue_Linked_List_Implementation
{
class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
Object fruit;
queue.enqueue("Apple"); // add apple at the rear of the queue
queue.enqueue("Banana"); // add banana at the rear of the queue
queue.enqueue("Grape"); // add grape at the rear of the queue
Console.WriteLine(queue.Front()); // return the item at the front of the queue
queue.enqueue("Guava"); // add gauva at the rear of the queue
queue.enqueue("Orange"); // add orange at the rear of the queue
fruit = queue.dequeue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
fruit = queue.dequeue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
fruit = queue.dequeue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
Sunday, September 18, 2016
Queue ADT - Array Implementation
using System;
namespace Queue_Array_Implementation
{
class Queue
{
private Object[] queue;
private int rear;
private int front;
private int size;
private int count;
public Queue(int size)
{
this.size = size;
queue = new Object[size];
front = 0;
rear = size - 1;
}
public bool isFull()
{
return count == size;
}
public void enQueue(Object item)
{
if (!isFull())
{
rear = (rear + 1) % (size);
queue[rear] = item;
count++;
}
else
{
Console.WriteLine("Queue is full");
}
}
public bool isEmpty()
{
return (count == 0);
}
public Object Front()
{
Object item = null;
if (!isEmpty())
{
item = queue[front];
}
else
{
Console.WriteLine("Queue is Empty");
}
return item;
}
public Object deQueue()
{
Object item = null;
if (!isEmpty())
{
item = queue[front];
front = (front + 1) % (size);
count--;
}
else
{
Console.WriteLine("Queue is Empty");
}
return item;
}
}
}
---------------------------------------------------------------------------------------------------------------
Driver Program using Queue Class
using System;
namespace Queue_Array_Implementation
{
class Program
{
static void Main(string[] args)
{
Queue queue = new Queue(5);
Object fruit;
queue.enQueue("Apple"); // add apple at the rear of the queue
queue.enQueue("Banana"); // add banana at the rear of the queue
queue.enQueue("Grape"); // add grape at the rear of the queue
Console.WriteLine(queue.Front()); // return the item at the front of the queue
queue.enQueue("Guava"); // add gauva at the rear of the queue
queue.enQueue("Orange"); // add orange at the rear of the queue
queue.enQueue("Melon"); // can't add melon. queue is full.
fruit = queue.deQueue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
fruit = queue.deQueue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
fruit = queue.deQueue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
namespace Queue_Array_Implementation
{
class Queue
{
private Object[] queue;
private int rear;
private int front;
private int size;
private int count;
public Queue(int size)
{
this.size = size;
queue = new Object[size];
front = 0;
rear = size - 1;
}
public bool isFull()
{
return count == size;
}
public void enQueue(Object item)
{
if (!isFull())
{
rear = (rear + 1) % (size);
queue[rear] = item;
count++;
}
else
{
Console.WriteLine("Queue is full");
}
}
public bool isEmpty()
{
return (count == 0);
}
public Object Front()
{
Object item = null;
if (!isEmpty())
{
item = queue[front];
}
else
{
Console.WriteLine("Queue is Empty");
}
return item;
}
public Object deQueue()
{
Object item = null;
if (!isEmpty())
{
item = queue[front];
front = (front + 1) % (size);
count--;
}
else
{
Console.WriteLine("Queue is Empty");
}
return item;
}
}
}
---------------------------------------------------------------------------------------------------------------
Driver Program using Queue Class
using System;
namespace Queue_Array_Implementation
{
class Program
{
static void Main(string[] args)
{
Queue queue = new Queue(5);
Object fruit;
queue.enQueue("Apple"); // add apple at the rear of the queue
queue.enQueue("Banana"); // add banana at the rear of the queue
queue.enQueue("Grape"); // add grape at the rear of the queue
Console.WriteLine(queue.Front()); // return the item at the front of the queue
queue.enQueue("Guava"); // add gauva at the rear of the queue
queue.enQueue("Orange"); // add orange at the rear of the queue
queue.enQueue("Melon"); // can't add melon. queue is full.
fruit = queue.deQueue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
fruit = queue.deQueue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
fruit = queue.deQueue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
Thursday, September 8, 2016
Stack ADT - Using Generic List Collection of C#
using System;
using System.Collections.Generic;
namespace Generic_Stack
{
class Program
{
static void Main(string[] args)
{
Stack<string> stack = new Stack<string>(); ;
stack.Push("Apple");
stack.Push("Banana");
stack.Push("Guava");
Console.WriteLine(string.Join(",",stack.ToArray()));
Console.WriteLine(stack.Pop());
Console.WriteLine(stack.Peek());
stack.Push("Grape");
stack.Push("Orange");
stack.Push("Lemon");
Console.WriteLine(string.Join(",", stack.ToArray()));
Console.WriteLine(stack.Pop());
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
using System.Collections.Generic;
namespace Generic_Stack
{
class Program
{
static void Main(string[] args)
{
Stack<string> stack = new Stack<string>(); ;
stack.Push("Apple");
stack.Push("Banana");
stack.Push("Guava");
Console.WriteLine(string.Join(",",stack.ToArray()));
Console.WriteLine(stack.Pop());
Console.WriteLine(stack.Peek());
stack.Push("Grape");
stack.Push("Orange");
stack.Push("Lemon");
Console.WriteLine(string.Join(",", stack.ToArray()));
Console.WriteLine(stack.Pop());
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
Stack ADT - Linked List Implementation
using System;
namespace Stack_Linked_List_Implementation
{
class Stack
{
public class Node
{
public object item;
public Node next;
public Node() {
}
public Node(object item) {
this.item = item;
}
}
private int length;
private Node top;
public Stack()
{
length = 0;
top = null;
}
public int Length
{
get { return length; }
}
public bool isEmpty()
{
return top == null;
}
public void push(Object item)
{
length++;
top = new Node()
{
next = top,
item = item
};
}
public Object pop()
{
Object item = "";
if (!isEmpty())
{
item = top.item;
top = top.next;
length--;
}
else
{
Console.WriteLine("Error! Stack Underflow");
}
return item;
}
public Object peek()
{
Object item = "";
if (!isEmpty())
{
item = top.item;
}
else
{
Console.WriteLine("Error! Stack Underflow");
}
return item;
}
}
}
-----------------------------------------------------------------------------------------------------------------------
Driver Program Using Stack Class
using System;
namespace Stack_Linked_List_Implementation
{
class Program
{
static void Main(string[] args)
{
Stack stack = new Stack();
stack.push("Apple");
stack.push("Banana");
stack.push("Guava");
Console.WriteLine(stack.pop());
Console.WriteLine(stack.peek());
stack.push("Grape");
stack.push("Orange");
stack.push("Lemon");
Console.WriteLine(stack.pop());
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
namespace Stack_Linked_List_Implementation
{
class Stack
{
public class Node
{
public object item;
public Node next;
public Node() {
}
public Node(object item) {
this.item = item;
}
}
private int length;
private Node top;
public Stack()
{
length = 0;
top = null;
}
public int Length
{
get { return length; }
}
public bool isEmpty()
{
return top == null;
}
public void push(Object item)
{
length++;
top = new Node()
{
next = top,
item = item
};
}
public Object pop()
{
Object item = "";
if (!isEmpty())
{
item = top.item;
top = top.next;
length--;
}
else
{
Console.WriteLine("Error! Stack Underflow");
}
return item;
}
public Object peek()
{
Object item = "";
if (!isEmpty())
{
item = top.item;
}
else
{
Console.WriteLine("Error! Stack Underflow");
}
return item;
}
}
}
-----------------------------------------------------------------------------------------------------------------------
Driver Program Using Stack Class
using System;
namespace Stack_Linked_List_Implementation
{
class Program
{
static void Main(string[] args)
{
Stack stack = new Stack();
stack.push("Apple");
stack.push("Banana");
stack.push("Guava");
Console.WriteLine(stack.pop());
Console.WriteLine(stack.peek());
stack.push("Grape");
stack.push("Orange");
stack.push("Lemon");
Console.WriteLine(stack.pop());
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
Wednesday, September 7, 2016
List ADT - Linked List Implementation
using System;
namespace Linked_List
{
public class List
{
public class Node
{
public object item;
public Node next;
public Node() {
}
public Node(object item) {
this.item = item;
}
}
private int length;
private Node head, lastnode;
public List()
{
length = 0;
head = null;
}
public int Length
{
get { return length; }
}
public bool isEmpty()
{
return length == 0;
}
public Node search(Object item)
{
Node nodeptr = head;
while (nodeptr != null)
{
if (nodeptr.item.ToString().Equals(item.ToString()))
{
break;
}
nodeptr = nodeptr.next;
}
return nodeptr;
}
public void remove(Object item) {
Node nodeptr = head;
Node prevptr = null;
while (nodeptr != null)
{
if (nodeptr.item.ToString().Equals(item.ToString()))
{
break;
}
prevptr = nodeptr;
nodeptr = nodeptr.next;
}
if (nodeptr != null)
{
prevptr.next = nodeptr.next;
length--;
}
else
{
Console.WriteLine("Can't Remove! Item not found.");
}
}
public Object front()
{
if (!isEmpty())
{
return head.item;
}
else
{
Console.WriteLine("Error! The list is empty.");
return "";
}
}
public Object tail()
{
if (!isEmpty())
{
return lastnode.item;
}
else
{
Console.WriteLine("Error! The list is empty.");
return "";
}
}
public void insertAtEnd(object item)
{
length++;
var node = new Node(item);
if (head == null)
{
head = node;
}
else
{
lastnode.next = node;
}
// Makes newly added node the current node
lastnode = node;
}
public void insertAtFront(object item)
{
length++;
head = new Node()
{
next = head,
item = item
};
if (length == 1)
lastnode = head;
}
public override string ToString()
{
Node nodeptr = head;
string nodes = "";
while (nodeptr != null)
{
nodes += nodeptr.item + " ";
nodeptr = nodeptr.next;
}
return nodes;
}
public void print()
{
Console.WriteLine(ToString());
}
}
}
-----------------------------------------------------------------------------------------------------------------
Driver Program Using List Class
using System;
namespace Linked_List
{
class Program
{
static void Main(string[] args)
{
List list = new List();
list.insertAtFront("Banana");
list.insertAtFront("Apple");
list.insertAtEnd("Guava");
list.insertAtEnd("Orange");
list.print();
Console.WriteLine(list.front());
Console.WriteLine(list.tail());
list.remove("Banana");
list.print();
Console.WriteLine("\nPress any key to continue..");
Console.ReadKey();
}
}
}
namespace Linked_List
{
public class List
{
public class Node
{
public object item;
public Node next;
public Node() {
}
public Node(object item) {
this.item = item;
}
}
private int length;
private Node head, lastnode;
public List()
{
length = 0;
head = null;
}
public int Length
{
get { return length; }
}
public bool isEmpty()
{
return length == 0;
}
public Node search(Object item)
{
Node nodeptr = head;
while (nodeptr != null)
{
if (nodeptr.item.ToString().Equals(item.ToString()))
{
break;
}
nodeptr = nodeptr.next;
}
return nodeptr;
}
public void remove(Object item) {
Node nodeptr = head;
Node prevptr = null;
while (nodeptr != null)
{
if (nodeptr.item.ToString().Equals(item.ToString()))
{
break;
}
prevptr = nodeptr;
nodeptr = nodeptr.next;
}
if (nodeptr != null)
{
prevptr.next = nodeptr.next;
length--;
}
else
{
Console.WriteLine("Can't Remove! Item not found.");
}
}
public Object front()
{
if (!isEmpty())
{
return head.item;
}
else
{
Console.WriteLine("Error! The list is empty.");
return "";
}
}
public Object tail()
{
if (!isEmpty())
{
return lastnode.item;
}
else
{
Console.WriteLine("Error! The list is empty.");
return "";
}
}
public void insertAtEnd(object item)
{
length++;
var node = new Node(item);
if (head == null)
{
head = node;
}
else
{
lastnode.next = node;
}
// Makes newly added node the current node
lastnode = node;
}
public void insertAtFront(object item)
{
length++;
head = new Node()
{
next = head,
item = item
};
if (length == 1)
lastnode = head;
}
public override string ToString()
{
Node nodeptr = head;
string nodes = "";
while (nodeptr != null)
{
nodes += nodeptr.item + " ";
nodeptr = nodeptr.next;
}
return nodes;
}
public void print()
{
Console.WriteLine(ToString());
}
}
}
-----------------------------------------------------------------------------------------------------------------
Driver Program Using List Class
using System;
namespace Linked_List
{
class Program
{
static void Main(string[] args)
{
List list = new List();
list.insertAtFront("Banana");
list.insertAtFront("Apple");
list.insertAtEnd("Guava");
list.insertAtEnd("Orange");
list.print();
Console.WriteLine(list.front());
Console.WriteLine(list.tail());
list.remove("Banana");
list.print();
Console.WriteLine("\nPress any key to continue..");
Console.ReadKey();
}
}
}
Stack ADT - Array Implementation
// List ADT - Array implementation of Stack ADT
// Written by : Jun Y. Ercia
// Date created : 7 September 2016
using System;
namespace Stack_Array_Implementation
{
class Stack
{
private Object[] stack;
int top;
int size;
public Stack(int size)
{
this.size = size;
stack = new Object[size];
top = 0;
}
public int Count
{
get { return top; }
}
public bool isEmpty()
{
return top == 0;
}
public bool isFull()
{
return top == size;
}
public void push(Object item)
{
if (!isFull())
{
stack[top++] = item;
}
else
{
Console.Write("Error! Stack Overflow.");
}
}
public Object pop()
{
if (!isEmpty())
{
return stack[--top];
}
else
{
Console.Write("Error! Stack Underflow.");
return "";
}
}
public Object peek()
{
if (!isEmpty())
{
return stack[top - 1];
}
else
{
Console.Write("Error! Stack Underflow.");
return "";
}
}
}
}
// Written by : Jun Y. Ercia
// Date created : 7 September 2016
using System;
namespace Stack_Array_Implementation
{
class Stack
{
private Object[] stack;
int top;
int size;
public Stack(int size)
{
this.size = size;
stack = new Object[size];
top = 0;
}
public int Count
{
get { return top; }
}
public bool isEmpty()
{
return top == 0;
}
public bool isFull()
{
return top == size;
}
public void push(Object item)
{
if (!isFull())
{
stack[top++] = item;
}
else
{
Console.Write("Error! Stack Overflow.");
}
}
public Object pop()
{
if (!isEmpty())
{
return stack[--top];
}
else
{
Console.Write("Error! Stack Underflow.");
return "";
}
}
public Object peek()
{
if (!isEmpty())
{
return stack[top - 1];
}
else
{
Console.Write("Error! Stack Underflow.");
return "";
}
}
}
}
-------------------------------------------------------------------------------------------------------------
Driver Program Using Stack Class
using System;
namespace Sorted_List
{
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.removeAt("Grape"); //remove "Grape"
fruits.print(); //print Apple, Banana, Orange
fruits.makenull(); //make the list empty
fruits.print(); //print "The List is Empty"
Console.ReadKey();
}
}
}
namespace Sorted_List
{
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.removeAt("Grape"); //remove "Grape"
fruits.print(); //print Apple, Banana, Orange
fruits.makenull(); //make the list empty
fruits.print(); //print "The List is Empty"
Console.ReadKey();
}
}
}
Tuesday, September 6, 2016
List ADT - Using Generic List Collection of C#
// List ADT - Using Generic List Collection
// Written by : Jun Y. Ercia
// Date created : 6 September 2016
using System;
using System.Collections.Generic;
namespace Generic_List
{
class Program
{
static void Main(string[] args)
{
List<string> fruits = new List <string> (5);
fruits.Add("Banana"); //add an item at the end
fruits.Add("Apple"); //add an item at the end
fruits.Insert(1, "Grape"); //insert at position 1
foreach (string fruit in fruits) //print Apple, Grape, Banana
Console.Write("{0} ", fruit);
fruits.AddRange(new string [] {"Guava", "Orange"}); //add two items at the end
Console.WriteLine();
Console.WriteLine(string.Join(", ", fruits)); //print Banana, Grape, Apple, Guava, Orange
Console.WriteLine(fruits[0]); //print "Banana"
Console.WriteLine(fruits[fruits.Count-1]); //print "Orange"
fruits.RemoveAt(0); //remove "Banana"
Console.WriteLine(string.Join(", ", fruits)); //print Grape, Apple, Guava, Orange
fruits.Remove("Grape"); //remove "Grape"
Console.WriteLine(string.Join(", ", fruits)); //print Apple, Guava, Orange
fruits.Reverse(); //reverse the items
Console.WriteLine(string.Join(", ", fruits)); //print Orange, Guava, Apple
fruits.Sort(); //sort the items
Console.WriteLine(string.Join(", ", fruits)); //print Apple, Guava, Orange
fruits.Clear(); //make the list empty
Console.WriteLine(string.Join(", ", fruits)); //print nothing
Console.WriteLine("\nPress any key to continue..");
Console.ReadKey();
}
}
}
// Written by : Jun Y. Ercia
// Date created : 6 September 2016
using System;
using System.Collections.Generic;
namespace Generic_List
{
class Program
{
static void Main(string[] args)
{
List<string> fruits = new List <string> (5);
fruits.Add("Banana"); //add an item at the end
fruits.Add("Apple"); //add an item at the end
fruits.Insert(1, "Grape"); //insert at position 1
foreach (string fruit in fruits) //print Apple, Grape, Banana
Console.Write("{0} ", fruit);
fruits.AddRange(new string [] {"Guava", "Orange"}); //add two items at the end
Console.WriteLine();
Console.WriteLine(string.Join(", ", fruits)); //print Banana, Grape, Apple, Guava, Orange
Console.WriteLine(fruits[0]); //print "Banana"
Console.WriteLine(fruits[fruits.Count-1]); //print "Orange"
fruits.RemoveAt(0); //remove "Banana"
Console.WriteLine(string.Join(", ", fruits)); //print Grape, Apple, Guava, Orange
fruits.Remove("Grape"); //remove "Grape"
Console.WriteLine(string.Join(", ", fruits)); //print Apple, Guava, Orange
fruits.Reverse(); //reverse the items
Console.WriteLine(string.Join(", ", fruits)); //print Orange, Guava, Apple
fruits.Sort(); //sort the items
Console.WriteLine(string.Join(", ", fruits)); //print Apple, Guava, Orange
fruits.Clear(); //make the list empty
Console.WriteLine(string.Join(", ", fruits)); //print nothing
Console.WriteLine("\nPress any key to continue..");
Console.ReadKey();
}
}
}
List ADT - Array Implementation of Sorted List
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();
}
}
}
// 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();
}
}
}
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();
}
}
}
// 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();
}
}
}
Subscribe to:
Posts (Atom)