Transcript Slide 1

1
CS212: DATA STRUCTURES
Computer Science
Department
Lab 5: Stacks
Stacks
2
What is
Stack ?


20-Jul-15
A stack is a linear list in which all additions and
deletions are restricted to one end, called the top.
It is a Last in-First out (LIFO) data structure.
In Stacks we DO NOT
 Search
 Add in arbitrary positions
 Sort
 Access anything beyond the top element.
Computer Science Department
Basic Stack Operation
3


Push
 Place data on
the top of the
stack.
Pop


Remove data
from the top of
the stack.
Stack Top

Returns the top
element of the
stack.
20-Jul-15
Computer Science Department
Stack Linked List Implementation
4
Head
count
top
Data nodes
Top
(a) Conceptual
20-Jul-15
(b) Physical
Computer Science Department
Stack LL Implementation (cont.) C++
5

Node Class – same Node class in the Linked List
class Node{ // Declare Node class
public :
Node *link;
int info;
Node(int d){ // constructor
info=d;
link =NULL;
}
};
20-Jul-15
Computer Science Department
Stack LL Implementation (cont.) C++
6

Stack Class
class Stack{ // Declare Stack class
private :
Node *top; // pointer to the first Node in the list
int count; // count the number of nodes in the list
public :
Stack(){ // constructor
count=0;
top =NULL;
}
int StackCount(){ // return the count of nodes in the list
return count;}
bool emptyStack(){// check is it empty stack or not
return (count==0);}
20-Jul-15
Computer Science Department
Stack LL Implementation (cont.) C++
7

Stack Class – pushStack function
void pushStack( int data){
Node *NewNode = new Node(data);
NewNode->next = top;
top = NewNode;
count ++;
}
Stack LL Implementation (cont.) C++
8

Stack Class – popStack function
bool popStack( int & dataout){
if (!emptyStack()){
dataout = top->Data;
top = top->next;
count --;
return true;
}
else
return false;
}
Stack LL Implementation (cont.) C++
9

Stack Class – StackTop function
bool StackTop( int & dataout){
if (!emptyStack()){
dataout = top->Data;
return true;
}
else
return false;
}
Example 1 (C++)
10
Push some Nodes in the stack,
and then write a function that
Display a stack in reverse.
20-Jul-15
Computer Science Department
Example 1 (C++)
11
void DisplayReverse ()
{
int dataout;
cout << endl << "The reverse Stack is: "<<endl;
while ( ! emptyStack())
{
popStack(dataout);
cout << dataout << " " ;
}}
};\\end of stack class
void Main (){
Stack s ;
s.pushStack(3);
s.pushStack(1);
s.pushStack(10);
s.pushStack(6);
s.pushStack(4);
s.DisplayReverse ();
System(“pause”);}
20-Jul-15
Computer Science Department
Example 2 (C++)
12
Push some Nodes in the stack, and
then write a function that Display
only the even numbers in a stack.
20-Jul-15
Computer Science Department
Example 2 (C++)
13
void DisplayEven()
{
int dataout;
cout << endl << "The Even elements in a Stack are: "<<endl;
while ( ! emptyStack())
{
popStack(dataout);
if((dataout%2)==0){
cout << dataout << " " ;}
}}
};\\end of stack class
void Main (){
Stack s ;
s.pushStack(3);
s.pushStack(1);
s.pushStack(10);
s.pushStack(6);
s.pushStack(4);
s.DisplayEven ();
System(“pause”);}
20-Jul-15
Computer Science Department
Example 3 (C++)
14
Push some Nodes in the stack, and
then write a function that Display the
first element that is entered to the
stack.
20-Jul-15
Computer Science Department
Example 3 (C++)
15
void DisplayFirst()
{
int dataout;
cout << endl << "The First element in a Stack is: "<<endl;
while ( ! emptyStack())
{
popStack(dataout);}
cout << dataout << " " ;}
};\\end of stack class
void Main (){
Stack s ;
s.pushStack(3);
s.pushStack(1);
s.pushStack(10);
s.pushStack(6);
s.pushStack(4);
s.DisplayFirst();
System(“pause”);}
20-Jul-15
Computer Science Department