SlideShare a Scribd company logo
1 of 20
© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia 1
© 2004 Goodrich, Tamassia
Arrays: pros and cons
Advantages:
Fast element access.
Disadvantages:
 Impossible to resize.
Why require Linked List:
• Many applications require resizing!
• Required size not always immediately available.
2
© 2004 Goodrich, Tamassia
Singly Linked Lists (SLL)
A singly linked list is a concrete data
structure consisting of a sequence of
nodes
Each node stores value
Each node stores link to the next
node
Last node has null value
SLL can only point to the next Node
in the list but not to the previous.
3
next
value node
20 15 12 10
NULL
© 2004 Goodrich, Tamassia
Creating list structure
struct Node
{
int Data;
struct Node *Next;
}*Head;
4
© 2004 Goodrich, Tamassia
Starting SLL
Initially we set 'Head' as NULL which means list is
empty.
//Set HEAD as NULL
Head=NULL;
5
© 2004 Goodrich, Tamassia
Add element at beginning
6
We create 'temp' node and save the Data part.
If Head is NULL it means list is empty. So we set temp
node as the Head of the list and set the Next as NULL.
Else we set the Next in temp node as Head and reassign
Head with temp.
© 2004 Goodrich, Tamassia
Code for add at beginning
// Adding a Node at the Beginning of the List  
  
void addBeg(int num)  
{  
   struct Node *temp;  
  
   temp=(struct Node *)malloc(sizeof(struct Node));  
   temp->Data = num;  
  
   if (Head == NULL)  
   {  
      //List is Empty  
      Head=temp;  
      Head->Next=NULL;  
   }  
   else  
   {  
      temp->Next=Head;  
      Head=temp;  
   }  
}  
7
© 2004 Goodrich, Tamassia
Add element at the end
8
© 2004 Goodrich, Tamassia
Code for add at the end
//Adding a Node at the end of the list  
  
 void addEnd(int num)  
 {  
    struct Node *temp1, *temp2;  
  
    temp1=(struct Node *)malloc(sizeof(struct Node));  
    temp1->Data=num;  
  
    // Copying the Head location into another node.  
    temp2=Head;  
  
    if(Head == NULL)  
    {  
       // If List is empty we create First Node.  
       Head=temp1;  
       Head->Next=NULL;  
    }  
    else  
    {  
       // Traverse down to end of the list.  
       while(temp2->Next != NULL)  
       temp2=temp2->Next;  
  
       // Append at the end of the list.  
       temp1->Next=NULL;  
       temp2->Next=temp1;  
    }  
 }  
9
© 2004 Goodrich, Tamassia
Add at specific position
10
© 2004 Goodrich, Tamassia
Counting Elements
// Counting number of elements in the List  
  
