Presentation is loading. Please wait.

Presentation is loading. Please wait.

Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.

Similar presentations


Presentation on theme: "Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3."— Presentation transcript:

1 Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3

2

3

4

5 tail

6 pointer to a next node pointer to an element node Illustration of a linked list in memory:

7 pointer to a next node pointer to an element node

8 pointer to a next node pointer to an element node

9 pointer to a next node pointer to an element node

10

11 Singly Linked Lists and Arrays

12 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; }

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

14 Insertion of an Element at the Head

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

16 x.setNext(head); head = x;

17 Deleting an Element at the Head

18 head = head.getNext();

19

20 Insertion of an Element at the Tail

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

22

23 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; }

24 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);} }

25 Deleting an Element at the Tail

26

27

28 should be removed

29 How to insert a new node in the middle of a singly linked list? How to remove a node which in the middle of a singly linked list?

30 Data Structure Exercises 5.1 Write a Java program to create a linked list as shown below. 019  … headtail

31 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++; }

32 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;; }


Download ppt "Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3."

Similar presentations


Ads by Google