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

Similar documents
CSE 143. Lecture 4: Stacks and Queues

Building Java Programs

Stacks. Revised based on textbook author s notes.

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.

1. Stack Implementation Using 1D Array

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Stack Abstract Data Type

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

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

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

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

Foundations of Data Structures

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CMPT 225. Lecture 9 Stack

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

Lab 7 1 Due Thu., 6 Apr. 2017

Stack and Its Implementation

IT 4043 Data Structures and Algorithms

PA3 Design Specification

Stacks Fall 2018 Margaret Reid-Miller

Largest Online Community of VU Students

CS 206 Introduction to Computer Science II

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

csci 210: Data Structures Stacks and Queues

[CS302-Data Structures] Homework 2: Stacks

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

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

CSE 143 SAMPLE MIDTERM

Data Abstraction and Specification of ADTs

CSC148 Week 2. Larry Zhang

Lecture No.04. Data Structures

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Data Structure (CS301)

File Input and Output

Problem with Scanning an Infix Expression

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

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

CSC 273 Data Structures

CS 106B Lecture 5: Stacks and Queues

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

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

Linear Data Structure

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

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

More Data Structures (Part 1) Stacks

CS 206 Introduction to Computer Science II

Top of the Stack. Stack ADT

MIDTERM WEEK - 9. Question 1 : Implement a MyQueue class which implements a queue using two stacks.

Stack Implementation

Lecture 2: Implementing ADTs

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Basic Data Structures

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Basic Data Structures 1 / 24

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Lecture 12 ADTs and Stacks

Application of Stack (Backtracking)

Stacks and Queues

Stacks and Queues. Chapter Stacks

Lecture 3: Stacks & Queues

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

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

CSE 143 SAMPLE MIDTERM SOLUTION

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

Data Structures and Algorithms

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

ADTs Stack and Queue. Outline

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

Assignment 5. Introduction

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

Queues. Stacks and Queues

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

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

Recursive Data Structures and Grammars

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

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

ITI Introduction to Computing II

Lecture Data Structure Stack

Data Types & Variables

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

CSC148H Week 3. Sadia Sharmin. May 24, /20

Top of the Stack. Stack ADT

UNIT VI. STACKS AND QUEUES

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Lists are great, but. Stacks 2

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

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

What is an algorithm?

CISC-235. At this point we fnally turned our atention to a data structure: the stack

Stacks, Queues (cont d)

Name CPTR246 Spring '17 (100 total points) Exam 3

ITI Introduction to Computing II

Lecture 2: Stacks and Queues

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

12 Abstract Data Types

STACKS AND QUEUES. Problem Solving with Computers-II

Stacks and Their Applications

Transcription:

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 Searching Algorithms Analysis 2

STACK retrieves elements in reverse order as added 3

Stacks stack: A collection based on the principle of adding elements and retrieving them in the opposite order. Last-In, First-Out ("LIFO") Elements are stored in order of insertion. We do not think of them as having indexes. Client can only add/remove/examine the last element added (the "top"). push pop, peek basic stack operations: push: Add an element to the top. pop: Remove the top element. peek: Examine the top element. top 3 2 bottom 1 stack

Stacks in computer science Programming languages and compilers: method calls are placed onto a stack (call=push, return=pop) compilers use stacks to evaluate expressions Matching up related pairs of things: find out whether a string is a palindrome examine a file to see if its braces { } match convert "infix" expressions to pre/postfix method3 method2 method1 return var local vars parameters return var local vars parameters return var local vars parameters Sophisticated algorithms: searching through a maze with "backtracking" many programs use an "undo stack" of previous operations

Class Stack Stack() constructs a new stack with elements push(value) places given value on top of stack pop() peek() size() isempty() removes top value from stack and returns it; throws EmptyStackException if stack is empty returns top value from stack without removing it; throws EmptyStackException if stack is empty returns number of elements in stack returns true if stack has no elements

Stack Operations 7

Array Based Stack 6 1 9 8

Stack Class 9

Stack Class 10

Stack Class 11

Stack limitations/idioms You cannot loop over a stack in the usual way. Stack s = new Stack(10);... for (int i = 0; i < s.size(); i++) { do something with s.get(i); } Instead, you pull elements out of the stack one at a time. common idiom: Pop each element until the stack is empty. // process (and destroy) an entire stack while (!s.isempty()) { do something with s.pop(); }

Application 13

Example 1: Binary Conversion Write a Java Program to read decimal number and convert it into binary. (Use Stack and print the binary value ) 14

Model Answer 15

Different types of Stack Stack can be implement for different data types Change the type Int, float, char, double etc 16

Object Stack Stack can also implement for a class object Date Item Student Re implement all the methods 17

Example 2: Reversing (palindromic) A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. Like 16461 Write a Java program to check given number is palindrome or NOT. Create new Stack class (char Stack) and improve your program to check given word is palindrome or NOT. 18

Example 3: Delimiter Matching The delimiters are the braces { and }, brackets [ and ], and parentheses ( and ). Each opening or left delimiter should be matched by a closing or right delimiter; that is, every { should be followed by a matching } and so on. Example c[d] // correct a{b[c]d}e // correct a{b(c]d}e // not correct; ] doesn t match a[b{c}d]e} // not correct; nothing matches final a{b(c) // not correct; nothing matches opening { 19

Delimiter Matching Let s see what happens on the stack for a typical correct string: a{b(c[d]e)f} 20

Answer 21

Built-in Stack class 22

Stack Example (String) 23

Sample Questions a) Discuss the Abstract Data Type with considering a Stack b) What are the practical examples for the usage of Stack, and Leaner List. c) Write suitable C++ code (functions) to implement the following stack Operations (Use Array based stack to represent integer values ) Stack() Puch(value) Pop() isempty() isfull() [5 marks] Use the above Stack class and write a C++ program to read decimal number and convert it into Octal. 24