Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

Size: px
Start display at page:

Download "Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan."

Transcription

1 Data Structure Chapter 3 Stacks and Queues Instructor: Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 29 Spring

2 Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2

3 Figure 3.: Inserting and Deleting Elements in Stack top top top E top D D top C C C B B B B A A A A A top D C B A Stack: an ordered list in which insertions and deletions are made at one end called the top The last element inserted into a stack is the first element removed Last-In-First-Out (LIFO) list C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 3

4 Implementation of Stack by Array a n- a 2 a a a a a 2 a n- Array index 2 3 n- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 4

5 Functions for Stacks Create #define MAX_STACK_SIZE /*maximum stack size*/ typedef struct{ int key; /* other fields */ }element; element stack[max_stack_size]; Int top = -; IsFull top >=MAX_STACK_SIZE-; IsEmpty top < Add Delete C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 5

6 Program 3.: Add to Stack void add (int *top, element item) { /* add an item to the global stack */ if (*top >=MAX_STACK_SIZE-) { stack_full(); return; } stack[++*top]=item; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 6

7 Program 3.2: Delete from a Stack Element delete (int *top) { /* return the top element from the stack */ if (*top == -) return stack_empty(); /* returns an error key */ return stack[(*top)--]; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 7

8 How Does the System Stack Work after a Function Call? Old frame pointer Return address 2 Old frame pointer Return address Local variables Old frame pointer Return address fp main fp: a pointer to the current stack frame a: function name fp a C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 8

9 Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 9

10 Figure 3.4: Inserting and Deleting Elements in A Queue rear rear rear D rear C C B B B A A A A rear front front front front front Queue: an ordered list in which insertions take place at one end and all deletions take place at the opposite end The first element inserted into a queue is the first element removed First-In-First-Out (FIFO) list D C B a a a 2 a n- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 front rear

11 Functions of Queue CreateQ #define MAX_QUEUE_SIZE /* Maximum queue size */ typedef struct{ int key; /* other fields */ } element; element queue[max_queue_size]; int rear = -; int front = -; IsFullQ rear == MAX_QUEUE_SIZE- IsEmptyQ front == rear AddQ DeleteQ C.E., NCU, Taiwan Angela Chih-Wei Tang, 29

12 Program 3.3: Add to A Queue void addq (int *rear, element item) { /* add an item to the queue */ if (*rear == MAX_QUEUE_SIZE-) { queue_full(); return; } queue[++*rear] = item; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2

13 Program 3.4: Delete from A Queue element deleteq (int *front, int rear) { /* remove element at the front of the queue */ if (*front == rear) return queue_empty(); /*return an error key */ return queue[++*front]; } Is there any problem with current queue representation? C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 3

14 Initialization: front=rear= Circular Queues front will always point one position counterclockwise from the first element in the queue circular queue assigns next element to q[] when rear == MaxSize If front == rear, is queue empty or full? front = ; rear = J3 J2 J n- n-2 front = ; rear = 3 n-4 n-3 J4 J3 n- J2 J n-2 front = n-4; rear = C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 4 n-3 n-4

15 Examples of Full Circular Queues J8 J7 Jn- J6 J5 J3 n- Jn J2 n-2 front = ; rear = n- n-3 n-4 J8 J7 J6 J5 J2 J4 J3 n- Jn- n-4 n-3 n-2 front = n-3; rear = n-4 Solution: A circular queue of size MAX_QUEUE_SIZE will be Permitted to hold at most MAX_QUEUE_SIZE- elements! C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 5

16 Program 3.5: Add to A Circular Queue void addq (int front, int *rear, element item) { /* add an item to the queue */ *rear = (*rear+)% MAX_QUEUE_SIZE; if (front == *rear) { queue_full(rear); /* reset rear and print error */ return; } queue[*rear] = item; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 6

17 Program 3.6: Delete from A Circular Queue element deleteq (int *front, int rear) { } element item; /* remove front element from the queue and put it in item */ if (*front == rear) return queue_empty(); /* queue_empty returns an error key */ *front = (*front+)% MAX_QUEUE_SIZE; return queue[*front]; C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 7

18 Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 8

19 A Mazing Problem An Example Entrance C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 9 Exit : open path : barrier

20 Allowable Moves NW [i-][j-] N [i-][j] NE [i-][j+] W [i][j-] X [i][j] [i][j+] E [i+][j-] SW [i+][j] S [i+][j+] SE C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2

21 A Mazing Problem Data Structure Stack : store the coordinates and direction #define MAX_STACK_SIZE /*maximum stack size*/ typedef struct { short int row; short int col; short int dir; } element; element stack[max_stack_size]; Save current position and arbitrarily pick a possible move we can return to it and try another path if fails! Use of anothr 2-D array to mark any position that has been visited C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2

22 Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 22

23 Order in Which A Second-Degree Polynomial Is Evaluated C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 23

24 Figure 3.3: Infix and Postfix Notation Expressions are converted from Infix notation into Postfix notation before compiler can process them! Infix? Postfix 2+3*4 234*+ a*b+5 ab*5+ (+2)*7 2+7* a*b/c ((a/(b-c+d))*(e-a)*c a/b-c+d*e-a*c ab*c/ abc-d+/ea-*c* ab/c-de*+ac*- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 24

25 Infix to Postfix Step : Fully parenthesize the expression Step 2: Move all binary operators so that they replace their corresponding right parentheses Step 3: Delete all parentheses An example: a/b-c+d*e-a*c After step ((((a/b)-c)+(d*e))-a*c)) After steps 2 and 3 ab/c=de*+ac*- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 25

26 Figure 3.5: Translation of a+b*c (Infix) to PostFix Token Stack [] [] [2] Top a - a Output + + a b + ab * + * ab c + * abc eos - abc*+ C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 26

27 Figure 3.6: Translation of a*(b+c)*d to Postfix Token Stack [] [] [2] Top a - a * * a ( * ( a b * ( ab + * ( + 2 ab c * ( + 2 abc ) * abc+ * * abc+* d * abc+*d eos * abc+*d* Output C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 27

28 Postfix Evaluation : The Representations of Postfix Expression & Stack Expression character array Binary operators: +-*/% Operands: Stack int array Operands only Declarations: #define MAX_STACK_SIZE /* maximum stack size */ #define MAX_EXPR_SIZE /* max size of expression */ typedef enum{lparen, rparen, plus, minus, times, divide, mod, eos, operand} precedence; int stack[max_stack_size]; /* global stack */ char expr[max_expr_size]; /* input string */ C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 28

29 Figure 3.4: Postfix Evaluation Input postfix expression: 6 2 / * + Token Stack Top [](bottom) [] [2] / 6/2 3 6/2 3-6/ / / * 6/2-3 4*2 + 6/2-3+4*2 C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 29

30 Postfix Expression Evaluation Step : Make a single left-to-right scan of the postfix Step 2: Place the operands on a stack until we find an operator 2.: Remove the correct number of operands for the operator from the stack 2.2: Perform the operation 2.3: Place the result back on the stack Step 3: Go to step until we reach the end of the expression Step 4: Remove the answer from the top of the stack C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 3

CHAPTER 3 STACKS AND QUEUES

CHAPTER 3 STACKS AND QUEUES CHAPTER 3 STACKS AND QUEUES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed Fundamentals of Data Structures in C /2nd Edition, Silicon Press, 2008.

More information

STACKS AND QUEUES CHAPTER 3

STACKS AND QUEUES CHAPTER 3 CHAPTER 3 STACKS AND QUEUES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed Fundamentals of Data Structures in C, CHAPTER 3 1 Stack: a Last-In-First-Out

More information

Chapter3 Stacks and Queues

Chapter3 Stacks and Queues Chapter Stacks and Queues Stacks Stacks Using Dynamic Arrays Queues Circular Queues Using Dynamic Arrays A Maze Problem Evaluation of Expressions Multiple Stacks and Queues C-C Tsai P. Stacks A stack is

More information

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 3. Stacks And Queues

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 3. Stacks And Queues CHAPTER 3 Stacks And Queues 1 Stack Stack: a Last-In-First-Out (LIFO/FILO) list Push Pop C A B C B A Top 2 An application of stack: stack frame of function call Old frame pointer fp Return address al Old

More information

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi Data Structures & Algorithm Analysis Lec(3) Stacks Lecturer: Souad Alonazi What is a stack? Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT = LIFO It means: the last element

More information

Fall, 2015 Prof. Jungkeun Park

Fall, 2015 Prof. Jungkeun Park Data Structures t and Algorithms Stacks Application Infix to Postfix Conversion Fall, 2015 Prof. Jungkeun Park Copyright Notice: This material is modified version of the lecture slides by Prof. Rada Mihalcea

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 5: Stacks and Queues 2017 Fall Stacks A list on which insertion and deletion can be performed. Based on Last-in-First-out (LIFO) Stacks are used for a number of applications:

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 6: Stacks and Queues 2015-2016 Fall Stacks A list on which insertion and deletion can be performed. Based on Last-in-First-out (LIFO) Stacks are used for a number of applications:

More information

Chapter 3. Stack & Queue 1

Chapter 3. Stack & Queue 1 Chapter 3 Stacks & Queues Templates in C++ The Stack ADT The Queue ADT SubTypeing & Inheritance in C++ A Mazing Problem Evaluation of Expressions Multiple Stacks & Queues March 30, 2015 Stack & Queue 1

More information

Two abstract data types. performed on them) Stacks Last In First Out (LIFO) Queues First In First Out (FIFO)

Two abstract data types. performed on them) Stacks Last In First Out (LIFO) Queues First In First Out (FIFO) Two abstract data types o Can be implemented using arrays or linked lists o Restricted lists (only a small number of operations are performed on them) Stacks Last In First Out (LIFO) Queues First In First

More information

Data Structure - Stack and Queue-

Data Structure - Stack and Queue- Data Structure - Stack and Queue- Hanyang University Jong-Il Park STACK Stack ADT List that insertions and deletions can be performed at the end of the list Operations Push(X, S): insert X in the list

More information

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP Stacks Ordered list with property: Insertions and deletions always occur at the same end. INSERT A3 A3 TOP DELETE A2 TOP A2 A2 TOP A1 A1 A1 A0 A0 A0 Stacks Implementation Implementation with arrays: Declare

More information

Lecture Data Structure Stack

Lecture Data Structure Stack Lecture Data Structure Stack 1.A stack :-is an abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards

More information

Infix to Postfix Conversion

Infix to Postfix Conversion Infix to Postfix Conversion Infix to Postfix Conversion Stacks are widely used in the design and implementation of compilers. For example, they are used to convert arithmetic expressions from infix notation

More information

Stacks and Queues. Introduction to Data Structures Kyuseok Shim SoEECS, SNU.

Stacks and Queues. Introduction to Data Structures Kyuseok Shim SoEECS, SNU. Stacks and Queues Introduction to Data Structures Kyuseok Shim SoEECS, SNU. 1 3.1 Templates in C++ Make classes and functions more reusable Without using templates (example) program 1.6 : Selection sort

More information

Data Structure. Chapter 4 List (Part II) Department of Communication Engineering National Central University Jhongli, Taiwan.

Data Structure. Chapter 4 List (Part II) Department of Communication Engineering National Central University Jhongli, Taiwan. Data Structure Chapter 4 List (Part II) Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 2009 Spring Outline Polynomials Sparse matrices Doubly linked

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 6: EVALUATION of EXPRESSIONS 2018-2019 Fall Evaluation of Expressions Compilers use stacks for the arithmetic and logical expressions. Example: x=a/b-c+d*e-a*c If a=4, b=c=2,

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 6: EVALUATION of EXPRESSIONS 2017 Fall Evaluation of Expressions Compilers use stacks for the arithmetic and logical expressions. Example: x=a/b-c+d*e-a*c If a=4, b=c=2,

More information

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

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1 Data Structure using C++ Lecture 04 Reading Material Data Structures and algorithm analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1 Summary Stack Operations on a stack Representing stacks Converting an expression

More information

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

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

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

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out Stack and Queue 1 Stack Data structure with Last-In First-Out (LIFO) behavior In Out C B A B C 2 Typical Operations Pop on Stack Push isempty: determines if the stack has no elements isfull: determines

More information

CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008

CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008 HAPTER 3 STAKS AND QUEUES Iris Hui-Ru Jiang Fall 2008 2 ontents Templates in ++ Stack (LIFO) Queue (FIFO) Subtyping and Inheritance in ++ A Mazing Problem Evaluation of Expressions Readings hapter 3 ++

More information

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

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Stacks and Queues Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Two New ADTs Define two new abstract data types Both are restricted lists Can be implemented using arrays

More information

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1 Some Applications of Stack Spring Semester 2007 Programming and Data Structure 1 Arithmetic Expressions Polish Notation Spring Semester 2007 Programming and Data Structure 2 What is Polish Notation? Conventionally,

More information

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan Data Structure Chapter 5 Trees (Part II) Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 2010 Spring Threaded Binary Tree Problem: There are more

More information

Types of Data Structures

Types of Data Structures DATA STRUCTURES material prepared by: MUKESH BOHRA Follow me on FB : http://www.facebook.com/mukesh.sirji4u The logical or mathematical model of data is called a data structure. In other words, a data

More information

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

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks Stack Chapter 4 Outline Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications Recursive Programming Evaluation of Expressions ADT for stacks Introduction

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering SOLUTION FOR INTERNAL ASSESSMENT TEST 2 Date : 6/10/2017 Marks:

More information

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

DEEPIKA KAMBOJ UNIT 2. What is Stack?

DEEPIKA KAMBOJ UNIT 2. What is Stack? What is Stack? UNIT 2 Stack is an important data structure which stores its elements in an ordered manner. You must have seen a pile of plates where one plate is placed on top of another. Now, when you

More information

Content: Learning Objectives

Content: Learning Objectives 1 BLOOM PUBLIC CHOOL Vasant Kunj, New Delhi Lesson Plan Class: XII ubject: Computer cience Month : July No of s: 21 Chapter:Data structure: Linked List TTT: 8 WT: 12 Content: Learning Objectives At the

More information

Data Structures Week #3. Stacks

Data Structures Week #3. Stacks Data Structures Week #3 Stacks Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation of Stacks Stack Applications October 5, 2015 Borahan Tümer, Ph.D. 2 Stacks (Yığınlar)

More information

Linear Data Structure

Linear Data Structure Linear Data Structure Definition A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal

More information

Stacks and their Applications

Stacks and their Applications Stacks and their Applications Lecture 23 Sections 18.1-18.2 Robb T. Koether Hampden-Sydney College Fri, Mar 16, 2018 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) An interesting definition for stack element The stack element could be of any data type struct stackelement int etype; union int ival;

More information

Top of the Stack. Stack ADT

Top of the Stack. Stack ADT Module 3: Stack ADT Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Stack ADT Features (Logical View) A List that

More information

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

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues The Bucharest University of Economic Studies Data Structures ADTs-Abstract Data Types Stacks and Queues Agenda Definition Graphical representation Internal interpretation Characteristics Operations Implementations

More information

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

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms Introduction Problem Solving on Computer Data Structures (collection of data and relationships) Algorithms 1 Objective of Data Structures Two Goals: 1) Identify and develop useful high-level data types

More information

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

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 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

More information

An Introduction to Trees

An Introduction to Trees An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search

More information

Top of the Stack. Stack ADT

Top of the Stack. Stack ADT Module 3: Stack ADT Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Stack ADT Features (Logical View) A List that

More information

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science IT 4043 Data Structures and Algorithms Budditha Hettige Department of Computer Science 1 Syllabus Introduction to DSA Abstract Data Types List Operation Using Arrays Stacks Queues Recursion Link List Sorting

More information

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305 Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n

More information

Lecture 4 Stack and Queue

Lecture 4 Stack and Queue Lecture 4 Stack and Queue Bo Tang @ SUSTech, Spring 2018 Our Roadmap Stack Queue Stack vs. Queue 2 Stack A stack is a sequence in which: Items can be added and removed only at one end (the top) You can

More information

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

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017 Stack Applications Lecture 27 Sections 18.7-18.8 Robb T. Koether Hampden-Sydney College Wed, Mar 29, 2017 Robb T. Koether Hampden-Sydney College) Stack Applications Wed, Mar 29, 2017 1 / 27 1 Function

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Class Teacher: Pralay Mitra Department of Computer Science and Engineering Indian Institute of Technology Kharagpur Conceptual Idea

More information

DS Assignment II. Full Sized Image

DS Assignment II. Full Sized Image DS Assignment II 1. A) For the Towers of Hanoi problem, show the call tree during the recursive call Towers(3, A, C, B). In the tree, label the root node as Towers (3, A, C, B) while marking all the intermediate

More information

Lecture No.04. Data Structures

Lecture No.04. Data Structures Lecture No.04 Data Structures Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i

More information

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

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero). Suppose we have a circular array implementation of the queue,with ten items in the queue stored at data[2] through data[11]. The current capacity of an array is 12. Where does the insert method place the

More information

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques Stacks Manolis Koubarakis 1 Stacks and Queues Linear data structures are collections of components arranged in a straight line. If we restrict the growth of a linear data structure so that new components

More information

DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES

DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT III LINEAR DATA STRUCTURES PART A 1. What is meant by data

More information

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

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms... First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms.... Q1) What are some of the applications for the tree data structure? Q2) There are 8, 15, 13, and

