Arrays, Link Lists, Stacks and Queues

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

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

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

10CS35: Data Structures Using C

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

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

STACKS,QUEUES, AND LINKED LISTS

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

Universidad Carlos III de Madrid

DATA STRUCTURES USING C

Chapter 3: Restricted Structures Page 1

Sequential Data Structures

DATA STRUCTURE - QUEUE

Module 2 Stacks and Queues: Abstract Data Types

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

Common Data Structures

PES Institute of Technology-BSC QUESTION BANK

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

CmpSci 187: Programming with Data Structures Spring 2015

Programming with Data Structures

Analysis of a Search Algorithm

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

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

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

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

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

Data Structure [Question Bank]

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!

Unordered Linked Lists

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

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

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

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

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

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

Data Structures and Algorithms Stacks and Queues

Chapter 8: Bags and Sets

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

LINKED DATA STRUCTURES

Data Structures. Level 6 C Module Descriptor

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

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

Node-Based Structures Linked Lists: Implementation

Introduction to Stacks

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

Introduction to Data Structures and Algorithms

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

Lecture 1: Data Storage & Index

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

Record Storage and Primary File Organization

BCS2B02: OOP Concepts and Data Structures Using C++

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

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.

Chapter 13. Disk Storage, Basic File Structures, and Hashing

Algorithms and Data Structures

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

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Data Structures UNIT III. Model Question Answer

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

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

7.1 Our Current Model

Data Structures and Data Manipulation

Data Structures Fibonacci Heaps, Amortized Analysis

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

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 13-1

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Computer Programming using C

Data Structures and Algorithms

Chapter 13 Disk Storage, Basic File Structures, and Hashing.

AP Computer Science AB Syllabus 1

Data Structures in the Java API

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

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

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

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

DATA STRUCTURE - STACK

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

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Chapter 13: Query Processing. Basic Steps in Query Processing

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

PROBLEMS (Cap. 4 - Istruzioni macchina)

Linked Lists Linked Lists, Queues, and Stacks

INTRODUCTION The collection of data that makes up a computerized database must be stored physically on some computer storage medium.

Java Software Structures

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

Linked Lists: Implementation Sequences in the C++ STL

ADTs,, Arrays, Linked Lists

Chapter 13. Chapter Outline. Disk Storage, Basic File Structures, and Hashing

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

PROBLEMS. which was discussed in Section

Lecture 12 Doubly Linked Lists (with Recursion)

Sequences in the C++ STL

Introduction to Programming System Design. CSCI 455x (4 Units)

D06 PROGRAMMING with JAVA

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

Data Structures and Algorithms Lists

Abstract Data Types. Chapter 2

Queue Implementations

Chapter 8: Structures for Files. Truong Quynh Chi Spring- 2013

Transcription:

Arrays, Link Lists, Stacks and Queues

Deliverables Arrays Link Lists Stacks Queues 2

Arrays Arrays are stored in contiguous memory locations and contain similar data. Position of any array element can be accessed from its index position with help of array s starting address. An element can be accessed, inserted or removed by specifying its position (number of elements preceding it) Vector is abstraction of array data structure, where we can access by rank. 3

Array operations Deletion: In operation removeatposition(r), we need to fill the hole left by the removed element by shifting backward n-r-1 elements A[r + 1],, A[n - 1]. In the worst case (r = 0), this takes O(n) time. In the average case also it is O(n). A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 3 10 0-135 2 21 41 6 87 11 Is it necessary to shift the array elements after deletion? 4

Deletion of -34 5 23-34 1 81 9 123 45 2-31 After deletion of -34 5

Insertion Insertion in the array again requires shifting all the elements one index ahead to make room for insertion. Need to understand shifting different in deletion and insertion, otherwise data may be lost. In Insertion shifting is to start from the last element of the array and in deletion shifting is to start from the next array index from where the element is to be deleted. 6

Inserting 100 after -34 5 23-34 1 81 100 9 123 45 2 7

Array complexity Accessing an array: O(1) time Copying an array: O(n) time Merging two arrays: O(m+n) time Splitting an array: O(n) time Intersection of two arrays: O(n 2 ) Union of two arrays: O(n 2 ) 8

Array incrementing by C or doubling Let n be the number of elements in the array. The space used is O(n) and each operation runs in time O(1) The maximum size of the array must be defined apriori and cannot be changed. Incremental strategy: increase the size by a constant C Doubling strategy: double the size 9

Array incrementing by C or Doubling If increment by C every time then Creation copying insertion (iteration wise) ci c(i-1) c Total = ci+ci-c+c=2ci If there are n elements in end then total iterations will be n/c, So it is 2c(1+2+3+..n/c) = n n 2 c( )( 1) 2 c c n 2 On ( ) 2 c 10

Array incrementing by C or Doubling If we double the array every time then the creation, copying, insertion cost will be as follows Creation copying insertion (iteration wise) 2 i 2 i-1 2 i-1 Total cost in every iteration is 2 i +( 2 i-1 +2 i-1 ) =2 i +2 i =2 i+1 if there are n elements to be inserted then there will be lgn iterations, total becomes (2 1 +2 2 +2 3 + + 2 lgn+1 )=4n-1=O(n) 11

