Upcoming SlideShare
×

# Stack

779

Published on

Published in: Education
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
779
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
62
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Stack

1. 1. Stack Y S N L Srihari
2. 2. Introduction on Stack <ul><li>Def: Stack is a collection of similar type of elements. It is like one side open box, it is having a property ‘FILO’ (First In Last Out). </li></ul><ul><li>In stack last inserted element can serve first. </li></ul><ul><li>Stack is a linear data structure. </li></ul><ul><li>Stack can be implemented in two way’s, by using arrays and by using linked list. </li></ul>
3. 3. Operations on Stack <ul><li>Stack can perform two types of operations, those are, </li></ul><ul><li>Push: It is a process to insert an element in to a stack. </li></ul><ul><li>Pop: It is a process remove an element from stack. </li></ul>
4. 4. Working procedure of a stack <ul><li>Variable ‘TOP’ is used for performing ‘PUSH’ and ‘POP’ operations. </li></ul><ul><li>Push and Pop an element of stack at the position of a stack. </li></ul>
5. 5. Stack using arrays <ul><li>Array is a collection of similar type of elements. </li></ul><ul><li>Array is a linear data structure. </li></ul><ul><li>Procedure: </li></ul><ul><ul><ul><li>Declare an array for stack, with a maximum size to store elements. </li></ul></ul></ul><ul><ul><ul><li>Initialize the position value of variable top is ‘-1’. </li></ul></ul></ul><ul><ul><ul><li>When we push an element into a stack, check the top value, if it reaches the maximum limit or not, if it is not push an element into stack. </li></ul></ul></ul><ul><ul><ul><li>When we Pop an element from stack, check the top value, if it reaches the ‘-1’or not, if it is not Pop an element from stack. </li></ul></ul></ul>
6. 6. Push code <ul><li>Step 1: Start. </li></ul><ul><li>Step 2:If (top<max) true go to step 3 else go to Step 5. </li></ul><ul><li>Step 3: Push an element into a stack. </li></ul><ul><li>Step 4: Increment the value in ‘top’ once. (top=top+1) go to Step 6: </li></ul><ul><li>Step 5: Print “push operation is not possible” </li></ul><ul><li>Step 6: Stop. </li></ul>
7. 7. Pop code <ul><li>Step 1: Start. </li></ul><ul><li>Step 2:If (top== -1) true go to step 3 else go to Step 5. </li></ul><ul><li>Step 3: Pop an element from stack and print value. </li></ul><ul><li>Step 4: Decrement the value in ‘top’ once. (top=top+1) go to Step 6: </li></ul><ul><li>Step 5: Print “Pop operation is not possible” </li></ul><ul><li>Step 6: Stop. </li></ul>
8. 8. Diagrammatical representation of Stack 20 10 30 50 Inserting position Removing position
9. 9. Push and Pop operations Push an element ’45’ into stack Pop an element ’45’ from stack 20 10 30 50 45 20 10 30 50 45 20 10 30 50 45
10. 10. Stack using linked list <ul><li>When we implement stack using array is having some limitations. The major one is size. </li></ul><ul><li>In arrays we can push certain number of elements only. </li></ul><ul><li>To overcome this problem we need linked list. </li></ul><ul><li>Linked list is a linear data structure. </li></ul><ul><li>In linked list, each link having two elements, one is data, another one is address of a next link. </li></ul><ul><li>Linked list can be implemented with the help of pointers. Here the link will be called as node. </li></ul>
11. 11. Push algorithm using linked list <ul><li>Step 1: Start. </li></ul><ul><li>Step 2: Create a new node. </li></ul><ul><li>Step 3: Make a link between last node and new node by Changing the address of last node pointer to new node. </li></ul><ul><li>Step 4: Set the address of a ‘top’ node as new node. </li></ul><ul><li>Step 5: Stop. </li></ul><ul><li>Step 1: Start. </li></ul><ul><li>Step 2: Remove the link between top and last node of a list. </li></ul><ul><li>Step 3: Set the address of a ‘top’ node as previous node with the help of a temp node move from starting node to previous node. </li></ul><ul><li>Step 4: Stop. </li></ul>Pop algorithm using linked list
12. 12. Example: Data Address link 10 * 20 * 30 * 40 NULL Top
13. 13. Push operation: 10 * 20 * 30 * 40 NULL Top New node 45 Null Before Push: 45 Null 10 * 20 * 30 * 40 * Top Step 1: Step 2: 45 Null 10 * 20 * 30 * 40 * Top
14. 14. Push operation: Before Pop: 45 Null 10 * 20 * 30 * 40 Null Top Step 2: Step 1: 45 Null 10 * 20 * 30 * 40 * Top Temp
15. 15. Applications of Stack <ul><li>Happstack ( H askell App lication Stack ) is a free application server for websites written in the functional programming language Haskell. It integrates handling Web, persistence, XML/XSLT, and templating functionality . </li></ul><ul><li>Stack is used by the compiler to compile the program. By using stack C- language compiler can store the special characters like {,},(,),[,]. </li></ul><ul><li>It can perform checking for each open have a close or not, if it is not rise an error messages. </li></ul><ul><li>Stack can be used by the operating system to schedule the ‘process’ for processing. </li></ul>
16. 16. <ul><li>These are used to convert the postfix and prefix notations into infix notations. </li></ul>Applications of Stack
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.