# Common Data Structures

Size: px
Start display at page:

Transcription

1 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 and Graphs will be discussed later.

2 Data Structures 2 Linked Lists A linked list is a linear set of nodes with the following properties: Each node has at least two fields, the key and the next. th element of the list. The next field of the th element points to the The first element of the linked list is called the head, and the last element the tail. The head contains no data, and the next field of the tail points to NULL. The key field contains the data we are actually interested in. What exactly is meant by things like points to and NULL depends on the implementation used. head A L I S T

3 Data Structures 3 Linked List Operations There are several operations that we must be able to do on a linked list: Insert a node Delete a node Find an element with key There are other operations which might be useful: Move an element Swap two elements We will talk about a few of these next, and give one possible way to implement a linked list (in part, anyway).

4 Data Structures 4 Linked List: C++ Declaration struct node { char key; struct node *next; } struct node *head; head = new node; head->next = NULL; This creates an empty linked list. In this implementation, we have the tail point to NULL, which is usually defined to be 0. Thus, if the next field of an element is 0, we know we have reached the tail. Here point to means that. We really used pointers. We will see shortly that we don t need to use actual pointers to implement a linked list.

5 Data Structures 5 Linked List: JAVA Declaration The JAVA implementation is similar to the C++ one. public class node { public char key; public node next; } node head = new node(); head.next = NULL;

6 Data Structures 6 Linked List: Insert The simplest insert would always place the new item at the front (or back) of the list. One may also want to insert elements in the middle of the list. To insert X between I and S: //C++ //JAVA struct node *A; node A; A=new node; A=new node(); A->key = X; A.Key=X; A->next = I->next; A.next=I.next; I->next = A; I.next=A; X I S head A L I X S T Only two references need to be changed no matter how long the list is. How does this compare with arrays?

7 Data Structures 7 Linked List: Delete Deleting a node is very simple. We just need to change one pointer. However, we need to know which node points to the node we wish to delete. Assuming we know that node points to node, we can delete by: // C++ // JAVA i->next = x->next; i.next=x.next; head A L I X S T Only one reference needs to be changed, no matter how long the list is. How does this compare with an array? Note: There is a slight problem with the C++ code. What is it?

8 Data Structures 8 Linked List: Moving a node Moving a node consist of a delete operation follow by an insert operation. Example: Move T from the end of the list to the beginning: head A L I S T head A L I S T head T A L I S Again, this requires changing only 3 references, no matter how long the list is. What about with arrays?

9 Data Structures 9 Comparison: Linked Lists and Arrays A linked list can grow and shrink during its lifetime, and its maximum size doesn t need to be specified in advance. In contrast, arrays are always of fixed size. We can rearrange, add, and delete items from a linked list with only a constant number of operations. With arrays, these operations are generally linear in the size of the array. To find the th entry of a linked list, we need to follow pointers, which requires operations. With an array, this takes only one operation. Similarly, it may not be obvious how large a linked list is, whereas we always know the size of an array. (This problem can be eliminated very easily. How?)

10 Data Structures 10 Other Linked Lists A doubly linked list is like a linked list, but each node also has a previous field, which points to the nodes predecessor. head A L I S T This can simplify searching, and makes the deletion operation (potentially) easier. There is obviously added storage cost, and the number of instructions needed for the various operations approximately doubles. Circular-linked list: The last node points to the first node. It can be single or doubly linked list. It can be implemented with a fixed or moving head. head A L I S T

11 Data Structures 11 Linked Lists without Pointers Instead of using pointer to implement linked lists, we can use arrays. We won t look at this in depth, but it is not too hard to imagine how we could do it. There are a few complications in this type of implementation, but they can easily be worked around. Example: next key S L A I T head A L I S T z

12 Data Structures 12 Linked Lists Summary Linked lists are data structures that are in many ways similar to arrays. Inserting, deleting or accessing items in linked lists are operations which can be performed. Insertion and deletion can be done in constant time. Finding an element in a linked list generally takes linear time. This is true whether we are trying to find an element whose value is, or we are trying to find the th element on the list. It is this last fact that can limit the usefulness of linked lists.

13 Data Structures 13 Stacks A Stack is a sequential organization of items in which the last element inserted is the first element removed. They are often referred to as LIFO, which stands for last in first out. Examples: letter basket, stack of trays, stack of plates. The only element of a stack that may be accessed is the one that was most recently inserted. There are only two basic operations on stacks, the push (insert), and the pop (read and delete). push pop Stack

