Linked list
-
Upload
furqan-m-lodhi -
Category
Education
-
view
201 -
download
0
Transcript of Linked list
Kanwal AftabBI-121011
Furqan Munir Lodhi
BC-141004Muhammad Abid
BC-141005CUST
GROUP MEMBERS
Linked Lists
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
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.
Data Members Pointer
Linked List is an Efficient Data Structure for Creating Arrays.
A linked list can grow or shrink in size during execution.
TYPES OF DATASTRUCTURE
DATASTRUCTURE
LINEAR
LINKLISTARRAYSTACKQUEUE
NONLINEAR
TREES&GRAPH
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]
There are four basic types of linked list
Single Linked ListDouble Linked ListMultipleCircular
TYPE OF 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
Singly Linked List
DR. SHARIQ BASHIR CUST
Doubly Linked List
◈ Simply introduction about double linked list.
◈ Each node have two link in parts.
Doubly 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
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.
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)
DR. SHARIQ BASHIR CUST
EXAMPLE OF LINKED LIST
◈ Patients data stored in hospitals.
◈ costumers data stored in malls.
◈ Many others..
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
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
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
MAP OF MEMORY
HEAD_PTR
20
77
68
18
100
NULL
Thanks!Any questions?