Introduction to Object-Oriented Programming



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

DATA STRUCTURES USING C

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

10CS35: Data Structures Using C

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

Unordered Linked Lists

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:

Data Structures Fibonacci Heaps, Amortized Analysis

Lecture 12 Doubly Linked Lists (with Recursion)

PES Institute of Technology-BSC QUESTION BANK

Data Structures and Algorithms Lists

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

Analysis of a Search Algorithm

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

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

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

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.

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

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Data Structures and Algorithms

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

Data Structure [Question Bank]

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

Node-Based Structures Linked Lists: Implementation

Data Structures and Algorithms

D06 PROGRAMMING with JAVA

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

Chapter 14 The Binary Search Tree

Data Structures. Level 6 C Module Descriptor

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

Lecture Notes on Binary Search Trees

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

Class 32: The Java Collections Framework

Ordered Lists and Binary Trees

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

Section IV.1: Recursive Algorithms and Recursion Trees

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

AP Computer Science AB Syllabus 1

Searching Algorithms

DATA STRUCTURE - STACK

Basic Programming and PC Skills: Basic Programming and PC Skills:

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Binary Search Trees CMPSC 122

Sample Questions Csci 1112 A. Bellaachia

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

Introduction to Object-Oriented Programming

Binary Heap Algorithms

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

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

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

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

ADTs,, Arrays, Linked Lists

LINKED DATA STRUCTURES

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

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

Introduction to Data Structures

Converting a Number from Decimal to Binary

Java Software Structures

Heaps & Priority Queues in the C++ STL 2-3 Trees

Lecture Notes on Binary Search Trees

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

Efficient Data Structures for Decision Diagrams

Data Structures and Algorithms Written Examination

Lecture 2: Data Structures Steven Skiena. skiena

Persistent Binary Search Trees

JAVA COLLECTIONS FRAMEWORK

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

TREE BASIC TERMINOLOGIES

CmpSci 187: Programming with Data Structures Spring 2015

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

CIS 631 Database Management Systems Sample Final Exam

Glossary of Object Oriented Terms

Binary Search Trees (BST)

7.1 Our Current Model

Data Structures and Data Manipulation

A COMPARATIVE STUDY OF LINKED LIST SORTING ALGORITHMS

Relational Databases. Christopher Simpkins

Data Structures and Algorithms(5)

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

Sequences in the C++ STL

Linked Lists: Implementation Sequences in the C++ STL

Circular Linked List. Algorithms and Data Structures

Data Structures in the Java API

Binary search algorithm

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

COMPUTER SCIENCE. Paper 1 (THEORY)

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

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

CMSC 202H. ArrayList, Multidimensional Arrays

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

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

Introduction to Data Structures and Algorithms

Physical Data Organization

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

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

Transcription:

Introduction to Object-Oriented Programming Linked Lists Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Linked Lists 1 / 12

Linked Lists Dynamic data structures Singly linked lists Generic linked lists Doubly linked lists CS 1331 (Georgia Tech) Linked Lists 2 / 12

Dynamic Data Structures ArrayList was our first dynamically-allocated data structure. ArrayList is a hybrid static and dynamically allocated data structure. ArrayList automatically allocates a larger backing (static) array when the capacity of its current backing array is exceeded. In a purely dynamic data structure, storage for each new element is allocated when the element is added. A purely dynamically allocated data structure is (slightly) more space efficient, but slower because heap allocation occurs every time an element is added. CS 1331 (Georgia Tech) Linked Lists 3 / 12

Linked Data Structures The core concept in a linked data structures is the node. A data structure is a collection of nodes linked in a particular way. A node holds a data item and links to other nodes. The nature of the links determines the kind of data structure, e.g., singly linked list, doubly linked list, binary tree, etc. Here is a depiction of a node for a singly linked list and code that implements such a node (public members for brevity) private class Node { public Object data; public Node next; } public Node(Object data, Node next) { this.data = data; this.next = next; } CS 1331 (Georgia Tech) Linked Lists 4 / 12

Singly Linked Lists A singly linked list Contains a pointer to a head node (null if empty). The head node s next points to the second node, the second node s next points to the third node, and so on. The next reference of the last node is null Here s a depiction of the nodes in a singly linked list with three elements: CS 1331 (Georgia Tech) Linked Lists 5 / 12

Adding Elements to a Singly Linked List 1. Create a new Node. 2. Set the next reference of the new Node to the current head. 3. Set the head reference to the new Node See LinkedList.java for the code. CS 1331 (Georgia Tech) Linked Lists 6 / 12

Finding an Item in a Linked List An algorithm for finding an item in a linked list: foundnode: Node := null currentnode: Node := LinkedList.head while currentnode!= null && foundnode = null if currentnode.data = queryitem foundnode := currentnode currentnode := currentnode.next The postcondition of this algorithm is that foundnode will be: The node containing the query item, or null if the query item is not in the list. CS 1331 (Georgia Tech) Linked Lists 7 / 12

Inserting Elements into a Linked List 1. Find the existing Node to insert new element after. 2. Create a new Node. 3. Set the next reference of the new Node to the next reference of the existing node. 4. Set the next reference of the existing node to the new Node. See LinkedList.java for the code. CS 1331 (Georgia Tech) Linked Lists 8 / 12

Computing the Length of a Linked List An algorithm for computing the length a linked list: length: int := 0 currentnode: Node := LinkedList.head while currentnode!= null length := length + 1 currentnode := currentnode.next The postcondition of this algorithm is that length will be equal to the number of nodes in the list. CS 1331 (Georgia Tech) Linked Lists 9 / 12

Generic Linked Lists To make our LinkedList generic we only need to add a type parameter to the declaration: public class GenericLinkedList<E> {... and replace Object with E in the body of the class. See GenericLinkedList.java CS 1331 (Georgia Tech) Linked Lists 10 / 12

Doubly Linked Lists A doubly linked list simply adds a previous reference to the Node class so that the list can be traversed forwards or backwards. private class Node<E> { E data; Node<E> next; Node<E> previous; } public Node(E data, Node<E> next, Node<E> previous) { this.data = data; this.next = next; this.previous = previous; } Doubly linked lists work the same, except that the algorithms for inserting and removing items requires a bit more link fiddling (have to set previous links as well). See DoublyLinkedList.java. CS 1331 (Georgia Tech) Linked Lists 11 / 12

Programming Exercises Programming Exercise 1 Add a get(int index) method to GenericLinkedList. Add a remove(t item) method to GenericLinkedList. Programming Exercise 2 Implement public static int binarysearch(int[] a, int v). Return -1 if v is not in a. Bonus: implement public static <T> int binarysearch(t[] a, T v, Comparator<? super T> c). Return -1 if v is not in a. Bonus: for either of the options above, implement your method using a recursive helper method. Bonus question: if we wanted to implement a similar method for a Collection, how would we do it? Could we define such a binary search method for any Collection? Bonus question 2: what is the running time (Big-O) of binary search? CS 1331 (Georgia Tech) Linked Lists 12 / 12