// DELETING LAST NODE FROM A SIMPLE LINKED LIST
# include <iostream.h>
# include <alloc.h>
# include<conio.h>
struct link
{
char info;
struct link *next;
};
class D_node
{
private:
int i;
int number ;
link start, *previous;
public:
void Delete_node(link *);
void creat_node(link *);
};
void D_node :: creat_node(link *node)
{
char ch;
i = 0;
start.next = NULL; // Empty list
node = &start; // Point to the start of the list
cout<<"\n Input choice n for break:";
ch = getche();
while(ch != 'n')
{
node->next = (struct link* ) malloc(sizeof(struct link));
node = node->next;
cout<<"\n Input the node :"<<(i+1)<<" : ";
cin>>node->info;
node->next = NULL;
cout<<"\n Input choice n for break:";
ch = getche();
i++;
}
node = start.next;
previous = &start;
cout<<"\n Created list is as follows\n";
while (node)
{
cout<<"\n "<<node;
cout<<" "<< node->info;
node = node->next;
}
}
// Removing a node
void D_node:: Delete_node(link *node)
{
int node_number = 0;
node = start.next;
previous = &start;
if (node == NULL)
{
cout<<"\n Underflow";
}
else
while(node)
{
node = node->next;
previous = previous->next;
node_number ++;
}
cout<<"\n Total Nodes = "<<node_number;
node = start.next;
previous = &start;
while(node_number != 1)
{
node = node->next;
previous = previous->next;
node_number --;
}
if(node_number == 1)
{
previous->next = node->next;
free(node);
}
// Display the list
node = start.next;
cout<<"\n After deleting last node list is as follows\n";
while (node)
{
cout<<"\n "<<node;
cout<<" "<< node->info;
node = node->next;
}
}
// Function main
void main()
{
D_node del_node;
link *node = (link *) malloc(sizeof(link));
del_node.creat_node(node);
del_node.Delete_node(node);
}
# include <iostream.h>
# include <alloc.h>
# include<conio.h>
struct link
{
char info;
struct link *next;
};
class D_node
{
private:
int i;
int number ;
link start, *previous;
public:
void Delete_node(link *);
void creat_node(link *);
};
void D_node :: creat_node(link *node)
{
char ch;
i = 0;
start.next = NULL; // Empty list
node = &start; // Point to the start of the list
cout<<"\n Input choice n for break:";
ch = getche();
while(ch != 'n')
{
node->next = (struct link* ) malloc(sizeof(struct link));
node = node->next;
cout<<"\n Input the node :"<<(i+1)<<" : ";
cin>>node->info;
node->next = NULL;
cout<<"\n Input choice n for break:";
ch = getche();
i++;
}
node = start.next;
previous = &start;
cout<<"\n Created list is as follows\n";
while (node)
{
cout<<"\n "<<node;
cout<<" "<< node->info;
node = node->next;
}
}
// Removing a node
void D_node:: Delete_node(link *node)
{
int node_number = 0;
node = start.next;
previous = &start;
if (node == NULL)
{
cout<<"\n Underflow";
}
else
while(node)
{
node = node->next;
previous = previous->next;
node_number ++;
}
cout<<"\n Total Nodes = "<<node_number;
node = start.next;
previous = &start;
while(node_number != 1)
{
node = node->next;
previous = previous->next;
node_number --;
}
if(node_number == 1)
{
previous->next = node->next;
free(node);
}
// Display the list
node = start.next;
cout<<"\n After deleting last node list is as follows\n";
while (node)
{
cout<<"\n "<<node;
cout<<" "<< node->info;
node = node->next;
}
}
// Function main
void main()
{
D_node del_node;
link *node = (link *) malloc(sizeof(link));
del_node.creat_node(node);
del_node.Delete_node(node);
}
No comments:
Post a Comment