More information

Postfix (and prefix) notation

Postfix (and prefix) notation Postfix (and prefix) notation Also called reverse Polish reversed form of notation devised by mathematician named Jan Łukasiewicz (so really lü-kä-sha-vech notation) Infix notation is: operand operator

More information

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3 UNIT 3 LINEAR DATA STRUCTURES 1. Define Data Structures Data Structures is defined as the way of organizing all data items that consider not only the elements stored but also stores the relationship between

More information

Queue: Queue Representation: Basic Operations:

Queue: Queue Representation: Basic Operations: Queue: Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove

More information

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

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1 Data Structure using C++ Lecture 04 Reading Material Data Structures and algorithm analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1 Summary Infix to Postfix Example 1: Infix to Postfix Example 2: Postfix Evaluation

More information

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

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows: STACKS A stack is a linear data structure for collection of items, with the restriction that items can be added one at a time and can only be removed in the reverse order in which they were added. The

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

CS6202 - PROGRAMMING & DATA STRUCTURES I Unit IV Part - A 1. Define Stack. A stack is an ordered list in which all insertions and deletions are made at one end, called the top. It is an abstract data type

More information

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) Unit - 2 By: Gurpreet Singh Dean Academics & H.O.D. (C.S.E. / I.T.) Yamuna Institute of Engineering & Technology, Gadholi What is a Stack? A stack is a

