Pages

Wednesday, July 31, 2013

Linked List IN Classes C++

/*
QUESTION 
IMPLEMENT following functions by using linked list in classes

1 to insert a node in link list at head
2 to insert a node in link list at tail: 
3 to display link list: 
4 to count how many times an integer occurs in your link list:
5 to see node's data by entering its number:  
6 to delete complete link list and head of link list get value NULL
7 to pop your link list: 


CODE:

*/


#include<iostream>
using namespace std;
class link{
protected:
int id;
int other;
link * next;
link * previous;
public:
    link(){
next = NULL;
previous = NULL;
}
//----------------------------
int get_id(){

return id;
}
//----------------------------
link * get_data(){

link *p = new link;
cout<<"\nEnter ID: ";cin>>p->id;
cout<<"\nEnter oter: ";cin>>p->other;

return p;
}
//----------------------------
void insert_link_at_head(link **h,link **t,link *temp){
if((*h)==NULL && (*t)==NULL){
(*h)=temp;
(*t)=temp;
}
        else
temp->next = (*h);
(*h)->previous = temp;
(*h)=(*h)->previous;
}
//-----------------------------
void insert_link_at_tail(link **h,link **t,link *temp){
if((*h)==NULL && (*t)==NULL){
(*h)= temp;
(*t)=temp;
}
        (*t)->next = temp;
temp->previous = (*t);
(*t)=temp;

}
//------------------------------
int count(link *h,int id){
link *p = h;
int count = 0;
while(p!=NULL){

if(p->id == id){
count++;
}
p = p->next;
}
return count;
}
//------------------------------
void display(link *h){

link *p = h;

while(p != NULL){
cout<<"\nID is : "<<p->id;
cout<<"\nOther is: "<<p->other;
p = p->next;
}
}
//------------------------------
link  * Get_nth(link *h, int id){
int count = 0;
link *ptr = h;
while(ptr != NULL){

if(count == id-1){
return ptr;
}
ptr = ptr->next;
count ++;
}

}

//------------------------------
void DeleteLIST(link**h){
while((*h) != NULL ){

link *temp = (*h);
(*h) = (*h)->next;
delete temp;
}


}
//------------------------------
link * POP(link **h){
link *p = (*h);
if((*h)->next ==NULL){
cout<<"\nID of node which is being deleted: "<<(*h)->id;
delete (*h);
(*h) = NULL;
return (*h);
}
else
{
cout<<"\nID of node which is being deleted: "<<(*h)->id;
(*h)= (*h)->next;
delete p;
return (*h);


}
}

//void InsertNth(link **h,link *t,int index){
//link *ptr = (*h);

/*if(index == 0){
Ninsert_link_at_head((h),t);
}

 
while(ptr != NULL){

 
for(int i=0;i<index;i++){
if(ptr->next != NULL)
{
ptr = ptr->next;
}

 }
 if(ptr == (*h))
 Ninsert_link_at_head((h),t);
 else if(ptr== NULL)
 Ninsert_link_at_tail((h),t);
 
ptr->previous->next = t;
t->next = ptr;
t->previous = ptr->previous;
ptr->previous = t;
break;

 }
 

 
}

 
void Ninsert_link_at_head(link **h,link *temp){
if((*h)==NULL ){

 temp->next =(*h);
 (*h)->previous = temp;
 (*h)= temp;
 (*h)->previous = NULL;
 } 
}*/

};






int main(){

link *head = NULL;
link *tail = NULL;
link *temp = NULL;

link obj;

while(1){
cout<<"\nEnter ";
cout<<"\n1 to insert a node in link list at head: ";
cout<<"\n2 to insert a node in link list at tail: ";
cout<<"\n3 to display link list: ";
cout<<"\n4 to count how many times an integer occurs in your link list:";
cout<<"\n5 to see node's data by entering its number:  ";
cout<<"\n6 to delete complete link list and head of link list get value NULL: ";
cout<<"\n7 to pop your link list: ";


cout<<"\n\n\n";

int opt;
cin>>opt;
switch(opt){
case 1:
{
temp = obj.get_data();
obj.insert_link_at_head(&head,&tail,temp);
}
break;
case 2:
{
temp = obj.get_data();
obj.insert_link_at_tail(&head,&tail,temp);
}
break;
case 3:
{
obj.display(head);
}
break;
case 4:
{
cout<<"\nEnter ID which you want o check in the link list: ";int id;
cin>>id;

cout<<"\n :"<<id<<":  Occurs : "<<obj.count(head,id)<<": times \n";
}
break;
case 5:
{
cout<<"\nEnter the node no whose data you want: ";
int id; cin>>id;
link *ptr;
ptr = obj.Get_nth(head,id);
//obj.display(ptr);
cout<<"\n ID is: "<<ptr->get_id();
}
break;
case 6:
{
obj.DeleteLIST(&head);
cout<<"\nLINked list deleted: \n";
cout<<head;
}
case 7:
{
obj.POP(&head);
}
break;
case 8:
{

}
break;

default:
break;

}
}

return 0;
}

No comments:

Post a Comment