14 Data Structures 14 Stacks: Push and Pop The operation push( ) places the item onto the top of the stack. The operation pop() removes the top item from the stack, and returns that item. We need some way of detecting an empty stack (This is an underfull stack). In some cases, we can have pop() return some value that couldn t possibly be on the stack. Example: If the items on the stack are positive integers, we can return -1 in case of underflow. In other cases, we may be better off simply keeping track of the size of the stack. In some cases, we will also have to worry about filling the stack (called overflow). One way to do this is to have push( ) return 1 if it is successful, and 0 if it fails.

15 Data Structures 15 An Example Stack Operations Assume we have a stack of size 3 which holds integers between -100 and 100. Here is a series of operations, and the results. Operation Stack Contents Return create () push(55) (55) 1 push(-7) (-7,55) 1 push(16) (16,-7,55) 1 pop (-7,55) 16 push(-8) (-8,-7,55) 1 push(23) (-8,-7,55) 0 pop (-7,55) -8 pop (55) -7 pop () 55 pop () 101

16 Data Structures 16 Implementing Stacks: Array Stacks can be implemented with an array and an integer that stores the array index of the top of the stack. Empty stack has, and a full stack has, where is the size of the array. To push, increment the counter, and write in the array position. To pop, decrement the counter.

17 Data Structures 17 Example of Array Implementation of Stack We use an array to store the elements and a variable to keep track of the top. The last column, labeled R is the result of the function call. Operation p E0 E1 E2 E3 E4 R create -1????? push(55) 0 55???? 1 push(-7) ??? 1 push(16) ?? 1 pop ?? 16 push(-8) ?? 1 pop ?? -8 pop ?? -7 Notice that some values are still in the array, but are no longer considered to be in the stack. In general, elements are garbage if. Why don t we erase the element (i.e. set it to some default value.)?

18 Data Structures 18 Example Application of Stacks Stacks can be used to check a program for balanced symbols (such as!#",(),[ ]). Example:! ()" is legal, as is! ()(!\$" )", whereas! ((" and! (" ) are not (so simply counting symbols does not work). If the symbols are balanced correctly, then when a closing symbol is seen, it should match the most recently seen unclosed opening symbol. Therefore, a stack will be appropriate. The following algorithm will do the trick: While % there is still input: = next symbol (% if is an opening symbol) push(% ) else //% is a closing symbol if (Stack.Empty) report an error else& = pop() if(! Match(%,& )) report an error If (! Stack.Empty) report an error

19 Data Structures Input:! ( ) " Read!, so push! Examples Read (, so push (. Stack has! ( Read ), so pop. popped item is ( which matches ). Stack has now!. Read ", so pop; popped item is! which matches ". End of file; stack is empty, so the string is valid. 2. Input:! ( ) (! ) " " (This will fail.) 2. Input:! (! " )! " ( ) " (This will succeed.) 3. Input:! ( ) " ) (This will fail.)

20 Data Structures 20 Stack: C++ Array Implementation The following is a C++ stack that holds items of type ItemType class Stack { private: ItemType *stack; int p; public: Stack(int max=100) {stack = new ItemType[max]; p = 0; } Stack() {delete stack; } void push(itemtype v) { stack[p++] = v; } ItemType pop() {return stack[--p]; } int empty() {return!p; } };

21 Data Structures 21 Stack: JAVA Array Implementation The following is a JAVA stack that holds items of type ItemType public class Stack { private ItemType[] stack; private int p; public Stack(int max) { stack = new ItemType[max]; p = 0; } public void push(itemtype v) { stack[p++] = v; } public ItemType pop() { return stack[--p]; } public int empty() { return!p; } };

22 Data Structures 22 Operator Precedence Parsing We can use the stack class we just defined to parse and evaluate mathematical expressions like: ' ( *) +, -( (/.0 1 2# First, we transform it to postfix notation (How would you do this?): ( ( 7 + ( Then, the following C++ routine uses a stack to perform this evaluation: char c; Stack acc(50); int x; while (cin.get(c)) { x = 0; while (c == ) cin.get(c); if (c == + ) x = acc.pop() + acc.pop(); if (c == * ) x = acc.pop() * acc.pop(); while (c>= 0 && c<= 9 ) {x = 10*x + (c- 0 ); cin.get(c); } acc.push(x); } cout << acc.pop() << \n ;

23 Data Structures 23 Stack Implementation: C++ Linked Lists We can use linked lists to implement stacks. The head of the list represents the top of the stack. Example After43 % 5 76,83 %95 ;:,83 %95 *< 43 % 5 7=,, we have: Head A B C D ItemType pop () { ItemType x = head->key; head=head->next; return x; } void push(itemtype x) { node *x; x=new node; x->key = X; insert(x); } Note: Slight error in pop. What is it?

