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

Data Structure Program

computers

Uploaded by

Sakshi Bachhety
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Data Structure Program

computers

Uploaded by

Sakshi Bachhety
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

temp->next=p;

tail=p;
}
else
{
p->next=prev->next;
prev->next=p;
}
}
}


void polynomial::addtohead(int x,int y)
{
Node *p=new Node(x,y);

if(isempty())
head=tail=p;

else
{
p->next=head;
head=p;
}
}


void polynomial::addtotail(int x,int y)
{
Node *p=new Node(x,y);

if(isempty())
head=tail=p;

else
{
tail->next=p;
tail=p;
}
}


void polynomial::add(polynomial l1,polynomial l2)
{
Node *temp1=l1.head;
Node *temp2=l2.head;
int x;

while(temp1!=0 && temp2!=0)
{
if(temp1->power<temp2->power)
{
addtotail(temp1->c,temp1->power);
temp1=temp1->next;
}
else if(temp1->power>temp2->power)
{
addtotail(temp2->c,temp2->power);
temp2=temp2->next;
}
else if(temp1->power==temp2->power)
{
x=temp1->c+temp2->c;
addtotail(x,temp2->power);
temp1=temp1->next;
temp2=temp2->next;
}

}

while(temp1!=0)
{
addtotail(temp1->c,temp1->power);
temp1=temp1->next;
}

while(temp2!=0)
{
addtotail(temp2->c,temp2->power);
temp2=temp2->next;
}


}


void polynomial::display()
{
Node *temp;

temp=head;

while(temp!=0)
{
cout<<temp->c<<"^"<<temp->power<<"+";
temp=temp->next;
}
}

void main()
{
polynomial l1,l2,l3;
int c,num,num2;
char ans,a;

do{

cout<<"\nENTER THE POLYNOMIAL 1:\n";
do
{
cout<<"\n\nENTER THE COEFFICIENT :";
cin>>num;
cout<<"ENTER THE POWER :";
cin>>num2;

l1.insert(num,num2);

cout<<"\nENTER MORE ELEMENT(y/n) :";
cin>>a;
}while(a=='y'||a=='Y');

l1.display();
cout<<"\n\n------------------------\n\n";

cout<<"\nENTER THE POLYNOMIAL 2:\n";
do
{
cout<<"\n\nENTER THE COEFFICIENT :";
cin>>num;
cout<<"ENTER THE POWER :";
cin>>num2;

l2.insert(num,num2);

cout<<"\nENTER MORE ELEMENT(y/n) :";
cin>>a;
}while(a=='y'||a=='Y');

l2.display();

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

l3.add(l1,l2);

cout<<"addtion";

if(!l3.isempty())
{
cout<<"\n\nLINKED LIST 1 IS \n:";
l1.display();
cout<<"\n\nLINKED LIST 2 IS \n:";
l2.display();
cout<<"\n\nLINKED LIST AFTER MERGING IS \n:";
l3.display();
}
else
cout<<"\nLIST EMPTY";




cout<<"\n\nDO YOU WISH TO CONTINUE??";
cin>>ans;
}while(ans=='y' || ans=='Y');

}
STACK USING LINKED LIST
#include<iostream>
using namespace std;

template<class T>
class Dnode
{
public:
T info;
Dnode *next,*prev;

Dnode()
{
info=0;
next=0;
prev=0;
}

Dnode(T x,Dnode *n=0,Dnode *p=0)
{
info=x;
next=n;
prev=p;
}
};

template<class T>
class Dstack
{
Dnode<T> *head;

public:

Dstack()
{
head=0;
}

int isempty();
void push(T);
T pop();
void display();
// ~Dstack();
};

template<class T>
int Dstack<T>::isempty()
{
if(head==0)
return 1;

else
return 0;
}

template<class T>
void Dstack<T>::push(T x)
{
Dnode<T> *temp=new Dnode<T>(x);

if(isempty())
{
head=temp;
}

else
{
temp->next=head;
head->prev=temp;
head=temp;
}
}

template<class T>
T Dstack<T>::pop()
{
T y=head->info;

if(head->next==0)
{
delete head;
head=0;
}

else
{
head=head->next;
delete head->prev;
head->prev=0;
}
return y;
}

