0% found this document useful (0 votes)
4 views

Lab 3

Uploaded by

hassanilyas033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 3

Uploaded by

hassanilyas033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

// PROGRAM 1

#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;
newNode->next=NULL;
tail=newNode;
}
}

Node* search(int v){


Node*temp=head;
while(temp!=NULL){
if(temp->value==v)
return temp;
else
temp=temp->next;
}
return NULL;
}

void del(int v){


Node*t=search(v);
if(t==NULL){
cout<<"List is empty";
}
else{
if(head==t){
head=head->next;
t->next=NULL;
head->pre=NULL;
delete t;
}
else if(tail==t){
tail=t->pre;
t->pre->next=NULL;
t->pre=NULL;
delete t;
}
else{
t->pre->next=t->next;
t->next->pre=t->pre;
t->next=NULL;
t->pre=NULL;
delete t;
}
}
}
void AddStart(int v){
Node*newNode=new Node();
newNode->value=v;
newNode->next=head;
head->pre=newNode;
head=newNode;
}
int count(){
int c=0;
Node*temp=head;
while(temp!=NULL){
temp=temp->next;
c++;
}return c;
}
void display(){
cout<<head->value<<" ";
Node*temp=head;
while(temp->next!=NULL){
temp=temp->next;
cout<<temp->value<<" ";
}
}
};

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);
}

void del(int v){


Node*t=search(v);
if(t==NULL){
cout<<"List is empty";
}
else{
if(head==t){
head=head->next;
t->next=NULL;
head->pre=NULL;
delete t;
}
else if(tail==t){
tail=t->pre;
t->pre->next=NULL;
t->pre=NULL;
delete t;
}
else{
t->pre->next=t->next;
t->next->pre=t->pre;
t->next=NULL;
t->pre=NULL;
delete t;
}
}
}
void display(){
cout<<head->value<<" ";
Node*temp=head;
while(temp->next!=tail->next){
temp=temp->next;
cout<<temp->value<<" ";
}
}
};

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);
}

You might also like