ADTs,, Arrays, Linked Lists



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

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

DATA STRUCTURES USING C

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

10CS35: Data Structures Using C

STACKS,QUEUES, AND LINKED LISTS

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

Lecture 12 Doubly Linked Lists (with Recursion)

PES Institute of Technology-BSC QUESTION BANK

LINKED DATA STRUCTURES

Data Structure [Question Bank]

Analysis of a Search Algorithm

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

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

Data Structures and Algorithms Lists

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

Sequential Data Structures

Linked List as an ADT (cont d.)

Chapter 3: Restricted Structures Page 1

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

Common Data Structures

7.1 Our Current Model

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Node-Based Structures Linked Lists: Implementation

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

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

D06 PROGRAMMING with JAVA

CmpSci 187: Programming with Data Structures Spring 2015

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

Chapter 8: Bags and Sets

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

Sequences in the C++ STL

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

Data Structures. Level 6 C Module Descriptor

Algorithms and Data Structures

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

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 14 The Binary Search Tree

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

Linked Lists: Implementation Sequences in the C++ STL

recursion, O(n), linked lists 6/14

Unordered Linked Lists

Industrial Programming

Data Structures and Data Manipulation

Ordered Lists and Binary Trees

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

Binary Heap Algorithms

CS 111 Classes I 1. Software Organization View to this point:

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

Sample Questions Csci 1112 A. Bellaachia

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

Universidad Carlos III de Madrid

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

Data Structures and Algorithms Stacks and Queues

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

Data Structures and Algorithms Written Examination

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

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Lecture Notes on Binary Search Trees

Glossary of Object Oriented Terms

Introduction to Data Structures and Algorithms

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

Data Structures UNIT III. Model Question Answer

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Data Structures and Algorithms

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:

AP Computer Science AB Syllabus 1

Circular Linked List. Algorithms and Data Structures

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

Introduction to Object-Oriented Programming

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

Basic Data Structures and Algorithms

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

Singly linked lists (continued...)

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

The ADT Binary Search Tree

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Linked Lists Linked Lists, Queues, and Stacks

Java Software Structures

Two Parts. Filesystem Interface. Filesystem design. Interface the user sees. Implementing the interface

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

Data Structures Using C++

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

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Project 4 DB A Simple database program

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

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

CSE 211: Data Structures Lecture Notes VII

Introduction to data structures

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

TECHNICAL UNIVERSITY OF CRETE DATA STRUCTURES FILE STRUCTURES

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Binary Search Trees (BST)

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Zabin Visram Room CS115 CS126 Searching. Binary Search

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

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

Transcription:

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 implementation details are not specified! Abstract Data Type entity that consists of: 1) data structure (DS) 2) set of operation supported on the DS 3) error conditions ADT Data Structure Interface add() remove() find() request result Basic Data Structures array (used in advanced ADT) linked list

Abstract Data Type (ADT) (cont.) 3 Data Structure Interface add() remove() find() request result Designer s responsibility: choice of data structure implementation of operations User s requirements: correct performance efficient performance The interior mechanisms of an implemented ADT should be hidden and inaccessible to the user! (Remember encapsulation, i.e. information hiding, OO-programming principle!)

Abstract Data Type (ADT) (cont.) 4 Standard ADTs Stacks, Queues, Vectors, Lists, Trees, Why should we know standard ADTs? standard ADTs are great reusable components - can be effectively used in solving many real world problems we may be required to adapt algorithms which use some of the standard ADTs What should we know about standard ADTs? (1) what operations they support (2) complexity of supported operations (3) memory cost of operations

ADT Taxonomy 5 Linear ADTs - we call an ADT linear, if the following is true: (1) there is a unique first element (2) there is a unique last element (3) every element has a unique predecessor (except 1 st ) (4) every element has a unique successor (except last) 0 1 2 3 4 5 6 7 A1 A2 Non-linear ADTs - if one or more of the above is not true, the ADT is non-linear A1 A2 A3 A1

Abstract Data Type (ADT) (cont.) 6 Example 1 [ selecting an ADT ] (a) If organizing a tour route, where we have to add/delete a city - use Linked List. (c) If managing a telephone directory that should provide short search times - use Sorted Tree.

Array 7 Array sequence of indexed components, with the following general properties: array size is fixed at the time of array s construction int numbers = new numbers [10]; any array component can be efficiently inspected or updated using its index, in O(1) time randomnumber = numbers[5]; numbers[2] = 100; Index = 0 1 2 3 4 5 6 7 Element at position 5 Array of length 8 Major Limitation size fixed, and must be known in advance

Properties of Java Arrays 8 (1) for an array of length n, the index bounds are 0 and (n-1) (2) array elements are stored in side by side memory locations (3) every array is homogeneous - all its components must be of the same type (4) an array is itself an object it is allocated dynamically by means of new it is automatically deallocated when no longer referred to

Linked List 9 Linked List sequence of nodes arranged one after another, with each node connected to the next by a link (like a chain) each node is an object containing: 1) a single element - stored object or value 2) links - reference to one or both neighbouring nodes each node (except the last one) has a successor, and each node (except the first one) has a predecessor node A1 head node A2 An tail node element link NOTE: neighbouring nodes can be far away physically!