More information

STACKS AND QUEUES. Problem Solving with Computers-II

STACKS AND QUEUES. Problem Solving with Computers-II STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()

More information

Queue Definition. Ordered list with property:

Queue Definition. Ordered list with property: Queue Definition Ordered list with property: All insertions take place at one end (tail) All deletions take place at other end (head) Queue: Q = (a 0, a 1,, a n-1 ) a0 is the front element, a n-1 is the

More information

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

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO) Outline stacks stack ADT method signatures array stack implementation linked stack implementation stack applications infix, prefix, and postfix expressions 1 Stacks stacks of dishes or trays in a cafeteria

More information

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

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. DATA STRUCUTRES A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. An algorithm, which is a finite sequence of instructions, each of which

More information

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4 Lists, Stacks and Queues in C CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4 Outline Structure Linked List Overview Implementation Stack Overview Implementation Queue Overview Implementation 2

More information

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion. 1. What is the full form of LIFO? The full form of LIFO is Last In First Out. 2. Give some examples for stack application. The examples of stack application are reverse a string, post fix evaluation, infix

More information

[CS302-Data Structures] Homework 2: Stacks

[CS302-Data Structures] Homework 2: Stacks [CS302-Data Structures] Homework 2: Stacks Instructor: Kostas Alexis Teaching Assistants: Shehryar Khattak, Mustafa Solmaz, Bishal Sainju Fall 2018 Semester Section 1. Stack ADT Overview wrt Provided Code

