Download presentation

Presentation is loading. Please wait.

1
**Linear Lists – Linked List Representation**

Computer College

2
**Outlines Linked list nodes Linked list operations**

Insertion Deletion Linked list representation & implementation Other types of linked lists Sorted Doubly-linked Circular

3
Single Linked List Definition: A linked list is a collection of nodes that together form a linear ordering. Each node is a compound object that stores an element and a reference, called next, to another node. Structure of a node Data Link Node Node Node Node first A0 A1 A2 A3

4
**Characteristics Information (data) Link to the next node**

Insert and delete nodes in any order The nodes are connected Each node has two components Information (data) Link to the next node The nodes are accessed through the links between them

5
**For each node the node that is in front of it is called predecessor.**

Head Predecessor of X Node X Success-or of X tail For each node the node that is in front of it is called predecessor. The node that is after it is called successor.

6
Terminology Head (front, first node): The node without predecessor, the node that starts the lists. Tail (end, last node): The node that has no successor, the last node in the list. Current node: The node being processed. From the current node we can access the next node. Empty list: No nodes exist

7
**Node Linking 1. Single linked lists : 2. Double linked lists :**

Each node contains two links - to the previous and to the next node 2. Double linked lists : Each node contains a link only to the next node 3. Circular lists: The tail is linked to the head.

8
**Single linked List Properties**

Stores a collection of items non-contiguously. Each item in the list is stored with an indication of where the next item is. Must know where first item is. The list will be a chain of objects, called nodes, of type Node that contain the data and a reference to the next Node in the list. Allows addition or deletion of items in the middle of collection with only a constant amount of data movement. Contrast this with array.

9
**Singly Linked List Let L = (e1,e2,…,en)**

Each element ei is represented in a separate node Each node has exactly one link field that is used to locate the next element in the linear list The last node, en, has no node to link to and so its link field is NULL. This structure is also called a chain. e1 e2 e3 en …… first Link Field Data Field

10
**Class ‘ChainNode’ public class Node { Object data; Node next;**

Node(Object obj, Node element) data = obj; next = element; }

11
**Operations in ADT Notation**

Insert(L,obj) Inserts a node with information e before the current position Delete(L) Deletes the current node in L , the current position indicates the next node. RetrieveInfo(L) obj Returns the information in the current node.

12
Insertion Insertion To insert a node X between the nodes A and B: .Create a link from X to B. .Create a link from A to X,

13
Insertion X A B

14
**Adding an element at the beginning**

Create a new node; Element in the node has the same value as the new element; Node pointer points to the first element (non-header) Pointer from the header points to new node; Create(newnode); newnode.next header.next.next header.next newnode; O(1);

15
**Code Fragment to insert at a head**

public void inserthead(Object obj) { Node newNode = new Node(obj); // make new Node newNode.next = head; // newNode --> old head head = newNode; // head --> newNode } The time complexity O(1)

16
Deletion Deletion To delete a node X between A and B: Create a link from A to B, Remove node X

17
**Code Fragment to delete first node**

public Node deleteHead() // delete head item { // (assumes list not empty) Node temp = head; // save reference to link head = head.next; / delete it: head-->old next return temp; // return deleted link } The time complexity O(1)

18
**Code Fragment to insert at a tail**

public void insertLast(Object obj) { Node newNode = new Node(obj); // make new link if( isEmpty() ) // if empty list, head = newNode; // first --> newNode else tail.next = newNode; // old tail --> newNode tail = newNode; // newNode <-- tail } The time complexity O(1)

19
**Traversal public int countNodes() { int count = 0; Element e = head;**

while(e != null) count++; e = e.next; } return count; A method that computes the number of elements in any list:

20
**Code Fragment to delete at a tail**

public Node deleteTail( ) // delete link with given key { Node current = head; // search for link Node previous = head; while(current.next != null) if(current.next == null) return null; // didn't find it else previous = current; // go to next link current = current.next; } } // found it if(current == head) // if first link, head= head.next; // change first Else // otherwise, previous.next = current.next; // bypass it return current; The time complexity O(1)

21
**Delete any Node To delete the fourth element from the chain, we**

locate the third and fourth nodes link the third node to the fifth free the fourth node so that it becomes available for reuse 20 10 30 11 first link data 80

22
**The List ADT Implementation A0 A1 A2 AN-1 O(1) running time A0 A1 A2**

by Array A0, A1, A2, ..., AN-1 Operation:findKth A0 A1 A2 AN-1 O(1) running time A0 A1 A2 AN-1 return Arr[2];

23
**The List ADT Implementation A0 A1 A2 AN-1 O(N) running time A0 A1 A2**

by Array A0, A1, A2, ..., AN-1 Operation: deletion A0 A1 A2 AN-1 O(N) running time A0 A1 A2 AN-1 A0 A2 A3 AN-1

24
**The List ADT Implementation A0 A1 A2 AN-1 O(N) running time A0 A1 A2**

by Array A0, A1, A2, ..., AN-1 Operation: insertion A0 A1 A2 AN-1 O(N) running time A0 A1 A2 AN-1 A0 A1 A’ A2 A3 AN-2 AN-1

25
**The List ADT a node: element A3 Implementation next link**

A0, A1, A2, ..., AN-1 by Linked List Operation: FindKth next next next next null O(N) running time

26
**The List ADT Implementation O(1) running time A0, A1, A2, ..., AN-1**

by Linked List Operation: deletion O(1) running time

27
**The List ADT Implementation O(1) running time A0, A1, A2, ..., AN-1**

by Linked List Operation: insertion O(1) running time

28
**The List ADT Implementation A0, A1, A2, ..., AN-1**

by doubly linked list

29
**The List ADT Summary Array List (Single) Linked List findKth O(1) O(n)**

running time coomparion when to use Array list or Linked list? Array list: numerous findKth operations + seldom delete/insert operations Linked list: numerous delete/insert operations + seldom findKth operations Array List (Single) Linked List findKth O(1) O(n) insert delete

30
**Circular List Representation**

Programs that use chains can be simplified or run faster by doing one or both of the following: Represent the linear list as a singly linked circular list (or simply circular list) rather than as a chain Add an additional node, called the head node, at the front

31
**Circular List Representation**

32
**Doubly Linked List Representation**

An ordered sequence of nodes in which each node has two pointers: left and right.

33
**Class ‘DoubleNode’ public class Node { Public String element;**

Public Node prev; Public Node next; Public Node(Object obj, Node p, Node n) Element=obj; Prev=p; Next=n; }

34
**Circular Doubly Linked List**

Add a head node at the left and/or right ends In a non-empty circular doubly linked list: LeftEnd->left is a pointer to the right-most node (i.e., it equals RightEnd) RightEnd->right is a pointer to the left-most node (i.e., it equals LeftEnd) Can you draw a circular doubly linked list with a head at the left end only by modifying Figure 6.7?

Similar presentations

OK

Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.

Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on primary data collection methods Running message display ppt on ipad Ppt on trade fairs Download ppt on search engine Ppt on recycling of waste Ppt on autonomous car project Ppt on geography of asia Ppt on automobile related topics of psychology Ppt on sets for class 11 Ppt on european central bank