Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Similar documents
Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

STACKS AND QUEUES. Problem Solving with Computers-II

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Data Structures Week #3. Stacks

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Associate Professor Dr. Raed Ibraheem Hamed

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Infix to Postfix Conversion

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Linear Data Structure

Stacks and Queues. CSE Data Structures April 12, 2002

DS Assignment II. Full Sized Image

Lecture Data Structure Stack

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Sample Question Paper


VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Stacks Calculator Application. Alexandra Stefan

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

March 13/2003 Jayakanth Srinivasan,

Computer Science BS Degree - Data Structures (I2206) Abstract Data Types (ADT)

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Chapter 9 STACK, QUEUE

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

CS 211. Project 5 Infix Expression Evaluation

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

INSTITUTE OF AERONAUTICAL ENGINEERING

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

BBM 201 DATA STRUCTURES

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stacks Stack Examples:

An Introduction to Trees

Stacks and their Applications

Stacks. Revised based on textbook author s notes.

Stacks, Queues and Hierarchical Collections

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

NCS 301 DATA STRUCTURE USING C

Monday, November 13, 2017

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Data Structures and Algorithms

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

CS 211 Programming Practicum Spring 2018

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University

Computer Science. Section 1B

1. Stack Implementation Using 1D Array

BBM 201 DATA STRUCTURES

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

ADTs Stack and Queue. Outline

The time and space are the two measure for efficiency of an algorithm.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

CS 211 Programming Practicum Spring 2017

Stack and Its Implementation

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

The program simply pushes all of the characters of the string into the stack. Then it pops and display until the stack is empty.

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Programming in C++ 5. Integral data types

Data Structure. Recitation IV

Linked List. April 2, 2007 Programming and Data Structure 1

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

List, Stack, and Queues

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Postfix (and prefix) notation

Answer D) ADT does not depend on the implementation

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

1 P age DS & OOPS / UNIT II

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code:

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Types of Data Structures

Formal Languages and Automata Theory, SS Project (due Week 14)

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Stack and Queue Types

CS 211 Programming Practicum Fall 2018

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

Lecture 4 Stack and Queue

Transcription:

What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end called Top Visual Representation of Stack: Field Value Size of the Stack 6 Maximum Value of Stack Top 5 Minimum Value of Stack Top 0 Value of Top when Stack is Empty -1 Value of Top when Stack is Full 5 Basic Operations Performed on Stack : 1. Create 2. Push 3. Pop 4. Empty Creating Stack: 1. Stack can be created by declaring the structure with two members. 2. One Member can store the actual data in the form of array. 3. Another Member can store the position of the topmost element. typedef struct stack int data[max]; int top; stack;

Push on Stack: We have declared data array in the above declaration. Whenever we add any element in the data array then it will be called as Pushing Data on the Stack. Suppose top is a pointer to the top element in a stack. After every push operation, the value of top is incremented by one. Pop on Stack : Whenever we try to remove element from the stack then the operation is called as POP Operation on Stack. Some basic terms : Concept Stack Push Stack Overflow Definition The procedure of inserting a new element to the top of the stack is known as Push Operation Any attempt to insert a new element in already full stack is results into Stack Overflow.

Concept Stack Pop Stack Underflow Definition The procedure of removing element from the top of the stack is called Pop Operation. Any attempt to delete an element from already empty stack results into Stack Underflow. When Stack is Empty When Stack is said to empty then it does not contain any element inside it. Whenever the Stack is Empty the position of topmost element is -1. When Stack is Not Empty Whenever we add very first element then topmost position will be incremented by 1. After adding First Element top = 0. After Deletion of 1 Element Top Will be Decremented by 1 Position of Top and Its Value: Position of Top Status of Stack -1 Stack is Empty 0 First Element is Just Added into Stack N-1 Stack is said to Full N Stack is said to be Overflow Values of Stack and Top: Operation Explanation top = -1-1 indicated Empty Stack top = top + 1 After push operation value of top is incremented by integer 1 top = top 1 After pop operation value of top is decremented by 1 Check Whether Stack is Empty or Not? We are using Empty Function for Checking whether stack is empty or not 1. Function returns True if Stack is Empty. 2. Function returns False if Stack is Non-Empty. 3. Function Takes Pointer to Stack int empty (stack *s)

1. Return Type : Integer. [Empty Stack Return 1, Non Empty Stack Return 0 ] 2. Parameter : Address of Variable of Type Stack. 3. How to Call this Function From Main? typedef struct stack int data[max]; int top; stack; Empty Function : int empty(stack *s) if(s->top == -1) //Stack is Empty return(1); else return(0); stack; Check Whether Stack is Full or Not? We are using Empty Function for Checking whether stack is full or not 1. Function returns True if Stack is Full 2. Function returns False if Stack is Not Full. 3. Function Takes Pointer to Stack int full (stack *s) 1. Return Type : Integer. [If full Stack Return 1, not full Stack Return 0 ] 2. Parameter : Address of Variable of Type Stack. How to Call this Function From Main? typedef struct stack int data[max]; int top; stack; void main() stack s; // Declare Stack Variable ------- ------- i = full(&s); // Pass By Reference ------ ------ 1. Pass Stack Variable to full Function using pass by reference. 2. As full Function returns integer, we have facility to store returned value into some integer variable so we have written [ i = full(&s) ]

Complete Code int full(stack *s) if(s->top == MAX-1) //Stack is Full return(1); else return(0); Push Operation: 1. Push Refers as Adding Elements onto Stack. 2. Push Operation carried out in following 2 steps o o First Increment Variable top so that it now refers to next memory location. Secondly Add Element Onto Stack by accessing array. 3. Main Function Should ensure that stack is not full before making call to push() in order to prevent Stack Overflow Push Function void push(stack *s,int num) s->top = s->top + 1; s->data[s->top] = num; Pop Operation Arguments and Return Type : 1. Argument : Variable of Type Stack. 2. Return Type : Integer [ Removed Element ] Steps in Pop Operation : 1. Store Topmost Element in another Variable. 2. Decrement Top by 1 3. Return Topmost Element. Pre-requisites : Stack Type Definition.: Click Here Pop Function : int pop(stack *s) int x; x = s->data[s->top]; s->top = s->top - 1; return(x);

Application of Stack : 1. Parsing 2. Recursive Function 3. Calling Function 4. Expression Evaluation 5. Expression Conversion I. Infix to Postfix II. III. IV. Infix to Prefix Postfix to Infix Prefix to Infix 6. Towers of hanoi Expression Representation Techniques : 1. Infix Expression 2. Prefix Expression 3. Postfix Expression Evaluation of Postfix Expression : [ Click Here ] Expression Example Note Infix a + b Operator Between Operands Prefix + a b Operator before Operands Postfix a b + Operator after Operands Generally postfix expressions are free from Operator Precedence thats why they are preferred in Computer system.computer System Uses Postfix form to represent expression. Following is the example that shows evaluation of the Postfix expression using stack as data structure. Algorithm for Evaluation of Postfix Expression Initialize(Stack S) x = ReadToken(); // Read Token while(x) if ( x is Operand ) Push ( x ) Onto Stack S. if ( x is Operator )

Operand2 = Pop(Stack S); Operand2 = Pop(Stack S); Evaluate (Operand1,Operand2,Operator x); x = ReadNextToken(); // Read Token