More information

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

Formal Languages and Automata Theory, SS Project (due Week 14) Formal Languages and Automata Theory, SS 2018. Project (due Week 14) 1 Preliminaries The objective is to implement an algorithm for the evaluation of an arithmetic expression. As input, we have a string

More information

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University Introduction to Computer and Program Design 2 Lesson 6 Stacks James C.C. Cheng Department of Computer Science National Chiao Tung University Introduction Stack A data collection, a grouping of data items

More information

Chapter 2 Arrays and Structures

Chapter 2 Arrays and Structures Data Structure Chapter 2 Arrays and Structures Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 2 Spring Outline Array Structures Polynomial Sparse

More information

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

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 IV. Stacks 1 A. Introduction 1. Consider the problems on pp. 170-1 (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses checker () Calculate and display base-two

More information

C Structures, Unions, Bit Manipulations, and Enumerations

C Structures, Unions, Bit Manipulations, and Enumerations C Structures, Unions, Bit Manipulations, and Enumerations Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 10.2 Structure Definitions 10.4

More information

Abstract Data Type: Stack

Abstract Data Type: Stack Abstract Data Type: Stack Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations

More information

UNIT VI. STACKS AND QUEUES

UNIT VI. STACKS AND QUEUES UNIT VI. STACKS AND QUEUES Syllabus: /*-------------------------------------------------------------------------*/ Stack: Definition: "Stack is an ordered collection of data elements having similar data

More information

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK Contents: Definition and Examples Representing stacks in C Example: infix, prefix, and postfix Exercises THE STACK Definition and Examples A stack is an ordered collection of items into which new items

More information

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by

More information

Stacks and Their Applications

Stacks and Their Applications Chapter 5 Stacks and Their Applications We have been discussing general list structures. In practice, we often work with some restricted cases, in which insertions and/or deletions occur only at one or

More information

Discussion of project # 1 permutations

Discussion of project # 1 permutations Discussion of project # 1 permutations Algorithm and examples. The main function for generating permutations is buildp. We don t need a separate class for permutations since the permutations objects are

More information

1. Stack Implementation Using 1D Array

1. Stack Implementation Using 1D Array Lecture 5 Stacks 1 Lecture Content 1. Stack Implementation Using 1D Array 2. Stack Implementation Using Singly Linked List 3. Applications of Stack 3.1 Infix and Postfix Arithmetic Expressions 3.2 Evaluate

More information

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Chapter 2. Stack & Queues.   M hiwa ahmad aziz . Chapter 2 Stack & Queues www.raparinweb.com M hiwa ahmad aziz 1 Stack A stack structure with a series of data elements that Allows access to only the last item inserted. An item is inserted or removed

More information

S.E. Sem. III [CMPN] Data Structures. Primitive Linear Non Linear

S.E. Sem. III [CMPN] Data Structures. Primitive Linear Non Linear S.E. Sem. III [CMPN] Data Structures Time : 3 Hrs.] Prelim Paper Solution [Marks : 80 Q.1(a) Explain different types of data structures with examples. [5] Ans.: Types of Data Structure : Data Structures

More information

UNIT-2 Stack & Queue

UNIT-2 Stack & Queue UNIT-2 Stack & Queue 59 13. Stack A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards

More information

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

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)

