C++ programming language part- 77 Deleting a Node
Deleting a Node
This involves deletion of a node
from the linked list. The following steps are followed to delete a node from
the beginning of the list.
Step 1: Store the value of start into a temporary variable
Step 2: Reassign Start so that it points to the next node in the
linked list
Step 3: Free the memory occupied by the temporary variable.
The Delete_node() function is
defined as follows:
Void
list::Delete_node()
{
node *Temp;
Temp=Start;
Start=Start->Next;
delete Temp;
}
Program 50_3
//linkedList
node.demo
class
node
{
char
name[10];
public:
node
*next;
void
accept()
{
cout<
cin>>name;
}
void
display()
{
cout<
}
};
class
list
{
node
*start;
public:
list()
{
start=NULL; } // Constructor, so start will carry NULL
void
add_node()
{
node
*fresh;
fresh=new
node;
fresh->accept();
fresh->next=start;
start=fresh;
}
void
delete_node()
{
node
*temp;
temp=start;
start=start->next;
delete
temp;
}
void
traversal()
{
node
*temp;
for(temp=start;temp!=NULL;temp=temp->next)
temp->display();
}
};
main()
{
list
L;
L.add_node();
L.add_node();
L.add_node();
L.add_node();
L.traversal();
L.delete_node();
L.traversal();
L.delete_node();
L.traversal();
}