int length()  
{  
   struct Node *cur_ptr;  
   int count=0;  
  
   cur_ptr=Head;  
  
   while(cur_ptr != NULL)  
   {  
      cur_ptr=cur_ptr->Next;  
      count++;  
   }  
   return(count);  
}  
11
© 2004 Goodrich, Tamassia
// Adding a new Node at specified position
 void addAt(int num, int loc)
 {
    int i;
    struct Node *temp, *prev_ptr, *cur_ptr;
    cur_ptr=Head;
    if(loc > (length()+1) || loc <= 0)
    {
       printf("nInsertion at given location is not 
possiblen ");
    }
    
12
© 2004 Goodrich, Tamassia
else
    {
        // If the location is starting of the list
        if (loc == 1)
        {
            addBeg(num);
        }
        else
        {
            for(i=1;i<loc;i++)
            {
                prev_ptr=cur_ptr;   
                cur_ptr=cur_ptr->Next;
            }
            temp=(struct Node *)malloc(sizeof(struct Node));
            temp->Data=num;
            prev_ptr->Next=temp;
            temp->Next=cur_ptr;
        }
    }
13
© 2004 Goodrich, Tamassia
Delete node using value
14
• If the value to be deleted is the head of the list.
© 2004 Goodrich, Tamassia
Delete node using value
15
• If the value to be deleted is not the head of the list.
© 2004 Goodrich, Tamassia
Delete node using value
// Deleting a node from List depending upon the data in the node.  
  
int delNodeData(int num)  
{  
   struct Node *prev_ptr, *cur_ptr;  
  
   cur_ptr=Head;  
  
   while(cur_ptr != NULL)  
   {  
      if(cur_ptr->Data == num)  
      {  
         if(cur_ptr==Head)  
         {  
            Head=cur_ptr->Next;  
            free(cur_ptr);  
            return 0;  
         }  
         else  
         {  
            prev_ptr->Next=cur_ptr->Next;  
            free(cur_ptr);  
            return 0;  
         }  
      }  
      else  
      {  
         prev_ptr=cur_ptr;  
         cur_ptr=cur_ptr->Next;  
      }  
   }  
  
   printf("nElement %d is not found in the List", num);  
   return 1;  
}  
16
© 2004 Goodrich, Tamassia
Delete using location
// Deleting a node from List depending upon the location in the list.  
  
int delNodeLoc(int loc)  
{  
   struct Node *prev_ptr, *cur_ptr;  
   int i;  
  
   cur_ptr=Head;  
  
   if(loc > (length()) || loc <= 0)  
   {  
       printf("nDeletion of Node at given location is not possiblen ");  
   }  
   else  
   {  
       // If the location is starting of the list  
       if (loc == 1)  
       {  
           Head=cur_ptr->Next;  
           free(cur_ptr);  
           return 0;  
       }  
       else  
       {  
           for(i=1;i<loc;i++)  
           {  
               prev_ptr=cur_ptr;  
               cur_ptr=cur_ptr->Next;  
           }  
  
           prev_ptr->Next=cur_ptr->Next;  
           free(cur_ptr);  
       }  
   }  
   return 1;  
}  
17
© 2004 Goodrich, Tamassia
Displaying List
// Displaying list contents  
  
void display()  
{  
   struct Node *cur_ptr;  
  
   cur_ptr=Head;  
  
   if(cur_ptr==NULL)  
   {  
      printf("nList is Empty");  
   }  
   else  
   {  
       printf("Elements in the List: ");  
       //traverse the entire linked list  
       while(cur_ptr!=NULL)  
       {  
           printf(" -> %d ",cur_ptr->Data);  
           cur_ptr=cur_ptr->Next;  
       }  
       printf("n");  
   }  
}  
18
© 2004 Goodrich, Tamassia
Reversing List
//Reversesing a Linked List  
  
void reverse()  
{  
   struct Node *prev_ptr, *cur_ptr, *temp;  
  
   cur_ptr=Head;  
   prev_ptr=NULL;  
  
   while(cur_ptr != NULL)  
   {  
      temp=prev_ptr;  
      prev_ptr=cur_ptr;  
  
      cur_ptr=cur_ptr->Next;  
      prev_ptr->Next=temp;  
   }  
  
   Head=prev_ptr;  
}  
19
© 2004 Goodrich, Tamassia
Disadvantages
Slower access to node
Can’t access particular node directly. We have to
traverse whole list till the concern node found.
Every insertion and deletion my involve shifting
nodes
20

More Related Content

What's hot

What's hot (20)

linked list using c
linked list using clinked list using c
linked list using c
 
Implementation of queue using singly and doubly linked list.
Implementation of queue using singly and doubly linked list.Implementation of queue using singly and doubly linked list.
Implementation of queue using singly and doubly linked list.
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
Linklist
LinklistLinklist
Linklist
 
Linked list
Linked listLinked list
Linked list
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Linked list
Linked listLinked list
Linked list
 
Python lists
Python listsPython lists
Python lists
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUE
 
Linked lists 1
Linked lists 1Linked lists 1
Linked lists 1
 
Singly link list
Singly link listSingly link list
Singly link list
 
Stack & Queue using Linked List in Data Structure
Stack & Queue using Linked List in Data StructureStack & Queue using Linked List in Data Structure
Stack & Queue using Linked List in Data Structure
 
Linked list
Linked listLinked list
Linked list
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Linked list in Data structure
Linked list in Data structureLinked list in Data structure
Linked list in Data structure
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 

Viewers also liked

Viewers also liked (10)

Client Presentation
Client PresentationClient Presentation
Client Presentation
 
Articulacoes
ArticulacoesArticulacoes
Articulacoes
 
E learning con ideas de begoña gros
E learning con ideas de begoña grosE learning con ideas de begoña gros
E learning con ideas de begoña gros
 
Ativ7.3 zeniaportfolio 2014
Ativ7.3 zeniaportfolio 2014Ativ7.3 zeniaportfolio 2014
Ativ7.3 zeniaportfolio 2014
 
Svest, novembar 2014
Svest, novembar 2014Svest, novembar 2014
Svest, novembar 2014
 
Choquela
ChoquelaChoquela
Choquela
 
Aspire ppt - 01.06.15
Aspire   ppt - 01.06.15Aspire   ppt - 01.06.15
Aspire ppt - 01.06.15
 
Blood supply-of-brain
Blood supply-of-brainBlood supply-of-brain
Blood supply-of-brain
 
Ici project 2
Ici project 2 Ici project 2
Ici project 2
 
Presentation_NEW.PPTX
Presentation_NEW.PPTXPresentation_NEW.PPTX
Presentation_NEW.PPTX
 

Similar to Linked Lists: Advantages, Disadvantages, and Operations

This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdffathimahardwareelect
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
public class SLLT { protected SLLNodeT head, tail; pub.pdf
public class SLLT {     protected SLLNodeT head, tail;     pub.pdfpublic class SLLT {     protected SLLNodeT head, tail;     pub.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdfclarityvision
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked listAmit Vats
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxMatthPYNashd
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.pptWaf1231
 
Use the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdfUse the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdfsales87
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxBrianGHiNewmanv
 

Similar to Linked Lists: Advantages, Disadvantages, and Operations (20)

DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Linked list
Linked list Linked list
Linked list
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
Linked list
Linked listLinked list
Linked list
 
Savitch Ch 13
Savitch Ch 13Savitch Ch 13
Savitch Ch 13
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Savitch ch 13
Savitch ch 13Savitch ch 13
Savitch ch 13
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
public class SLLT { protected SLLNodeT head, tail; pub.pdf
public class SLLT {     protected SLLNodeT head, tail;     pub.pdfpublic class SLLT {     protected SLLNodeT head, tail;     pub.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdf
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 
Use the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdfUse the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdf
 
Linked lists
Linked listsLinked lists
Linked lists
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 

Linked Lists: Advantages, Disadvantages, and Operations

  • 1. © 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia 1
  • 2. © 2004 Goodrich, Tamassia Arrays: pros and cons Advantages: Fast element access. Disadvantages:  Impossible to resize. Why require Linked List: • Many applications require resizing! • Required size not always immediately available. 2
  • 3. © 2004 Goodrich, Tamassia Singly Linked Lists (SLL) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores value Each node stores link to the next node Last node has null value SLL can only point to the next Node in the list but not to the previous. 3 next value node 20 15 12 10 NULL
  • 4. © 2004 Goodrich, Tamassia Creating list structure struct Node { int Data; struct Node *Next; }*Head; 4
  • 5. © 2004 Goodrich, Tamassia Starting SLL Initially we set 'Head' as NULL which means list is empty. //Set HEAD as NULL Head=NULL; 5
  • 6. © 2004 Goodrich, Tamassia Add element at beginning 6 We create 'temp' node and save the Data part. If Head is NULL it means list is empty. So we set temp node as the Head of the list and set the Next as NULL. Else we set the Next in temp node as Head and reassign Head with temp.
  • 7. © 2004 Goodrich, Tamassia Code for add at beginning // Adding a Node at the Beginning of the List      void addBeg(int num)   {      struct Node *temp;         temp=(struct Node *)malloc(sizeof(struct Node));      temp->Data = num;         if (Head == NULL)      {         //List is Empty         Head=temp;         Head->Next=NULL;      }      else      {         temp->Next=Head;         Head=temp;      }   }   7
  • 8. © 2004 Goodrich, Tamassia Add element at the end 8
  • 9. © 2004 Goodrich, Tamassia Code for add at the end //Adding a Node at the end of the list       void addEnd(int num)    {       struct Node *temp1, *temp2;          temp1=(struct Node *)malloc(sizeof(struct Node));       temp1->Data=num;          // Copying the Head location into another node.       temp2=Head;          if(Head == NULL)       {          // If List is empty we create First Node.          Head=temp1;          Head->Next=NULL;       }       else       {          // Traverse down to end of the list.          while(temp2->Next != NULL)          temp2=temp2->Next;             // Append at the end of the list.          temp1->Next=NULL;          temp2->Next=temp1;       }    }   9
  • 10. © 2004 Goodrich, Tamassia Add at specific position 10
  • 11. © 2004 Goodrich, Tamassia Counting Elements // Counting number of elements in the List      int length()   {      struct Node *cur_ptr;      int count=0;         cur_ptr=Head;         while(cur_ptr != NULL)      {         cur_ptr=cur_ptr->Next;         count++;      }      return(count);   }   11
  • 12. © 2004 Goodrich, Tamassia // Adding a new Node at specified position  void addAt(int num, int loc)  {     int i;     struct Node *temp, *prev_ptr, *cur_ptr;     cur_ptr=Head;     if(loc > (length()+1) || loc <= 0)     {        printf("nInsertion at given location is not  possiblen ");     }      12
  • 13. © 2004 Goodrich, Tamassia else     {         // If the location is starting of the list         if (loc == 1)         {             addBeg(num);         }         else         {             for(i=1;i<loc;i++)             {                 prev_ptr=cur_ptr;                    cur_ptr=cur_ptr->Next;             }             temp=(struct Node *)malloc(sizeof(struct Node));             temp->Data=num;             prev_ptr->Next=temp;             temp->Next=cur_ptr;         }     } 13
  • 14. © 2004 Goodrich, Tamassia Delete node using value 14 • If the value to be deleted is the head of the list.
  • 15. © 2004 Goodrich, Tamassia Delete node using value 15 • If the value to be deleted is not the head of the list.
  • 16. © 2004 Goodrich, Tamassia Delete node using value // Deleting a node from List depending upon the data in the node.      int delNodeData(int num)   {      struct Node *prev_ptr, *cur_ptr;         cur_ptr=Head;         while(cur_ptr != NULL)      {         if(cur_ptr->Data == num)         {            if(cur_ptr==Head)            {               Head=cur_ptr->Next;               free(cur_ptr);               return 0;            }            else            {               prev_ptr->Next=cur_ptr->Next;               free(cur_ptr);               return 0;            }         }         else         {            prev_ptr=cur_ptr;            cur_ptr=cur_ptr->Next;         }      }         printf("nElement %d is not found in the List", num);      return 1;   }   16
  • 17. © 2004 Goodrich, Tamassia Delete using location // Deleting a node from List depending upon the location in the list.      int delNodeLoc(int loc)   {      struct Node *prev_ptr, *cur_ptr;      int i;         cur_ptr=Head;         if(loc > (length()) || loc <= 0)      {          printf("nDeletion of Node at given location is not possiblen ");      }      else      {          // If the location is starting of the list          if (loc == 1)          {              Head=cur_ptr->Next;              free(cur_ptr);              return 0;          }          else          {              for(i=1;i<loc;i++)              {                  prev_ptr=cur_ptr;                  cur_ptr=cur_ptr->Next;              }                 prev_ptr->Next=cur_ptr->Next;              free(cur_ptr);          }      }      return 1;   }   17
  • 18. © 2004 Goodrich, Tamassia Displaying List // Displaying list contents      void display()   {      struct Node *cur_ptr;         cur_ptr=Head;         if(cur_ptr==NULL)      {         printf("nList is Empty");      }      else      {          printf("Elements in the List: ");          //traverse the entire linked list          while(cur_ptr!=NULL)          {              printf(" -> %d ",cur_ptr->Data);              cur_ptr=cur_ptr->Next;          }          printf("n");      }   }   18
  • 19. © 2004 Goodrich, Tamassia Reversing List //Reversesing a Linked List      void reverse()   {      struct Node *prev_ptr, *cur_ptr, *temp;         cur_ptr=Head;      prev_ptr=NULL;         while(cur_ptr != NULL)      {         temp=prev_ptr;         prev_ptr=cur_ptr;            cur_ptr=cur_ptr->Next;         prev_ptr->Next=temp;      }         Head=prev_ptr;   }   19
  • 20. © 2004 Goodrich, Tamassia Disadvantages Slower access to node Can’t access particular node directly. We have to traverse whole list till the concern node found. Every insertion and deletion my involve shifting nodes 20