Common Data Structures



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

Module 2 Stacks and Queues: Abstract Data Types

DATA STRUCTURES USING C

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

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

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

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

Linked Lists Linked Lists, Queues, and Stacks

10CS35: Data Structures Using C

CmpSci 187: Programming with Data Structures Spring 2015

Introduction to Data Structures

Algorithms and Data Structures

Data Structures and Algorithms Stacks and Queues

Sequential Data Structures

Stacks. Linear data structures

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

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

Chapter 3: Restricted Structures Page 1

Stack Allocation. Run-Time Data Structures. Static Structures

Introduction to Data Structures and Algorithms

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

DATA STRUCTURE - QUEUE

PES Institute of Technology-BSC QUESTION BANK

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

Programming with Data Structures

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

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

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

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

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

Data Structures and Data Manipulation

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

Analysis of a Search Algorithm

Data Structures and Algorithms

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

Data Structure with C

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

Big O and Limits Abstract Data Types Data Structure Grand Tour.

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

Stacks. Data Structures and Data Types. Collections

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Data Structure [Question Bank]

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

7.1 Our Current Model

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

COMPUTER SCIENCE. Paper 1 (THEORY)

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

Universidad Carlos III de Madrid

Cpt S 223. School of EECS, WSU

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

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

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

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

STACKS,QUEUES, AND LINKED LISTS

1 Abstract Data Types Information Hiding

Unordered Linked Lists

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

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

Data Structures UNIT III. Model Question Answer

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.

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

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

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

Data Structures Using C++

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

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

Lecture Notes on Linear Search

Basic Data Structures and Algorithms

Introduction to data structures

Lecture Notes on Binary Search Trees

Lecture 12 Doubly Linked Lists (with Recursion)

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

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

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:

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!

Sample Questions Csci 1112 A. Bellaachia

Algorithms and Data Structures

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

Data Structures Fibonacci Heaps, Amortized Analysis

DATA STRUCTURE - STACK

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

Lecture 2: Data Structures Steven Skiena. skiena

C++ INTERVIEW QUESTIONS

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

Symbol Tables. Introduction

Lecture Notes on Binary Search Trees

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

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

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

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

Lecture Notes on Stacks & Queues

ADTs,, Arrays, Linked Lists

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

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

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

Converting a Number from Decimal to Binary

Comp151. Definitions & Declarations

TREE BASIC TERMINOLOGIES

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Transcription:

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.

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

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

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.

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;

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?

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?

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?

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

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

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 4 1 6 5 3 2 1 key S L A I T head A L I S T z

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.

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

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.

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

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.

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 55-7??? 1 push(16) 2 55-7 16?? 1 pop 1 55-7 16?? 16 push(-8) 2 55-7 -8?? 1 pop 1 55-7 -8?? -8 pop 0 55-7 -8?? -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.)?

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

Data Structures 19 1. 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.)

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

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

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?): 5 9 8 + 4 6 ( ( 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 ;

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?

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

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?

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.

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

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: >@?A> B %, and pairs 7CEDF indicating that event C Topological ordering: given a sequence of should occur prior to F, provide a schedule.

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?

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?

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& >@N > 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.

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)); } };

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)); } };

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) 0 1 55??? insert(-7) 0 2 55-7?? insert(16) 0 3 55-7 16? remove() 1 3 55-7 16? 55 insert(-8) 1 0 55-7 16-8 remove() 2 0 55-7 16-8 -7 remove() 3 0 55-7 16-8 16 insert(11) 3 1 11-7 16-8 Note that some of the values remain physically in the array, but are logically no longer in the queue.

Data Structures 35 Queues: Linked List Implementation We can maintain JR&$ S and C HG pointers. & >@N?A> : 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.