Linked Lists Revision Based on http www academic
Linked Lists Revision Based on: http: //www. academic. marist. edu/~jzb v/
Linked Lists • Linked. List is an important data structure in OO languages such as Java and C# • What you need to learn is how to write the implementation. • Our philosophical principle: in pursuit of absolute simplicity. • How: Divide, Conquer, and Glue
What to Do? • Interface IList is given • Two implementations are discussed: – Solution One: Doubly Linked List Class – Solution Two: Singly Linked List Class
Interface IList • Interface IList has the following methods: a. boolean add( Object x) -- append to end of the list b. boolean add(int i, Object x) – insert at position i c. boolean remove(int i) – remove object at position i
Linked Lists A Linked. List implements the interface IList There a number of ways in which a Linked. List can be implemented internally, but each implementation should provide the client (code that uses a Linked. List) with a single common set of operations that can be evoked by messages to the Linked. List object. Types of Linked. List implementations include: • Doubly linked list • Singly linked list We will examine how the main List operations are implemented in each of these alternatives
Linked Lists Doubly-linked List A Linked. List is a connected sequence of Nodes. It is composed of two classes of objects: Linked. Lists and Nodes. class Node { class Linked. List implements IList { //Node methods go here //class Node (see left) is declared here //attributes private Node pred, succ; private Object data; //implementation of List methods goes here //attributes private Node head, tail; } private int size; } Node is an internal class visible only to Linked. List objects. The private attributes: pred and succ, have class scope and are thus indirectly accessible via the setter/getter methods in class Linked. List. BUT for the illustration purpose, we treat them as protect, which you can set and get a value for them.
Linked Lists Useful terminology for referring to Nodes in a Linked. List Nodes are not named objects, but are referred to by a “pointer” Node p = new Node(the. Object); p. succ = new Node(next. Object); p pred data succ This is the Node that p points to pred data succ This Node is the successor of the Node that p points to
Linked Lists Linked. List a. List = new Linked. List( ); Integer temp = new Integer(21); Integer(24); a. List. add( new Integer(24) ); Node p = new Node(temp); Node (temp); a. List. add( new Integer(21) ); if (head!===0 0) //head tail. succ head == p; p; a. List tail = p; = tail; p. pred size++; tail = p; size 210 tail p head size++; p 24 21
Linked Lists Putting it together public boolean add (Object the. Obj) { Node p = new Node(the. Obj); if (head == 0) head = p; else { tail. succ = p; p. pred = tail; } tail = p; size++; return true; }
Linked Lists Inserting an object at position i Integer temp = new Integer (3); Node p = new Node(temp); p. pred = 0; a. List p. succ = head; if (head != 0) head size 34 21 10 p tail head. pred = p; head = p; size++; 24 Insert at front of the list (i == 0) 3 The of Node referenced is Nodelist referenced else successor (head ==0) -- inserting intobyanp empty -by head set tail = p
Linked Lists Integer temp = new Integer (3); Node p = new Node(temp); Inserting an object at position i a. List head size 10 if (i == size) { //insert at end 34 tail p. pred = tail; p. succ = 0; tail. succ = p; tail = p; size++; 21 Inserting at end of non-empty list (i==size) p’ s predecessor is theto. Node tail points Make Node points ofto Node tail points to The Node pp points to is the nowsuccessor at the end of the list 24 p 41
Linked Lists Inserting an object at position i a. List Integer temp = new Integer (23); Node p = new Node(temp); int count = 0; Node q = head; while (count < i) { head size 43 q count++; q = q. get. Next( ); tail } q 10 Inserting at general position – (0 <= i < size) 21 24 p The order of these statements Insert Node the Node Move anew handle tobefore the Node atis that important statement MUST q pointsito. position – –Let i = 2 in 3 this example precede statement 4. q 23 p. pred = q. get. Prev( )); count p. succ = q; = count = 102 q. get. Prev( ). succ = p; //3 q. pred = p; //4 size++;
Putting it all together Linked Lists public boolean add(int i, Object x) { if (i < 0 || i > size) { //range error System. err. println (“error – index out of range”); System. exit(1); //better – throw Exception } else { //insert at general position Node p = new Node(x); int count = 0; if (i ==0) { //insert at front Node q = head; p. pred = 0; //superfluous while (count < i) { p. succ = head; count++; if (head !=0) q = q. get. Next( ); head. pred = p; } else p. pred = q. get. Prev( ); tail = p; p. succ = q; head = p; q. get. Prev( ). succ = p; } q. pred = p; else if (i == size) { //insert at end } p. succ = 0; size++; p. pred = tail; } return true; tail. succ = p; tail = p; } q. get. Prev( ) returns a reference to the previous Node – Make the successor of that Node = p.
Linked Lists Node p = head; if (i ==0) { //remove first Node head = p. get. Next( ); if (head != 0) head. pred = 0; else //removing last node tail = 0; size--; } Removing objects from a List a. List head size 32 tail p 21 24 41 Remove first Node – (i == 0) Automatic garbage collection reclaims the unreferenced Node when p goes out of scope.
Linked Lists Removing objects from a List a. List head size 32 tail if (i ==size-1) { //remove last Node p = tail; //Node * p; set previously if (head != tail) { tail. get. Prev(). succ = 0; tail = tail. get. Prev( ); } else { //removing last node tail = 0; head = 0; } size--; } p 21 24 Remove the last node – (i == size – 1) 41 tail. get. Prev( ) Automatic garbage collection reclaims memory when p goes out of scope
Linked Lists Node p = head; int pos = 0; while (pos < i) { //find node to cut p = p. get. Next( ); //move handle pos++; } p. get. Prev( ). succ = p. succ; p. get. Next( ). pred = p. pred; size--; } Removing objects from a List a. List head size p 32 tail p 21 24 41 p. get. Prev( ) p. get. Next( ) The Node referenced by p is deleted when p goes out of scope pos = 10 Remove Node in general position – ( i ==1)
Linked Lists else if (i == size-1) { //remove last node p = tail; public boolean remove(int i) { if (head != tail) { if (i < 0 || i >= size) { //range error tail. get. Prev. succ = 0; System. err. println (“error – index out of range”); tail = tail. get. Prev( ); System. exit(1); } } else { Node p; head = 0; tail = 0; } if (i == 0 ) // remove first node } //end else if p = head; else { //remove Node in general position head = p. succ; p = head; if (head != 0) int pos = 0; head. pred = 0; while (pos < i ) { //move handle else p = p. get. Next( ); tail = 0; pos++ } //end if } p. get. Prev. succ = p. succ; p. get. Next. pred = p. pred; } //end else p. set. Prev(0); p. set. Next(0); //for safety size--; return true; } //end remove Bringing it all back home
Linked Lists public class Node { Singly-linked Lists public Node(Object x) {…} public void set. Next( ) {…} a. List public Node get. Next( ) {…} public Object get. Data( ) {…} private Node succ; size 3 head tail private Object data; } data pos ==01 pos=2 succ 21 q 24 p q 47 p p Implementing To remove the methods Node add( index pos, remove( remove the ) Move will Node beafter two left that handles as p an references. exercise. in that sequence until for(int To Node perform pos p ==head, 0; an pos insert q= < head; i; atpos++) at pos, ) {and insert the new Node the Node q. Use the Node that qand references to reattach theplinks. p reaches position i – (let i = 2) references before the Node that references q = p; p = p. get. Next( ); }
- Slides: 18