0% found this document useful (0 votes)
75 views11 pages

Stacks: Epublicist - Ca © 2007, 2008, 2009

The document discusses stacks and how they work as a data structure. Stacks follow the LIFO principle where the last item added is the first item removed. The key operations are push to add an item and pop to remove the top item. Code examples are provided to demonstrate initializing a stack and using the push and pop functions.

Uploaded by

Yoel Ben-Avraham
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views11 pages

Stacks: Epublicist - Ca © 2007, 2008, 2009

The document discusses stacks and how they work as a data structure. Stacks follow the LIFO principle where the last item added is the first item removed. The key operations are push to add an item and pop to remove the top item. Code examples are provided to demonstrate initializing a stack and using the push and pop functions.

Uploaded by

Yoel Ben-Avraham
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Stacks

ePublicist.ca © 2007, 2008, 2009


Introduction

A 'Stack' was first proposed in 1955, and then


patented in 1957, by the German Friedrich L.
Bauer. The same concept was developed
independently, at around the same time, by the
Australian Charles Leonard Hamblin.
In short, a 'stack' is a way of manipulating data
items in such a way that the last item added is
the first itel removed. (LIFO – Last In First Out)

ePublicist.ca © 2007, 2008, 2009


How Does a Stack Work?

There are two basic operations to any stack:


 Push() – which 'pushes' a data item onto the stack
 Pop() – which removed the 'top' item from the stack

In addition there can be several 'helper' functions, for


instance:
 IsEmpty() - tests if stack is empty
 IsFull() - tests if stack is full

ePublicist.ca © 2007, 2008, 2009


Pushing a Value

Code Example:

myStack s;
push( s, 10 );

ePublicist.ca © 2007, 2008, 2009


Initializing the Stack
Code Example:

#define STACKSIZE 10
#define false 0
#define true 1

// Global Variables
static int stackTop = 0;
static int stack[STACKSIZE];

ePublicist.ca © 2007, 2008, 2009


Pushing & Popping

void main(){
push( 10 );
push( 7 );
push( 3 );

printf("\nPop(%d) :", pop());


printf("\nPop(%d) :", pop());
printf("\nPop(%d) :", pop());
printf("\nPop(%d) :", pop());
}
ePublicist.ca © 2007, 2008, 2009
Pushing a Value

void push(int x)
{
if( isFull() != true )
stack[stackTop++] = x;
else return;
}

ePublicist.ca © 2007, 2008, 2009


Popping a Value

int pop( )
{
if( isEmpty() != true )
return stack[--stackTop];
else return 0;
}

ePublicist.ca © 2007, 2008, 2009


Helper Functions

int isEmpty(){
if( stackTop == 0 )
return true;
else return false;
}

int isFull(){
if( stackTop == STACKSIZE )
return true;
else return false;
}
ePublicist.ca © 2007, 2008, 2009
Anatomy of a Data Structure
Three components:
• Internal variables
• Necessary (public) functions
• Ancillary (private) helper functions
In C/C++ these are found:
• Internal variables (and prototypes) are defined in the
header files .h
• The definition of what the functions do is defined in
the .c or .cpp
• The actual use of the structure and functions is in
main()

ePublicist.ca © 2007, 2008, 2009


Additional Resources

Videos
• http://www.youtube.com/watch?v=gYxmm79zxTQ

ePublicist.ca © 2007, 2008, 2009

You might also like