Answer to Question #227006 in C for Neil

Question #227006

Write a menu driven single program, using switch case to perform the following operations in a single linked list by using different suitable user defined functions for each case.

[Ask the user "Do you want to continue (Press 1 or 0)"]


Traverse the list

Check if the list is empty [Call this to check underflow condition during the delete operation]

Insert a node after a given data item

Insert a node before a given data item

Delete a node after a given data item

Delete a node before a given data item

Insert a node at the certain position (at beginning/end/any position)

Delete a node at the certain position (at beginning/end/any position)

Delete a node for the given key

Search for an element in the linked list

Sort the elements of the linked list

Print the elements of the linked list in the reverse order

Reverse the nodes of the linked list

Print nth node from the last of a linked list

Delete the duplicate elements in a linked list


1
Expert's answer
2021-08-17T16:53:45-0400


#include <stdio.h>
#include <stdlib.h>
 


struct Node
{
  int data;
  struct Node *next;
};
void TraverseList(struct Node *head)
{
  while (head != NULL)
  {
     printf(" %d ", head->data);
     head = head ->next;
  }
}
//Checking if the list is empty
int CheckEmpty(struct Node * head){
 	if(head!=NULL){
 		return 1;
	 }
	 else{
	 	return 0;
	 }
 }
 Node* insertBefore(Node* beforeNode, int val)
{
 
    
   
 
    
  Node *p, *n = head;
 
        
        for (n, p; n != beforeNode;
             p = n, n = n->next)
            ;
 
        
        Node* m = new Node(val);
 
        
        m->next = p->next;
 
        
        p->next = m;
 
        return m;
    
}
 
void deleteNode(struct Node** headRef, struct Node* dele)
{
  
    if (*headRef == NULL || dele == NULL)
        return;
 
   
    if (*headRef == dele)
        *headRef = dele->nextNode;
 
    
    if (dele->nextNode != NULL)
        dele->nextNode->prevNode = dele->prevNode;
 
    
    if (dele->prevNode != NULL)
        dele->prevNode->nextNode = dele->nextNode;
 
    
    free(dele);
}
 




void deleteNodeAtGivenPos(struct Node** headRef, int data)
{
    
    if (*headRef == NULL || data <= 0)
        return;
 
    struct Node* currentNode = *headRef;
    int i;
 
    
    for ( i = 1; currentNode != NULL && i < data; i++)
        currentNode = currentNode->nextNode;
 
    
    if (currentNode == NULL)
        return;
 
    
    deleteNode(headRef, currentNode);
}
 


 


void insertAfter(struct Node* prevNode, int newData)
{
    
    if (prevNode == NULL)
    {
      printf("Previous node cannot be null");
      return;
    }
 


    struct Node* newNode =(struct Node*) malloc(sizeof(struct Node));
 
    
    newNode->data  = newData;
 
    
    newNode->next = prevNode->next;
 
    
    prevNode->next = newNode;
}
 


int main()
{
  struct Node* head1 = NULL;
  int userChoice;
	printf("Do you want to continue. Press 0 or 1");
	scanf("%d", userchoice);
  
 
  return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Neil
21.08.21, 19:21

Thanks a lot !!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS