SlideShare uma empresa Scribd logo
1 de 21
L I N K E D L IST
  (Download file to view animation)

               Dr. S. Lovelyn Rose
               PSG College of Technology
               Coimbatore, India
SINGLY LINKED LIST
                    DATA         ADDRESS



                Data 1 Data 2 . . . . . . Data n    ADDRESS



DOUBLY LINKED LIST
                ID                 Name     Desig Address
        Start
                                                  of Node2
     ADDRESS      Data 1 Data 2 . . . . . Data n ADDRESS
     Start         NULL               Data 1     Address of
                         ID     Name Desig        Address of
                                                   Node 2
                                                  Node 3
  Previous node             Data part                  Next node
                   Address of         Data 2         Address of
                     NodeID1       Name Desig         NULL 3
                                                       Node

                   Address of           Data 3
                     Node 2                            NULL
 CIRCULAR LINKED LIST
          SINGLY
  Start



          Data 1    Node 2       Data 2   Node 3            Data 3     Node1
              Node 1                  Node 2                    Node 3
          DOUBLY
  Start



Node3     Data 1    Node 2   Node 1   Data 1   Node 3

           Node 1                     Node 2       Node 2     Data 1     Node1

                                                              Node 3
struct node
{
  int x;
                     x   c[10]   next
   char c[10];
struct node *next;
}*current;
create_node()
{
  current=(struct node *)malloc(sizeof(struct node));
  current->x=10; current->c=“try”; current->next=null;
  return current;
}
Allocation               x    c[10]     next



Assignment                10    try     NULL
Create a linked list for maintaining the employee details
  such as Ename,Eid,Edesig.
Steps:
1)Identify the node structure struct node
                                 {
2)Allocate space for the node
                                   int Eid;
3)Insert the node in the list      char Ename[10],Edesig[10];
                                     struct node *next;
                                  } *current;



     EID        EName        EDesig          next
Number of nodes
                                      Allocation
Insert_N_Nodes(N)
                                      and
{                                     Assignment
  Start=current=create_node();
 Start,
current

            011     ABI          HR         NULL
for(i=1;i<n-1;i++)
  {
      current->next=create_node();
      current=current->next;
} }
  Start,
 current

             011       ABI           HR


     i=1     012       Banu      DBA      NULL
start


Insert()
{                      10     c
  c=create_node();
  start=c;
                       20     c1
  c1=create_node();
  c->next=c1;
  c2=create_node();    30     c2
  c1->next=c2;
}
Start
                        20                        30
Insert_a_node(Start)
{                              Start
current= create_node();                     10
current->next=Start;                             20
Start=current;
}                                current               30

                                 10
Note : Passing Start as a parameter implies the list already exists
Start


   10            20             30             40



                         25
Steps:
1)Have a pointer (slow) to the node after which the new
  node is to be inserted
2)Create the new node to be inserted and store the address
  in ‘current’
3) Adjust the pointers of ‘slow’ and ‘current’
 Use two pointers namely, fast and slow
 Move the ‘slow’ pointer by one element in the list
 For every single movement of ‘slow’, move ‘fast’ by 2
  elements
 When ‘fast’ points to the last element, ‘slow’ would be
  pointing to the middle element
 This required moving through the list only once
 So the middle element is found in a time complexity of
  O(n)
{
current=(struct node *)malloc(sizeof(struct node))
fast=slow=Start                    Start          10
do
                                                 fast slow
{
  if(fast->next->next!=null)                      20
        fast=fast->next->next     current
  else break;
  slow=slow->next                25               30
}while(fast->next!=null)
current->next=slow->next
slow->next=current                                40
}
Note : fast and slow are below the node they point to
Insert_last(Start)
{                               Start   10
  current=Start;
  while(current->next!=null)
                                        20
  {
      current=current->next;
  }                           temp
                                        30
  temp=create_node();        25
  current->next=temp;
}                                       40
delete_node(start,x)
{
  temp=start;
  temp1=temp;
  while(temp!=null)     Last node
  {
  if(temp->n==x)                      start
  {               Only one node
  if(temp==start && temp->next==null)         10
       start=null                              temp
start
else if(temp==start)
{                         First node
                                                 10           temp
  start=start->next;
  temp->next=null;
                                           20
  break;
}
                                 start
else if(temp->next==null)
                                                      temp1
{
                                                  10
  temp1->next=null;
                                                      temp
}
                                                  20

       Last node
Start                 50
else
{
  temp1->next=temp->next;              temp1        20

  temp->next=null;
  break;
                                      temp     10
}
}
                   Any node
temp1=temp;                                         40

temp=temp->next;
}
}
Inserting N nodes
Insert_node(N)          create_node()
{                       {
                        c=(struct node*)malloc(sizeof(struct
start=c=create_node();
                                                      node))
for(i=0;i<N-1;i++)             c->x;
{                              c->previous=null;
  c1=create_node();            c->next=null;
                               return c;
  c->next=c1;           }
  c1->previous=c;
}
}                      10                      20

    start              c                      c1
