Click here to load reader

Seminar on Linked List

Embed Size (px)

Citation preview

SEMINAR ON LINKED LIST (CIRCULAR AND DOUBLE)Presented by Joydip Ghosh 3rd Year Roll-08 Department of Information Technology Academy Of TechnologyJoydip Ghosh (Academy Of Technology)

`

In computer science, a linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence.

Joydip Ghosh (Academy Of Technology)

ANATOMY OF LINKED LISTy A linked list consists of: y A sequence of nodes

myList a b c d

The list have a header Each node contains a value and a link (pointer or reference) to some other node The last node contains a null link

Joydip Ghosh (Academy Of Technology)

y Single linked list

Head

NULL

This is the most basic type of linked list, with each node containing a single pointer, to the next node.

y Multi linked list

Head

NULL

More advanced than the single linked list, each node may be connected to many other nodes.Special case : Doubly linked listsHead

y Circular linked listWith a circular linked list, the last node is connected to the first, to form a circle.Joydip Ghosh (Academy Of Technology)

MULTI OR DOUBLY LINKED LISTy In computer science, a doubly-linked list is a linked data

structure that consists of a set of data records, each having two special link fields that contain references to the previous and to the next record in the sequence. It can be viewed as two singly-linked lists formed from the same data items, in two opposite orders.

Joydip Ghosh (Academy Of Technology)

Basic Structure Of Double Linked Listy data: the user's data y next, prev: the address of the next and previous node

in the list

prev

data

next

A

Creation Of Single NodeXNULL NULL

typedef struct dnode { struct dnode *prev; int data; struct dnode *next; }dn; x=(dn*)malloc(sizeof(dn)); x->prev=x->next=NULL; *head=x;Joydip Ghosh (Academy Of Technology)

head

Empty Doubly Linked List

c

head// constructor DoubleList() { head = new tail = new head->next tail->prev }

tail

DoubleListNode (); DoubleListNode (); = tail; = head;Joydip Ghosh (Academy Of Technology)

Inserting into a Doubly Linked Lista c

head current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

tail

Joydip Ghosh (Academy Of Technology)

Inserting into a Doubly Linked Lista c

head current

b

tail

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNodeJoydip Ghosh (Academy Of Technology)

Inserting into a Doubly Linked Lista c

head current

b

tail

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNodeJoydip Ghosh (Academy Of Technology)

Inserting into a Doubly Linked Lista c

head current

b

tail

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNodeJoydip Ghosh (Academy Of Technology)

Inserting into a Doubly Linked Lista c

head current

b

tail

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNodeJoydip Ghosh (Academy Of Technology)

Inserting into a Doubly Linked Lista c

head current

b

tail

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNodeJoydip Ghosh (Academy Of Technology)

Inserting into a Doubly Linked Lista c

head current

b

tail

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNodeJoydip Ghosh (Academy Of Technology)

Deleting an element from a double linked lista c

head current

b

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; Joydip Ghosh (Academy Of Technology) delete oldNode;

Deleting an element from a double linked list

a

c

head current

b oldNode

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

Joydip Ghosh (Academy Of Technology)

Deleting an element from a double linked list

a

c

head current

b oldNode

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; Joydip Ghosh (Academy Of Technology) delete oldNode;

Deleting an element from a double linked lista c

head current

b oldNode

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; Joydip Ghosh (Academy Of Technology) delete oldNode;

Deleting an element from a double linked list

a

c

head current

b oldNode

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

Joydip Ghosh (Academy Of Technology)

Deleting an element from a double linked list

a

c

head current

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

Joydip Ghosh (Academy Of Technology)

DLLs compared to SLLsAdvantages: Can be traversed in either direction (may be essential for some programs)Some operations, such as deletion and inserting before a node, become easierJoydip Ghosh (Academy Of Technology)

Disadvantages: Requires more space List manipulations are slower (because more links must be changed) Greater chance of having bugs (because more links must be manipulated)

Circularly Linked ListsaList

size

3

head

21

24

47

A circularly-linked list is a singly-linked list where the last Node refers back to the first Node in the list. A tail reference is not used.Joydip Ghosh (Academy Of Technology)

Traversing a circularly-linked listaList

size

3

head

To traverse a circularly-linked An insertion can be performed list, move a Node handles until pos after thepair ofthat q references, == i (or p.getNext( ) == head) and a removal will remove the Node referenced by p, using q to Locate the node containing x = 47 reattach the list.

21

24

47

p Node p = head, q; while (p.getNext( ) != head && p.getData( ) != x) { q = p; p = p.getNext( ); Joydip Ghosh (Academy Of Technology) }

q

p

q p

A circularly-linked list with a single NodeaList

Special case List contains a single Node The succ field of a single Node contains a handle to itself Constructor for Node21public Node(Object obj) { data = obj; succ = this; }The object under construction

size

1

head

The only special circumstances about adding the first or removing the last Node from a circularly-linked list involve setting the contents of head.Joydip Ghosh (Academy Of Technology)

Create a Node referring to itself and overwrite the succ field using setNext( ) when adding to the list if Node is not the first Node to be added.

APPLICATIONS OF CIRCULAR LINKED LISTJheophus ProblemSTART

2 3

1WINNER

4

5Joydip Ghosh (Academy Of Technology)

FOR NODES NO = 5 FOR COUNT = 3

Joydip Ghosh (Academy Of Technology)