Archive

Archive for the ‘Data Structures’ Category

Implementation of Stack in Python and C++

Stack is a powerful linear data structure. It stores data on the basis of LIFO (Last in First Out) type. That is, insertion and deletion of elements takes place at the top of the stack. Stack doesn’t allows insertion or deletion of elements to an indexed input. It performs two fundamental operation’s, Push and Pop. Push, appends the elements to the top of the stack and Pop, removes the element from top of the stack. 2 initial conditions must be checked before doing the Push and Pop operations. Stack Over Flow is checked while pushing the elements into the stack¬† and Stack Under Flow is checked while popping the elements from the stack. Usually Stacks are created using Arrays in C,C++,Java¬† and using List’s in Python. Here is a very simple Python snippet, which implements the Stack and it’s operations!


def push(contents):
    if len(stack) >= limit:
        print 'Stack Overflow!'
    else:
        stack.append(contents)
    print 'Stack after Push',stack
def pop():
    if len(stack) <= 0:
        print 'Stack Underflow!'
        return 0
    else:
        return stack.pop()
stack = []
top,traversal,contents = -1,0,0
limit = input('Enter the no of elements to be stored in stack:')
for traversal in range(limit):
contents = input('Enter element '+str(traversal)+':')
push(contents)
traversal = 0
for traversal in range(limit):
print 'Popping '+str(limit-traversal)+'th element:',pop()
print 'Stack after Popping!',stack

In C++:

#include<iostream>
using namespace std;
class stack
{
    int s[100];
    int capacity,top;
public:
void init(int t,int c)
{
    capacity = c;
    top = t;
}
int isFull()
{
    if (top == capacity-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int isEmpty()
{
    if (top == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
void push(int data)
{
    if (isFull() == 1)
    {
         cout<<"\nStack Full!";
    }
    else
    {
        s[++top] = data;
    }
}
int pop()
{
    int temp=0;
    if (isEmpty() == 0)
    {
        temp = s[top];
        top--;
        return temp;
    }
    else
    {
        cout<<"\nStack is Empty";
        return 0;
    }
}
};
int main()
{
stack obj;
int data,limit;
cout<<"\nEnter the no of elements to be pushed:";
cin>>limit;
obj.init(-1,limit);
obj.isFull();
cout<<"\nEnter the Elements to be pushed onto stack:";
for(int i=0;i<limit;i++)
{
    cin>>data;
    obj.push(data);
}
obj.isEmpty();
cout<<"\nPOP!:";
for(int i=0;i<limit;i++)
{
    cout<<obj.pop()<<"\n";
}
return 0;
}

Stacks are used in various applications like Tower of Hanoi, Compiler Designs, Syntax Parsing, Push Down Automaton etc. Please visit the link Stack, for more information!

Advertisements
This Week In 4n6

Your weekly roundup of Digital Forensics and Incident Response news

RAM Slack - Random Thoughts from a Computer Forensic Examiner

Random Thoughts from a Computer Forensic Examiner

InCTF

behind the scenes

DFIR Journal

Trials and Tribulations of a DFIR life

I dont know zilch !

For the noobs out there like me

X-Ways Forensics Practitioner's Guide

The Guide to X-Ways Forensics!

Forensic Focus - Articles

Digital forensics articles and research papers

my abbreviations......

gain,learn and share knowledge!!!!

digirati82

Windows Logging Service (WLS), DFIR, etc.

Belkasoft Forensic: The Digital Evidence Blog

Searching for, analyzing and recovering digital evidence

Gail Tredwell. Amma. Truth. Lies. Scandals. Fraud. And. Reality

Three things cannot be long hidden: the sun, the moon, and the truth.

Integriography: A Journal of Broken Locks, Ethics, and Computer Forensics

Musings about UAVs, search & rescue, computer forensics, cyber security, and the state of play in all .....

Life is beautiful

when the mind is full with love, you see beauty in every thing

Techno Krat

.... Dare To Try .......