#include "stdafx.h"
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *link;
Node()
{
data=0;
link=NULL;
}
};
class Link_List
{
private:
Node *first;
Node *last;
public:
Link_List()
{
first=NULL;
last=NULL;
}
void insert(int value)
{
if(first==NULL)
{
first=new Node;
first->data=value;
first->link=NULL;
last=first;
//return;
}
Node *ptr=new Node;
ptr->data=value;
ptr->link=first;
first=ptr;
last->link=first;
}
void insert_last(int value)
{
if(first==NULL)
{
first=new Node;
first->data=value;
first->link=first;
last=first;
// return;
}
Node *ptr=new Node;
last->link=ptr;
last=ptr;
last->data=value;
}
void remove()
{
if(first==NULL)
{
cout<<"\nNo value to Display!!!\n";
//return;
}
if(first->link==first)
{
delete first;
last=NULL;
//return;
}
Node *ptr=first;
first=first->link;
last->link=first;
delete ptr;
}
void remove_last()
{
if(first==NULL)
{
cout<<"\nNo value to Display!!!\n";
//return;
}
if(first->link==first)
{
delete first;
last=NULL;
//return;
}
Node *ptr=first;
while(ptr->link!=last)
ptr=ptr->link;
ptr->link=first;
delete last;
last=ptr;
}
void remove_at(int index)
{
Node *temp1=first;
int i=1;
while(i<index-1)
{
i++;
if(temp1==NULL)
{
cout<<"\nLink not found!!!"<<endl;
}
else
{
temp1=temp1->link;
}
}
Node *temp2=temp1->link;
temp1->link=temp1->link->link;
delete temp2;
}
void display()
{
if(first==NULL)
{
cout<<"\nNo value to Display!!!\n";
//return;
}
Node *ptr=first;
while(ptr->link!=first)
{
cout<<ptr->data<<"\t";
ptr=ptr->link;
}
cout<<ptr->data<<"\t";
}
~Link_List()
{
Node *ptr=first;
while(first->link!=last)
{
first=first->link;
delete ptr;
ptr=first;
}
delete ptr;
ptr=first->link;
}
};
int main()
{
Link_List s1;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
s1.insert(5);
s1.display();
s1.remove_at(2);
s1.display();
s1.remove();
cout<<"\n";
s1.display();
s1.remove();
cout<<"\n";
s1.display();
s1.remove_last();
cout<<"\n";
s1.display();
s1.remove_last();
cout<<"\n";
s1.display();
//s1.remove_last();
// s1.remove_last();
return 0;
}
No comments:
Post a Comment