Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Universidad Carlos III de Madrid Algorithms and Data Structures (ADS) Bachelor in Informatics Engineering Computer Science Department Lists, Stacks and Queues. Authors: Isabel Segura Bedmar April 2011 Departamento de Informtica, Laboratorio de Bases de Datos Avanzadas (LaBDA)

2 2

3 Lists, Stacks and Queues. Most of the information has been sourced from the books [1, 2]. Lists A list is a collection of n elements stored in a linear order. The most common way for storing lists is using an array data structure. Each element in a list can be refered by an index in the range [0,n 1] indicating the number of elements that preced e in the list. This representation provides that all operations performed on a given element take O(1) time. An array stores all elements of the list contiguously in memory and requires to initially know the maximum size of the list. This may produce an unnecessary waste of memory and other cases insufficient memory. A possible solution is to use a dynamic array (java.util.arraylist) that is able to be reallocated when the space reserved for the dynamic array is exceeded. Unfortunately, this reallocating of the elements of a dynamic array is a very expensive operation. 1 A linked list is a data structure that consists of a sequence of nodes such that each node contains a reference to the next node in the list. This representation does not require that elements of the list are stored contiguously in memory. Also, it just uses the space actually needed to store the elements of the list. On the other hand, linked lists do not mantain index numbers for the nodes and allow only sequential access to elements, while arrays allow constant-time random access. As it will be shown in following sections, the linked list data structure allows us us to implement some important abstract data structures such as stacks, queues, deques. An implementation of a linked list may include the following methods: isempty(): test whether or not a list is empty. size(): return the number of elements of the list. first(): return the first element of the list. An error occurs if the list is empty. 1 Most of the information has been sourced from the books [1, 2]. 3

6 6 Figure 3: We define the head of the list as a Node. The constructor sets this node to null. Figure 4: Insertion of an element at the head of a singly linked list. Now, it is your turn. Please, write a new method called inserthead in the SinglyLinkedList class that inserts a new element at the head of the list. Figure?? shows the implementation of this method. To insert an element at the end of the list is very easy if you add a reference to the tail node, that is, an instance variable (with type Node E ) to store the reference the last node in the list. For example, imagine that I like The Simpson, but I like it than less House, so I should insert it at the end of the list. I must follow the following steps: 1. Create a new node with the element The Simpson and its next reference

7 7 Figure 5: This method implements the insertion operation of an element at the beginning of a singly linked list. sets to null because this node will be the last node. 2. Then, the tail reference itself to this new node. Figure 6: Insertion of an element at the end of a singly linked list. The class must have a property to store the tail of the list. Figure 6 shows the above example. Please, try yourself defining the Node tail in the SinglyLinkedList class and adding the method addlast. You can find the solution in this new implementation in Figure reffig:addlastslist. Now, take few minutes and think about how you can insert an element at the end of the list when you do not keep the tail reference in your implementation of thesinglylinkedlist. Youshouldexaminethelistuntilyoufindnodewithanext

8 8 Figure 7: Implementation of the insertion operation of an element at the end of a singly linked list. reference to null. Do you dare to do it?. You can find a possible implementation in Figure 8. How can you remove an element in a singly linked list Now, let me show you how to remove at the head of the list. It is very easy!!!. You only need to set the head reference to its next node. This operation is illustrated in Figure 9 and its implementation is shown in Figure 10. In order to remove the last node or an node at a give position of the list, we must access its previous node. Thus, the only way to find this previous node is to traverse the list from its beginning until to find it. Traversing the list may involve a big number of operations, taking a long time when the size of the list

9 9 Figure 8: Implementation of the insertion operation of an element at the end of a singly linked list without keeping the tail reference. Figure 9: Removal of the head of the list. is big. The following section presents an effective solution for this problem.

10 10 Figure 10: This method removes the first element of the list.

12 12 Figure 12: Implementation of a node for a doubly linked list. Figure 13: Inserting an element at the start of the list. of the list or removing the last element of the list is very easy. Likewise, it is very easy to implement the methods addlast() and removefirst(). Do you dare

13 13 Figure 14: Removing an element from the end of the list. Figure 15: Partial Implementation of a doubly linked list including the definition of the sentinel nodes header and tailer, the constructor method and the addfirst() and removelast() methods. to implement them?. You can find the implementation of these operation in Figure 16

