Lab 3
Lab 3
#include<iostream>
using namespace std;
class Node{
public:
int value;
Node*next;
Node*pre;
Node(){
value=0;
next=NULL;
pre=NULL;
}
};
class Linkedlist{
public:
Node*head;
Node*tail;
Linkedlist(){
head=NULL;
tail=NULL;
}
int main(){
Linkedlist obj;
obj.add(2);
obj.add(9);
obj.add(7);
obj.add(5);
obj.add(8);
obj.add(3);
obj.AddStart(1);
obj.AddStart(4);
obj.del(1);
obj.del(3);
obj.del(7);
obj.display();
cout<<"\nCount: "<<obj.count();
cout<<"\nSearched: "<<obj.search(9);
}
<=====================================>
// PROGRAM 2
#include<iostream>
using namespace std;
class Node{
public:
int value;
Node*next;
Node*pre;
Node(){
value=0;
next=NULL;
pre=NULL;
}
};
class Linkedlist{
public:
Node*head;
Node*tail;
Linkedlist(){
head=NULL;
tail=NULL;
}
void add(int v){
Node*nw=new Node();
nw->value=v;
if(head==NULL){
head=nw;
tail=nw;
tail->next=NULL;
tail->pre=NULL;
}
else{
Node*newNode=new Node();
newNode->value=v;
tail->next=newNode;
newNode->pre=tail;
tail=newNode;
// for circular
tail->next=head;
head->pre=tail;
}
}
Node* search(int v){
Node*temp=head;
if(head==NULL)
return NULL;
do{
if(temp->value==v)
return temp;
else
temp=temp->next;
}while(temp!=head);
}
int main(){
Linkedlist obj;
obj.add(4);
obj.add(3);
obj.add(7);
obj.add(8);
obj.add(1);
obj.add(5);
obj.add(2);
obj.add(9);
cout<<"before deletion:\n";
obj.display();
obj.del(5);
obj.del(8);
obj.del(4);
cout<<"\nafter deletion:\n";
obj.display();
cout<<"\nSearched: "<<obj.search(7);
}