24 Data Structures 24 Stack Implementation: JAVA Linked Lists We assume head points to the first element of the list, and is the top of the stack. public ItemType pop () { ItemType x = head.key; head=head.next; return x; } public void push(itemtype x) { node A=new node(); A.key = x; A.next=head; head=a; }

25 Data Structures 25 Linked Lists Arrays Stack Implementation: Array or Linked List? Use 1 pointer extra memory per item. If an item is an integer, that means twice as much space is used. If an item is a structure/class consisting of many objects, it is only a small price to pay. Are unlimited in size. Allocate a constant amount of space, some of which may never be used. The amount of wasted memory is the number of unused elements times the size of the item, which could be large in some cases. The maximum size is determined when the stack is created. Which is better? Why?

26 Data Structures 26 Stack Applications Recursion removal can be done with stacks. Reversing things is easily done with stacks. Procedure call and procedure return is similar to matching symbols: When a procedure returns, it returns to the most recently active procedure. When a procedure call is made, save current state on the stack. On return, restore the state by popping the stack.

27 Data Structures 27 Queues A Queue is a sequential organization of items in which first element entered is first removed. They are often referred to as FIFO, which stands for first in first out. Examples: standing in a line, printer queue. The basic operations are: insert( ) places at the beginning of the queue. remove() returns and deletes the item at the end of the queue. Example: Operation Queue Return CreateQueue () insert(7) (7) insert(8) (7,8) insert(5) (7,8,5) remove() (8,5) 7 remove() (5) 8

28 Data Structures 28 Operating systems: Queue Applications Queue of jobs or processes ready to run (waiting for CPU): Queues of processes waiting for I/O. Files sent to printer Simulation of real-world queuing systems: Customers in a grocery store, bank, etc. Orders in a factory Hospital emergency room or doctor s office Telephone calls for airline reservations, customer orders, information, etc. Problem applications: B %, and pairs 7CEDF indicating that event C Topological ordering: given a sequence of should occur prior to F, provide a schedule.

29 Data Structures 29 Queues: Naive Implementation Using array: Store items in an array. The head it the first element, and the tail is indicated by a variable tail. insert( ) is easy: increment C HG, and insert element. remove() is inefficient: all elements have to be shifted. Thus, remove isi. a b c d tail = 3 remove a b c d tail = 3 update tail b c d tail = 2 How can we improve this?

30 Data Structures 30 Queues: A Better Implementation Keep track of both the head and the tail. To remove, increment JE&K B. a b c d remove a b c d tail = 3 head = 0 tail = 3 update head b c d head = 1 tail = 3 There is still a problem. What is it, and how can we fix it?

31 Data Structures 31 Queues: Circular Array Implementation Previous implementation isi L per operation, which is great. However, after inserts (where is the size of the array), the array is full even if the queue is logically nearly empty. Solution: Use wraparound to reuse the cells at the start of the array. To 1M& > B, add one, but if that goes past end, reset to zero. How do you detect a full or empty queue? We will give a simplified implementation for the queue data structure. A better implementation would detect an empty (full) queue before performing a dequeue (enqueue) operation.

32 Data Structures 32 Queue C++ Declaration Here is a C++ declaration of an integer queue using a circular array: class Queue { private: int *queue; int cap,head,tail; public: Queue(int s=100) { cap= s; queue = new int[cap]; head = 0; tail = 0; } Queue() { delete queue; } void Enqueue(int v) { queue[tail] = v; tail = (tail+1) % cap; } int Dequeue() { int t = queue[head]; head = (head + 1) % cap; return t; } int Empty() {return (head == tail ); } int Full() {return (head == ((tail+1)%cap)); } };

33 Data Structures 33 Queue JAVA Declaration public class Queue { private int[] queue; private int cap,head,tail; public Queue(int s) { cap=s; queue = new int[cap]; head = 0; tail = 0; i } public void Enqueue(int v) { queue[tail] = v; tail = (tail+1) % cap; } public int Dequeue() { int t = queue[head]; head = (head + 1) % cap; return t; } public int Empty() { return (head == tail ); } public int Full() { return (head == ((tail+1)%cap)); } };

34 Data Structures 34 Example of Queue Using Circular Array 5 Here we use an array OQP9 to store the elements and the variables (head) and (tail) to keep track of the beginning and end of the queue. The value R is the return value of the operation. Operation h t E0 E1 E2 E3 R create 0 0???? insert(55) ??? insert(-7) ?? insert(16) ? remove() ? 55 insert(-8) remove() remove() insert(11) Note that some of the values remain physically in the array, but are logically no longer in the queue.

