Get Off-Campus Placement Jobs Info !!!
Programs asked in Mettl Coding Round
Click To Practce List of Programs asked in Nagarro !!!

Program Discussion :: Basics

Home > Programs > Basics

4 / 248

Write a program to delete a node in single linklist, if address of that node is given.

Answer:

#include
#include
#include
#include
using namespace std; 
struct Node
{
    int data;
    struct Node* next;
}; 
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
   struct Node* new_node =
             (struct Node*) malloc(sizeof(struct Node));
   new_node->data  = new_data;
   /* link the old list off the new node */
   new_node->next = (*head_ref);
   /* move the head to point to the new node */
   (*head_ref)    = new_node;
}
void printList(struct Node *head)
{
   struct Node *temp = head;
   while(temp != NULL)
   {
     coutdata;
      temp = temp->next;
   }
}
void deleteNode(struct Node *node_ptr)
{
   struct Node *temp = node_ptr->next;
   node_ptr->data    = temp->data;
   node_ptr->next    = temp->next;
   free(temp);

/* Drier program to test above function*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;
    /* Use push() to construct below list
    1->12->1->4->1  */
    push(&head, 1);
    push(&head, 4);
    push(&head, 1);
    push(&head, 12);
    push(&head, 1); 
    cout

Asked In :: Adobe

Post Your Answer Here:

Language:

Post Your Reply Here:



Language:

Post Your Reply Here: