LIFO : Last In First Out

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

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Lecture Data Structure Stack

DEEPIKA KAMBOJ UNIT 2. What is Stack?


csci 210: Data Structures Stacks and Queues

1 P age DS & OOPS / UNIT II

Stack and Queue. Stack:

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

.:: UNIT 4 ::. STACK AND QUEUE

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Data structure and algorithm in Python

Week 6. Data structures

Lecture 3: Stacks & Queues

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

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

data structures and algorithms lecture 6

UNIT-2 Stack & Queue

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

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

Data Structure. Recitation VII

Data Abstraction and Specification of ADTs

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

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

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Chapter 18: Stacks And Queues

Queue: Queue Representation: Basic Operations:

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

infix expressions (review)

15. Stacks and Queues

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Stacks and queues (chapters 6.6, 15.1, 15.5)

Introduction to Data Structures and Algorithms

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

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

Queue. Data Structure

Chapter 18: Stacks And Queues

STACKS AND QUEUES. Problem Solving with Computers-II

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

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

CMSC Introduction to Algorithms Spring 2012 Lecture 7

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CMSC 132: Object-Oriented Programming II. Stack and Queue

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

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

What is an algorithm?

Textbook chapter 10. Abstract data structures are. In this section, we will talk about. The array The stack Arithmetic using a stack

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

ADTs: Stacks and Queues

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

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

Associate Professor Dr. Raed Ibraheem Hamed

Linear Data Structures

Stacks and Queues. CSE 373 Data Structures Lecture 6

Postfix (and prefix) notation

ITI Introduction to Computing II

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Week 4 Stacks & Queues

CSC 1052 Algorithms & Data Structures II: Queues

Lecture 4 Stack and Queue

// The next 4 functions return true on success, false on failure

Queues Fall 2018 Margaret Reid-Miller

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

Apply to be a Meiklejohn! tinyurl.com/meikapply

CSC148 Week 2. Larry Zhang

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

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

Stacks, Queues (cont d)

12 Abstract Data Types

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Ashish Gupta, Data JUET, Guna

CS 206 Introduction to Computer Science II

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Chapter 2. Stack & Queues. M hiwa ahmad aziz

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

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

Data Structures. Lecture 5 : The Queues

BBM 201 DATA STRUCTURES

Information Science 2

Stacks and Queues. CSE Data Structures April 12, 2002

Foundations of Data Structures

CE204 Data Structures and Algorithms Part 2

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

! A data type for which: ! An ADT may be implemented using various. ! Examples:

8. Fundamental Data Structures

Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall Topic Notes: Linear Structures

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

Sequence Abstract Data Type

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

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

Motivation for Queues

Queue. COMP 250 Fall queues Sept. 23, 2016

Problem with Scanning an Infix Expression

DATA STRUCTURES: OVERVIEW AND ITS APPLICATIONS

CSE 143 SAMPLE MIDTERM

CSE 143. Lecture 4: Stacks and Queues

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Transcription:

Introduction Stack is an ordered list in which all insertions and deletions are made at one end, called the top. Stack is a data structure that is particularly useful in applications involving reversing. LIFO : Last In First Out Stack Implementation Stack can be implemented in two different ways: 1. Contiguous stack: the stack is implemented as an array. 2. Linked stack: pointers and dynamic memory allocation is used to implement the stack. Stack operations push(e): Insert element e, to be the top of the stack. pop(): Remove from the stack and return the top element on the stack; an error occurs if the stack is empty. size(): Return the number of elements in the stack. isempty(): Return a Boolean indicating if the stack is empty. top(): Return the top element in the stack, without removing it; an error occurs if the stack is empty. Initialise: creates/initialises the stack Initialise Creates the structure i.e. ensures that the structure exists but contains no elements e.g. Initialise(S) creates a new empty stack named S push e.g. Push(X,S) adds the value X to the TOP of stack S 5

pop e.g. Pop(S) removes the TOP node and returns its value Example We could try the same example with actual values for A, B and C. A = 1 B = 2 C = 3 EXERCISE: Stack operations What would the state of a stack be after the following operations: create stack push A onto stack push F onto stack push X onto stack pop item from stack push B onto stack pop item from stack pop item from stack Static and dynamic data structures A stack can be stored in: a static data structure OR a dynamic data structure 6

Static data structures These define collections of data which are fixed in size when the program is compiled. An array is a static data structure. Dynamic data structures These define collections of data which are variable in size and structure. They are created as the program executes, and grow and shrink to accommodate the data being stored like linked list. A Simple Array-Based Stack Implementation We can implement a stack by storing its elements in an array. Specifically, the stack in this implementation consists of an N element array S plus an integer variable t that gives the index of the top element in array S. (See Figure 2&3) Figure 1: Implementing a stack with an array S. The top element in the stack is stored in the cell S[t]. Recalling that arrays start at index 0 in Java, we initialize t to 1, and we use this value for t to identify an empty stack. Likewise, we can use t to determine the number of elements (t + 1). We also introduce a new exception, called FullStackException, to signal the error that arises if we try to insert a new element into a full array.. We give the details of the array-based stack implementation in Code Fragment 1. Code Fragment 1: Implementing a stack using an array of a given size, N. The main Algorithms in Stack: 7

Figure 2: Stack Operation (a) Stack is empty. (b) Push the first entry. (c) n items on the stack. Figure 3. Representation of data in a contiguous stack 8

