Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT-II Topics to be covered Singly linked list Circular linked list

Similar presentations


Presentation on theme: "UNIT-II Topics to be covered Singly linked list Circular linked list"— Presentation transcript:

1 UNIT-II Topics to be covered Singly linked list Circular linked list
Doubly linked list Representing Stack with linked list Representing Queues with linked list

2 In array elements are stored in consecutive memory locations
In array elements are stored in consecutive memory locations. To occupy the adjacent space, block of memory that is required for the array should be allocated before hand. Once memory allocated it cannot be extended any more. So that array is called the static data structure. Wastage of memory is more in arrays.

3 What’s wrong with Array and Why lists?
Disadvantages of arrays as storage data structures: slow searching in unordered array slow insertion in ordered array Fixed size Linked lists solve some of these problems Linked lists are general purpose storage data structures and are versatile.

4 Linked list is able to grow in size as needed
Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to grow in size as needed Does not require the shifting of items during insertions and deletions.

5 Linked Lists

6 Linked list\singly linked list
It is a dynamic data structure where amount of memory required can be varied during its use. A linked list consists of a collection of elements called nodes linked together by pointers. In linked list, adjacency between the elements are maintained by means of links or pointers.

7 Linked List Collection of links with reference to the first.
Each link (node) has Data Part: part to store data Link Part :link that refers to the next node in the list. Data part of the node can be an integer, a character, a String or an object of any kind.

8 Node Head Node

9 Classification of linked list
Depending on the requirements the pointers are maintained, and accordingly linked list can be classified into three groups. Single linked list Circular linked list Double linked list\doubly linked list.

10 Singly Linked Lists A linked list is an ordered collection of homogeneous data elements called nodes where linear order is maintained by means of links and pointers. Each node stores Element\data(may be integer, char or string) link to the next node next node elem Start Null A B C D

11 single linked list First link has dummy value for data
reference to next link refers to the first link that hold information Last link does not refer to the next one. Reference to the next link is null

12 Operations on singly linked list
Insertion Deletion Traverse Search

13 Insertion in linked list
There are various positions where node can be inserted. Insert at front ( as a first element) Insert at end ( as a last node) Insert at middle ( any position)

14 Insert new node at front in linked list
Algorithm step1- create a new node Step2- new->link=header->link Step3- new->data=item Step4- header->link=new. Step5-stop

15 header Null 10 20 30 2 2 1 1 5 Newnode

16 Insert new node at end of the linked list
Algorithm Step1- create a new node Step2- ptr=header Step3- while(ptr->link!=null) 3.1. ptr=ptr->link Step4- ptr->link=new Step5- new->data=item Step6- new->link=null Step7-stop

17 header ptr Null 10 20 30 1 40 Newnode

18 Insert new node at any position in linked list
Algorithm 1.Create new node 2. ptr=header 3. while((ptr->data!=key)&&(ptr->link!=null) 3.1 ptr=ptr->link 4. if(ptr->link=null 4.1.print key is not found in the list 5. Else 5.1 new->link=ptr->link 5.2 new->data=item 5.3. ptr->link= new 6. End if 7. stop.

19 ptr header Null 10 20 30 2 1 5 Newnode

20 Deletion of a node from ll
Like insertion, there are also various cases of deletion: Deletion at the front Deletion at the end Deletion at any position in the list

21 1.Deletion at the front Algorithm ptr=header->link If( ptr==NULL)
2.1 print “the list is empty” 3. else 3.1 ptr1=ptr->link 3.2 header->link=ptr1 3.3 return ptr 4. End if 5. stop

22 header Null KEY 20 30 ptr ptr1

23 2. Deletion at the end Algorithm 1.ptr=header
2. if(ptr->link==NULL) 2.1 print “ the list is empty” 3. Else 3.1 while(ptr->link!=NULL) 3.2 ptr1=ptr 3.3 ptr=ptr->link; 3.4 end while 4 ptr1->link=NULL 4.1 return ptr 4. End if 5. stop

24 ptr header ptr1 Null KEY 20 30

25 3. Deletion at any position
Algorithm ptr1=header ptr=ptr1->link While(ptr!=NULL) 3.1 if(ptr->data!=key) 3.2 ptr1=ptr 3.3 ptr=ptr->link 4. Else 4.1 ptr1->link=ptr->link 4.2 return ptr 5. End while 6. If (ptr==NULL) 6.1 print “key does not exist” 7. stop

26 ptr1 header ptr Null 10 KEY 30


Download ppt "UNIT-II Topics to be covered Singly linked list Circular linked list"

Similar presentations


Ads by Google