Files For C#ode Studio.
Stack
Lovely Professional University

Attribution Non-Commercial (BY-NC)

602 views

Files For C#ode Studio.
Stack
Lovely Professional University

Attribution Non-Commercial (BY-NC)

- Array Data Structure Lect-3
- WSIB Quality Improvement
- SPAU ANS SPDD
- Doc 2466
- HP-15C Owner's Handbook 1985 B&W
- Tree Data Structure
- Cliecnt Work
- CCT203 Study Guide - Chapter4
- Complexity of an Algorithm lect-2.pps
- Linked List Lect-4
- Data Structure Programs-Final Rishabh Jain
- 78935398 JNTU JAVA Lab Manual Programs
- Sorting Technique 2
- 35_02 RPN Mode
- Queue Lect 6
- Stack Implementation
- Review 1
- c++.pdf
- Sharp Pc-1211 Hp41c
- ATTiny Traffic Light

You are on page 1of 21

Stack- A stack is a linear data structure in which items may be added or removed only at one end . Accordingly, stacks are also called lastin-first-out or LIFO lists. The end at which element is added or removed is called the top of the stack. Two basic operations associated with stacks are : Push- Term used to denote insertion of an element onto a stack. Pop- Term used to describe deletion of an element from a stack. The order in which elements are pushed onto a stack is reverse of the

Representation of stacks Stacks may be represented in memory in various ways usually by means of one-way list or a linear array In array representation of stack, stack is maintained by an array named STACK , a variable TOP which contains the location/index of top element of stack and a variable MAXSTK giving the maximum number of elements that can be held by the stack. The condition TOP=0 or TOP=NULL indicates that stack is empty. The operation of addition of an item on stack and operation of removing an item from a stack may be implemented respectively by sub algorithms, called PUSH and POP. Before executing operation PUSH on to a stack, one must first test whether there is room in the stack for the new item. If not, then we have the condition known as overflow. Analogously, in executing the POP operation, one must first test where there is an element in stack to be deleted. If not, then we have condition known as underflow.

C#ODE Studio || codstudio.wordpress.com

C#ODE Studio || codstudio.wordpress.com

Algorithm: PUSH (STACK,TOP,MAXSTK,ITEM) This algorithm pushes an item onto the stack array. TOP stores the index of top element of the stack and MAXSTK stores the maximum size of the stack.

Step 1: [Stack already filled] If TOP=MAXSTK, then: Write: OVERFLOW Return Step 2: Set TOP:=TOP+1 Step 3: Set STACK[TOP]:=ITEM Step 4: Return

Algorithm: POP(STACK,TOP,ITEM) This procedure deletes the top element of STACK array and assign it to variable ITEM

Step 1: If TOP=0,then: Write: UNDERFLOW Return Step 2: Set ITEM:=STACK[TOP] Step 3: Set TOP:=TOP-1 Step 4: Return

A stack represented using a linked list is also known as linked stack. The array based representation of stack suffers from following limitations: Size of stack must be known in advance Representing stack as an array prohibits the growth of stack beyond finite number of elements. In a linked list implementation of stack, each memory cell will contain the data part of the current element of stack and pointer that stores the address of its bottom element and the memory cell containing the bottom most element will have a NULL pointer

Push operation on linked list representation of stack Algorithm: PUSH(INFO, LINK, TOP, ITEM, AVAIL) This algorithm pushes an element to the top of the stack Step 1: If AVAIL=NULL, then Write: OVERFLOW Return Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] Step 3: Set INFO[NEW]:=ITEM Step 4: If TOP=NULL, then Set LINK[NEW]:=NULL Set TOP:=NEW Return Else: Set LINK[NEW]:=TOP Set TOP:=NEW Step 5: Return

C#ODE Studio || codstudio.wordpress.com

POP operation on linked list representation of stack Algorithm: POP(INFO, LINK, TOP, AVAIL) This algorithm deletes an element from the top of the stack Step 1: If TOP=NULL , then: Write: UNDERFLOLW Return Step 2: Set PTR:=TOP Set TOP:=LINK[TOP] Write: INFO[PTR] Step 3: Set LINK[PTR]:=AVAIL and AVAIL:=PTR Step 4: Return

Application of stack Evaluation of arithmetic expression. For most common arithmetic operations, operator symbol is placed between its operands. This is called infix notation of an expression. To use stack to evaluate an arithmetic expression, we have to convert the expression into its prefix or postfix notation. Polish notation , refers to the notation in which operator symbol is placed before its two operands. This is also called prefix notation of an arithmetic expression. The fundamental property of polish notation is that the order in which operations are to be performed is completely determined by positions of operators and operands in expression. Accordingly, one never needs parentheses when writing expression in polish notation. Reverse Polish notation refers to notation in which operator is placed after its two operands. This notation is frequently called postfix notation. Examples of three notations are:

INFIX NOTATION: A+B PREFIX OR POLISH NOTATION: +AB POSTFIX OR REVERSE POLISH NOATATION: AB+ Convert the following infix expressions to prefix and postfix forms A+(B*C) (A+B)/(C+D) Prefix: +A*BC Postfix: A BC*+ Prefix: / +AB+CD Postfix: AB+CD+/