A Drawback with the Array-Based Stack Implementation The array implementation of a stack is simple and efficient. Nevertheless, this implementation has one negative aspect it must assume a fixed upper bound, CAPACITY, on the ultimate size of the stack. In Code may be you chose 100 (the capacity value) more or less arbitrarily. An application may actually need much less space than this, which would waste memory. Examples of applications Stack: Example 1: Stack of books Example 2: Towers of Hanoi Example 3: Internet Web browsers store the addresses of recently visited sites on a stack. Each time a user visits a new site, that site's address is "pushed" onto the stack of addresses. The browser then allows the user to "pop" back to previously visited sites using the "back" button. Example 4: Text editors usually provide an "undo" mechanism that cancels recent editing operations and reverts to former states of a document. This undo operation can be accomplished by keeping text changes in a stack. Example 5: as processor executes a program, when a function call is made, the called function must know how to return back to the program, so the current address of program execution is pushed onto a stack. Once the function is finished, the address that was saved is removed from the stack, and execution of the program resumes. If a series of function calls occur, the successive return values are pushed onto the stack in LIFO order so that each function can return back to calling program. Stacks support recursive function calls in the same manner as conventional nonrecursive calls. Homework : Example: Reversing a Word by using a stack, we ll examine a very simple task: reversing a word. When you run the program, it asks you to type in a word. When you press Enter, it displays the word with the letters in reverse order. A stack is used to reverse the letters. First, the characters are extracted one by one from the input string and pushed onto the stack. Then they re popped off the stack and displayed. Because of its Last-In-First-Out characteristic, the stack reverses the order of the characters. 9

Reverse Word Write a program in Java language to implement this example. Queues The word queue is British for line (the kind you wait in). In Britain, to queue up means to get in line. In computer science a queue is a data structure that is somewhat like a stack, except that in a queue the first item inserted is the first to be removed (First-In-First-Out, FIFO), while in a stack, as we ve seen, the last item inserted is the first to be removed (LIFO). A queue works like the line at the movies: The first person to join the rear of the line is the first person to reach the front of the line and buy a ticket. The last person to line up is the last person to buy a ticket (or if the show is sold out to fail to buy a ticket). Figure 4 shows how such a queue looks. Figure 4: A queue of people. Examples of applications Queue: 1. Queue used to model real-world situations such as people waiting in line at a bank, airplanes waiting to take off, or data packets waiting to be transmitted over the Internet. 2. There are various queues quietly doing their job in your computer s (or the network s) operating system. 3. There s a printer queue where print jobs wait for the printer to be available. Also there are several possible applications for queues. 4. Stores, reservation centers, and other similar services typically process customer requests according to the FIFO principle. A queue would therefore be a logical choice for a data structure to handle transaction processing for such applications. For example, it would be a natural choice for handling calls to the reservation center of an airline. 10

The Queue Abstract Data Type Formally, the queue abstract data type defines a collection that keeps objects in a sequence, where element access and deletion are restricted to the first element in the sequence, which is called the front of the queue, and element insertion is restricted to the end of the sequence, which is called the rear of the queue. This restriction enforces the rule that items are inserted and deleted in a queue according to the first-in first-out (FIFO) principle. The queue abstract data type (ADT) supports the following two fundamental methods: enqueue(e): Insert element e at the rear of the queue. dequeue(): Remove and return from the queue the object at the front; an error occurs if the queue is empty. Additionally, similar to the case with the Stack ADT, the queue ADT includes the following supporting methods: size(): Return the number of objects in the queue. isempty(): Return a Boolean value that indicates whether the queue is empty. front(): Return, but do not remove, the front object in the queue; an error occurs if the queue is empty. Figure 5: Operations of the Queue enqueue(e): Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE. ITEM is the value to be inserted. 1. If (REAR == N) Then [Check for overflow] 2. Print: Overflow 3. Else 4. If (FRONT and REAR == 0) Then [Check if QUEUE is empty] (a) Set FRONT = 1 (b) Set REAR = 1 11

5. Else 6. Set REAR = REAR + 1 [Increment REAR by 1] [End of Step 4 If] 7. QUEUE[REAR] = ITEM 8. Print: ITEM inserted [End of Step 1 If] 9. Exit dequeue(): Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE. 1. If (FRONT == 0) Then [Check for underflow] 2. Print: Underflow 3. Else 4. ITEM = QUEUE[FRONT] 5. If (FRONT == REAR) Then [Check if only one element is left] (a) Set FRONT = 0 (b) Set REAR = 0 6. Else 7. Set FRONT = FRONT + 1 [Increment FRONT by 1] [End of Step 5 If] 8. Print: ITEM deleted [End of Step 1 If] 9. Exit Figure 6: A Queue with some items removed When you insert a new item in the Queue, the Front arrow moves upward, when you remove an item, Rear also moves upward. The trouble with this arrangement is that pretty soon the rear of the queue is at the end of the array (the highest index). Even if there are empty cells at the beginning of the array, because 12

you ve removed them with F, you still can t insert a new item because Rear can t go any further. Or can it? This situation is shown in Figure 7. Figure 7. Rear arrow at the end of the array. To avoid the problem of not being able to insert more items into the queue even when it s not full, the Front and Rear arrows wrap around to the beginning of the array. The results a circular queue (sometimes called a ring buffer). Insert enough items to bring the Rear arrow to the top of the array (index 9). Remove some items from the front of the array. Now insert another item. You ll see the Rear arrow wrap around from index 9 to index 0; the new item will be inserted there. This situation is shown in Figure 8. Figure 8. Rear arrow wraps around 13