singly linked lists - ed. 2, 3: chapter 4 - ed. 4.: chapter 3

32
Singly Linked Lists Scottw antsto find inform ation about com puternetw orks. Scott Bill W iley M cFee G ary - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3

Post on 21-Dec-2015

240 views

Category:

Documents


0 download

TRANSCRIPT

Singly Linked Lists

Scott wants to find information aboutcomputer networks.

Scott

Bill

Wiley

McFee

Gary

- Ed. 2, 3: Chapter 4- Ed. 4.: Chapter 3

D e f i n i t i o n : A l i n k e d l i s t i s a c o l l e c i t o n o f n o d e s t h a t t o g e t h e r f o r m a l i n e a r o r d e r i n g . n o d e : A c o m p o u n d o b j e c t t h a t s t o r e s a r e f e r e n c e t o a n e l e m e n t a n d a r e f e r e n c e , c a l l e d n e x t , t o a n o t h e r n o d e .

R e f e r e n c e t oa n o t h e r n o d e

R e f e r e n c e t o a ne l e m e n t

n e x t

E l e m e n t

N o d e

h e a d

n e x t

e l e m e n t

n e x t n e x tn e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r e R o m e S e a t t l e T o r o n t o

l i n k : T h e n e x t r e f e r e n c e i n s i d e a n o d e i s a l i n k o r p o i n t e r t o a n o t h e r n o d e .

W e c a n s t a r t f r o m a g i v e n n o d e , a n d m o v e f r o m i t t o t h e n e x t a n d s o o n . T h i s i s c a l l e d l i n k h o p p i n g o r p o i n t e r h o p p i n g .

h e a d

n e x t

e l e m e n t

n e x t n e x tn e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r e R o m e S e a t t l e T o r o n t o

h e a d : T h e f i r s t n o d e o f a l i n k e d l i s t t a i l : T h e l a s t n o d e o f a l i n k e d l i s t - i t h a s a n u l l n e x t r e f e r e n c e .

h e a d

n e x t

e l e m e n t

n e x t n e x tn e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r e R o m e S e a t t l e T o r o n t o

S u c h a l i n k e d l i s t i s c a l l e d a s i n g l y l i n k e d l i s t .

tail

5050

5060

5080

5070

5090

50A0

50B0

50D0

50C0

50E0

50F0

5110

5100

50C0

5060 50E05080

50D05070

51100

Rome

Baltimore

Seattle

Toronto

pointer to anext node

pointer toan element

node

Illustration of a linked list in memory:

5050

5060

5080

5070

5090

50A0

50B0

50D0

50C0

50E0

50F0

5110

5100

50C0

5060 50E05080

50D05070

51100

head

Rome

Baltimore

Seattle

Toronto

pointer to anext node

pointer toan element

node

5050

5060

5080

5070

5090

50A0

50B0

50D0

50C0

50E0

50F0

5110

5100

50C0

5060 50E05080

50D05070

51100

head

Rome

Baltimore

Seattle

Toronto

pointer to anext node

pointer toan element

node

5050

5060

5080

5070

5090

50A0

50B0

50D0

50C0

50E0

50F0

5110

5100

50C0

5060 50E05080

50D05070

51100

head

Rome

Baltimore

Seattle

Toronto

pointer to anext node

pointer toan element

node

5050

5060

5080

5070

5090

50A0

50B0

50D0

50C0

50E0

50F0

5110

5100

50C0

5060 50E05080

50D05070

51100

head

Rome

Baltimore

Seattle

Toronto

Singly Linked Lists and Arrays

Singly linked list Array Elements are stored in linear order, accessible with links. Do not have a fixed size. Cannot access the previous element directly. No binary search.

Elements are stored in linear order, accessible with an index. Have a fixed size. Can access the previous element easily. Binary search.

Class Node

Here is an implementation of nodes in Java: public class Node { private Object element; private Node next; public Node() { this( null, null ); } public Node( Object e, Node n ) { element = e; next = n; }

Object getElement() { return element } Node getNext() { return next; } void setElement( Object newElem ) { element = newElem; } void setNext( Node newNext ) { next = newNext; } }