35 Data Structures 35 Queues: Linked List Implementation We can maintain JR&\$ S and C HG pointers. & : advance JE&\$LB. % > & : add to end of list and adjust C HG. front tail A B C D The details are not very hard to work out, so will not be presented.

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

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

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

### CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

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

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

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

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

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

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

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

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

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

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

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

### Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the

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

### Stack Allocation. Run-Time Data Structures. Static Structures

Run-Time Data Structures Stack Allocation Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers,

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

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

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

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

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

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

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

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

### Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know

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

### UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming 1 2 Foreword First of all, this book isn t really for dummies. I wrote it for myself and other kids who are on the team. Everything

### Data Structures and Data Manipulation

Data Structures and Data Manipulation What the Specification Says: Explain how static data structures may be used to implement dynamic data structures; Describe algorithms for the insertion, retrieval

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

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

### Data Structures and Algorithms

Data Structures and Algorithms Lecture 4 2016 Stacks and... 1/28 1 2 Circular Linked 3 Queue Syntax and Functions in C++ 4 Stacks and... 2/28 FIFO and LIFO Outline Data structures can be specialized versions

### Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Stack & Queue Explain Array in detail One Dimensional Array Simplest data structure that makes use of computed address to locate its elements is the onedimensional array or vector; number of memory locations

### Data Structure with C

Subject: Data Structure with C Topic : Tree Tree A tree is a set of nodes that either:is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which

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

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

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

### Stacks. Data Structures and Data Types. Collections

Data Structures and Data Types Data types Set values. Set operations on those values. Some are built in to Java: int, double, char,... Most are not: Complex, Picture, Charge, Stack, Queue, Graph,... Data

### Data Structure [Question Bank]

Unit I (Analysis of Algorithms) 1. What are algorithms and how they are useful? 2. Describe the factor on best algorithms depends on? 3. Differentiate: Correct & Incorrect Algorithms? 4. Write short note:

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

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

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

### COMPUTER SCIENCE. Paper 1 (THEORY)

COMPUTER SCIENCE Paper 1 (THEORY) (Three hours) Maximum Marks: 70 (Candidates are allowed additional 15 minutes for only reading the paper. They must NOT start writing during this time) -----------------------------------------------------------------------------------------------------------------------

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

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

### 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.,

### Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Data Structures Jaehyun Park CS 97SI Stanford University June 29, 2015 Typical Quarter at Stanford void quarter() { while(true) { // no break :( task x = GetNextTask(tasks); process(x); // new tasks may

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

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

### Application of Stacks: Postfix Expressions Calculator (cont d.)

Application of Stacks: Postfix Expressions Calculator (cont d.) Postfix expression: 6 3 + 2 * = FIGURE 7-15 Evaluating the postfix expression: 6 3 + 2 * = Data Structures Using C++ 2E 1 Application of

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

### 1 Abstract Data Types Information Hiding

1 1 Abstract Data Types Information Hiding 1.1 Data Types Data types are an integral part of every programming language. ANSI-C has int, double and char to name just a few. Programmers are rarely content

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

### PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members

### PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks 1 STACK A structure with a series of data elements with last sent element waiting for a delete operation.

### Data Structures UNIT III. Model Question Answer

Data Structures UNIT III Model Question Answer Q.1. Define Stack? What are the different primitive operations on Stack? Ans: Stack: A stack is a linear structure in which items may be added or removed

### 5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

1. The advantage of.. is that they solve the problem if sequential storage representation. But disadvantage in that is they are sequential lists. [A] Lists [B] Linked Lists [A] Trees [A] Queues 2. The

### Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010 Problem 1. In each of the following question, please specify if the statement

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

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

### Data Structures Using C++

Data Structures Using C++ 1.1 Introduction Data structure is an implementation of an abstract data type having its own set of data elements along with functions to perform operations on that data. Arrays

### Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Dynamic Storage Allocation CS 44 Operating Systems Fall 5 Presented By Vibha Prasad Memory Allocation Static Allocation (fixed in size) Sometimes we create data structures that are fixed and don t need

### Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Binary Search Trees A Generic Tree Nodes in a binary search tree ( B-S-T) are of the form P parent Key A Satellite data L R B C D E F G H I J The B-S-T has a root node which is the only node whose parent

### Lecture Notes on Linear Search

Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 29, 2013 1 Introduction One of the fundamental and recurring problems in computer science is

### Basic Data Structures and Algorithms

