#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
void insertFirst(node **start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
*start=temp;
cout<<"First:\n";
}
void insertLast(node *start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
while(start->link!=NULL)
{
start=start->link;
}
start->link=temp;
}
void deleteAlternate(node *start)
{
node *currnext=start->link;
node *nexttonext=start->link->link;
node *curr=start;
while(curr!=NULL &&curr->link!=NULL && curr->link->link!=NULL)
{
nexttonext=curr->link->link;
delete(curr->link);
curr->link=nexttonext;
curr=curr->link;
}
}
void disp(node *start)
{
while(start!=NULL)
{
cout<<" "<<start->info;
start=start->link;
}
cout<<"\n";
}
main()
{
node *start=NULL;
insertFirst(&start,1);
insertLast(start,2);
insertLast(start,3);
insertLast(start,4);
insertLast(start,5);
cout<<"Before Deleting Linked List\n:";
disp(start);
deleteAlternate(start);
cout<<"After Deleting Alternate Node:\n";
disp(start);
}
using namespace std;
struct node
{
int info;
node *link;
};
void insertFirst(node **start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
*start=temp;
cout<<"First:\n";
}
void insertLast(node *start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
while(start->link!=NULL)
{
start=start->link;
}
start->link=temp;
}
void deleteAlternate(node *start)
{
node *currnext=start->link;
node *nexttonext=start->link->link;
node *curr=start;
while(curr!=NULL &&curr->link!=NULL && curr->link->link!=NULL)
{
nexttonext=curr->link->link;
delete(curr->link);
curr->link=nexttonext;
curr=curr->link;
}
}
void disp(node *start)
{
while(start!=NULL)
{
cout<<" "<<start->info;
start=start->link;
}
cout<<"\n";
}
main()
{
node *start=NULL;
insertFirst(&start,1);
insertLast(start,2);
insertLast(start,3);
insertLast(start,4);
insertLast(start,5);
cout<<"Before Deleting Linked List\n:";
disp(start);
deleteAlternate(start);
cout<<"After Deleting Alternate Node:\n";
disp(start);
}
No comments:
Post a Comment