Insertion of an Element at the Head

B efore the insertion :

hea d

nex t nex t nex t

ele m ent ele m ent ele m ent

R om e S eattle To ronto

H a v e a n e w n o d e :

h e a d

n e x t

e l e m e n t

n e x t n e x tn e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r e R o m e S e a t t l e T o r o n t o

Node x = new Node();x.setElement(new String(“Baltimore”));The following statement is not correct:x.element = new String(“Baltimore”));

A f t e r t h e i n s e r t i o n :

h e a d

n e x t

e l e m e n t

n e x t n e x tn e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r e R o m e S e a t t l e T o r o n t o

x.setNext(head);head = x;

Deleting an Element at the Head

B efo re th e d e le tio n :

h ea d

n ex t

e le m en t

n ex t n ex tn ex t

e le m en t e le m en t e le m en t

B alt im ore R om e S ea tt le T o ronto

R e m o v e t h e n o d e f r o m t h e l i s t :

h e a d

n e x t

e l e m e n t

n e x t n e x tn e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r e R o m e S e a t t l e T o r o n t o

head = head.getNext();

A f te r th e d e le t io n :

h e a d

n e x t n e x t n e x t

e le m e n t e le m e n t e le m e n t

R o m e S e a tt le T o ro n t o

Insertion of an Element at the Tail

Before the insertion:

head

next next next

element element element

Rome Seattle Toronto

tail

H a v e a n e w n o d e :

h e a d

n e x t

e l e m e n t

n e x t n e x t n e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r eR o m e S e a t t l e T o r o n t o

t a i l

Node x = new Node( );x.setElement(new String(“Baltimore”));x.setNext(null);tail.setNext(x);tail = x;

A f t e r t h e i n s e r t i o n :

h e a d

n e x t

e l e m e n t

n e x t n e x t n e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r eR o m e S e a t t l e T o r o n t o

t a i l

How to keep “head” and “tail”?

public class Head_and_Tail {Node head;Node tail;

Head_and_Tail(Node x, Node y) {

head = x;tail = y;

}}

How to keep “head” and “tail”?

public class GeneratingList {Node head = null;Node tail = null;

Public Head_and_Tail linked_list () {Node x = null;for (int i = 0; i < 10; i++) {x = new Node(); x.setElement(new Integer(i)); if (i == 0 ) {x.setNext(null); tail = x;} else x.setNext(head); head = x;}

return new Head_and_Tail(head, tail);}}

Deleting an Element at the Tail

Deletion of an element at the tail of a singly linked list takes more effort. The difficulty is related with the fact that the last node does not have a link to the previous node which will become the new tail of the list.

Scott: Who is McFee?Gary: I don’t know.

Scott

Bill

Wiley

McFee

Gary

B e f o r e t h e d e l e t i o n :

h e a d

n e x t

e l e m e n t

n e x t n e x t n e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r eR o m e S e a t t l e T o r o n t o

t a i l

R e m o v e t h e n o d e : H o w c a n w e f i n d t h e n e w t a i l ?

n e x t

e l e m e n t

n e x t n e x t n e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r eR o m e S e a t t l e T o r o n t o

h e a d t a i l ?

should be removed

How to insert a new node in the middle of a singly linkedlist?

How to remove a node which in the middle of a singlylinked list?

Data Structure Exercises 5.1

Write a Java program to create a linked list as shown below.

0 1 9

… …

head tail

public class ListStack implements Stack {private int size = 0;private Node head = null;

public ListStack() {} 

public int size() {return size;

}public boolean isEmpty() {

return size == 0;}public void push( Object obj ){Node x = new Node();x.setElement(obj); x.setNet(head);head = x; size++;}

public object pop() throws StackEmptyException {if( isEmpty() )

throw new StackEmptyException( "Stack is Empty." );

Object elem = head;head = getNext(head); size--;return elem;}

public object top() throws StackEmptyException {if( isEmpty() )

throw new StackEmptyException( "Stack is Empty." );

return head;;}

}