140 likes | 315 Views
CSC 212 – Data Structures. Lecture 13: Linked Lists. 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
E N D
CSC 212 –Data Structures Lecture 13: Linked Lists
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
Interfaces • Interfaces create 2nd 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…
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
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
Singly Linked List With 4 Elements A B C D Singly Linked List • A singly linked list is sequence of nodes • Node contains • element • reference to next Node next elem node
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;} }
Singly Linked List public class SList {private Node head;// head is null list is empty// else head references 1st Node in list// Could add size field, but why?...// Methods go here, but don’t want to ruin// your “fun” }
Allocate new node New node’s next field alias old head Alias head to new node Done! Inserting at Head head B C D head A B C D head A B C D
Find node before target in list If target not the head Node’s next field alias target’s next Else Reassign head Done! Removing a Node head A B C D trav head A B C D trav
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…
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 head A B C D
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