Sunday, August 15, 2010

c++ Program list5

Program list 
 1. Delete node from circular list
 2. Circular link list
 3. Add node from circular link list
 4. Delete the node from circular link list 

/*
* Delete the node
* from circular list
* */


#include<iostream>

using namespace std;
class node
{
public:
int data;
node *next;
};

class circular
{
public:
circular();
void create_list(int data);
void delete_data(int data);
friend ostream& operator<<(ostream& o,circular &c);
node *first;
};
circular::circular()
{
first=NULL;
}
void circular::create_list(int data)
{
node *last;
node *p =new node;
p->data =data;
if(first==NULL)
{
first=p;
p->next =first;
}
else
{
last=first;
while(last->next!=first)
last=last->next;
p->next =first;
last->next =p;
}
}
void circular::delete_data(int data)
{
node *last=first;
node *p;
do
{
if(last->next->data ==data)
{
p=last->next;
last->next =p->next;
if(first==p)
first=first->next;
}
last=last->next;
}while(last!=first);

}
ostream& operator<<(ostream &o,circular &c)
{
node *last =c.first;
do
{
o<<"----->"<<last->data;
last =last->next;
}while(last!=c.first);
}

int main()
{
circular c;
for(int i =1;i<9;i++)
c.create_list(i);
int choice;
do
{
cout<<"enter your choice between 1 or 2"<<endl;
cout<<" 1. add data into list"<<endl;
cout<<" 2. delete node"<<endl;
cout<<c;
cout<<endl<<endl;
cin>>choice;
switch(choice)
{
case 1:
int number;
cout<<"enter the data which you want to add"<<endl;
cin>>number;
c.create_list(number);
cout<<c;
break;
case 2:
int delete_number;
cout<<"enter the data which you want to delete"<<endl;
cin>>delete_number;
c.delete_data(delete_number);
cout<<c;
break;
default:
break;
}
}while(choice ==2 ||choice ==1);

}

/*

* Circular link list
* */

#include<iostream>

using namespace std;

template<class T>
class node
{
public:
T data;
node<T> *next;
};

template<class T>
class circular
{
public:
circular();
void add_node(T data);
node<T> *first;
template<typename T2>
friend ostream& operator<< (ostream& o,circular<T2>& list);
};

template<class T>
circular<T>::circular()
{
first =NULL;
}
template<class T>
void circular<T>::add_node(T data)
{
node<T> *last;
node<T> *p =new node<T>;
p->data =data;
if(first==NULL)
first=p;
else
{
last =first;
while(last->next!=first)
last=last->next;

last->next=p;
first=p;
}
}

template<class T>
ostream& operator << (ostream& o,circular<T>& list)
{
node<T> *last =list.first;
while(last->next!=last)
{
o<<" last--->data "<<last->data;
last =last->next;
}
}

int main()
{
circular<int> c;
for(int i=0;i<5;i++)
c.add_node(i);
cout<<c;
return 0;
}

/*

* Program for circular list:
* 1. Add node before node
* 2. Add node after node
* */

#include<iostream>

using namespace std;

class node
{
public:
int data;
node *next;
};

class circular
{
public:
circular();
void create_list(int data);
void add_before(int data);
void add_after(int data);
friend ostream& operator<<(ostream&o,circular &c);
node *first;
};

circular::circular()
{
first=NULL;
}
void circular::create_list(int data)
{
node *last;
node *p =new node;
p->data =data;
if(first==NULL)
{
first=p;
p->next =first;
}
else
{
last =first;
while(last->next!=first)
last =last->next;
p->next =first;
last->next=p;
}
}
void circular::add_before(int data)
{
cout<<"enter the data"<<endl;
int number;
cin>>number;
node *last =first;
if(first==NULL)
cout<<" the node is empty"<<endl;

else if(first!=NULL)
{
while(last->next!=first)
{
if(last->next->data ==data)
{
node *p =new node;
p->data =number;
p->next =last->next;
last->next=p;
last=p;
}
last=last->next;
}
}
else if(first->data ==data)
{
node *p =new node;
p->data =number;
p->next=first;
last->next =p;
first=p;
}
else
cout<<"No node is present"<<endl;
}
void circular::add_after(int data)
{
node *last =first;
int number;
cout<<"Enter the number which you want to add"<<endl;
cin>>number;
node *p =new node;
p->data =number;

if(first==NULL)
cout<<"no node is present"<<endl;

else if(first!=NULL)
{
while(last->next!=first)
{
if(last->next->data ==data)
{
last =last->next;
p->next =last->next;
last->next =p;
last=p;
}
last =last->next;
}
}
else if(first->data ==data)
{
p->next =first->next;
first->next=p;
first=p;
}

else
cout<<"no node is found"<<endl;
}

ostream& operator<<(ostream& o,circular &c)
{
node *last =c.first;
while(last->next!=c.first)
{
o<<" --->"<<last->data;
last =last->next;
}
return o;
}
int main()
{
circular c;
for(int i=0;i<8;i++)
c.create_list(i);
c.add_before(4);
cout<<c;
c.add_after(6);
cout<<c;
cout<<c;
return 0;
}

No comments:

Post a Comment