Академический Документы
Профессиональный Документы
Культура Документы
and algorithms
Submitted To:
Ma`am Aiemen Akif
Sumitted By:
Ameer Hamza
Muhammad Muneeb
Malik
Muhammad
Shoaib
Class:
B.E.T.E-51
Section:
C
TE-51
ASSIGNMENT 1
DATA STRUCTURES AND ALGORITHMS
Answer:
#include<iostream.h>
#include<conio.h>
struct node{
int data;
node *next,*prev;
};
class dl{
private:
node *head,*tail,*temp,*temp1,*temp2;
int k;
public:
dl(){
head=NULL;
k=0;
}
void count(){
temp1=head;
while(temp1->next!=head){
temp1=temp1->next;
k++;
}
}
//---------------------------create--------------------void create(){
int n,x;
cout<<"enter the number of nodes\t:";
cin>>n;
for(int i=0;i<n;i++){
if(head==NULL){
temp= new node;
cout<<"enter the data\t:";
cin>>x;
temp->data=x;
head=temp;
tail=temp;
temp->next=head;
temp->prev=tail;
TE-51
}
else{
temp1=head;
while(temp1->next!=head){
temp1=temp1->next;
}
temp=new node;
cout<<"enter the data\t:";
cin>>x;
temp->data=x;
temp1->next=temp;
temp->next=head;
temp->prev=temp1;
tail=temp;
}
}
}
//------------------------delete--------------------void del(){
temp1=head;
if(k==1){
delete temp1;
head=NULL;
}
else{
for(int i=0;i<=k;i++){
temp2=temp;
temp1=temp1->next;
head=temp1;
delete temp2;
}
head=NULL;
}
}
//-----------------------display--------------------void display(){
if(head!=NULL){
temp1=head;
for(int i=0;i<=k;i++){
cout<<"your data is \t:"<<temp1->data<<endl;
temp1=temp1->next;
}
}
else{
cout<<"nothing to show\n";
}
}
};
int main(){
dl a;
a.create();
a.count();
a.display();
cout<<"do yo want to delete enter y,n\n";
char d;
cin>>d;
TE-51
if(d=='y'){
a.del();
a.display();}
else{
cout<<"ok";
}
getch();
}
TE-51
#include<iostream.h>
#include<conio.h>
struct node{
int data;
node *next;
};
class work{
private:
node *head,*head1,*head2,*temp,*temp1,*temp2,*temp3;
int n,q;
public:
work(){
head1=NULL;
head2=NULL;
head=NULL;
n=0;
q=0;
}
//-----------------------------------------------------------------------------void create(){
int x;
cout<<"enter the number of nodes for A\t:";
cin>>n;
for(int i=0;i<n;i++){
if(head1==NULL){
temp=new node;
cout<<"enter the data\t:";
cin>>x;
temp->data=x;
temp->next=NULL;
head1=temp;
}
else{
temp1=head1;
while(temp1->next!=NULL){
temp1=temp1->next;
}
temp=new node;
cout<<"enter the data\t:";
cin>>x;
temp->data=x;
temp->next=NULL;
temp1->next=temp;
}
}
//------------------------------for B------------------------------------------cout<<"enter the number of nodes for B\t:";
cin>>q;
for(int i=0;i<q;i++){
if(head2==NULL){
temp=new node;
cout<<"enter the data\t:";
cin>>x;
temp->data=x;
temp->next=NULL;
head2=temp;
}
else{
temp1=head2;
while(temp1->next!=NULL){
temp1=temp1->next;
}
temp=new node;
cout<<"enter the data\t:";
TE-51
cin>>x;
temp->data=x;
temp->next=NULL;
temp1->next=temp;
}
}
}
//-----------------------------------------------------------------------------void sep(){
int t=0,g=0;
temp1=head1;
temp3=head;
temp2=head2;
for(int i=0;(temp1!=NULL)&&(temp2!=NULL);i++){
temp2=head2;
while(temp2->next!=NULL){
if(temp1->data==temp2->data){
g++;
}
temp2=temp2->next;
}
temp1=temp1->next;
}
temp1=head1;
temp2=head2;
for(int i=0;i<g;i++){
if(head==NULL){
temp=new node;
head=temp;
temp->next=NULL;
}
else{
temp3=head;
while(temp3->next!=NULL){
temp3=temp3->next;
}
temp=new node;
temp->next=NULL;
temp3->next=temp;
}
}
temp3=head;
while(temp3!=NULL){
temp2=head2;
while(temp2!=NULL){
if(temp1->data==temp2->data){
temp3->data=temp1->data;
}
temp2=temp2->next;
}
temp3=temp3->next;
temp1=temp1->next;
}
}
//-----------------------------------------------------------------------------void display(){
temp3=head;
while(temp3!=NULL){
cout<<"your data is :"<<temp3->data<<endl;
temp3=temp3->next;
}
TE-51
}
};
int main(){
work a;
a.create();
a.sep();
a.display();
getch();
}
TE-51
TE-51
TE-51
TE-51
TE-51