More information

CSE 401 Midterm Exam 11/5/10

CSE 401 Midterm Exam 11/5/10 Name There are 5 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed books, closed notes, closed

More information

Stack and Its Implementation

Stack and Its Implementation Stack and Its Implementation Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1 Definition of Stack Usage of Stack Outline

More information

Lab 7 1 Due Thu., 6 Apr. 2017

Lab 7 1 Due Thu., 6 Apr. 2017 Lab 7 1 Due Thu., 6 Apr. 2017 CMPSC 112 Introduction to Computer Science II (Spring 2017) Prof. John Wenskovitch http://cs.allegheny.edu/~jwenskovitch/teaching/cmpsc112 Lab 7 - Using Stacks to Create a

More information

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

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015 Stack Applications Lecture 25 Sections 18.7-18.8 Robb T. Koether Hampden-Sydney College Mon, Mar 30, 2015 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 1 / 34 1 The Triangle

More information

CSE 230 Intermediate Programming in C and C++

CSE 230 Intermediate Programming in C and C++ CSE 230 Intermediate Programming in C and C++ Structures and List Processing Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Self-referential Structure

More information

Graphs: Graph Data Structure:

Graphs: Graph Data Structure: Graphs: A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. The interconnected objects are represented by points termed as vertices, and the links

More information