Properties of Linked Lists 10 (1) linked list can be of any length, i.e. it can contain any number of elements, and it can grow (2) the element in any node can be accessed, however we must hold a link to that node (3) nodes can be inserted and deleted ordering of nodes can be changed in minimal running time (4) there are two different types of linked lists Singly Linked List (each node is linked to one of its neighbours) Doubly Linked List (each node is linked to both of its neighbours)

Object Reference 11 Reference Variable contains the location (address) of an object when we declare a reference variable, it does not reference anything, i.e. it is initialized to null if we attempt to use a reference variable before initiating an object for it, NullPointerException will be thrown Integer intref; intref = new Integer(5); Reference intref Reference intref null 5 Integer Object

Object Reference (cont.) 12 Declaring Reference Variables Integer p, q; p q Allocating an Object p = new Integer(5); p 5 Allocating Another Object p = new Integer(6); 5 p marked for garbage collection 6 Assigning a Reference q = p; p q 6

Object Reference (cont.) 13 Allocating an Object q = new Integer(9); p q 6 9 Assigning null to a Reference Variable p = null; p q marked for garbage collection 6 9 Assigning a Reference with a null Value q = p; p q null 9 marked for garbage collection

Singly Linked List 14 Singly Linked List each node contains a data-element together with a link to its successor A1 head node A2 An tail node SLLNode public class SLLNode { Object element; SLLNode next; public SLLNode(Object elem, SLLNode succ) { } this.element = elem; this.next = succ; } Reference variables!

Singly Linked List (cont.) 15 SLLNode Complying with Requirements hidden and inaccessible public class SLLNode { private Object element; private SLLNode next; public SLLNode(Object elem, SLLNode succ) { this.element = elem; this.next = succ; } public Object getelement() { return element; } public SLLNode getnext() { return next; } public void setelement(object newelement){ element = newelement; } public void setnext(sllnode newnext){ next = newnext; } }

Singly Linked List (cont.) 16 Creating and Linking Two SLLNodes SLLNode n = new SLLNode(new Integer(5), null); n 5 SLLNode first = new SLLNode(NewInteger(9), n) n first 5 9 SLLNode n = new SLLNode(NewInteger(9), n) n 5 9 Should, in the 2 nd case, the first node be collected by the garbage collection!?

Singly Linked List (cont.) 17 SLL A1 A2 An-1 An head tail public class SLL { private SLLNode head; private SLLNode tail; public SLL() { this.head = null; this.tail = null; } Constructs an empty linked list! It is a good practice to maintain direct references to head and tail; with them: 1) easy to delete or insert new node at the front of SLL; 2) easy to insert new node at the rear. But, it is still costly to delete the end node. Why?!

Singly Linked List (cont.) 18 Adding New Node at the Rear of SLL with Reference to Head Only! A1 A2 An head curr public class SLL { } private SLLNode head;... public void addlast(sllnode newnode){ SLLNode curr; if (head==null) head=newnode; else { } for (curr = head; curr.getnext()!= null; curr=curr.getnext()){ }; curr.setnext=newnode; }

Singly Linked List (cont) 19 Example 1 [ SLL traversal ] public void traversesll() { for (SLLNode curr = head; curr!= null; curr = curr.getnext()) { System.out.print(curr.element + ); } } Example 2 [ deletion of 1 st SLL node ] public void deletefirst() {... head = head.next; } A1 A2 head

Singly Linked List (cont) 20 Example 3 [ deletion of 1 st SLL node, with memory management ] public void deletefirst() {... } curr = head; head = head.next; curr.setnext(null); curr = null; A1 A2 marked for garbage collection head

Singly Linked List (cont) 21 Example 4 [ deletion of SLL node after node referenced by prev ] public void delete(sllnode prev) {... } SLLNode curr = prev.getnext(); prev.setnext(curr.getnext()); curr.setnext(null); curr = null; A1 Ak Ak+1 head prev marked for garbage collection Ak+2

Singly Linked List (cont) 22 Example 5 [ insertion of SLL node after node referenced by prev ] public void insert(object element) {... } SLLNode curr = prev.getnext(); SLLNode newnode = new SLLNode(element, curr); prev.setnext(newnode); Ak+1 A1 Ak Ak+1 head prev

Doubly Linked List 23 Doubly Linked List each node contains an element together with a link to its predecessor and a link to its successor A1 A2 An DLLNode public class DLLNode { private Object element; private DLLNode prev, next; public DLLNode(Object elem, SLLNode pred, DLLNode succ) { this.element = elem; this.prev = pred; this.next = succ; } }

Arrays vs. Single- and Double- Linked Lists 24 Guidelines for Choosing Between an Array and a Linked List ADT Requirement frequent random access operations add/remove at a cursor add/remove at a two-way cursor frequent capacity changes Suggested Implementation Use an array. Use a singly linked list. Use a doubly linked list. Use a linked list.

Questions 25 Q.1 Suppose, in your program, you have been using a collection of numbers, which has been stored in an array of size 1000, named intcollection. (int intcollection = new int[1000];) However, you do not need this collection any longer, and you want to free the memory. What should you do? Q.2 Examine the following code, and determine how the corresponding SLL (the sequence of SLL s elements) looks like. SLLNode c = new SLLNode( not to be, null); SLL phrase = SLL(); phrase.head = new SLLNode( to be, new SLLNode( or, c) ); Q.3 Repeat Examples 1 to 5 for Doubly Linked List. Q.4 Write a short program that swaps the 1 st and 2 nd node of a) a singly linked list (SLL) b) a doubly linked list (DLL)