15 15 Figure 17: To add a new node after the. Figure 18: Methods addbefore, addafter, remove for a doubly linked list. off them from the top of the stack. This is just the LIFO principle. Formally, a stack is an abstract data structure that is characterized by the following operations: push(e): add the element e to the top of the stack. pop(): remove the top element from the stack and return it. Other additionally operations are: size(): return the size of the stack. isemtpy(): return true if the stack is empty; false eoc. top(): return the top element in the stack, without removing it.

16 16 Figure 19: Stack of plates. Figure 20: Push and pop operations. Operation Stack Output push( h ) (h) - push( e ) (h,e) - top() (h,e) e push( l ) (h,e,l) - push( l ) (h,e,l,l) - push( o ) (h,e,l,l,o) - top() (h,e,l,l,o) o push(! ) (h,e,l,l,o,!) - top() (h,e,l,l,o,!)! size() (h,e,l,l,o,!) 6 isempty() (h,e,l,l,o,!) false pop() (h,e,l,l,o)! Table 1: This table shows a secuence of operations on a stack of characters This data structure is very useful for many applications which require to store the secuence of operations in order to reverse or undo them. For example, web browsers store the urls recently visited on a stack in order to allow users to visit the previously urls by pushing the back button. Likewise, stacks can be used to provide an undo mechanism to the text editors.