Bharati Vidyapeeth s College Of Engineering for Women Pune-43 Department E & TC. SE- Unit Test I Subject-DS

Bharati Vidyapeeth s College Of Engineering for Women Pune-43 Department E & TC. SE- Unit Test I Subject-DS Bharati Vidyapeeth s College Of Engineering for Women Pune-43 SE- Unit Test I Subject-DS Date: 25/02/2010 Q-1 a) What is sorting? State different types of sorting and write a function in C to implement

More information

Information Science 2

Information Science 2 Information Science 2 - Basic Data Structures- Week 02 College of Information Science and Engineering Ritsumeikan University Today s class outline l Basic data structures: Definitions and implementation

More information

Stacks. Revised based on textbook author s notes.

Stacks. Revised based on textbook author s notes. Stacks Revised based on textbook author s notes. Stacks A restricted access container that stores a linear collection. Very common for solving problems in computer science. Provides a last-in first-out

More information

BSc.(Hons.) Business Information Systems, BSc. (Hons.) Computer Science with Network Security, BSc.(Hons.) Software Engineering

BSc.(Hons.) Business Information Systems, BSc. (Hons.) Computer Science with Network Security, BSc.(Hons.) Software Engineering BSc.(Hons.) Business Information Systems, BSc. (Hons.) Computer Science with Network Security, BSc.(Hons.) Software Engineering Cohorts BIS/04 Full Time - BCNS/04 Full Time SE/04 Full Time Examinations