The computer usually evaluates an arithmetic expression written in infix notation in two steps. Converts expression to postfix notation Evaluates the postfix notation.

Evaluation of postfix expression Suppose P is an arithmetic expression written in postfix notation. The following algorithm which uses a STACK to hold operands and evaluates P

Algorithm: This algorithm finds the VALUE of an arithmetic expression P written in Postfix notation. Step 1: Add a right parentheses ) at the end of P Step 2: Scan P from left to right and repeat step 3 and 4 for each element of P until ) is encountered Step 3: If an operand is encountered, put the operand on the stack Step 4: If an operator is encountered , then: (a) Remove the two top elements of stack, where A is top element and B is next-top-element. (b) Evaluate B A (c ) Place the result of (b) back on stack [End of if structure] [End of step 2 loop] Step 5: Set VALUE equal to the top element of stack Step 6: Exit

Transforming Infix Expression into Postfix Expression The algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression P will be constructed from left to right using operands from Q and operators which are removed from STACK. The algorithm begins by pushing a left parentheses onto stack and adding a right parentheses at the end of Q Algorithm: POSTFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P Step 1: Push ( on to the STACK and add ) to the end of Q Step 2: Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty Step 3: If an operand is encountered, add it to P Step 4: If left parentheses is encountered, add it to STACK

Step 5: If an operator is encountered, then: (a) Repeatedly pop from STACK and add to P each operator which has same precedence or higher precedence than (b) Add to STACK Step 6: If a right parentheses is encountered , then: (a) Repeatedly pop from STACK and add to P each operator until a left parentheses is encountered (b) Remove the left parentheses [End of Step 2 Loop] Step 7: Exit