delete_node(start,n)
{
                                 start
temp=start
while(temp!=null)    End of list          10

{                                        temp
if(temp->x==n)
{                     single node
 if(temp==start)
{
temp->next->previous=null;
start=temp->next
}
else if(temp->next==null)                    start

 temp->previous->next=null;
else                                           20
            Last node
{
temp->next->previous=temp->previous;
                                                10
temp->previous->next=temp->next;
                                              temp
}
                     middle node              30
}
else
  temp=temp->next;
}
if(temp==null) print(“Element not found”);
}
http://datastructuresinterview.blogspot.in/

http://talkcoimbatore.blogspot.in/

http://simpletechnical.blogspot.in/

Mais conteúdo relacionado

Mais procurados

NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variables
Teerawat Issariyakul
 
09 - Program verification
09 - Program verification09 - Program verification
09 - Program verification
Tudor Girba
 
Java весна 2013 лекция 2
Java весна 2013 лекция 2Java весна 2013 лекция 2
Java весна 2013 лекция 2
Technopark
 
ARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lecture
anishgoel
 

Mais procurados (20)

Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
2019-10-05 - Untangled - Voxxed days ticino
2019-10-05 - Untangled - Voxxed days ticino2019-10-05 - Untangled - Voxxed days ticino
2019-10-05 - Untangled - Voxxed days ticino
 
Image Recognition with Neural Network
Image Recognition with Neural NetworkImage Recognition with Neural Network
Image Recognition with Neural Network
 
The Node.js Event Loop: Not So Single Threaded
The Node.js Event Loop: Not So Single ThreadedThe Node.js Event Loop: Not So Single Threaded
The Node.js Event Loop: Not So Single Threaded
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variables
 
Lectia 3. aplicații greedy.
Lectia 3. aplicații greedy.Lectia 3. aplicații greedy.
Lectia 3. aplicații greedy.
 
09 - Program verification
09 - Program verification09 - Program verification
09 - Program verification
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
 
Página 115
Página 115Página 115
Página 115
 
C++ & Java JIT Optimizations: Finding Prime Numbers
C++ & Java JIT Optimizations: Finding Prime NumbersC++ & Java JIT Optimizations: Finding Prime Numbers
C++ & Java JIT Optimizations: Finding Prime Numbers
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluiton
 
Scale Up with Lock-Free Algorithms @ JavaOne
Scale Up with Lock-Free Algorithms @ JavaOneScale Up with Lock-Free Algorithms @ JavaOne
Scale Up with Lock-Free Algorithms @ JavaOne
 
Operational research
Operational researchOperational research
Operational research
 
Java весна 2013 лекция 2
Java весна 2013 лекция 2Java весна 2013 лекция 2
Java весна 2013 лекция 2
 
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
 
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
 
Lecture1 classes3
Lecture1 classes3Lecture1 classes3
Lecture1 classes3
 
ARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lecture
 

Destaque

Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
Tushar Aneyrao
 

Destaque (13)

Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Linked List data structure
Linked List data structureLinked List data structure
Linked List data structure
 
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
 
Linked list
Linked listLinked list
Linked list
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Linked lists
Linked listsLinked lists
Linked lists
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
linked list
linked list linked list
linked list
 
Link List
Link ListLink List
Link List
 
Linked list
Linked listLinked list
Linked list
 
Linked lists
Linked listsLinked lists
Linked lists
 
Linked list
Linked listLinked list
Linked list
 
Link list
Link listLink list
Link list
 

Semelhante a Linked list

#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
harihelectronicspune
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
mail931892
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdf
facevenky
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
arkmuzikllc
 
C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdf
anupambedcovers
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
aathiauto
 

Semelhante a Linked list (20)

data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
 
5. Summing Series.pptx
5. Summing Series.pptx5. Summing Series.pptx
5. Summing Series.pptx
 
Insertion operation
Insertion operationInsertion operation
Insertion operation
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
Linked list imp of list
Linked list imp of listLinked list imp of list
Linked list imp of list
 
double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdf
 
Implement of c &amp; its coding programming by sarmad baloch
Implement of c &amp; its coding  programming by sarmad balochImplement of c &amp; its coding  programming by sarmad baloch
Implement of c &amp; its coding programming by sarmad baloch
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdf
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 

