Linked list

23

Transcript of Linked list

Page 1: Linked list
Page 2: Linked list

Kanwal AftabBI-121011

Furqan Munir Lodhi

BC-141004Muhammad Abid

BC-141005CUST

GROUP MEMBERS

Page 3: Linked list

Linked Lists

Page 4: Linked list

DR. SHARIQ BASHIR CUST

Table of Contents INTRODUCTION TYPE OF DATASTRUCTURE ARRAY VS LINKED LIST TYPE OF LINKED LIST INSERTION RETRIEVING DELETING EXAMPLE OF LINKED LIST ADVANTAGES DISADVANTAGES MAP OF MEMORY COMPARISON BETWEEN ARRAY AND LINKED LIST

Page 5: Linked list

DR. SHARIQ BASHIR CUST

INTRODUCTION◈ A linked list is a linear data

structure.

◈ Nodes make up linked lists.

◈ Nodes are structures made up of data and a pointer to another node.

◈ Usually the pointer is called next.

Page 6: Linked list

Data Members Pointer

Page 7: Linked list

Linked List is an Efficient Data Structure for Creating Arrays.

A linked list can grow or shrink in size during execution.

Page 8: Linked list

TYPES OF DATASTRUCTURE

DATASTRUCTURE

LINEAR

LINKLISTARRAYSTACKQUEUE

NONLINEAR

TREES&GRAPH

Page 9: Linked list

ARRAY VS LINKED LISTArrays Linked list

Fixed size Dynamic size

Inefficient Insertions and Deletions

Efficient Insertions and Deletions

Random access i.e., efficient indexing

No random access

May result in much memory waste.

No waste of memory.

Sequential access is faster [Reason: Elements in contiguous memory locations]

Sequential access is slow [Reason: Elements not in contiguous memory locations]

Page 10: Linked list

There are four basic types of linked list

Single Linked ListDouble Linked ListMultipleCircular

TYPE OF LINKED LIST

Page 11: Linked list

DR. SHARIQ BASHIR CUST

Singly Linked List

◈ Each node has only one link part

◈ Each link part contains the address of the next node in the list

◈ Link part of the last node contains NULL value which signifies the end of the node

Page 12: Linked list

Singly Linked List

Page 13: Linked list

DR. SHARIQ BASHIR CUST

Doubly Linked List

◈ Simply introduction about double linked list.

◈ Each node have two link in parts.

Page 14: Linked list

Doubly Linked List

Page 15: Linked list

o Suitable for Databases when we don’t have prior knowledge about the size of Database

o Can delete both data and memory of elements

ADVANTAGES

Page 16: Linked list

DR. SHARIQ BASHIR CUST

DISADVANTAGES No longer have direct access to an

element of a list Many sorting algorithms need direct access Binary search needs direct access Access of nth item now less efficient Must go through first element, and then

second, and then third, etc.

Page 17: Linked list

COMPARISON BETWEEN ARRAY& LINKED LIST

Operation ID-Array Complexity Singly-linked list ComplexityInsert at beginning O(n) O(1)

Insert at end O(1) O(1) if the list has tail referenceO(n) if the list has no tail reference

Insert at middle O(n) O(n)

Delete at beginning O(n) O(1)

Delete at end O(1) O(n)

Delete at middle O(n): O(1) access followed by O(n) shift

O(n): O(n) search, followed by O(1) delete

Search O(n) linear searchO(log n) Binary search

O(n)

Indexing: What is the element at a given position k?

O(1) O(n)

Page 18: Linked list

DR. SHARIQ BASHIR CUST

EXAMPLE OF LINKED LIST

◈ Patients data stored in hospitals.

◈ costumers data stored in malls.

◈ Many others..

Page 19: Linked list

DR. SHARIQ BASHIR CUST

INSERTIONstruct Node{ int data; Node *link;};Node *head_ptr = NULL;void InsertNode (int parameter){ Node *nNode;

nNode = new Node;nNode->data = parameter;nNode->link = head_ptr; head_ptr = nNode;

}int main (){ InsertNode (10); InsertNode (16); InsertNode (35); return 0;}

35 16

10head_ptr

nNode

Page 20: Linked list

DR. SHARIQ BASHIR CUST

Deleting a User Specific Node (data = 16)

void DeleteNode (int pData){ Node *currentNode = head_ptr; Node *lastNode = head_ptr; while (true) { if ( currentNode->data == pData ) // 16 == 16 (true) { if ( currentNode == head_ptr )

head_ptr = currentNode->link; else

lastNode->link = currentNode->link;

delete currentNode; break;

} lastNode = currentNode; currentNode = currentNode->link; if ( currentNode == NULL ) break; }}

BEFORE DELETING NODE

head_ptr

10 16

20

AFTER DELETING NODE

head_ptr

10 20

currentNodelastNode

Page 21: Linked list

DR. SHARIQ BASHIR CUST

RETRIEVINGstruct Node{ int data; Node *link;};Node *head_ptr = NULL;void RetrievingLinkedList ();int main (){ RetrievingLinkedList ( ); return 0;}void RetrievingLinkedList ( ){ Node *retNode = head_ptr; while (true) { cout << “\n Data = “ << retNode->data ;

retNode = retNode->link;if ( retNode == NULL )

break; }}

head_ptr

10 16

20

NULL

Output Screen

Data = 10Data = 16Data = 20

Page 22: Linked list

MAP OF MEMORY

HEAD_PTR

20

77

68

18

100

NULL

Page 23: Linked list

Thanks!Any questions?