270 likes | 561 Views
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.
E N D
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
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.
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.
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.
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.
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.
Node Head Node
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.
Singly Linked Lists next • 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 node elem Start Null A B C D
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
Operations on singly linked list • Insertion • Deletion • Traverse • Search
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)
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
header Null 10 20 30 2 2 1 1 5 Newnode
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
header ptr Null 10 20 30 1 40 Newnode
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.
ptr header Null 10 20 30 2 1 5 Newnode
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
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
header Null KEY 20 30 ptr ptr1
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
ptr ptr1 header Null KEY 20 30
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
ptr1 ptr header Null 10 KEY 30