// 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();
}
}
}
No comments:
Post a Comment