#include <iostream>
using namespace std;
struct list
{
int data;
struct list *next;
};
typedef struct list node;
node *head=NULL;
void insert_node()
{
node *temp = new node();
node *last;
cout<<"Data: ";
cin>>temp->data;
temp->next = NULL;
if(head==NULL)
{
head = temp;
}
else
{
last = head;
while(last->next != NULL)
{
last = last ->next;
}
last -> next = temp;
}
}
void print_node()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<< " ";
temp = temp->next;
}
}
void find_max()
{
node *temp=head;
node *prev;
int max = temp->data;
while(temp!= NULL)
{
if(temp->data > max) ///find the max
{
max = temp->data;
}
temp = temp->next;
}
temp= head;
while(max != temp->next->data) ///find the max previous element
{
prev = temp->next;
temp= temp ->next;
}
prev ->next =temp->next->next; ///save previous element next to max next
delete(temp->next); ///delete max node
}
int main()
{
int flag;
do
{
cout<<"Press 1 to Insert Data: ";
cin>>flag;
if(flag!=1) break;
insert_node();
}
while(flag==1);
cout<<"Your Linked List: ";
print_node();
find_max();
cout<<"
Your Linked List: ";
print_node();
cout<<endl;
return 0;
}
Output
Press 1 to Insert Data: 1
Data: 2
Press 1 to Insert Data: 1
Data: 5
Press 1 to Insert Data: 1
Data: 75
Press 1 to Insert Data: 1
Data: 3
Press 1 to Insert Data: 1
Data: 50
Press 1 to Insert Data: 0
Your Linked List: 2 5 75 3 50
Your Linked List: 2 5 3 50
Add your comment