Tutorial 3 Basic Data Structures and Algorithms THINGS TO LOOK FOR 3.0 INTRODUCTION 3.1 Array Based Containers Definition and uses of containers. Array and list based containers. Designing and building

### Introduction to data structures

Notes 2: Introduction to data structures 2.1 Recursion 2.1.1 Recursive functions Recursion is a central concept in computation in which the solution of a problem depends on the solution of smaller copies

### Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 March 17, 2010 1 Introduction In the previous two lectures we have seen how to exploit the structure

### Lecture 12 Doubly Linked Lists (with Recursion)

Lecture 12 Doubly Linked Lists (with Recursion) In this lecture Introduction to Doubly linked lists What is recursion? Designing a node of a DLL Recursion and Linked Lists o Finding a node in a LL (recursively)

### System Software Prof. Dr. H. Mössenböck

System Software Prof. Dr. H. Mössenböck 1. Memory Management 2. Garbage Collection 3. Linkers and Loaders 4. Debuggers 5. Text Editors Marks obtained by end-term exam http://ssw.jku.at/misc/ssw/ 1. Memory

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

### Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Linear DT-1: Restricted Lists Stacks, Queues tul Gupta Restricted Lists Stack Queue Circular queue Priority queue General Lists rrays Linked list Circular list Doubly linked list Linear DTs 1 Stacks Using

### A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)

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

### Sample Questions Csci 1112 A. Bellaachia

Sample Questions Csci 1112 A. Bellaachia Important Series : o S( N) 1 2 N N i N(1 N) / 2 i 1 o Sum of squares: N 2 N( N 1)(2N 1) N i for large N i 1 6 o Sum of exponents: N k 1 k N i for large N and k

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

### Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Data Structures Page 1 of 24 A.1. Arrays (Vectors) n-element vector start address + ielementsize 0 +1 +2 +3 +4... +n-1 start address continuous memory block static, if size is known at compile time dynamic,

### Data Structures Fibonacci Heaps, Amortized Analysis

Chapter 4 Data Structures Fibonacci Heaps, Amortized Analysis Algorithm Theory WS 2012/13 Fabian Kuhn Fibonacci Heaps Lacy merge variant of binomial heaps: Do not merge trees as long as possible Structure:

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

### COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms 1 Activation Records activation declaration location Recall that an activation

### Lecture 2: Data Structures Steven Skiena. http://www.cs.sunysb.edu/ skiena

Lecture 2: Data Structures Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena String/Character I/O There are several approaches

### C++ INTERVIEW QUESTIONS

C++ INTERVIEW QUESTIONS http://www.tutorialspoint.com/cplusplus/cpp_interview_questions.htm Copyright tutorialspoint.com Dear readers, these C++ Interview Questions have been designed specially to get

### \Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Elementary Data Structures \Mankinds's progress is measured by the number of things we can do without thinking." Elementary data structures such as stacks, queues, lists, and heaps will be the \of-the-shelf"

### Symbol Tables. Introduction

Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The

### Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 17 October 23, 2014 1 Introduction In this lecture, we will continue considering associative

### INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY A PATH FOR HORIZING YOUR INNOVATIVE WORK A REVIEW ON THE USAGE OF OLD AND NEW DATA STRUCTURE ARRAYS, LINKED LIST, STACK,

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

### Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Recursion Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive. Why recursion? o For many problems, the recursion solution is more natural than the alternative

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

### Lecture Notes on Stacks & Queues

Lecture Notes on Stacks & Queues 15-122: Principles of Imperative Computation Frank Pfenning, André Platzer, Rob Simmons Lecture 9 February 12, 2013 1 Introduction In this lecture we introduce queues and

1 ADTs,, Arrays, Linked Lists Outline and Required Reading: ADTs ( 2.1.2) Arrays ( 1.5) Linked Lists ( 4.3.1, 4.3.2) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic Abstract Data Type (ADT) 2 abstract

### What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

CMPSC 24: Lecture 13 Recursion Divyakant Agrawal Department of Computer Science UC Santa Barbara 5/12/10 1 Lecture Plan Recursion General structure of recursive soluions Why do recursive soluions terminate?

### 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 Cohort: BIS/05/FT BCNS/05/FT BSE/05/FT Examinations for 2005-2006 / Semester

### 9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

9 Control Statements 9.1 Introduction The normal flow of execution in a high level language is sequential, i.e., each statement is executed in the order of its appearance in the program. However, depending

### Converting a Number from Decimal to Binary

Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive

### Comp151. Definitions & Declarations

Comp151 Definitions & Declarations Example: Definition /* reverse_printcpp */ #include #include using namespace std; int global_var = 23; // global variable definition void reverse_print(const