link list
Post on 07-Aug-2015
7 Views
Preview:
TRANSCRIPT
Link list
SupervisedDr Nuri
Prepared by Didar Rashad
2014-2015
Linked Lists
Linked Lists 2
Link list in data structure
Introduction to Linked List
1.Link list is a data Structure which consists of group of nodes that forms a sequence.2. Linked list is group of nodes in which each node have link to next node to form a chain,
Linked List definition
1. Linked List is Series of Nodes2. Each node Consist of two Parts Data Part & Pointer Part3. Data part stores value of node Pointer Part stores the address of the next node
Linked List Types Singly Linked list Circular singly linked list Doubly linked lists Circular doubly linked lists
Singly Linked List
10
1000 200015 NULL20
First
Singly Linked list Each Node contain address of the next node to be
followed. In Singly Linked List only Linear or
Forward Sequential movement is possible. Elements are accessed sequentially , no direct
access is allowed. First Node does not have predecessor while last
node does not have any successor. We can have multiple data fields inside Node but we
have only single “Link” for next node.
10004000
10
1000
2000
200015 400020
Circular Singly Linked ListLast node contains the address of the first node First
Circular Singly Linked List
Doubly linked lists
In Doubly Linked List , each node contain two address fields .
One address field for storing address of next node to be followed and second address field contain address of previous node linked to it.
So Two way access is possible ,We can start accessing nodes from start as well as from last .
Like Singly Linked List also only Linear but Bidirectional Sequential movement is possible.
3000
2000 30001000
10 15 20
2000 1000 2000 10003000
First
Circular Doubly Linked listContains the address of first node and last node
3000
2000 30001000
10 15 20
2000 1000 2000 10003000
First
Difference Between array and Linked List
Access Random\sequential
Memory Structure
Memory Allocation
Insert\ deletion
Difference Between array
and Linked List
array elements can be randomly Accessed using Subscript Variable, a[0],a[1],a[3] can be randomly accessed
While In Linked List We have to Traverse Through the Linked List for Accessing Element. So O(n) Time required for Accessing Element .Generally In linked List Elements are accessed Sequentially.
Access Random\sequential
Memory Structure
But link list is not necessary to store next element at the Consecutive memory Location In link list Element is stored at any available Location , but the Pointer to that memory location is stored in Previous Node.
array is stored in contiguous Memory Locations , i.e Suppose first element is Stored at 2000 then Second Integer element will be stored at 2001 .
As the Array elements are stored in contiguous memory Locations , so While Inserting elements ,we have to create space for Insertion.So More time required for Creating space and Inserting ElementSimilarly We have to Delete the Element from given Location and then Shift All successive elements up by 1 position
Insert\ deletion
In Linked List we have to Just Change the Pointer address field (Pointer),So Insertion and Deletion Operations are quite easy to implement
Insert\ deletion
Memory Should be allocated at Compile-Time in array . at the time when Programmer is Writing Program,array uses Static Memory Allocation
In Linked list memory can be allocated at Run-Time , After executing ProgramLinked List Uses Dynamic Memory Allocation
Memory Allocation
Advantages of linked list1.Linked List is Dynamic data Structure .
Linked List Data Structure is Dynamic in nature.
just create Linked List structure and memory will be allocated at run time.
while running program at run time we can allocate much memory.
Oslo we can allocate any number of nodes .Because this link list can grow and shrink during run time.
2. Insertion and Deletion Operations are Easier
Advantages of linked list3. Efficient Memory Utilization , no need to pre-allocate memory
we don’t have to allocate memory at compile time.
Memory is allocated at run time, so that Linked list data structure provides us strong command on memory utilization.
4. Faster Access time , can be expanded in constant time without memory overhead5. Linear Data Structures such as array , Queue can be easily implemented by using Linked list
Disadvantages of linked list1. Wastage of memory
. Pointer Requires extra memory for storage.
Suppose we want to store 3 integer data items then we have to allocate memory -
in case of array
Memory Required in Array = 3 Integer * Size
=3 * 2 bytes
= 6 bytes
Memory Required in LL = 3 Integer * Size of Node
= 3 * Size of Node Structure
= 3 * Size(data + address pointer)
= 3 * (2 bytes + x bytes)
= 6 bytes + 3x bytes
Disadvantages of linked list
2. No random AccessIn Linked list no random access is given to user, we have to access each node sequentially.
3. Heap space restrictionWhenever memory is dynamically allocated , It utilizes memory from heap.
Memory is allocated to Linked List at run time if only there is space available in heap.
If there is insufficient space in heap then it can’t create any memory.
4. Reverse Traversing is difficultIn case if we are using singly linked list then it is very difficult to traverse linked list from end.
If using doubly linked list then though it becomes easier to traverse from end to start
21
Insertion Doubly linked lists
A B X C
A B C
p
A B C
p
X
q
p q
Linked Lists 22
Deletion Doubly linked lists
A B C D
p
A B C
D
p
A B C
Thank you
top related