Link List Singly linked list is a data structure consisting of a sequence of nodes Each node stores element and link to the next node There may be a header link and trailer link 12

Link list operations Access (): Accessing an element from a list Worst case: O (n) Average Case: 1+2+3+ n = n(n-1)/2 divided by n=n-1/2 = O(n) Delete (): O(1) complexity, but it is preceded by accessing that position, effectively taking O(n) time. Delete first takes O(1) time 13

Link list complexity Insert : O(1) but inserting before or after an element will require accessing element effectively taking O(n) time. Inserting at first place is O(1). Merging : O(1) If the trailer node information is not available then it will be equal to the length of the smaller list to reach at the end of the list for merging with the second list Size: O(n) time Intersection: O(n 2 ) Union: O(n 2 ) 14

Doubly link list A doubly linked list provides a natural implementation of the List Nodes implement Position and stores element, link to the previous node and link to the next node Special trailer and header nodes. Deletion an Insertion becomes easy as there is no need to remember the address of the previous node. 15

Insertion 16

Deletion 17

Circular link list There will be no difference in the time complexity of various operations Circular Link List and circular doubly link list Input output buffers use circular queue for buffering. 18

Comparison link list vs. array It is easier to delete in the link list it is easier to insert in the link list It is easier to access in the array We have to predefine the array so there can be wastage of memory 19

Comparison link list vs. array Array Size has to be defined and is limited to the defined size while link list can grow to limit of memory Continuous allocation is required for array, while this is not with link list Arrays compute address of data elements by arithmetic operations while in linked list addresses are in structure itself. Arrays can be accessed backward and forward but we have to use special link lists like doubly linked list for backward access 20

Comparison link list vs. array Arrays definition is part of the language construct but link list we have to create Merging two arrays is very difficult while merging link lists is easy There will be more cache misses in the Link list then in the arrays With large records, moving pointers is easier and faster than moving the items themselves. 21

Comparison link list vs. array There can be an array of pointers associated with a link list, in that case address part of the link list is not required. In link lists two different processors may work on two different parts of link list. Due to address part there is wastage of memory in link list, it is not much if we look at practical size of record of name, age etc. 22

Stack Last in First out, in and out only from one end, other end is closed Chain of method calls in the Java Virtual Machine, Function Calls Page-visited history in a Web browser Recursion Undo sequence in a text editor In other data Structures 23

Simulating Pop and Push 6 98 25 12 5 37 24

Stack Example 25

Stack Creation of an stack: O(n) push(object): inserts an element O(1) pop(): removes and returns the last inserted element. O(1) top(): returns last inserted element without removing O(1) size(): returns number of elements stored. O(1) isempty(): indicates whether no elements are stored. O(1) isfull(): Indicates whether array limit is over. O(1) 26

Link List Vs. Array Implementation Pop Array: return element at top and decrement top Linked list: return and remove at front of linked list Push Array: increment top and insert element. Must check for overflow! Linked list: insert element at front of linked list Array: more memory efficient Linked list: don t have to worry about overflow 27

Axioms pop(push(s, v))=s Top(push(S, v))=v Top(pop(push(push(S,12),15))) = top(push(s,12)) 28

Queues Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Two variables keep track of the front and rear. Careful implementation is necessary to avoid overlapping. Operations complexity may vary depending upon the implementation, whether front or rear is fixed or not 29

Queue Example 30

Simulating Enqueue and Dequeue 5 45 71 99 Perform Operations Enqueue 99 Dequeue Dequeue 31

Queues enque(object o): inserts an element o at end of queue. O(1) deque(): removes and returns front element of queue. O(1) front(): returns element at front without removing it. O(1) size(): returns number of elements stored. O(1) isempty():return s Boolean indicating queue is empty or not O(1) 32

Queue Applications Waiting lists, bureaucracy Access to shared resources (e.g. printer) Multiprogramming Tunnel In other Data Structures 33

Axioms Front(Enqueue(Enqueue(Q, w),v))=front(enqueue(q, w)) Deque(Enque(Enque(Q, w),v))=enque(deque(enque(q, w)),v) In an Enqueue operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one, Similar to what we did for an array based stack. 34

Circular queue Circular Queue is essentially implemented as a linear array in a wrapped around fashion where even rear pointer of the queue can come before the front pointer 35 r f

Questions, Comments and Suggestions 36

Question 1 What is result of Last operation in given sequence : Push(&);Push($);Push(@);Top();Pop();Push(#); Pop();Pop();Push(%);Pop(); 37

Question 2 What will be the result of last result of given priority queue if number 1 is the highest priority Enqueue(3), Enqueue(6), Enqueue(2),dequeue, dequeue, Enqueue(5),Enqueue(1),dequeue, dequeue 38

Question 3 In a Singly circular linked list organization, insertion of a record involves modification of A. One pointer B. Two pointers C. Three pointers D. Four pointer E. No Pointer 39