CS 171: Introduction to Computer Science II Linked List Li Xiong - - PowerPoint PPT Presentation

cs 171 introduction to computer
SMART_READER_LITE
LIVE PREVIEW

CS 171: Introduction to Computer Science II Linked List Li Xiong - - PowerPoint PPT Presentation

CS 171: Introduction to Computer Science II Linked List Li Xiong Roadmap Basic data structure Arrays Abstract data types Stacks Queues Implemented using resizing arrays Linked List Concept and implementations


slide-1
SLIDE 1

CS 171: Introduction to Computer Science II Linked List

Li Xiong

slide-2
SLIDE 2

Roadmap

  • Basic data structure

–Arrays

  • Abstract data types

–Stacks –Queues –Implemented using resizing arrays

  • Linked List

–Concept and implementations –Re-implementing Stacks and Queues using Linked List

slide-3
SLIDE 3

Linked List

  • A Linked List is a sequence of nodes chained

together.

  • Each node (element, link) contains a data item,

and a reference to next node

slide-4
SLIDE 4

Node

class Node { Item item; Node next; }

  • This is called self-referential

– A class containing a reference to itself. Data Reference to the next node

slide-5
SLIDE 5

Object vs. Object Reference

  • bject
  • bject reference
slide-6
SLIDE 6

Linked List

slide-7
SLIDE 7

Linked List vs. Arrays

  • Arrays

–stores elements continuously in memory –Fixed size –supports indexed access

  • Linked list

–Does not store elements continuously in memory –supports dynamic size (create a node as needed) –Does not support indexed access –incurs some memory overhead due to the need to store references

slide-8
SLIDE 8

Building a linked list

  • Example: to build a linked

list that contains the items ”to”, ”be”, and ”or”

  • Create a Node for each

item

–set the item field to the desired value –set the next field to next node

  • Maintains a link to the

first node of the list, also called root, head

slide-9
SLIDE 9

Linked List operations

  • Traverse a linked list
  • Search an item with a key
  • Insert an item (at beginning and end)
  • Delete an item (at beginning and end, with a given

key)

slide-10
SLIDE 10
  • Example: print out the

values of the linked list

Traversing a linked list

slide-11
SLIDE 11
  • Example: print out the

values of the linked list

  • Traversing a linked list
  • Traversing an array

Traversing a linked list

for (Node x = first; x != null; x = x.next) { // process x.item } for (int i = 0; i< N; i++) { // process a[i] }

slide-12
SLIDE 12
  • Example: search if there is

“be” in the linked list

Search in a linked list

slide-13
SLIDE 13
  • Example: search if there is

“be” in the linked list

  • Search in a linked list

Search in a linked list

for (Node x = first; x != null; x = x.next) { if x.item.equals(“be”) return x; }

slide-14
SLIDE 14
  • Example: insert “not” at

the beginning

Insert at the beginning

slide-15
SLIDE 15
  • Example: insert “not” at

the beginning

  • What if the list is empty,

i.e. first is null? // create a new node

Node x = new Node(); x.item = “not”; // update links x.next = first; first = x;

Insert at the beginning

slide-16
SLIDE 16
  • Example: insert “not”

at the beginning

Insert at the beginning – book version

// save a link to first Node oldfirst = first; // create a new first first = new Node(); // set first node first.item = “not”; First.next = oldfirst;;

slide-17
SLIDE 17
  • Example: remove “to” at

the beginning

Remove from the beginning

slide-18
SLIDE 18
  • Example: remove “to” at

the beginning

  • Set the root to the next

node in the list

  • What if the list is empty,

i.e. first is null?

Remove from the beginning

slide-19
SLIDE 19
  • Example: insert “not” at

the end

  • Example: remove “or” at

the end

Insert/remove at the end

slide-20
SLIDE 20
  • Example: insert “not” at

the end

  • Example: remove “or” at

the end

  • Traverse the list to find

last node, then insert/remove

Insert/remove at the end

slide-21
SLIDE 21

Double-ended Linked List

  • Similar to an ordinary linked list, but in addition to

keep ‘first’, also keeps a reference to the ‘last’ element in the list.

  • What happens when the list is empty? Has only one

element?

slide-22
SLIDE 22
  • Example: remove “be” from the

linked list

  • Search the item, then remove

Remove a given item

for (Node x = first; x != null; x = x.next) { if x.item.equals(“be”) // how to remove x? }

slide-23
SLIDE 23
  • Example: remove “be” from the linked list
  • Search the item, then remove
  • Keep a reference to the previous and

current element

Remove a given item

Node current = first; Node previous = first; while (current != null && !current.item.equals(“be”)){ previous = current; current = current.next; } // remove current previous.next = current.next; // What if the item is the first node? // What if the item does not exist?

slide-24
SLIDE 24
  • Example: remove “be” from the linked list
  • Search the item, then remove it
  • Need to keep a reference to the previous

and current element.

  • Need to consider the cases when item is

the first and when item does not exist

Remove a given item

Node current = first; Node previous = first; while (current != null && !current.item.equals(“be”)){ previous = current; current = current.next; } // remove current if (current == first) first = first.next; else if (current != null) previous.next = current.next;

slide-25
SLIDE 25

Doubly linked list

  • A doubly linked list has bidirectional references,
  • ne to the next, one to the previous link
  • Pros: flexibility
  • Cons: complexity, memory overhead
slide-26
SLIDE 26

Linked List

  • (Singly) linked list
  • Double ended linked list
  • Doubly linked list
slide-27
SLIDE 27

Halloween Costume – Linked List

slide-28
SLIDE 28

Doubly Linked List

slide-29
SLIDE 29

Circularly Linked List

slide-30
SLIDE 30

Binary Tree

slide-31
SLIDE 31

Null Pointer