CIS3023: Programming Fundamentals for CIS Majors
This presentation is the property of its rightful owner.
Sponsored Links
1 / 28

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 PowerPoint PPT Presentation


  • 70 Views
  • Uploaded on
  • Presentation posted in: General

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. Linked Lists. “ A list is only as strong as its weakest link .” --Donald Knuth. Course Lecture Slides 23 July 2010. Ganesh Viswanathan. [News] Tropical storm Bonnie hit Biscayne Bay, FL this morning!. Credits: NOAA.

Download Presentation

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cis3023 programming fundamentals for cis majors ii summer 2010

CIS3023: Programming Fundamentals for CIS Majors IISummer 2010

Linked Lists

“A list is only as strong as its weakest link.” --Donald Knuth

Course Lecture Slides

23 July 2010

GaneshViswanathan


Cis3023 programming fundamentals for cis majors ii summer 2010

[News] Tropical storm Bonnie hit Biscayne Bay, FL this morning!

Credits: NOAA


Data structure

Data structure

  • A way of structuring, storing and organizing data in computers that facilitates efficient operations on the data.

  • Data structures help to devise algorithms for solving complex operations on the data in an efficient manner.


Data structures

Data structures

And many more!


Arraylist

ArrayList

  • To insert (remove) an element in (from) the interior of an ArrayList requires shifting of data and is a linear-time O(n) operation!


Linked list

Linked List

  • Is a collection of linked nodes (think of a chain!)

  • Memory allocation for nodes is non-contiguous

  • Sports constant time O(1) insertion and updates


Linked list variations

Linked List Variations

  • Singly-Linked Lists

  • Doubly-Linked Lists

  • Multi-Linked Lists

  • Circular Linked Lists

  • Unrolled Linked Lists (multiple elements in each node)

  • Lists with only head pointer nodes

  • With head and tail nodes

  • With head, tail and cursor (currently requested) nodes

And many more!


Singly linked list

Singly Linked List

  • A set of nodes, each containing some data and a link to the next node.

  • Dynamic data-structure.


Singly linked list1

Singly Linked List

  • A set of nodes, each containing some data and a link to the next node.

  • Dynamic data-structure.

  • Advantages:

    • Simple implementation,

    • Efficient, constant time O(1) insertion and removal operation.


Singly linked list2

Singly Linked List

  • Node : self-referencing structure

  • Link : reference to a node

  • Linkedlist: List of nodes

  • Has dedicatedhead and tail indicators


Operations on singly linked list

Operations on Singly Linked List

  • Create list

  • Add node

    • Add node at head

    • Add node at tail

    • Add node at index i

  • Remove node

    • Remove node at head

    • Remove node at tail

    • Remove node at index i

  • Iterate (traverse)the list

  • Find node at index

    • Find previous node to the one at index i

  • Set node at index ito new node

  • Size of list

  • Clear list


  • Create node

    Create (Node)

    • public class Node<T>

    • {

    • // data held by the node

    • public T nodeValue;

    • // next node in the list

    • public Node<T> next;

    • // default constructor with no initial value

    • public Node()

    • {

    • nodeValue = null;

    • next = null;

    • }...

    • }


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Add node

    Empty List

    If list is empty (head == null) Set both head and tail to point to new node


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Add node

    Add node to head

    Insert new node before current head node

    Two-step process


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Add node

    Add node to head

    1. Update the next link of a new node, to point to the current head node.

    2. Update head link to point to the new node.


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Add node

    Add node to tail

    Insert new node after current tail node

    Two-step process


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Add node

    Add node to head

    1. Update the next link of the current tail node, to point to the new node.

    2. Update tail link to point to the new node.


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Add node

    Add node at index i

    If node at index i represents head or tail node, Refer previous pseudo-code for “add node at head/ tail”

    Else

    Two-step process


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Add node

    Add node at index i

    1. Update link of the new node, to point to the "next" node.

    2. Update link of the "previous" node, to point to the new node.


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Remove node

    Remove node from list with only one node

    • Both head and tail point to the (same node) only existing node.

      • Remove link to the node from both the head and tail, by setting both to null.

      • Dispose off the node (set to null)


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Remove node

    Remove node from head

    • Remove the node pointed to by head.

    • Two-step process


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Remove node

    Remove node from head

    1. Update head link to point to the node, next to the head.

    2. Dispose removed node.


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Remove node

    Remove node from tail

    • Remove the node pointed to by tail.

    • Need access to the previous node of current tail.

    • Three-step process


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Remove node

    Remove node from tail

    1. Update tail link to point to the node, before the tail. In order to find it, list should be traversed first, beginning from the head.

    3. Dispose removed node.

    2. Set next link of the new tail to NULL.


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Remove node

    Remove node at index i

    • If index i is not 0 or n-1, this operation removes the node between two nodes.

    • Two-step process


    Cis3023 programming fundamentals for cis majors ii summer 2010

    • Singly Linked List: Remove node

    Remove node at index i

    1. Update next link of the previous node, to point to the next node, relative to the removed node.

    2. Dispose removed node.


    Doubly linked list

    Doubly Linked List

    • Has links to both previous and next nodes

    • Advantage: faster (bi-directional) traversal

    • But, more control data (links) stored


    Cis3023 programming fundamentals for cis majors ii summer 2010

    Get more info!

    http://java.sun.com/developer/technicalArticles/J2SE/generics/

    http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/collections/index.html

    • Java docs: Linked List (doubly linked list)

    • Java docs: Using and programming generics in J2SE 5.0

    • Java docs: Collections framework tutorial


  • Login