please go in to the detail and explain the purpose and function to each line & function of code listed below.  #include      //Represent a node of the singly linked list   struct node{       int data;       struct node *next;   };           //Represent the head and tail of the singly linked list   struct node *head, *tail = NULL;       //addNode() will add a new node to the list   void addNode(int data) {       //Create a new node       struct node *newNode = (struct node*)malloc(sizeof(struct node));       newNode->data = data;       newNode->next = NULL;              //Checks if the list is empty       if(head == NULL) {           //If list is empty, both head and tail will point to new node           head = newNode;           tail = newNode;       }       else {           //newNode will be added after tail such that tail's next will point to newNode           tail->next = newNode;           //newNode will become new tail of the list           tail = newNode;       }   }       //removeDuplicate() will remove duplicate nodes from the list   void removeDuplicate() {       //Node current will point to head       struct node *current = head, *index = NULL, *temp = NULL;              if(head == NULL) {           return;       }       else {           while(current != NULL){               //Node temp will point to previous node to index.               temp = current;               //Index will point to node next to current               index = current->next;                              while(index != NULL) {                   //If current node's data is equal to index node's data                   if(current->data == index->data) {                       //Here, index node is pointing to the node which is duplicate of current node                       //Skips the duplicate node by pointing to next node                       temp->next = index->next;                   }                   else {                       //Temp will point to previous node of index.                       temp = index;                   }                   index = index->next;               }               current = current->next;           }               }   }       //display() will display all the nodes present in the list   void display() {       //Node current will point to head       struct node *current = head;       if(head == NULL) {           printf("List is empty \n");           return;       }       while(current != NULL) {           //Prints each node by incrementing pointer           printf("%d ", current->data);           current = current->next;       }       printf("\n");   }          int main()   {       //Adds data to the list       addNode(1);       addNode(2);       addNode(3);       addNode(2);       addNode(2);       addNode(4);       addNode(1);              printf("Originals list: \n");       display();              //Removes duplicate nodes       removeDuplicate();              printf("List after removing duplicates: \n");       display();                  return 0;   }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

please go in to the detail and explain the purpose and function to each line & function of code listed below. 

#include <stdio.h>  
  
//Represent a node of the singly linked list  
struct node{  
    int data;  
    struct node *next;  
};      
   
//Represent the head and tail of the singly linked list  
struct node *head, *tail = NULL;  
   
//addNode() will add a new node to the list  
void addNode(int data) {  
    //Create a new node  
    struct node *newNode = (struct node*)malloc(sizeof(struct node));  
    newNode->data = data;  
    newNode->next = NULL;  
      
    //Checks if the list is empty  
    if(head == NULL) {  
        //If list is empty, both head and tail will point to new node  
        head = newNode;  
        tail = newNode;  
    }  
    else {  
        //newNode will be added after tail such that tail's next will point to newNode  
        tail->next = newNode;  
        //newNode will become new tail of the list  
        tail = newNode;  
    }  
}  
   
//removeDuplicate() will remove duplicate nodes from the list  
void removeDuplicate() {  
    //Node current will point to head  
    struct node *current = head, *index = NULL, *temp = NULL;  
      
    if(head == NULL) {  
        return;  
    }  
    else {  
        while(current != NULL){  
            //Node temp will point to previous node to index.  
            temp = current;  
            //Index will point to node next to current  
            index = current->next;  
              
            while(index != NULL) {  
                //If current node's data is equal to index node's data  
                if(current->data == index->data) {  
                    //Here, index node is pointing to the node which is duplicate of current node  
                    //Skips the duplicate node by pointing to next node  
                    temp->next = index->next;  
                }  
                else {  
                    //Temp will point to previous node of index.  
                    temp = index;  
                }  
                index = index->next;  
            }  
            current = current->next;  
        }          
    }  
}  
   
//display() will display all the nodes present in the list  
void display() {  
    //Node current will point to head  
    struct node *current = head;  
    if(head == NULL) {  
        printf("List is empty \n");  
        return;  
    }  
    while(current != NULL) {  
        //Prints each node by incrementing pointer  
        printf("%d ", current->data);  
        current = current->next;  
    }  
    printf("\n");  
}  
      
int main()  
{  
    //Adds data to the list  
    addNode(1);  
    addNode(2);  
    addNode(3);  
    addNode(2);  
    addNode(2);  
    addNode(4);  
    addNode(1);  
      
    printf("Originals list: \n");  
    display();  
      
    //Removes duplicate nodes  
    removeDuplicate();  
      
    printf("List after removing duplicates: \n");  
    display();  
          
    return 0;  
}  

 
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education