SlideShare une entreprise Scribd logo
1  sur  23
L I N K E D L IST
       Dr. S. Lovelyn Rose
       PSG College of Technology
SINGLY LINKED LIST
                   DATA         ADDRESS



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



DOUBLY LINKED LIST

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


  Previous node            Data part                     Next node
Start   ID   Name   Desig   Address
                            of Node2


        ID   Name   Desig   Address of
                            Node 3


        ID   Name   Desig   NULL
Start   NULL         Data 1   Address of
                                Node 2


        Address of   Data 2   Address of
          Node 1                Node 3

        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”);
}
 Download the file from
http://www.slideshare.net/lovelynrose/linked-list-17752737

Also visit my blogs :

  http://datastructuresinterview.blogspot.in/

  http://talkcoimbatore.blogspot.in/

  http://simpletechnical.blogspot.in/

Contenu connexe

Tendances

Aho-Corasick string matching algorithm
Aho-Corasick string matching algorithmAho-Corasick string matching algorithm
Aho-Corasick string matching algorithm
Takatoshi Kondo
 
Java весна 2013 лекция 2
Java весна 2013 лекция 2Java весна 2013 лекция 2
Java весна 2013 лекция 2
Technopark
 
Lesson 01 A Warmup Problem
Lesson 01 A Warmup ProblemLesson 01 A Warmup Problem
Lesson 01 A Warmup Problem
Mitchell Wand
 

Tendances (20)

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
 
Aho-Corasick string matching algorithm
Aho-Corasick string matching algorithmAho-Corasick string matching algorithm
Aho-Corasick string matching algorithm
 
The Ring programming language version 1.10 book - Part 28 of 212
The Ring programming language version 1.10 book - Part 28 of 212The Ring programming language version 1.10 book - Part 28 of 212
The Ring programming language version 1.10 book - Part 28 of 212
 
Java весна 2013 лекция 2
Java весна 2013 лекция 2Java весна 2013 лекция 2
Java весна 2013 лекция 2
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
 
Snake.c
Snake.cSnake.c
Snake.c
 
week-13x
week-13xweek-13x
week-13x
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methods
 
Short intro to the Rust language
Short intro to the Rust languageShort intro to the Rust language
Short intro to the Rust language
 
Lesson 01 A Warmup Problem
Lesson 01 A Warmup ProblemLesson 01 A Warmup Problem
Lesson 01 A Warmup Problem
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
Collection Core Concept
Collection Core ConceptCollection Core Concept
Collection Core Concept
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
c programming
c programmingc programming
c programming
 
Cquestions
Cquestions Cquestions
Cquestions
 
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
 
The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189
 
The Ring programming language version 1.2 book - Part 13 of 84
The Ring programming language version 1.2 book - Part 13 of 84The Ring programming language version 1.2 book - Part 13 of 84
The Ring programming language version 1.2 book - Part 13 of 84
 

En vedette (13)

linked list
linked list linked list
linked list
 
Linked List
Linked ListLinked List
Linked List
 
Link list
Link listLink list
Link list
 
Singly linked lists
Singly linked listsSingly linked lists
Singly linked lists
 
LINKED LISTS
LINKED LISTSLINKED LISTS
LINKED LISTS
 
Car Parking System (Singly linked list )
Car Parking System (Singly linked list ) Car Parking System (Singly linked list )
Car Parking System (Singly linked list )
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 
Team 10
Team 10Team 10
Team 10
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
De lab manual
De lab manualDe lab manual
De lab manual
 
Linked list
Linked listLinked list
Linked list
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 

Similaire à Linked list without animation

#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
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
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
 
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdfI will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
funkybabyindia
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
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
 
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
 
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
 
C++ Program to Implement Singly Linked List #includeiostream.pdf
 C++ Program to Implement Singly Linked List #includeiostream.pdf C++ Program to Implement Singly Linked List #includeiostream.pdf
C++ Program to Implement Singly Linked List #includeiostream.pdf
angelsfashion1
 
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
 

Similaire à Linked list without animation (20)

#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 structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
5. Summing Series.pptx
5. Summing Series.pptx5. Summing Series.pptx
5. Summing Series.pptx
 
Insertion operation
Insertion operationInsertion operation
Insertion operation
 
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
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdfI will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
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
 
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
 
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
 
Linked list imp of list
Linked list imp of listLinked list imp of list
Linked list imp of list
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
C++ Program to Implement Singly Linked List #includeiostream.pdf
 C++ Program to Implement Singly Linked List #includeiostream.pdf C++ Program to Implement Singly Linked List #includeiostream.pdf
C++ Program to Implement Singly Linked List #includeiostream.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
 
Senior design project code for PPG
Senior design project code for PPGSenior design project code for PPG
Senior design project code for PPG
 

Dernier

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
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

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
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
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
 
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
 
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
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
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
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
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
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Linked list without animation

  • 1. L I N K E D L IST Dr. S. Lovelyn Rose PSG College of Technology
  • 2. SINGLY LINKED LIST DATA ADDRESS Data 1 Data 2 . . . . . . Data n ADDRESS DOUBLY LINKED LIST ADDRESS Data 1 Data 2 . . . . . Data n ADDRESS Previous node Data part Next node
  • 3. Start ID Name Desig Address of Node2 ID Name Desig Address of Node 3 ID Name Desig NULL
  • 4. Start NULL Data 1 Address of Node 2 Address of Data 2 Address of Node 1 Node 3 Address of Data 3 Node 2 NULL
  • 5.  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
  • 6. struct node { int x; x c[10] next char c[10]; struct node *next; }*current;
  • 7. 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
  • 8. 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
  • 9. Number of nodes Allocation Insert_N_Nodes(N) and { Assignment Start=current=create_node(); Start, current 011 ABI HR NULL
  • 10. 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
  • 11. 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; }
  • 12. 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
  • 13. 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’
  • 14.  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)
  • 15. { 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. Start 50 else { temp1->next=temp->next; temp1 20 temp->next=null; break; temp 10 } } Any node temp1=temp; 40 temp=temp->next; } }
  • 20. 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
  • 21. 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 }
  • 22. 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”); }
  • 23.  Download the file from http://www.slideshare.net/lovelynrose/linked-list-17752737 Also visit my blogs : http://datastructuresinterview.blogspot.in/ http://talkcoimbatore.blogspot.in/ http://simpletechnical.blogspot.in/