Docsity

Singly Linked Lists: Data Structures and Advanced Programming CS062, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Singly Linked Lists: Data Structures and Advanced Programming CS062 and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

CS

DATA STRUCTURES AND ADVANCED PROGRAMMING

7: Singly Linked Lists

Some slides adopted from Princeton C0S226 course or Algorithms, 4th Edition

BASIC DATA STRUCTURES

Tom Yeh he/him/his

ARRAYLIST add(Item item):Appends the item to the end of the ArrayList

CS062 ROCKS

Front End/Rear Capacity = 4 Number of items = 3

al.add(“!”); DOUBLE CAPACITY SINCE IT’S FULL AND THEN ADD NEW ITEM INCREASE NUMBER OF ITEMS What should happen? al.add(1, “THROWS”);

THEORY OF ALGORITHMS Asymptotic analysis simplifies analyzing worst-case performance ▸ Sorting them by increasing rate of growth: ▸ O (^1 ),^ O (log^ n ),^ O ( n ),^ O ( n^ log^ n ),^ O ( n

), O ( n

), O ( 2 n ), O ( n !)

THEORY OF ALGORITHMS How to interpret Big O ▸ or "order one”: running time does not change as size of the

problem changes, that is running time stays constant and independent

of problem size.

▸ or "order log n”: running time increases as problem size grows.

Whenever problem size doubles, running time increases by a constant.

▸ or "order n”: time increases proportionally to the the rate of growth

of the size of the problem, that is in a linear rate. Double the problem

size, you get double running time.

▸ or "order n squared”: Double the problem size you get quadruple

running time.

O ( 1 ) O (log n ) O ( n ) O ( n 2 )

RUNNING TIME OF ARRAYLIST OPERATIONS Worst-case performance of add() is O ( n ) ‣Cost model: 1 for insertion, for copying items to a new array. ‣Worst-case: If ArrayList is full, add() will need to call resize to create a new array of double the size, copy all items, insert new one. ‣Total cost:. ‣Realistically, this won’t be happening often and worst-case analysis can be too strict. We will use amortized time analysis instead. n n n + 1 = O ( n )

RUNNING TIME OF ARRAYLIST OPERATIONS Amortized analysis ‣Amortized cost per operation: for a sequence of operations, it is the total cost of operations divided by. ‣Simplest form of amortized analysis called aggregate method. More complicated methods exist, such as accounting (banking) and potential (physicist’s). n n

RUNNING TIME OF ARRAYLIST OPERATIONS Amortized analysis for n add() operations when increasing ArrayList by 1. ‣ (^) (“cost of insertions”). ‣ (^) (“cost of copying”) =. ‣ (^) total cost) , therefore amortized cost is or. ‣Same idea when increasing ArrayList size by a constant. ‣This is why in the lab yesterday, we saw that doubling was the fastest and linear(1) the slowest 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (^1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ) Insertion Cost 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Copying Cost 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Total Cost 17 ∑ = n ∑ 1 + 2 + 3 +... + n − 1 = n ( n − 1 )/ 2 O ( = n + n ( n − 1 )/ 2 = n ( n + 1 )/ 2 ( n + 1 )/ 2 O ( n )

ASSIGNED READINGS AND PRACTICE PROBLEMS Readings: ▸ Oracle’s guides: ▸ Collections:^ https://docs.oracle.com/javase/tutorial/collections/intro/index.html ▸ ArrayLists:^ https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ▸ Textbook: ▸ Chapter 1.3 (Page 136–137) ▸ Chapter 1.4 (pages 197–199) ▸ Textbook Website: ▸ Resizable arrays:^ https://algs4.cs.princeton.edu/13stacks/ ▸ Analysis of Algorithms: https://algs4.cs.princeton.edu/14analysis/ Practice Problems: ▸ 1.4.1, 1.4.5 - 1.4.7, 1.4.32, 1.4.35-1.4.36.

TODAY’S LECTURE IN A NUTSHELL Lecture 7: Singly Linked Lists ▸ Singly Linked Lists Some slides adopted from Algorithms 4th Edition and Oracle tutorials

SINGLY LINKED LISTS Singly Linked Lists ‣ (^) Dynamic linear data structures. ‣ (^) In contrast to sequential data structures, linked data structures use pointers/links/references from one object to another.

? CS!

Head/Beginning/Front/First

SINGLY LINKED LISTS Node private class Node { Item item; Node next; } item Node

SINGLY LINKED LISTS Standard Operations

‣ SinglyLinkedList(): Constructs an empty singly linked list.

‣ isEmpty():Returns true if the singly linked list does not contain any item.

‣ size(): Returns the number of items in the singly linked list.

‣ Item get(int index): Returns the item at the specified index.

‣ add(Item item): Inserts the specified item at the head of the singly

linked list.

‣ add(int index, Item item): Inserts the specified item at the

specified index.

‣ Item remove(): Removes and returns the head of the singly linked list.

‣ Item remove(int index): Removes and returns the item at the

specified index.

SINGLY LINKED LISTS SinglyLinkedList(): Constructs an empty SLL first = null n = 0 SinglyLinkedList sll = new SinglyLinkedList(); What should happen? sll.add(“CS062”);

SINGLY LINKED LISTS add(Item item):Inserts the specified item at the head of the singly linked list sll.add(“CS062”) n= CS Head/Beginning/Front/First What should happen? sll.add(“ROCKS”);