Download presentation
Presentation is loading. Please wait.
1
CSC 212 – Data Structures Lecture 13: Linked Lists
2
Classes Every class & interface must be in own file File should have name of class/interface Files should be in same directory Subclass extends its superclass Extends exactly 1 class Subclass inherits methods & fields Makes no difference if inherited from superclass’s superclass Can use all that are not private
3
Interfaces Interfaces create 2 nd mode of inheritance Can only declare public abstract methods Provides “contract” of functionality Classes implement interfaces Can implement any number of interfaces Must have definition for all interface methods Automatically include interfaces implemented by superclasses Get more used to these as we continue…
4
Arrays Nice for simple storage & retrieval of data Bad when moving data around Ever try to resize an array? Guess too small and program cannot run Guess too large and memory wasted
5
Performance at Limited Memory
6
Better Option Solve this problem using linked lists Implementation that grows & shrinks with data Also our first recursive structure Many implementations of linked list Which one is best? Depends on situation Each has own strengths & weaknesses This will be a recurring refrain for this class Important to understand how each works
7
Singly Linked List A singly linked list is sequence of nodes Node contains element reference to next Node elem node Singly Linked List With 4 Elements ABCD next
8
Our First (Not Last) Node Class public class Node { private String element; private Node next; public Node() { this(null, null); } public Node(String e, Node n) { element = e; next = n; } public String getElement() { return element; } public Node getNext() { return next; } public void setElement(String newE) { element = newE; } public void setNext(Node newNext) { next = newNext; } }
9
Singly Linked List public class SList { private Node head; // head is null list is empty // else head references 1 st Node in list // Could add size field, but why?... // Methods go here, but don’t want to ruin // your “fun” }
10
Inserting at Head 1. Allocate new node 2. New node’s next field alias old head 3. Alias head to new node Done! BCD head BCD A BCD A
11
Removing a Node 1. Find node before target in list 2. If target not the head a) Node’s next field alias target’s next 3. Else a) Reassign head Done! BCD head A trav BCD head A trav
12
Doubly Linked List DNode subclass of Node; adds prev field DList is not subclass of List Defines identical methods… …all of which need to be redefined Now, let’s have some volunteers…
13
Circular Linked List Nodes identical to doubly linked list Big difference last node contains reference to first Notice this becomes a big circle Now you know the idea behind the name BCD head A
14
Before Next Lecture… Do week #5 assignment Continue programming assignment #1 It is due Friday Start reviewing for Midterm #1 It will be on Monday Fridays’s lecture introduces recursion Read Section 3.5 of the book If you don’t read, you won’t understand, and I don’t care
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.