Tuesday, May 10, 2011

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

Here is my List implementation of stack(Sample Code).
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C#
//StackADTList.cs
using System;
using System.Collections.Generic;

namespace StackUsingLists
{
public class StackADTList
{
int _top = -1;
List<int> listStack = null;

//We can implement automatically growable Stack as well normal size restricted stack also
//Here we will see limited and size restricted stack.
public StackADTList(int nCapacity)
{
listStack = new List<int>(nCapacity);
_top = -1;
}

public int Top
{
get{ return _top;}
}

public int StackSize
{
get{return listStack.Capacity;}
}

public void push(int item)
{
if ((_top + 1) == StackSize)
throw new Exception("Stack Overflow");
_top = _top + 1;
listStack.Add(item);
}

public int pop()
{
if (_top < 0)
throw new Exception("Stack Underflow");
int item = listStack[_top];
listStack.RemoveAt(_top);
_top = _top - 1;
return item;
}

public bool isFull()
{
if ((_top + 1) == StackSize)
return true;
else
return false;
}

public bool isEmpty()
{
if (_top < 0)
return true;
else
return false;
}

public override string ToString()
{
string strItems = "Stack : ";
if (isEmpty())
{
strItems += "empty";
}
else
{
foreach(int item in listStack)
{
strItems += item + " ";
}
}
return strItems;
}
}
}

//Program.cs
using System;
namespace StackUsingLists
{
class Program
{
static void Main(string[] args)
{
try
{
StackADTList objListStack = new StackADTList(10);

objListStack.push(10);
objListStack.push(25);
objListStack.push(50);
Console.WriteLine(" " + objListStack.ToString());
objListStack.pop();
objListStack.pop();
objListStack.pop();
Console.WriteLine(" " + objListStack.ToString());
objListStack.pop();
}
catch (Exception exp)
{
Console.WriteLine("Error : " + exp.Message);
}
Console.Read();
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Java

Note: Sorry for formatting, i will try to update as it is looks like in my IDE

package stacksadtlist;
import java.util.LinkedList;
class StacksADT
{
int _top;
int _size;
LinkedList objList = null;
public StacksADT(int nSize)
{
_size = nSize;
_top = -1;
objList = new LinkedList();
}
public int Size()
{
return objList.size();
}
public int TopIndex()
{
return _top;
}
public void push(int item) throws Exception
{
if((_top + 1) == _size)
{
throw new Exception("Stack Overlow");
}
_top = _top + 1;
objList.add((Integer)item);
}
public int pop() throws Exception
{
if(_top < 0)
{
throw new Exception("Stack Underflow");
}
int tmpItem = objList.remove(_top);
_top = _top - 1;
return tmpItem;
}
public boolean isFull()
{
if((_top + 1) == Size())
return true;
else
return false;
}
public boolean isEmpty()
{
if(_top < 0)
return true;
else
return false;
}

@ Override
public String toString()
{
String strItems = "Stack : ";
if (isEmpty())
{
strItems += "Empty";
} else {
for (int i = 0; i <= _top; i++) {
strItems += objList.get(i) + " ";
}
}
return strItems;
}
}

public class StacksADTList
{
public static void main(String[] args)
{
try
{
StacksADT objStack =new StacksADT(10);
objStack.push(10);
objStack.push(25);
objStack.push(50);
System.out.println(objStack.toString());

objStack.pop();
objStack.pop();
objStack.pop();
System.out.println(objStack.toString());

objStack.pop();

}
catch(Exception exp)
{
System.out.println("" + exp.getMessage());
}
try
{
System.in.read();
}
catch(Exception exp1)
{
//DOn't do anything
}
}
}


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C++

I will update very soon
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *nextNode;
}*head, *current, *tmp;

int top;
int stacksize = 10;
int data;

void push(int);
int pop();
int isFull();
int isEmpty();
void printStack();

int main(int argc, char *argv[])
{
int exitCondition = -1;
int data;
top = -1;
//initializing Head
head = malloc(sizeof(struct node));
if( head == NULL)
{
printf("Memory Error");
return;
}
head->data = -999;
head->nextNode = NULL;
current = head;
//Temporary node for traversing through the list
tmp = malloc(sizeof(struct node));
if( tmp == NULL)
{
printf("Memory Error");
return;
}
tmp = head;

//Stack Menu
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();
if(data == head->data)
{
printf("Stack :Underflow");
}
else
{
printf(" %d reomved from the stack\n", data);
printStack();
}
break;
case 3:
data = isEmpty();
if(data == 1)
{
printf(" Stack : Empty\n", data);
}
break;
case 4:
data = isFull();
if(data == 1)
{
printf(" Stack : Full\n", data);
}
break;
case 5:
printStack();
break;
case 6:
break;
default:
printf("Please enter a valid option. \n");
system("EXIT");
}
}
while(exitCondition != 6);
system("PAUSE");
return 0;
}

void push(int item)
{
//Creating the new node in the list - start
struct node *newNode;
newNode = malloc(sizeof(struct node));
if( newNode == NULL)
{
printf("Memory Error");
return;
}
newNode->data = item;
newNode->nextNode = NULL;
//Creating the new node in the list - end

if((top + 1) == stacksize)
printf("Stack : Overflow \n");
top = top + 1;
if( current != NULL)
{
current->nextNode = newNode;
current = newNode;
}
}

int pop()
{
if(top < 0)
{
data = head->data;
}
else
{
top = top - 1;
data = current->data;
//here we need to do some extra coding for removing the lst node in the list
tmp = head;
while(tmp->nextNode != NULL)
{
if(tmp->nextNode == current)
{
current = tmp;
current->nextNode = NULL;
break;
}
tmp = tmp->nextNode;
}
tmp = head;
}
return data;
}
int isFull()
{
if((top+1) == stacksize)
return 1;
else
return 0;
}
int isEmpty()
{
if(top < 0)
return 1;
else
return 0;
}
void printStack()
{
printf("Stack :");
if(top < 0 )
{
printf(" Empty");
}
else
{
tmp = head;
do
{
tmp = tmp->nextNode;
if(tmp!=NULL)
{
printf ("%d ", tmp->data);
}
}
while(tmp->nextNode != NULL);
}
printf("\n");
}

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


No comments:

Post a Comment