Linear Data Structures

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

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Information Science 2

Vectors. 9/14/2007 2:55 PM Vectors 1

CH 6 : VECTORS, LISTS AND SEQUENCES

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CH 6 : VECTORS, LISTS AND SEQUENCES

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

1 P age DS & OOPS / UNIT II

csci 210: Data Structures Stacks and Queues

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

PA3 Design Specification

CH 6. VECTORS, LISTS, AND SEQUENCES

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering


LIFO : Last In First Out

Lecture 3: Stacks & Queues

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

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

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Stack and Queue. Stack:

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

Arrays. Array Definition

CH7. LIST AND ITERATOR ADTS

Lecture 3 Linear Data Structures

infix expressions (review)

Lists ADT and Iterators

L11: Improving the Problem not the Code

Foundations of Data Structures

CSC 1052 Algorithms & Data Structures II: Queues

This lecture. Iterators ( 5.4) Maps. Maps. The Map ADT ( 8.1) Comparison to java.util.map

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

The java.util.list ADT

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

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

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

CSED233: Data Structures (2017F) Lecture7: Lists and Iterators

Lecture 3 Linear Data Structures

Ashish Gupta, Data JUET, Guna

Stacks Goodrich, Tamassia, Goldwasser Stacks

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.

Summer Final Exam Review Session August 5, 2009

Dynamic Data Structures

DATA STRUCTURES AND ALGORITHMS

Department of Computer Science and Technology

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

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

THE UNIVERSITY OF WESTERN AUSTRALIA

INSTITUTE OF AERONAUTICAL ENGINEERING

CS8391-DATA STRUCTURES

Introduction to Data Structures

Your Topic Goes Here Queue

Outline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack.

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

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

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

Data structure and algorithm in Python

CSED233: Data Structures (2018F) Lecture3: Arrays and Linked Lists

Memory Management Prof. James L. Frankel Harvard University

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

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

data structures and algorithms lecture 6

Iterators and Sequences

About this exam review

Lists and Sequences. 1/18/2005 4:12 AM Sequences 1

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

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

Lecture 4 Stack and Queue

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Data Structures and Algorithms Notes

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Lecture 16. Lecture

17CS33:Data Structures Using C QUESTION BANK

Stacks and Queues. CSE 373 Data Structures Lecture 6

Arrays & Linked Lists

Chapter 18: Stacks And Queues

Data Structures Lecture 5

CS301 - Data Structures Glossary By

Computer Science 62. Midterm Examination

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

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

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

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

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

Lecture 3 Linear Data Structures

Lecture No.04. Data Structures

Abstract Data Types! Documentation!

Array Lists Outline and Required Reading: Array Lists ( 7.1) CSE 2011, Winter 2017, Section Z Instructor: N. Vlajic

Linked Lists

CS 8391 DATA STRUCTURES

Linked Structures. See Section 3.2 of the text.

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

Transcription:

Linear Data Structures

Arrays Arrays are stored in contiguous memory locations and contain similar data An element can be accessed, inserted or removed by specifying its position (number of elements preceding it) Deletion: In operation removeatposition(r), we need to fill the hole left by the removed element by shifting backward the n - r - 1 elements V[r + 1],, V[n - 1]. In the worst case (r = 0), this takes O(n) time. In the average case also it is O(n) time. Is it necessary to shift the array elements after deletion Insertion: In operation insertatposition(r, o), we need to make room for the new element by shifting forward the n - r elements V[r] V[n - 1]. In the worst case (r = 0), this takes O(n) time. In the average case also it is O(n) time.

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

Link Lists A singly linked list is a concrete 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 Operations on a link list 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 every deletion operation will actually be preceded by accessing or reaching that position of the element, effectively taking O(n) time. Delete first will take O(1) time

Link Lists Insert (): O(1) complexity, but inserting before or after an element will require accessing element effectively taking O(n) time. Inserting at first place will take O(1) time but insertion in array at first place will take O(n). Merging: O(1)Time complexity. 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 )

Doubly and Circular Link List A doubly linked list provides a natural implementation of the List Nodes implement Position and store: Element link to the previous node link to the next node Special trailer and header nodes 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. List: we specify the element by its position: example is the link list

Arrays Vs Link List 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 Due to Address part there is wastage of memory in link list, it is not much if we look at practical size of individual record consisting of name, age, address etc We have to predefine the array so there can be wastage of memory Array Size has to be defined and is limited to the defined size while link list can grow to limit of memory

Arrays Vs Link List Continuous allocation is required for array, while this is not with link list Arrays can be accessed backward and forward but we have to use special link lists like doubly linked list for backward access 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

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 a priori and cannot be changed Incremental strategy: increase the size by a constant c Doubling strategy: double the size If we increment by C every time then the creation, copying, insertion cost will be as follows Creation copying insertion (iteration wise) c*i c(i-1) c Total = 2ci if there are n elements in the end then the total number of iteration will be n/c So it comes out to be 2c(1+2+3+..n/c) = 2c(n/c)(n/c +1)/2 = n 2 /c = O(n 2 )

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+1 if there are n elements to be inserted then there will be lgn iterations so the total becomes (2 1 +2 2 +2 3 + + 2 lgn+1 )= 4n-1 = O(n)

Stacks Last in First out, in and out only from one end, other end is closed Direct applications Page-visited history in a Web browser, Undo sequence in a text editor, Chain of method calls in the Java Virtual Machine, Function Calls, Recursion, in other data Structures Creation of an stack: O(n) push(object): inserts an element O(1)

Stack pop(): removes and returns the last inserted element O(1) top(): returns the last inserted element without removing it O(1) size(): returns the number of elements stored O(1) isempty(): indicates whether no elements are stored O(1) isfull(): indicates whether array limit is over O(1) axioms pop(push(s,v))=s Top(push(S,v))=v

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 operations: (complexity may vary depending upon the implementation, whether front or rear is fixed or not) enque(object o): inserts an element o at end of the queue time is O(1) deque(): removes and returns element at the front of the queue O(1) front(): returns the element at the front without removing it O(1) size(): returns the number of elements stored O(1) isempty(): returns a Boolean indicating whether no elements are stored O(1)

Queues Direct applications Waiting lists, bureaucracy Access to shared resources (e.g., printer) Multiprogramming Tunnel In other Data Structures 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