Upcoming SlideShare
×

# Stack project

2,913 views

Published on

Stack

Published in: Education, Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
2,913
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
152
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Stack project

1. 1. Data Structures StackSubmitted to : Dr. Mohammed Belal
2. 2. Data Structures & AlgorithmsA data structureAlgorithms
3. 3. Characteristics of Data StructuresData 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. 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. 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. 6. StackA stack is a list like a structure in which all insertionsand deletions are made at one end, called the top.The last element to be inserted into the stack will bethe first to be removed. Thus stacks are sometimesreferred 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. 7. StackA stack is a list like a structure in which all insertionsand deletions are made at one end, called the top.The last element to be inserted into the stack will bethe first to be removed. Thus stacks are sometimesreferred to as Last In First Out (LIFO) lists.Basic operations are push and pop.Often top and isEmpty are available, too. Top BAlso known as "last-in, first-out" or LIFO
8. 8. StackA stack is a list like a structure in which all insertionsand deletions are made at one end, called the top.The last element to be inserted into the stack will bethe first to be removed. Thus stacks are sometimesreferred to as Last In First Out (LIFO) lists.Basic operations are push and pop. Top COften top and isEmpty are available, too. BAlso known as "last-in, first-out" or LIFO
9. 9. StackA stack is a list like a structure in which all insertionsand deletions are made at one end, called the top.The last element to be inserted into the stack will bethe first to be removed. Thus stacks are sometimesreferred to as Last In First Out (LIFO) lists.Basic operations are push and pop.Top DOften top and isEmpty are available, too. C BAlso known as "last-in, first-out" or LIFO
10. 10. StackA stack is a list like a structure in which all insertionsand deletions are made at one end, called the top.The last element to be inserted into the stack will bethe first to be removed. Thus stacks are sometimesreferred to as Last In First Out (LIFO) lists. Top EBasic operations are push and pop. DOften top and isEmpty are available, too. C BAlso known as "last-in, first-out" or LIFO
11. 11. Stack Applications Real life  Pile of books  Plate trays More applications related to computerscience  Program execution stack  Evaluating expressions
12. 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 (donot remove)• IsEmpty()check whether the stack is empty
13. 13. Push Function : TopTop Top Algorithm push(o) if t = S.length − 1 then throw FullStackException else t←t+1 S[t] ← o
14. 14. Pop Function :Top Top Top Algorithm pop( ) if isEmpty( ) then throw EmptyStackException else t←t−1 return S[t + 1]
15. 15. Top Function : Algorithm Top() Top { Return s [ Top] ; }Is Empty Function :Algorithm Is Empty()If S null{Return true ;elseReturn false ;} Top 0
16. 16. Stack Code For Javaclass Stack X{Private int maxSize ; // size of stack arrayPrivate long [] stackArray ;Private int top ; // top of stack--------------------------------------------------------------Public Stack X ( int s ) // constructor{maxSize = s ; // set array sizestackAr 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. 17. Stack Code For Java (cont’d)public long pop( ) // take item from top of stackReturn stackArray [ top-- ] ; // access item, decrement top--------------------------------------------------------------public long top( ) // top of stackReturn stackArray [ top ] ;--------------------------------------------------------------public boolean isEmpty( ) // true if stack is emptyreturn ( top == -1 ) ;--------------------------------------------------------------public boolean isFull( ) // true if stack is fullreturn ( top == maxSize-1 ) ;} // end class StackX
18. 18. Array-based Stack
19. 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
20. 20. Array-based Stack Implementation
21. 21. Array-based Stack Implementation (cont’d)
22. 22. Array-based Stack Implementation (cont’d)
23. 23. Array-based Stack Implementation (cont’d)
24. 24. Array-based Stack Implementation (cont’d)