1 / 14

CSC 212 – Data Structures

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

dorit
Download Presentation

CSC 212 – Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 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…

  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 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

  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 1st Node in list// Could add size field, but why?...// Methods go here, but don’t want to ruin// your “fun” }

  10. 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 

  11. 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

  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 head A B C D

  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

More Related