3. Characteristics of Data Structures
Data Structure Advantages Disadvantages
Quick insertion, very Slow search,
Array search, slow deletion, fixed size.
Fast access if index
known.
Quicker search than Slow insertion and
Ordered array unsorted array. deletion, fixed size.
Provides last-in, first- Slow access to other
Stack out access. items.
Provides first-in, first- Slow access to other
Queue out access. items.
Linked list Quick insertion ,quick Slow search.
deletion.
4. Characteristics of Data Structures (cont’d)
Data Structure Advantages Disadvantages
Quick search, insertion, Deletion algorithm is
Binary tree deletion (if tree is complex.
complex.
remains balanced).
Quick search, insertion, Complex.
Red-black tree deletion. Tree always
balanced.
Quick search, insertion, Complex.
2-3-4 tree deletion. Tree always
balanced. Similar trees
good for disk storage.
Quick insertion ,quick Slow deletion,
Hash table deletion. nefficient
memory usage.
5. Characteristics of Data Structures (cont’d)
Data Structure Advantages Disadvantages
Fast insertion, deletion, Slow access to other
Heap Slow access to largest items.
item.
Models real-world Some algorithms are
Graph situations. slow and complex.
The data structures shown in Table , except the arrays,
can be thought of asAbstract Data Types, or ADTs.
6. Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.
Basic operations are push and pop.
Often top and isEmpty are available, too.
Also known as "last-in, first-out" or LIFO
Top
7. Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.
Basic operations are push and pop.
Often top and isEmpty are available, too.
Top B
Also known as "last-in, first-out" or LIFO
8. Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.
Basic operations are push and pop.
Top C
Often top and isEmpty are available, too.
B
Also known as "last-in, first-out" or LIFO
9. Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.
Basic operations are push and pop.Top D
Often top and isEmpty are available, too. C
B
Also known as "last-in, first-out" or LIFO
10. Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.
Top E
Basic operations are push and pop. D
Often top and isEmpty are available, too. C
B
Also known as "last-in, first-out" or LIFO
11. Stack Applications
Real life
Pile of books
Plate trays
More applications related to computer
science
Program execution stack
Evaluating expressions
12. Stack Operations
• Push(x)
insert the element x to the top of the stack
• Pop()
remove element from the top
• Top()
check the next element to be removed (do
not remove)
• IsEmpty()
check whether the stack is empty
13. Push Function :
Top
Top Top
Algorithm push(o)
if t = S.length − 1 then
throw FullStackException
else
t←t+1
S[t] ← o
14. Pop Function :
Top
Top Top
Algorithm pop( )
if isEmpty( ) then
throw EmptyStackException
else
t←t−1
return S[t + 1]
15. Top Function :
Algorithm Top() Top
{
Return s [ Top] ;
}
Is Empty Function :
Algorithm Is Empty()
If S null
{
Return true ;
else
Return false ;
} Top 0
16. Stack Code For Java
class Stack X
{
Private int maxSize ; // size of stack array
Private long [] stackArray ;
Private int top ; // top of stack
--------------------------------------------------------------
Public Stack X ( int s ) // constructor
{
maxSize = s ; // set array size
stackAr ray = new long [ maxSize ] ; // create array
top = -1 ; // no items yet
--------------------------------------------------------------
public void push ( long j ) // put item on top of stack
{
stackArray [ ++ top ] = j ; // increment top, insert item
17. Stack Code For Java (cont’d)
public long pop( ) // take item from top of stack
Return stackArray [ top-- ] ; // access item, decrement top
--------------------------------------------------------------
public long top( ) // top of stack
Return stackArray [ top ] ;
--------------------------------------------------------------
public boolean isEmpty( ) // true if stack is empty
return ( top == -1 ) ;
--------------------------------------------------------------
public boolean isFull( ) // true if stack is full
return ( top == maxSize-1 ) ;
} // end class StackX
19. Array-based Stack Implementation
Allocate an array of some size (pre-defined)
Maximum N elements in stack
Bottom stack element stored at element 0
last index in the array is the top
Increment top when one element is pushed,
decrement after pop