Example: Convert Q=A+(B*C) / D) to its corresponding postfix form Solution: put ) at the end of Q and put ( on stack Starting from left: Operand A , put it on P Operator + move to stack as no operator there ( move on stack Operand B, put it on P Operator * , move to stack as no operator Operand C , move to P ) , pop from stack and put on P until ( is encountered. Pop ( also operator /, as precedence of / is higher than + on stack, no pop possible. Push / on stack Operand D , put it on P Right parentheses ), Pop all the elements and add the P until ( is encountered. Also remove ( from stack

* ( /

+

(

+

(

+

(

P= A B C* D / +

C#ODE Studio || codstudio.wordpress.com

Transforming Infix Expression into Prefix Expression Algorithm: [Polish Notation] PREFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent prefix expression P Step 1: Reverse the input string Step 2: Examine the next element in the input Step 3: If it is operand, add it to output string Step 4: If it is closing parentheses, push it on stack Step 5: If it is operator, then: (i) if stack is empty, push operator on stack (ii) if top of stack is closing parentheses, push operator on the stack (iii) If it has same or higher priority than top of stack, push operator on stack Else pop the operator from the stack and add it to output string, repeat step 5

C#ODE Studio || codstudio.wordpress.com

Step 6: If it is an opening parentheses, pop operators from stack and add them to output string until a closing parentheses is encountered, pop and discard the closing parentheses. Step 7: If there is more input go to step 2 Step 8: If there is no more input, unstack the remaining operators and add them to output string Step 9: Reverse the output string

Consider the following arithmetic expression P written in postfix notation P: 12, 7, 3 -, /, 2, 1, 5, +, *, + (a) Translate P, by inspection and hand, into its equivalent infix expression (b) Evaluate the infix expression Sol: (a) Scanning from left to right, translate each operator from postfix to infix notation P = 12, [7-3], /, 2, 1, 5, +, *, + = [12/[7-3]],2, [1+5],*,+ = 12/(7-3)+2*(1+5) (b) 12/(7-3)+2*(1+5) = [3],[2*6],+ = 3+12 = 15

C#ODE Studio || codstudio.wordpress.com

Stacks are used for implementing function calls in a program Used for implementing recursion. Used for conversion of infix expression to its postfix or prefix form Used for evaluation of postfix expression. Used in sorting of arrays (quicksort and mergesort technique)

References

Dinesh Mehta and Sartaj Sahni Handbook of Data Structures and Applications, Chapman and Hall/CRC Press, 2007. Niklaus Wirth, Algorithms and Data Structures, Prentice Hall, 1985. Diane Zak, Introduction to programming with c++, copyright 2011 Cengage Learning Asia Pte Ltd Schaumm Series ,McGraw Hill.

- Array Data Structure Lect-3Uploaded byTanmay Baranwal
- WSIB Quality ImprovementUploaded bySelvaraj Rangasamy
- SPAU ANS SPDDUploaded byPradeep Kumar
- Doc 2466Uploaded byHemanth Pattazhy
- HP-15C Owner's Handbook 1985 B&WUploaded byjjirwin
- Tree Data StructureUploaded byTanmay Baranwal
- Cliecnt WorkUploaded byArslan Ali
- CCT203 Study Guide - Chapter4Uploaded byjollycharm
- Linked List Lect-4Uploaded byTanmay Baranwal
- Complexity of an Algorithm lect-2.ppsUploaded byTanmay Baranwal
- Data Structure Programs-Final Rishabh JainUploaded byHarshit Tiwari
- 78935398 JNTU JAVA Lab Manual ProgramsUploaded bySuraj Yadav
- Sorting Technique 2Uploaded byTanmay Baranwal
- 35_02 RPN ModeUploaded byaapaulos
- Queue Lect 6Uploaded byTanmay Baranwal
- Stack ImplementationUploaded bystefan125
- Review 1Uploaded bymichu
- c++.pdfUploaded byfelipecamargo101
- Sharp Pc-1211 Hp41cUploaded byRodolfo Sergio Cruz Fuentes
- ATTiny Traffic LightUploaded byGoran Pantelic
- C++ ProgramUploaded bySunil Chopra
- DSAUploaded byNoman Ali
- AaaUploaded bynadhiya
- PythonUploaded byMast3rm1nd
- sundar.docxUploaded byashwin k
- f.txtUploaded bySadam Husen
- FPL2013.pdfUploaded bykrinunn
- Lecture07_ComputerApplicationsIE1_DrAtifShahzad.ppsxUploaded byatifshahzad
- Command tree 13217.pdfUploaded byCristhian Balderrama
- Chapter 3Uploaded byTammam Ichtay

- Uncertainty in Expert SystemsUploaded byTanmay Baranwal
- Bootstrapping in Compiler DesignUploaded byTanmay Baranwal
- Compile Farm – A Technique in Distributed CompilationUploaded byTanmay Baranwal
- Role of RBI in Economic Development of India.pdfUploaded byTanmay Baranwal
- Huffman CodingUploaded byTanmay Baranwal
- Compiler Farm SynopsisUploaded byTanmay Baranwal
- Sorting Technique 2Uploaded byTanmay Baranwal
- Queue Lect 6Uploaded byTanmay Baranwal
- 4 Bit Even Synchr. Counter DetailUploaded byTanmay Baranwal
- Sorting Technique - 1Uploaded byTanmay Baranwal
- Dijkstra's AlgorithmUploaded byTanmay Baranwal
- AVL TreeUploaded byTanmay Baranwal
- Tree Data StructureUploaded byTanmay Baranwal
- Minimum Spanning TreeUploaded byTanmay Baranwal
- Bellman Ford's AlgorithmUploaded byTanmay Baranwal
- Linked List Lect-4Uploaded byTanmay Baranwal
- Complexity of an Algorithm lect-2.ppsUploaded byTanmay Baranwal
- 14508_28. Minimum Spanning TreeUploaded byTanmay Baranwal
- 14508 18 Shortest Paths IIUploaded byTanmay Baranwal
- 4 Bit Even Synchronous Counter ImagesUploaded byTanmay Baranwal
- Introduction to Data Structure Lect-1Uploaded byTanmay Baranwal
- 14508 17 Shortest Paths IUploaded byTanmay Baranwal
- Queue Lect 6Uploaded byTanmay Baranwal

- Two Way Linked ListUploaded bySami119
- GE8151-Problem Solving & Python ProgrammingUploaded byDharmaraj T B
- Lecture 4 12Uploaded byDevin Mahon
- pertemuan -Uploaded byNuraini Azizah
- IGNOU MCA MCS-21 Solved Assignments 2011Uploaded bySyedkareem_hkg
- Algorthim ConceptUploaded byJilari Ram
- Practice 2Uploaded bySenior Alonzo
- Spot Weld Optimization TechniqueUploaded bykiran_wakchaure
- Data Structures Lab 15CSL38Uploaded bytejaswini
- 1071Uploaded byPrashanth Raghavan
- DS_1Uploaded bymanoj thiwari
- Linked ListUploaded byrashidul
- Incremental DesignUploaded byVinith Krishnan
- Investigación de Operaciones Ejercicio.pdfUploaded byUbaldo MP
- Decision TreeUploaded byabdi
- ArrayList Metode Java ProgramiranjeUploaded byДејан Кузмановић
- Dynamic programmingUploaded byAkash Ravi
- Searching in Linked ListUploaded byBunny Lucky Kalyan
- Data Structure and AlgorithmUploaded bySidra Tul Muntaha
- Binary Search AlgorithmUploaded byapi-19981779
- Difference between HashMap and HashSet in Java.docxUploaded byjogishokeen
- Greedy AlgorithmsUploaded byDevroy Malakar
- Top 50 Data Structure Interview QuestionsUploaded byakhtarrasool
- dct1Uploaded bySubbuNaidu
- Convolutional CodesUploaded byRashmi Jamadagni
- Astarn Algorithm SPUploaded byHari Haran
- Report on Seminar(Parametric Searching) by Indranil Nandy(06CS6010)Uploaded byIndranil Nandy
- Single Population Met a HeuristicsUploaded byAmaal Ghazi
- 67-13-115-1-10-20170718Uploaded byshafirahazmi
- PPL Lab ManualUploaded bypunitmittal

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.