More information

Application of Stack (Backtracking)

Application of Stack (Backtracking) Application of Stack (Backtracking) Think of a labyrinth or maze How do you find a way from an entrance to an exit? Once you reach a dead end, you must backtrack. But backtrack to where? to the previous

More information

CS 8391 DATA STRUCTURES

CS 8391 DATA STRUCTURES DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK CS 8391 DATA STRUCTURES UNIT- I PART A 1. Define: data structure. A data structure is a way of storing and organizing data in the memory for

More information

Stack Abstract Data Type

Stack Abstract Data Type Stacks Chapter 5 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty To understand how Java implements a stack To learn how to implement a

More information

CSE 214 Computer Science II Stack

CSE 214 Computer Science II Stack CSE 214 Computer Science II Stack Spring 2018 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Random and Sequential Access Random

More information

Your Topic Goes Here Queue

Your Topic Goes Here Queue Your Subtopics Go Here Your Topic Goes Here Queue Compiled By: Kiran Joshi 1 Queue Roadmap. 1. Definition 2. Queue examples 3. Working of Queue 4. Applications 2 Definition * A data structure of ordered

More information

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stacks, Queues and Hierarchical Collections. 2501ICT Logan Stacks, Queues and Hierarchical Collections 2501ICT Logan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Queues and Stacks Queues and Stacks

More information

DATA STRUCTURE UNIT I

DATA STRUCTURE UNIT I DATA STRUCTURE UNIT I 1. What is Data Structure? A data structure is a mathematical or logical way of organizing data in the memory that consider not only the items stored but also the relationship to

More information