template<class T>
void Dstack<T>::display()
{
Dnode<T> *temp;

temp=head;

while(temp!=0)
{
cout<<temp->info;
temp=temp->next;
}


}



void main()
{
Dstack<int> d1;
int num,c;
char ans;



do{

cout<<"\n\n-------------STACK AS LINKED LIST---------------\n";
cout<<"\n1. CHECK IF STACK IS EMPTY OR NOT ";
cout<<"\n2. PUSH A ELEMENT ";
cout<<"\n3. POP AN ELEMENT ";
cout<<"\n4. DISPLAY STACK ";
cout<<"\n\n ENTER YOUR CHOICE :";
cin>>c;

switch(c)
{
case 1: if(d1.isempty())
cout<<"\nSTACK IS EMPTY";
else
cout<<"\nSTACK IS NOT EMPTY";
break;

case 2: cout<<"\n\nENTER THE ELEMENT YOU WANT TO ADD IN LIST :";
cin>>num;

d1.push(num);
break;


case 3: if(!d1.isempty())
{
num=d1.pop();

cout<<"\n\nNUMBER DELETED IS "<<num;
}
else
cout<<"\nLIST IS EMPTY";

break;


case 4: if(!d1.isempty())
{
cout<<"\n\nSTACK IS \n:";
d1.display();
}
else
cout<<"\nSTACK EMPTY";

break;

default: cout<<"\n\nOPTION NOT PRESENT";
}

cout<<"\n\nDO YOU WISH TO CONTINUE??";
cin>>ans;
}while(ans=='y' || ans=='Y');
}
QUEUE USING LINKED LIST
#include<iostream>
using namespace std;

template<class T>
class Dnode
{
public:
T info;
Dnode *next,*prev;

Dnode()
{
info=0;
next=0;
prev=0;
}

Dnode(T x,Dnode *n=0,Dnode *p=0)
{
info=x;
next=n;
prev=p;
}
};

template<class T>
class Dqueue
{
Dnode<T> *head,*tail;

public:

Dqueue()
{
head=0;
tail=0;
}

int isempty();
void enqueue(T);
T dequeue();
void display();
// ~Dlist();
};

template<class T>
int Dqueue<T>::isempty()
{
if(head==0)
return 1;

else
return 0;
}

template<class T>
void Dqueue<T>::enqueue(T x)
{
Dnode<T> *temp=new Dnode<T>(x);

if(isempty())
{
head=tail=temp;
}

else
{
tail->next=temp;
temp->prev=tail;
tail=temp;
}
}

template<class T>
T Dqueue<T>::dequeue()
{
T y=head->info;

if(head==tail)
{
delete head;
head=tail=0;
}

else
{
head=head->next;
delete head->prev;
head->prev=0;
}
return y;
}

template<class T>
void Dqueue<T>::display()
{
Dnode<T> *temp;

temp=head;

while(temp!=0)
{
cout<<temp->info;
temp=temp->next;
}


}



void main()
{
Dqueue<int> d1;
int num,c;
char ans;


do{

cout<<"\n\n-------------QUEUE AS LINKED LIST---------------\n";
cout<<"\n1. CHECK IF QUEUE IS EMPTY OR NOT ";
cout<<"\n2. INSERT ";
cout<<"\n3. DELETE ";
cout<<"\n4. DISPLAY LISNKED LIST ";
cout<<"\n\n ENTER YOUR CHOICE :";
cin>>c;

switch(c)
{
case 1: if(d1.isempty())
cout<<"\nQUEUE IS EMPTY";
else
cout<<"\nQUEUE IS NOT EMPTY";
break;

case 2: cout<<"\n\nENTER THE ELEMENT YOU WANT TO ADD IN LIST :";
cin>>num;

d1.enqueue(num);
break;

case 3: if(!d1.isempty())
{
num=d1.dequeue();

cout<<"\n\nNUMBER DELETED IS "<<num;
}
else
cout<<"\nQUEUE IS EMPTY";

break;


case 4: if(!d1.isempty())
{
cout<<"\n\nQUEUE IS \n:";
d1.display();
}
else
cout<<"\nQUEUE EMPTY";

break;

default: cout<<"\n\nOPTION NOT PRESENT";
}

cout<<"\n\nDO YOU WISH TO CONTINUE??";

You might also like