17 17 The java.util package already includes an implementation of the stack data structure due to its importance. It is recommended to use the java.util.stack class, however in this section we design and implement ourselves a stack. First of all, we define an interface to declare the methods of the data structure (see Figure 21). You can note that this interface has been defined using the generic parameterized type E, which allows to store elements of any specified class. Also, we have defined the EmptyStackException class that will throw an exception when the methods pop() and top() are called on an empty stack. Figure 21: Interface Stack. This interface uses the generic parameterized type E to contain elements of any specified class in the stack. There are several ways to implement the Stack class. A simple way to represent a stack is to store its elements into an array. Thus, the stack consists of an array and an integer variable to indicate the index of the top element. Figure 23 shows a java class implementing an array-based stack. This class implements the interface Stack<E>. This implementation is based on the use of an array (for storing the elements of a stack. The instance variable top stores theindexinwhichthetopofthestack isstoredinthearray. Also, themaximum size of the array is defined in a constant MAXCAPACITY. Another instance variable (capacity) stores the actual capacity of the stack. The main drawback of this implementation is that it is necessary to initially know the maximum size of the stack. Thus, we have defined the FullStackException class that will throw an exception when the push method is called on a full stack (see Figure 22) Now, you must write the code to build a stack containing the operations shown in Table 1. Table 2 summarizes the computational complexity for each

18 18 Figure22: Anexceptionisthrownwhenthemethodspopandtopareperformed on an empty stack. method of the array-based implementation of a stack. The methods size() and isempty take O(1) time because they only access the instance variable top. The methods top() and pop() take constant time because they call the method isempty() and access the top element in the array (the method pop() also decreases the instance variable top). All of the previous operations take O(1). Likewise the method push() also takes O(1). Methods Time isemtpy(), size() O(1) top(), pop() O(1) push() O(1) Table 2: Performance of an array-based implementation of a stack Therefore, the array-based implementation is simple and efficient. However, the main drawback of this implementation is that it is necessary to know the maximum size of the stack. In some cases, this may cause an unnecessary waste of memory or an exception when the stack reaches this maximum size and it is not possible to add store elements. Another implementation that does not have the size limitation is to use a linked list to represent a stack. That is, the elements of a stack are stored into the nodes of a linked list. Of course, we may use the java.util.arraylist class or any API java class implementing lists to represent the stack, however, we provide ourselves implementation of a linked list in order to improve your knowledge and practice about linked lists. First of all, we define a java class to implement a generic node for a singly linked list (see Figure 25). Figure 26 shows the code of the linked list-based implementation. This class defines the instace variable top that stores the top element of the stack. We have decided that the top of the

19 19 Figure 23: An array-based implementation of a stack stack is stored at the head of the linked list. This fact allows to the operations pop(), top() and push() take constant time (see Table 3) because they only need to access the first element (head) of the list. If the top element of the stack was stored at the end of the list, then it would be necessary to trasverse all elements of the list, every time you would need to access the top element. Thus, the previous methods would take O(n) time. We also note that the method push() does not throw an exception related to the size overflow problem since in this implementation the size of the stack is not limited.

20 20 Figure 24: An exception is thrown when the method push is performed on a full stack. Figure 25: A java class for implementing a node of a generic singly linked list. Stacks have many interesting applications. You can find some applications such as reversing arrays or matching parentheses and HTML tags in in Chapter 5 (Stacks and Queues) (pages ) in the book [1].

21 21 Figure 26: A Linked-list based implementation of a stack. Queues Another important linear data structure is the queue. A queue is a collection of objects that are managed according to the the First-In First-out (FIFO) principle (see Figure 27), that is, only element at the front of the queue can be accessed or deleted and new elements must added at the end (rear) of the queue. In order to understand better this principle, think about a line of people

22 22 Methods Time isemtpy(), size() O(1) top(), pop() O(1) push() O(1) Table 3: Running times of the array-based implementation of a stack waiting a bus. Normally, the first person in the line will be the first one on getting onto the bus. If one arrives last, this should put oneself at the rear of the line. Queues are a nature option of many applications that requiere to process their requests according to FIFO principle, such as reservation systems for airlines, cinemas or many other public services. Figure 27: Representation of a FIFO Queue. Formally, a queue is an abstract data structure that is characterized by the following operations: enqueue(): add a element at the rear (end) of the queue. dequeue(): return and remove the first element of the queue, that is, the element at the front of the queue. If this one is empty, then this method should throw an exception. front(): return the element at the front of the queue. If this one is empty, then this method should throw an exception. In addition, similar to the Stack ADT, the queue ADT can also include the size() and isempty() methods. Figure 28 shows a java interface for this ADT. It uses the generic parameterized type E, which allows to store elements of any specified class. Also, the

23 23 Operation Queue Output enqueue( h ) (h) - enqueue( e ) (h,e) - front() (h,e) h dequeue() (e) h dequeue() () e enqueue( h ) (h) - enqueue( o ) (h,o) - enqueue( l ) (h,o,l) - front() (h,o,l) h enqueue( l ) (h,o,l,l) - front() (h,o,l,l) h enqueue( a ) (h,o,l,l,a) - size (h,o,l,l,a) 6 enqueue(! ) (h,o,l,l,a,!) - size (h,o,l,l,a,!) 7 front() (h,o,l,l,a,!) h dequeue() (o,l,l,a,!) h Table 4: A secuence of operations on a queue of characters EmptyQueueException class has been defined to throw an exception when the methods dequeue() and front() are called on an empty queue. A circular array-based implementation of a queue Likewise with the Stack ADT, we can use an array to represent a queue. Thus, elements of a queue are stored in an array. What is the more efficient option for storing the front of the queue: 1. at the first position of the array(that is, Array[0]) and adding the following elements from there. 2. as the last element of the array, that is, a new element is always inserted at the first position of the array and the The former one is not an efficient solution because each time the method dequeue() is called, all elements must moved to its previous cell, taking O(n) time. The second one is also an inefficient solution since each time a new element will be inserted (enqueue()), elements in the array must be moved to their following position, taking O(n) time. In order to achieve constant time for the methods of the Queue interface, we can use a circular array to store the elements and two instance variables front and rear to keep the index storing the first element of the queue and the index to store a new element. Each time we remove the first element of the queue, we should increase the variable front. Likewise, each time we add a new element,

24 24 Figure 28: A java interface for the Queue ADT. Figure 29: An exception is thrown when the methods dequeue() and front() are performed on an empty queue. we store it into the position rear at the array and increase the value of this variable. Figure 30 shows three different configuration of a queue implemented using a circular array. The first case (front rear length(array)) is the normal configuration. The second and thirds examples illustrate the configuration in which the rear reaches the lenght of the array and it is necessary to store a new element at the first position of the array. When rear reaches front, it implies that the queue is empty. Each time we need to increase the rear or front, we must estimate their the module value

25 25 Figure 30: Three different configurations of a queue. Each method in this implementation takes O(1) since they only involve a constant number of arithmetic opreations, comparisons and assignments. The only drawback of this implementation is that the size of the queueu is limited to the size of the array. However, if we are able to provide a good estimation of the size of the queue, this implementation is very efficient. Figure 31 shows a circular array-based implementation of a queue. A linked list-based implementation of a queue A linked list also provides an efficient implementation of a queue (see Figure 32). For efficiency reasons, the front of the queue is stored at the first node of the list and we also define a variable to store the tail of the list. These two variables allow all methods take O(1) time because they only need a constant number of simple statements. The main advantage of this implementation compared to the array-based implementation is that it is no necessary to specify a maximum size for the queue. Please, write a java program to assign the turn to every journalist in the TV program 59 seconds. You can find an interesting problem based on the use of a queue in in Chapter 5 (Stacks and Queues) (pages 212) in the book [1]. Double-Ended Queues (Dqueues) Figure 33 compares the three ADT: stack(lifo: last in, first out), queue(fifo: first in, first out) and dqueue. A double-ended queue ADT (dqueue is pronunced like deck) is power than stack and queue because it supports insertion and deletion at both its front and its rear. The main methods of the dqueue ADT are: addfirst(e): insert a new element at the head of the queue.

26 26 Figure 31: A circular array-based implementation of a queue. addlast(e): insert a new element at the end of the queue. removefirst(): remove the element at the front of the queue. If the queue is empty, then an exception is thrown. removelast(): remove the element at the end of the queue. If the queue is empty, then an exception is thrown. getfirst(): return the element at the head of the queue. If the queue is empty, then an exception is thrown. getlast(): return the element at the end of the queue. If the queue is empty, then an exception is thrown. size(): return the size of the queue. isempth(): return a boolean indicating if the queue is empty. The java.util.linkedlist E class already defines all the methods of a dequeue. Of course, you can use this class when you need to use a dequeue in your future applications. But first, you must learn how the dequeue ADT can be defined and implemented. Figure 34 contains a java interface for the dqueue

27 27 Figure 32: A Linked List-based implementation of a queue. Figure 33: The dequeu ADT a queue that allows invertion and deletion at both its front and its rear (source:

29 Figure 34: An interface for a double-ended queue ADT. 29

30 30 Figure 35: This class implements a node of a doubly linked list.

31 Figure 36: A doubly linked list class for implementing a deque. 31

32 32

33 Bibliography [1] M. Goodrich and R. Tamassia, Data structures and algorithms in Java. Wiley-India, [2] M. Weiss, Data structures and problem solving using Java. Addison Wesley Publishing Company,

### This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues 1. 2004 Goodrich, Tamassia

This lecture Abstract data types Stacks Queues ADTs, Stacks, Queues 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations

### csci 210: Data Structures Stacks and Queues

csci 210: Data Structures Stacks and Queues Summary Topics stacks and queues as abstract data types implementations arrays linked lists analysis and comparison application: searching with stacks and queues

### Stacks & Queues. Data structures and Algorithms

Stacks & Queues Data structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich, Tamassia and Mount (Wiley, 2004) Outline

### What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Stacks and Queues What is a Stack? Stores a set of elements in a particular order Accessed in Last-In In-First-Out (LIFO) fashion Real life examples: Pile of books PEZ dispenser Cup trays in cafeteria

### Chapter 7: Queues and Deques

Chapter 7: Queues and Deques After the stack, the next simplest data abstraction is the queue. As with the stack, the queue can be visualized with many examples you are already familiar with from everyday

### Lecture 4. The Java Collections Framework

Lecture 4. The Java s Framework Chapters 6.3-6.4-1 - Outline Introduction to the Java s Framework Iterators Interfaces Classes Classes - 2 - The Java s Framework We will consider the Java s Framework as

STACKS,QUEUES, AND LINKED LISTS Stacks Queues Linked Lists Double-Ended Queues Case Study: A Stock Analysis Applet 1 Stacks Astack is a container of objects that are inserted and removed according to the

Universidad Carlos III de Madrid Algorithms and Data Structures (ADS) Bachelor in Informatics Engineering Computer Science Department Binary Search Trees. Authors: Julian Moreno Schneider Isabel Segura-Bedmar

### Data Structures. Topic #4

Topic #4 Today s Agenda Stack and Queue ADTs What are they Like Ordered Lists, are position oriented Use of Data Structures for Stacks and Queues arrays (statically & dynamically allocated) linear linked

### Sequential Data Structures

Sequential Data Structures In this lecture we introduce the basic data structures for storing sequences of objects. These data structures are based on arrays and linked lists, which you met in first year

### Lecture 4. The Java Collections Framework

Lecture 4. The Java s Framework Chapters 6.3-6.4-1 - The Java s Framework We will consider the Java s Framework as a good example of how apply the principles of objectoriented software engineering (see

Arrays and Linked Lists Data Structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich, Tamassia and Mount (Wiley, 2004)

### Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Stacks (and Queues) 1 Stacks Stack: what is it? ADT Applications Implementation(s) 2 CSCU9A3 1 Stacks and ueues A stack is a very important data structure in computing science. A stack is a seuence of

### Algorithms and Data Structures

Basic Data Structures Page 1 BFH-TI: Softwareschule Schweiz Basic Data Structures Dr. CAS SD01 Basic Data Structures Page 2 Outline Data Structures and Abstract Data Types Linear Data Structures Implementing

### Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Outline Computer Science 1 Stacks Mike Jacobson Department of Computer Science University of Calgary Lecture #12 1 2 Applications Array-Based Linked List-Based 4 Additional Information Mike Jacobson (University

Linked Lists and Iterators Chapter 4 Click to proceed How To View This Presentation This presentation is arranged in outline format. To view the slides in proper order For each slide Read the entire slide

### 1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: 15.1-15.3. Data Structures

1.00 Lecture 35 Data Structures: Introduction Stacks, Queues Reading for next time: Big Java: 15.1-15.3 Data Structures Set of reusable classes used in algorithms, simulations, operating systems, applications

### Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Linked Lists, Stacks, Queues, Deques It s time for a chainge! Learning Goals After this unit, you should be able to... Differentiate an abstraction from an implementation. Define and give examples of problems

### Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Queues Outline and Required Reading: Queues ( 4. except 4..4) COSC, Fall 3, Section A Instructor: N. Vlajic Queue ADT Queue linear data structure organized according to first-in/first-out (FIFO) principle!

### Collections & Data Structures. CS 3: Computer Programming in Java

Collections & Data Structures CS 3: Computer Programming in Java Objectives The concept of a collection Separating the interface from the implementation Dynamic data structures Linked lists Queues and

### 7.1 Our Current Model

Chapter 7 The Stack In this chapter we examine what is arguably the most important abstract data type in computer science, the stack. We will see that the stack ADT and its implementation are very simple.

### Chapter 3: Restricted Structures Page 1

Chapter 3: Restricted Structures Page 1 1 2 3 4 5 6 7 8 9 10 Restricted Structures Chapter 3 Overview Of Restricted Structures The two most commonly used restricted structures are Stack and Queue Both

### Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structures Using C++ 2E Chapter 5 Linked Lists Doubly Linked Lists Traversed in either direction Typical operations Initialize the list Destroy the list Determine if list empty Search list for a given

### DATA STRUCTURES USING C

DATA STRUCTURES USING C QUESTION BANK UNIT I 1. Define data. 2. Define Entity. 3. Define information. 4. Define Array. 5. Define data structure. 6. Give any two applications of data structures. 7. Give

### D06 PROGRAMMING with JAVA

Cicles Formatius de Grau Superior Desenvolupament d Aplicacions Informàtiques D06 PROGRAMMING with JAVA Ch20 Data Structures I PowerPoint presentation, created by Angel A. Juan - ajuanp(@)gmail.com, for

### Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

EECS 281: Data Structures and Algorithms The Foundation: Data Structures and Abstract Data Types Computer science is the science of abstraction. Abstract Data Type Abstraction of a data structure on that

### Data Structures and Algorithms

Data Structures and Algorithms CS245-2016S-04 Stacks and Queues David Galles Department of Computer Science University of San Francisco 04-0: Abstract Data Types An Abstract Data Type is a definition of

### Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Definition Course: Programming II - Abstract Data Types The ADT Stack The ADT Stack is a linear sequence of an arbitrary number of items, together with access procedures. The access procedures permit insertions

### UNIVERSITI MALAYSIA SARAWAK KOTA SAMARAHAN SARAWAK PSD2023 ALGORITHM & DATA STRUCTURE

STUDENT IDENTIFICATION NO UNIVERSITI MALAYSIA SARAWAK 94300 KOTA SAMARAHAN SARAWAK FAKULTI SAINS KOMPUTER & TEKNOLOGI MAKLUMAT (Faculty of Computer Science & Information Technology) Diploma in Multimedia

### CHAPTER 4 ESSENTIAL DATA STRUCTRURES

CHAPTER 4 ESSENTIAL DATA STRUCTURES 72 CHAPTER 4 ESSENTIAL DATA STRUCTRURES In every algorithm, there is a need to store data. Ranging from storing a single value in a single variable, to more complex

### 12 Abstract Data Types

12 Abstract Data Types 12.1 Source: Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT).

### 10CS35: Data Structures Using C

CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a

### Comp 245 Data Structures. Stacks

Comp 245 Data Structures Stacks What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO random access to other elements within the stack An

### 22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department http://www.cs.uiowa.edu/~hzhang/c31/

22c:31 Algorithms Ch3: Data Structures Hantao Zhang Computer Science Department http://www.cs.uiowa.edu/~hzhang/c31/ Linear Data Structures Now we can now explore some convenient techniques for organizing

### Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Stacks Outline The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack Stacks 2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure

Linked Lists Linked Lists, Queues, and Stacks CSE 10: Introduction to C Programming Fall 200 Dynamic data structure Size is not fixed at compile time Each element of a linked list: holds a value points

### 1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated

### Collections and Data Structures. array vs ArrayList. Using a collection or ADT

Collections and Data Structures CMPT 126: Lecture 11 Collections: Abstract Data Types and Dynamic Representation (Chapt. 12) Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University

### Stacks. The stack ADT Stack Implementation. Stack Examples. using arrays using generic linked lists using List ADT. EECS 268 Programming II 1

Stacks The stack ADT Stack Implementation using arrays using generic linked lists using List ADT Stack Examples 1 Stacks and Queues Linear data structures each item has specific first, next, and previous

### Announcements. FINAL EXAM Monday May 21, 1:30pm Review Session. Wednesday May 16, 7-9pm

Announcements FINAL EXAM Monday May 21, 1:30pm Review Session Wednesday May 16, 7-9pm 1 Recitation 12 Root Finding, Sorting, Stacks, Queues 2 Outline Linked Lists Sorting Queues 3 Object References Let

### Analysis of a Search Algorithm

CSE 326 Lecture 4: Lists and Stacks 1. Agfgd 2. Dgsdsfd 3. Hdffdsf 4. Sdfgsfdg 5. Tefsdgass We will review: Analysis: Searching a sorted array (from last time) List ADT: Insert, Delete, Find, First, Kth,

### Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Queues and Stacks Atul Prakash Downey: Chapter 15 and 16 Queues Queues occur in real life a lot. Queues at checkout Queues in banks In software systems: Queue of requests at a web servers Properties of

### Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Definition Course: Programming II - Abstract Data Types The ADT Queue The ADT Queue is a linear sequence of an arbitrary number of items, together with access procedures. The access procedures permit addition

### Common Data Structures

Data Structures 1 Common Data Structures Arrays (single and multiple dimensional) Linked Lists Stacks Queues Trees Graphs You should already be familiar with arrays, so they will not be discussed. Trees

### Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

ECE 551 C++ Programming, Data structures, and Algorithms Abstract Data Type: Stack Last In First Out (LIFO) 1 2 2 1 4 3 1 3 4 Stacks in Programming Worst line ever! 5 3 1 5 Stacks are not useful for waiting

### CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Linda Shapiro Registration We have 180 students registered and others who want to get in. If you re thinking of dropping

### 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

C++ How to Program, 8/e 1992-2012 by Pearson Education, Inc. 1992-2012 by Pearson Education, Inc. 1992-2012 by Pearson Education, Inc. We ve studied fixed-size data structures such as one-dimensional arrays

### Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in

### Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Quiz 4 Solutions Q1: What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number

### QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

QUEUES A queue is simply a waiting line that grows by adding elements to its end and shrinks by removing elements from the. Compared to stack, it reflects the more commonly used maxim in real-world, namely,

### Module 2 Stacks and Queues: Abstract Data Types

Module 2 Stacks and Queues: Abstract Data Types A stack is one of the most important and useful non-primitive linear data structure in computer science. It is an ordered collection of items into which

### Data Structures Using Java

Data Structures Using Java D. S. Malik P. S. Nair THOMSON COURSE TECHNOLOGY Australia Canada Mexico Singapore Spain United Kingdom United States TABLE OF Contents PREFACE XXV 1.Software Engineering Principles

### Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Tutorial#1 Q 1:- Explain the terms data, elementary item, entity, primary key, domain, attribute and information? Also give examples in support of your answer? Q 2:- What is a Data Type? Differentiate

### DATA STRUCTURE - STACK

DATA STRUCTURE - STACK http://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm Copyright tutorialspoint.com A stack is an abstract data type ADT, commonly used in most programming

### Definition. E.g. : Attempting to represent a transport link data with a tree structure:

The ADT Graph Recall the ADT binary tree: a tree structure used mainly to represent 1 to 2 relations, i.e. each item has at most two immediate successors. Limitations of tree structures: an item in a tree

### Stacks, Queues and Linked Lists

Stacks, Queues and Linked Lists Adnan Aziz 1 Dynamic sets CLRS Part III, page 197 In mathematics, a set is a well-defined collection of elements (elements could be numbers, functions, geometric shapes);

### PES Institute of Technology-BSC QUESTION BANK

PES Institute of Technology-BSC Faculty: Mrs. R.Bharathi CS35: Data Structures Using C QUESTION BANK UNIT I -BASIC CONCEPTS 1. What is an ADT? Briefly explain the categories that classify the functions

Chapter 4: Linked Lists Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during

### Algorithms and Data Structures

Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016 Overview general linked lists stacks queues trees 2 2

### CmpSci 187: Programming with Data Structures Spring 2015

CmpSci 187: Programming with Data Structures Spring 2015 Lecture #12 John Ridgway March 10, 2015 1 Implementations of Queues 1.1 Linked Queues A Linked Queue Implementing a queue with a linked list is

### CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Nicki Dell Spring 2014

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Nicki Dell Spring 2014 Registration We have 140 students registered and 140+ on the wait list! If you re thinking of

### Big O and Limits Abstract Data Types Data Structure Grand Tour. http://gcc.gnu.org/onlinedocs/libstdc++/images/pbds_different_underlying_dss_1.

Big O and Limits Abstract Data Types Data Structure Grand Tour http://gcc.gnu.org/onlinedocs/libstdc++/images/pbds_different_underlying_dss_1.png Consider the limit lim n f ( n) g ( n ) What does it

Unordered Linked Lists Derive class unorderedlinkedlist from the abstract class linkedlisttype Implement the operations search, insertfirst, insertlast, deletenode See code on page 292 Defines an unordered

Arrays, Singly-Linked Lists, Stacks, Queues, Doubly-Linked Lists, Deques Slide Set 5: Learning Goals Differentiate an abstraction from an implementation. Define and give examples of problems that can be

### Algorithms and Data Structures

Algorithms and Data Structures Lists and Arrays Marcin Sydow Web Mining Lab PJWSTK Marcin Sydow (Web Mining Lab PJWSTK) Algorithms and Data Structures 1 / 35 Topics covered by this lecture: Linked Lists

### St S a t ck a ck nd Qu Q eue 1

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

### Introduction to Data Structures

Introduction to Data Structures Albert Gural October 28, 2011 1 Introduction When trying to convert from an algorithm to the actual code, one important aspect to consider is how to store and manipulate

### DATA STRUCTURE - QUEUE

DATA STRUCTURE - QUEUE http://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm Copyright tutorialspoint.com Queue is an abstract data structure, somewhat similar to stack. In contrast to

### Data Structures in the Java API

Data Structures in the Java API Vector From the java.util package. Vectors can resize themselves dynamically. Inserting elements into a Vector whose current size is less than its capacity is a relatively

### Introduction to Data Structures and Algorithms

Introduction to Data Structures and Algorithms Chapter: Elementary Data Structures(1) Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen Overview on simple data structures

### Vector ADT. A Vector stores a list of elements: Access via rank/index.

Vector ADT A Vector stores a list of elements: Access via rank/index. Accessor methods: elementatrank(r), Update methods: replaceatrank(r, o), insertatrank(r, o), removeatrank(r) Generic methods: size(),

### Algorithms and Data Structures Written Exam Proposed SOLUTION

Algorithms and Data Structures Written Exam Proposed SOLUTION 2005-01-07 from 09:00 to 13:00 Allowed tools: A standard calculator. Grading criteria: You can get at most 30 points. For an E, 15 points are

### Data Structures and Algorithms Stacks and Queues

Data Structures and Algorithms Stacks and Queues Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/23 6-0: Stacks and

### Data Structures. value. key. 5 Alpha. 3 Beta. 7 Gamma

Data Structures key value 5 Alpha 10 2 7 5 1 4 9 3 6 8 3 Beta 7 Gamma Sequence Associative e.g., C-Arrays, std::vector, std::deque, std::list e.g., C-Arrays, std::map, std::set, std::unordered_map Data

### Data Structure. Lecture 3

Data Structure Lecture 3 Data Structure Formally define Data structure as: DS describes not only set of objects but the ways they are related, the set of operations which may be applied to the elements

### Lecture 6b Linked List Variations. Similar but not the same

Lecture 6b Linked List Variations Similar but not the same Linked List Variations: Overview The linked list implementation used in List ADT is known as Singly (Single) Linked List Each node has one pointer

### TIE-20106 1 TIE-20106 2

TIE-20106 1 1 List structures In this chapter, a few simple data structures (stack and queue) that can be implemented with arrays and/or lists are covered. Two more complex structures based on lists are

Singly linked lists (continued...) I began the lecture by discussing two more methods that are commonly defined for linked lists, namely adding or removing an element at the back of the list. This requires

### MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push) -----------------------------------------

=============================================================================================================================== DATA STRUCTURE PSEUDO-CODE EXAMPLES (c) Mubashir N. Mir - www.mubashirnabi.com

### Java Software Structures

INTERNATIONAL EDITION Java Software Structures Designing and Using Data Structures FOURTH EDITION John Lewis Joseph Chase This page is intentionally left blank. Java Software Structures,International Edition

### Summary. Pre requisition. Content Details: 1. Basics in C++

Summary C++ Language is one of the approaches to provide object-oriented functionality with C like syntax. C++ adds greater typing strength, scoping and other tools useful in object-oriented programming

### Stacks, Queues & Trees. The Stack Interface. The Stack Interface. Prof. Dr. Harald Gall. Institut für Informatik Universität Zürich

Stacks, Queues & Trees Prof. Dr. Harald Gall Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch The Stack Interface!! Stack!! Data structure holding several items.!! New items added to the

### Stacks. Linear data structures

Stacks Linear data structures Collection of components that can be arranged as a straight line Data structure grows or shrinks as we add or remove objects ADTs provide an abstract layer for various operations

### Data Structures grand tour continues Diagnostic Quiz Review Comparable, Comparator, and Function Objects. Check out from SVN: DiagQuizReview

Data Structures grand tour continues Diagnostic Quiz Review Comparable, Comparator, and Function Objects Check out from SVN: DiagQuizReview Written Assignment 2 Pascal Christmas Tree Some review Some new

### Sequences in the C++ STL

CS 311 Data Structures and Algorithms Lecture Slides Wednesday, November 4, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn

### Stacks and queues. Algorithms and Data Structures, Fall 2011. Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Algorithms and Data Structures, Fall 2011 Stacks and queues Rasmus Pagh Based on slides by Kevin Wayne, Princeton Algorithms, 4 th Edition Robert Sedgewick and Kevin Wayne Copyright 2002 2011 Stacks and

### Cpt S 223. School of EECS, WSU

Abstract Data Types 1 Topics Abstract Data Types (ADTs) Some basic ADTs: Lists Stacks Queues 2 Primitive Data Type vs. Abstract Data Types Primitive DT: ADT: programmer progra ammer Interface (API) e.g.,

### A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION Tao Chen 1, Tarek Sobh 2 Abstract -- In this paper, a software application that features the visualization of commonly used

### Abstract Data Types. Chapter 2

Chapter 2 Abstract Data Types The second idea at the core of computer science, along with algorithms, is data. In a modern computer, data consists fundamentally of binary bits, but meaningful data is organized

### KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

KITES TECHNOLOGY 360 Degree Solution www.kitestechnology.com/academy.php info@kitestechnology.com technologykites@gmail.com Contact: - 8961334776 9433759247 9830639522.NET JAVA WEB DESIGN PHP SQL, PL/SQL

### University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Fall 2007 7p-9p, Thursday, October 4 Name: NetID: Lab Section

### The AP Java Subset. A Topics. Primitive Types. Operators. Topics. int double boolean

The AP Java Subset A Topics Topics Primitive Types Operators int double boolean +, -, *, /, % ++, -- = +=, -=, *=, /=, %= ==,!=, , = &&,,! Numeric Casts String Concatenation (int) (double) + will

### Datastrukturer och standardalgoritmer i Java

1 (8) Datastrukturer och standardalgoritmer i Java Dokumentet listar ett urval av konstruktorer och metoder för några vanliga Javaklasser. Class ArrayList ArrayList() Constructs an empty list. void

### Programming with Data Structures

Programming with Data Structures CMPSCI 187 Spring 2016 Please find a seat Try to sit close to the center (the room will be pretty full!) Turn off or silence your mobile phone Turn off your other internet-enabled

### Data Structures and Algorithms(3)

Ming Zhang Data Structures and Algorithms Data Structures and Algorithms(3) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 2008.6 (the "Eleventh

### Stack: What problems does it solve? Data Structures revisited. Simple stack example. Implementation is very simple

Data Structures revisited Stack: What problems does it solve? Linked lists and arrays and ArrayLists and Linear structures, operations include insert, delete, traverse, Advantages and trade-offs include