Вы находитесь на странице: 1из 12

Data Structures

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

1. Create a doubly circular link list of N elements where n is entered


by the user. Write functions to create, display and delete the list.
The display function called before delete would display the
complete list and the same function called after delete should
display a message indicating No elements to delete.

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

2. Assume two link lists A and B.


A - 2 , 5, 9, 14 , 15, 7 , 2 , 17 , 30
B14 , 2 , 9 , 13 , 37 , 8 , 7 28
Write a function to create a list C that contains only those
elements that are common in link list A and B. Your program
should work for any set of values.
Answer:

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

3. Write a program that performs addition of two polynomials using


linked lists. Both the polynomials are input by the user.
i) Initially ask the user to enter the number of terms for both the
polynomials. Number of terms in both the polynomials may vary.
ii) The coefficient and exponent of each term of polynomial is
input by the user. It is assumed that the user will enter the values
in the decreasing order of coefficients. i.e. 3x^4 + 14x^3 +5x^2
+7
It is not compulsory to enter every coefficient. User may skip any
coefficient as shown in the above example. Once the user has
entered both of the polynomials, the next task is to add them and
display the result. Consider the following node and class
structure
Answer:

TE-51

TE-51

TE-51

TE-51

TE-51

Вам также может понравиться