Último

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Linked list

  • 1. L I N K E D L IST (Download file to view animation) Dr. S. Lovelyn Rose PSG College of Technology Coimbatore, India
  • 2. SINGLY LINKED LIST DATA ADDRESS Data 1 Data 2 . . . . . . Data n ADDRESS DOUBLY LINKED LIST ID Name Desig Address Start of Node2 ADDRESS Data 1 Data 2 . . . . . Data n ADDRESS Start NULL Data 1 Address of ID Name Desig Address of Node 2 Node 3 Previous node Data part Next node Address of Data 2 Address of NodeID1 Name Desig NULL 3 Node Address of Data 3 Node 2 NULL
  • 3.  CIRCULAR LINKED LIST SINGLY Start Data 1 Node 2 Data 2 Node 3 Data 3 Node1 Node 1 Node 2 Node 3 DOUBLY Start Node3 Data 1 Node 2 Node 1 Data 1 Node 3 Node 1 Node 2 Node 2 Data 1 Node1 Node 3
  • 4. struct node { int x; x c[10] next char c[10]; struct node *next; }*current;
  • 5. create_node() { current=(struct node *)malloc(sizeof(struct node)); current->x=10; current->c=“try”; current->next=null; return current; } Allocation x c[10] next Assignment 10 try NULL
  • 6. Create a linked list for maintaining the employee details such as Ename,Eid,Edesig. Steps: 1)Identify the node structure struct node { 2)Allocate space for the node int Eid; 3)Insert the node in the list char Ename[10],Edesig[10]; struct node *next; } *current; EID EName EDesig next
  • 7. Number of nodes Allocation Insert_N_Nodes(N) and { Assignment Start=current=create_node(); Start, current 011 ABI HR NULL
  • 8. for(i=1;i<n-1;i++) { current->next=create_node(); current=current->next; } } Start, current 011 ABI HR i=1 012 Banu DBA NULL
  • 9. start Insert() { 10 c c=create_node(); start=c; 20 c1 c1=create_node(); c->next=c1; c2=create_node(); 30 c2 c1->next=c2; }
  • 10. Start 20 30 Insert_a_node(Start) { Start current= create_node(); 10 current->next=Start; 20 Start=current; } current 30 10 Note : Passing Start as a parameter implies the list already exists
  • 11. Start 10 20 30 40 25 Steps: 1)Have a pointer (slow) to the node after which the new node is to be inserted 2)Create the new node to be inserted and store the address in ‘current’ 3) Adjust the pointers of ‘slow’ and ‘current’
  • 12.  Use two pointers namely, fast and slow  Move the ‘slow’ pointer by one element in the list  For every single movement of ‘slow’, move ‘fast’ by 2 elements  When ‘fast’ points to the last element, ‘slow’ would be pointing to the middle element  This required moving through the list only once  So the middle element is found in a time complexity of O(n)
  • 13. { current=(struct node *)malloc(sizeof(struct node)) fast=slow=Start Start 10 do fast slow { if(fast->next->next!=null) 20 fast=fast->next->next current else break; slow=slow->next 25 30 }while(fast->next!=null) current->next=slow->next slow->next=current 40 } Note : fast and slow are below the node they point to
  • 14. Insert_last(Start) { Start 10 current=Start; while(current->next!=null) 20 { current=current->next; } temp 30 temp=create_node(); 25 current->next=temp; } 40
  • 15. delete_node(start,x) { temp=start; temp1=temp; while(temp!=null) Last node { if(temp->n==x) start { Only one node if(temp==start && temp->next==null) 10 start=null temp
  • 16. start else if(temp==start) { First node 10 temp start=start->next; temp->next=null; 20 break; } start else if(temp->next==null) temp1 { 10 temp1->next=null; temp } 20 Last node
  • 17. Start 50 else { temp1->next=temp->next; temp1 20 temp->next=null; break; temp 10 } } Any node temp1=temp; 40 temp=temp->next; } }
  • 18. Inserting N nodes Insert_node(N) create_node() { { c=(struct node*)malloc(sizeof(struct start=c=create_node(); node)) for(i=0;i<N-1;i++) c->x; { c->previous=null; c1=create_node(); c->next=null; return c; c->next=c1; } c1->previous=c; } } 10 20 start c c1
  • 19. delete_node(start,n) { start temp=start while(temp!=null) End of list 10 { temp if(temp->x==n) { single node if(temp==start) { temp->next->previous=null; start=temp->next }
  • 20. else if(temp->next==null) start temp->previous->next=null; else 20 Last node { temp->next->previous=temp->previous; 10 temp->previous->next=temp->next; temp } middle node 30 } else temp=temp->next; } if(temp==null) print(“Element not found”); }