Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Basics
Home > Programs > Basics

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

Answer:

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<iostream>
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)
   {
     cout<< temp->data;
      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<<"\n Before deleting \n";
    printList(head);
    /* I m deleting the head itself.
        You can check for more cases */
   deleteNode(head);
   cout<<"\n After deleting \n";
   printList(head);
   getchar();
}

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here:



Language:

Post Your Reply Here: