Monday, May 9, 2011

Array Implementation of Stacks(C#, Java, C++, C)

Here is my Array Implementation of stack(Sample Code)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C#
//StackADT.cs
using System;
namespace Stacks
{
public class StackADT
{
//Array for storing stack elements
private int[] items = null;
//Stack size
private int _size = 0;
//Top index
private int _top = -1;
//Read only property, because to stop modifying the size of the stack during the operation
public int Size
{
get {return _size;}
}

//Standard Stack Operations - start
//Constructor - for creating and initializing the stack
public StackADT(int nSize)
{
this._size = nSize;
items =
new int[_size];
}
//Getting the index of top element
public int Top
{
get {return this._top;}
}
//For inserting elements in to Stack
public void push(int item)
{
if (_top == (_size - 1))
throw new Exception("Stack Overflow");
items[++_top] = item;
}
//For deleting elements in to Stack
public int pop()
{
if (_top < 0)
throw new Exception("Stack Underflow");
return items[_top--];
}
public bool isFull()
{
if (_top == (_size - 1))
return true;
return false;
}
public bool isEmpty()
{
if (_top < 0)
return true;
return false;
}
//Stack Operations - end

//Just for printing the Stack
public override string ToString()
{
String strItems = "";
if (isEmpty())
{
strItems =
"Stack Empty";
}
else
{
for (int i = 0; i <= _top; i++)
{
strItems += items[i] +
" ";
}
}
Console.WriteLine("" + strItems);
return strItems;
}
}
}
//Program.cs
using System;
namespace Stacks
{
class Program
{
static void Main(string[] args)
{
StackADT objStack = new StackADT(10);
try
{
objStack.push(10);
objStack.push(25);
objStack.push(50);
objStack.ToString();
objStack.pop();
objStack.pop();
objStack.ToString();
objStack.pop();
objStack.ToString();
objStack.pop();
objStack.Restring();
}
catch (Exception exp)
{
Console.WriteLine("" + exp.Message);
}
Console.ReadLine();
}
}
}


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

C++
//StackADT.h
class StackADT
{
int _top;
int _size;
int* Items;
public:
StackADT(
int);
~StackADT(
void);
void push(int);
int pop();
int getTop();
bool isFull();
bool isEmpty();
void DisplayStack();
};
//StackADT.cpp
#include "StackADT.h"
#include <iostream>
#include <ios>
using namespace std;
StackADT::~StackADT(
void)
{
}
StackADT::StackADT(
int nSize)
{
Items =
new int[nSize];
_top = -1;
_size = nSize;
}
void StackADT::push(int item)
{
if((_top + 1) == _size)
{
cout <<
"Stack Overflow" << endl;
return;
}
_top = _top + 1;
Items[_top] = item;
}
int StackADT::pop()
{
if (_top < 0)
{
cout <<
"Stack Underflow" << endl;
return -1;
}
int item = Items[_top];
_top = _top - 1;
return item;
}
int StackADT::getTop()
{
return _top;
}
bool StackADT::isEmpty()
{
if (_top < 0)
return true;
return false;
}
bool StackADT::isFull()
{
if (_top == _size)
return true;
return false;
}
void StackADT::DisplayStack()
{
if(isEmpty())
{
cout <<
"Stack Empty" << endl;
}
else
{
for(int i=0;i<=_top;i++)
{
cout << Items[i] <<
" ";
}
}
cout << endl;
}
//Stacks.cpp
#include <iostream>
#include "StackADT.h"
using namespace std;

int main()
{
StackADT objStack(10);
objStack.push(10);
objStack.push(25);
objStack.push(50);
objStack.DisplayStack();
objStack.pop();
objStack.pop();
objStack.DisplayStack();
objStack.pop();
objStack.DisplayStack();
objStack.pop();
objStack.DisplayStack();
return 0;
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Java

//StackADT.java
package stacks;
public class StackADT
{
//Array for storing stack elements
private int[] items = null;
//Stack size
private int _size = 0;
//Top index
private int _top = -1;

//Read only property, because to stop modifying the size of the stack during the
//operation
public int getSize() {
return _size;
}
//Standard Stack Operations - start

//Constructor - for creating and initializing the stack
public StackADT(int nSize) {
this._size = nSize;
items =
new int[_size];
}

//Getting the index of top element
public int getTop() {
return this._top;
}

//For inserting elements in to Stack
public void push(int item) throws Exception {
if (_top == (_size - 1)) {
//System.out.println("Stack Overflow");
throw new Exception("Stack Overflow");
}
items[++_top] = item;
}

//For deleting elements in to Stack
public int pop() throws Exception {
if (_top < 0) {
//System.out.println("Stack Underflow");
throw new Exception("Stack Underflow");
}
return items[_top--];
}

public boolean isFull() {
if (_top == _size) {
return true;
}
return false;
}

public boolean isEmpty() {
if (_top < 0) {
return true;
}
return false;
}
//Stack Operations - end

//Just for printing the Stack
@ Override
public String toString() {
String strItems =
"";

if (isEmpty()) {
strItems =
"Stack Empty";
}
else {
for (int i = 0; i <= _top; i++) {
strItems += items[i] +
" ";
}
}
System.out.println(
"" + strItems);
return strItems;
}
}

//Stacks.java
package stacks;
public class Stacks
{
public static void main(String[] args)
{
StackADT objStack =
new StackADT(10);
try
{
objStack.push(10);
objStack.push(25);
objStack.push(50);
objStack.toString();

//50 will be removed
objStack.pop();
objStack.pop();
objStack.toString();
objStack.pop();
objStack.toString();
objStack.pop();
objStack.toString();
System.in.read();
}
catch(Exception exp)
{
System.out.println(
"" + exp.getMessage());
}

}
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

C
I will post the code in C very soon. I am getting some issues in installing C in my laptop.

#include <stdio.h>
#include <stdlib.h>
int top = -1;
int stacksize = 10;
int stack[10];
void push(int);
int pop();
void isFull();
void isEmpty();
void printStack();
int main(int argc, char *argv[])
{
int exitCondition = -1;
int data;
do
{
printf("\nSelect an Operation \n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Is Stack Empty\n");
printf("4. is Stack Full\n");
printf("5. Print Stack\n");
printf("6. Exit\n");
scanf("%d",&exitCondition);

switch(exitCondition)
{
case 1:
printf("Enter a number to push into the stack \n");
scanf("%d", &data);
push(data);
printStack();
break;
case 2:
data = pop();
printStack();
break;
case 3:
isEmpty();
break;
case 4:
isFull();
break;
case 5:
printStack();
break;
case 6:
return 0;
default:
printf("Please enter a valid option. \n");
}
}
while(exitCondition != 6);

system("PAUSE");
return 0;
}
void push(int item)
{
if(top == stacksize)
printf("Stack Overflow");
top = top + 1;
stack[top] = item;
}
int pop()
{
int item;
if(top < 0 )
printf("Stack Underflow");
item = stack[top];
top = top - 1 ;
return item;
}
void isFull()
{
if(top == stacksize)
printf("Stack : Full");
}
void isEmpty()
{
if(top < 0)
printf("Stack : Empty");
}
void printStack()
{
int i=0;
printf(" Stack : ");
if(top < 0)
{
printf(" Empty ");
}
else
{
for( i= 0;i<=top;i++)
{
printf(" %d ",stack[i]);
}
}
printf("\n");
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

No comments:

Post a Comment