// DELETE A NODE FROM A SIMPLE DOUBLY LINKED LIST
# include<iostream.h>
# include <stdio.h>
# include <alloc.h>
# include<conio.h>
struct Double
{
char info;
struct Double *next;
struct Double *previous;
};
class D_link
{
private: int num ;
Double start;
public:
void Doubly_Link_Del (Double *);
void Doubly_Link_Creat (Double *);
void display (Double *);
};
// Function creates a doubly linked list
void D_link :: Doubly_Link_Creat (Double *node)
{
start.next = NULL; // Empty list
start.previous = NULL;
node = &start; // Point to the start of the list
num = 0;
cout<<"\n Input choice n for break:";
char ch = getche();
while(ch != 'n')
{
node->next = (Double *) malloc(sizeof(Double));
node->next->previous = node;
node = node->next;
cout<<"\n Input the values of the node :"<<(num+1)<< " : ";
cin >> node->info;
node->next = NULL;
cout<<"\n Input choice n for break:";
ch = getche();
num ++;
}
cout<<"\n Total nodes = "<<num;
}
////////////////////////////////////////////////////
// Function delete
void D_link :: Doubly_Link_Del (Double *node)
{
int delete_node;
int search_counter = 0;
cout<<"\n Input the node number to which you want delete: ";
cin >> delete_node;
node = start.next;
if ( node == NULL)
{
cout<<"\n Underflow\n";
cout<<"\n List is empty\n";
}
else
while(node)
{
if((search_counter + 1) == delete_node)
{
node->previous->next = node->next ;
node->next->previous = node->previous ;
free(node);
}
else
{
node = node->next;
}
search_counter++;
}
}
////////////////////////////////////////////////////
// Display the list
void D_link :: display(Double *node)
{
node = start.next;
while (node)
{
cout<<"\n "<<node;
cout<<" "<< node->info;
node = node->next;
}
}
// Function main
void main()
{
D_link Doubly_del;
Double *node = (Double *) malloc(sizeof(Double));
Doubly_del.Doubly_Link_Creat(node);
cout<<"\n Created linked list is as follows\n";
Doubly_del.display(node);
Doubly_del.Doubly_Link_Del(node);
cout<<"\n After deletion of a node linked list is as follows\n";
Doubly_del.display(node);
}
# include<iostream.h>
# include <stdio.h>
# include <alloc.h>
# include<conio.h>
struct Double
{
char info;
struct Double *next;
struct Double *previous;
};
class D_link
{
private: int num ;
Double start;
public:
void Doubly_Link_Del (Double *);
void Doubly_Link_Creat (Double *);
void display (Double *);
};
// Function creates a doubly linked list
void D_link :: Doubly_Link_Creat (Double *node)
{
start.next = NULL; // Empty list
start.previous = NULL;
node = &start; // Point to the start of the list
num = 0;
cout<<"\n Input choice n for break:";
char ch = getche();
while(ch != 'n')
{
node->next = (Double *) malloc(sizeof(Double));
node->next->previous = node;
node = node->next;
cout<<"\n Input the values of the node :"<<(num+1)<< " : ";
cin >> node->info;
node->next = NULL;
cout<<"\n Input choice n for break:";
ch = getche();
num ++;
}
cout<<"\n Total nodes = "<<num;
}
////////////////////////////////////////////////////
// Function delete
void D_link :: Doubly_Link_Del (Double *node)
{
int delete_node;
int search_counter = 0;
cout<<"\n Input the node number to which you want delete: ";
cin >> delete_node;
node = start.next;
if ( node == NULL)
{
cout<<"\n Underflow\n";
cout<<"\n List is empty\n";
}
else
while(node)
{
if((search_counter + 1) == delete_node)
{
node->previous->next = node->next ;
node->next->previous = node->previous ;
free(node);
}
else
{
node = node->next;
}
search_counter++;
}
}
////////////////////////////////////////////////////
// Display the list
void D_link :: display(Double *node)
{
node = start.next;
while (node)
{
cout<<"\n "<<node;
cout<<" "<< node->info;
node = node->next;
}
}
// Function main
void main()
{
D_link Doubly_del;
Double *node = (Double *) malloc(sizeof(Double));
Doubly_del.Doubly_Link_Creat(node);
cout<<"\n Created linked list is as follows\n";
Doubly_del.display(node);
Doubly_del.Doubly_Link_Del(node);
cout<<"\n After deletion of a node linked list is as follows\n";
Doubly_del.display(node);
}
No comments:
Post a Comment