Download Stack - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Transcript
AL-HUSEEN BIN TALAL UNIVERSITY
College of Engineering
Department of Computer Engineering
Algorithms and Data Structures
Stack
Course No.: 0511363
Fall 2014
Stack
•
Stack is a linear data structure which can be accessed
only at one of its ends for storing and retrieving data.
• A stack is a container that implements the last-in-firstout (LIFO) protocol. This means that the only
accessible object in the container is the last one among
them that was inserted.
• There are two main operations applicable to a stack :
1. Push for insert.
2. Pop for deletion.
• Both operations are done using the pointer (top) and the
maximum item used defined by (size ).
Stack Operations
Push: an item is put on top of the stack, increasing
the stack size by one. As stack size is usually
limited, this may provoke a stack overflow if the
maximum size is exceeded
Pop : the top item is taken from the stack,
decreasing stack size by one. In the case where there
was no top item (i.e. the stack was empty), a stack
underflow occurs
Representation of Stack
• Two ways to representation of stacks in a memory
1- One dimensional array
2- Single Linked List
• We will implement these operations on a stack:






InitializeStack() :Initializes the stack to an empty state.
IsEmptyStack() : Determines whether the stack is empty. If the
stack is empty, it returns the value true; otherwise, it returns the
value false.
IsFullStack() : Determines whether the stack is full. If the stack is
full, it returns the value true; otherwise, it returns the value false.
Push() :Adds a new element to the top of the stack. The input to
this operation consists of the stack and the new element. Prior to
this operation, the stack must exist and must not be full.
Top() : Returns the top element of the stack. Prior to this
operation, the stack must exist and must not be empty.
Pop() : Removes the top element of the stack. Prior to this
operation, the
stack must exist and must not be empty.
Stack in one dimensional array
# include<iostream.h>
class stack {
int top;
unsigned size;
char *stk;
public:
stack(unsigned s) { size = s ; stk = new char[size] ;}
~ stack( ) { delete [ ] stk ; }
void init( ) { top = 0 ; }
void destroy( ) { top = 0 ; }
void push(char ch){
if( isFull( )) {
cerr<<" The stack is Full ! " ;
return ;}
stk [top++] = ch ;
}
char pop( ){
if ( isEmpty ( ) ) {
cerr<<" \n The stack is empty ! ";
return -1;}
return stk[ --top ];
}
int isFull( ) {return top == size ; }
int isEmpty( ) {return top == 0 ; }
};
Cont.
int main ( ) {
Stack s;
s.init ( ) ;
s.push(‘A') ;
s.push(‘B') ;
s.push(‘C') ;
s.push(‘D') ; // last in
cout<<s.pop( )<<" "; // first out
cout<<s.pop( )<<" ";
cout<<s.pop( )<<" ";
cout<<s.pop( )<<" ";
s.pop( ); // Try to popping from an Empty stack
return 0;}
The result:
D C B A
The stack is empty!
Linked Implementation of Stacks
• The operation of adding an item to the head of
a single linked list is quite similar to that of
pushing an item on to a stack; similarly, the
operation of deleting the first node from a
single linked list is analogous to popping a
stack
• A stack may be represented by a single (linear)
linked list. The first node (head) of the list is
the top of the stack
Linked Implementation of Stacks
# include<iostream.h>
struct node {
char data;
node *link;
};
class stack{
node *s;
public:
void init(){ s = NULL ; }
void push( char x){
node *p = new node;
p -> data = x ;
p ->link = s ;
s = p ;}
char pop(){
char x ;
node * p1 ;
if ( !s ) { cout<<" The stack is Empty ! ";}
p1 = s ;
s=s -> link ;
x = p1 -> data ;
delete p1 ;
return x ;}
};
Main function
main ( )
{
stack s ;
char item, size =5;
s.init();
for(int i = 0;i<size;i++)
{
cin>>item;
s.push(item);
}
for(int j=0;j<size;j++)
{
cout<<s.pop()<<" ";
}
}
Application of Stacks
• A classical application deals with evaluation of
arithmetic expression; the compiler uses a
stack to translate input arithmetic expression
into their corresponding object code.
• Stack as return Address
Evaluation of Arithmetic Expressions
• An arithmetic expression consists of operands
and operators. Operands are variables or
constants and operators are of various types
like arithmetic unary and binary operators, as
in the next table.
Evaluation of Arithmetic Expressions..
• A simple arithmetic expression is cited below:
A + B * C / D – E ^ F * G The problem is to evaluate this
expression, one of the most popular way is parenthesize the
expression fully, therefore the expression will be as follows:
((A + B) * ((C / D) – (E ^ (F * G))))
• With these parenthesizing, the innermost parenthesis part
(called subexpression) will be evaluated first, and then the next
innermost and so on; such a sequence is shown as follows:
Notation for arithmetic expressions
• Infix notation: the operators come in between the operands
<operand> <operator> <operand> . Following are simple
expressions in infix notation:
A+B, C-D
• Prefix notation, uses the convention:
<operator> <operand> <operand>
Here, the operators come before the operands. Following are simple
expressions in prefix notation:
+ A B,
- C D, * E F,
/GH
• Suffix (or postfix) notation: where the operator is suffixed by
operands:
<operand> <operand> <operator>
Following expressions are in postfix notation:
A B +,
C D -, E F *,
GH/
Convert expressions
• An expression given in infix notation can be easily
converted into its equivalent prefix or postfix
notation. Following rule is applied to convert an infix
expression into a postfix form:
 Consider the parentheses version for the infix expression.
 Move all operators so that they replace their corresponding
right part of parentheses.
 Remove all parentheses
 Ex .
Convert expressions…
• Similar technique can be applied to obtain
prefix notation for a given infix notation by
moving operators correspond to left
parentheses.
• Three notations for the given arithmetic
expression are listed below:
• Infix: ( ( A + ( ( B ^ C ) – D ) ) * ( E – ( A / C ) ) )
• Prefix: * + A - ^ B C D – E / A C
• Postfix: A B C ^ D - + E A C / - *
Convert expressions…
• Out of these three notations, postfix notation has
certain advantages over other notations.
• The main advantage is its evaluation. During the
evaluation of an expression in postfix notation it
only requires to scan the expression from left to
right several times, but exactly once. This can be
done by using stack. Therefore the evaluation of
an expression is a two step process:
• convert the expression into postfix notation
• evaluate the converted expression
• both steps need the stack data structure.
Conversion of an infix to postfix
• We have two priority values:
• a symbol will be pushed onto the stack if its incoming priority
value is greater than the in-stack priority value of the top-most
element
• a symbol will be popped from the stack if its in-stack priority
value is greater than or equal to the incoming priority value of
the incoming element
• Here’s an example,Given the infix expression, with its
corresponding numbers:
Read Symbol
Stack
1
(
2
((
3
(((
4
(((
A
5
(((+
A
6
(((+
AB
7
((
AB+
8
((^
AB+
9
((^
AB+C
10
(
AB+C^
11
(-
AB+C^
12
(-(
AB+C^
13
(-((
AB+C^
14
(-((
AB+C^
15
(-((*
AB+C^D
16
(-((*
AB+C^DE
17
(-(
AB+C^DE*
18
(-(/
AB+C^DE*
19
(-(/
AB+C^DE*F
20
(-
AB+C^DE*F/
21
Output
AB+C^DE*F/-
Evaluation of postfix expression
• To evaluate a postfix expression using a stack,
we can study the following postfix expression:
ABC^D-+EAC/-*
• using the following information:
• A = 4, B = 3, C = 2, D = 5, E = 3.
Symbol
Opernd1
Opernd2
Value Operand
Stack
4
4
3
43
2
432
^
3
2
9
5
49
495
-
9
5
4
44
+
4
4
8
8
3
83
4
834
2
8342
/
832
-
81
*
8
Stack as return Address
• The stack is using as a place to store the return
address of the a program that call a
subprograms.
Stack as return Address…