DSA Lab6 064

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DATA STRUCTURE AND

ALGORITHMS
TASK 1

Part A:
Write two functions CreateDList(), that creates a doubly linked list of five nodes and
DisplayAllNodes(),
that prints all the nodes values in the sequence.
Node 1 value: 10
Node 2 Value: 20
Node 3 value: 30
INPUT

#include<iostream>
using namespace std;
class node{
public:
int num;
node* next ;
node* prev ;
node(){
next = NULL;
prev = NULL;
}
void read(){
cout<<"Enter the number : ";
cin>>num;
}
void display(node* n){
cout<<"Node value is "<<n->num;
cout<<endl;
}
node* create_node(){
node* curr;
curr = new node;
curr->read();
return curr;
}
};
class double_link{
public:
node* head ;
double_link(){
head =NULL;
}
void display_list(){
node* temp ;
temp =head;
node* temp2 = head;
while(temp!=NULL){
temp2->display(temp);
temp =temp->next;
temp2 =temp2->next;
}

void insert_at_ascending(node* curr){

if(head==NULL){
head = curr;
}
else if( (head->num)>=(curr->num)){

curr->next = head;
curr->next->prev = curr;
head = curr;
}

else{
node* temp;
temp = head;
while(temp->next!=NULL)
if((temp->num )< (curr->num))
break;
temp= temp->next;

curr->next = temp->next;
if(temp->next !=NULL)
temp->next->prev = curr;
temp->next = curr;
curr->prev = temp;
}
}
};
int main(){
node* curr;
node* N;
double_link L1;
N = curr->create_node();
L1.insert_at_ascending(N);
N = curr->create_node();
L1.insert_at_ascending(N);
N = curr->create_node();
L1.insert_at_ascending(N);
N = curr->create_node();
L1.insert_at_ascending(N);
N = curr->create_node();
L1.insert_at_ascending(N);
L1.display_list();

OUTPUT

TASK 1 PART B
Write a program to find the maximum value from the doubly linked list and delete it. Create
three functions DeleteNode() and Maxvalue().
INPUT

#include<iostream>
using namespace std;
class node{
public:
int num;
node* next ;
node* prev ;
node(){
next = NULL;
prev = NULL;
}
void read(){
cout<<"Enter the number : ";
cin>>num;
}
void display(node* n){
cout<<"Node value is "<<n->num;
cout<<endl;
}
node* create_node(){
node* curr;
curr = new node;
curr->read();
return curr;
}
};
class double_link{
public:
node* head ;
double_link(){
head =NULL;
}
void display_list(){
node* temp ;
temp =head;
node* temp2 = head;
while(temp!=NULL){
temp2->display(temp);
temp =temp->next;
temp2 =temp2->next;
}

}
void insert_at_head(node* curr){
if(head==NULL){
head = curr;
}
else{
curr->next = head;
curr->prev = NULL;
if(head!=NULL){
head->prev = curr;
}
head=curr;
}
}
node* find_last(){
node* temp;
temp =head;
while(temp!=NULL){
if(temp->next==NULL)
{
return temp;
}
temp=temp->next;
}
}
void insert_at_last(node *curr){
if(head == NULL){
head = curr;
}
else{
node* last = find_last();

last->next = curr;
curr->prev = last;
curr->next =NULL;
}
}

void insert_at_ascending(node* curr){

if(head==NULL){
head = curr;
}
else if( (head->num)>=(curr->num)){

curr->next = head;
curr->next->prev = curr;
head = curr;
}

else{
node* temp;
temp = head;
while(temp->next!=NULL)
if((temp->num )< (curr->num))
break;
temp= temp->next;

curr->next = temp->next;
if(temp->next !=NULL)
temp->next->prev = curr;
temp->next = curr;
curr->prev = temp;
}
}

void delete_at_head(){
if(head == NULL){
cout<<"Head is empty "<<endl;
}
if(head!= NULL){
node* temp=head;
head = head->next;
temp->prev =NULL;
delete(temp);
}

}
//delete at end
void delete_at_end(){
node *last =find_last();
last->prev->next =NULL;
delete(last);
}
//delete at specific
void delete_at_specific(int ID){
node* last =find_last();
if(ID==head->num){
delete_at_head();
}
if(ID==last->num){
delete_at_end();
}
else{
node* temp = head;
while(temp!=NULL){
if(ID==temp->num){
break;
}
temp = temp->next;
}
temp->prev->next = temp->next;
temp->next->prev =temp->prev;
delete(temp);
}
}
int find_max(){
node *temp;
temp=head;
int numb ;
numb = 0;
while(temp!=NULL){
if(numb < (temp->num)){
numb =temp->num;
}
temp =temp->next;
}
return numb;
}
};
int main(){
node* curr;
node* N;
double_link L1;
N = curr->create_node();
L1.insert_at_ascending(N);
N = curr->create_node();
L1.insert_at_ascending(N);
N = curr->create_node();
L1.insert_at_ascending(N);
N = curr->create_node();
L1.insert_at_ascending(N);
N = curr->create_node();
L1.insert_at_ascending(N);
cout<<"______Displaying data______"<<endl;
L1.display_list();
cout<<"_____Displaying data after deleting maximum _____"<<endl;
int max_node;
max_node = L1.find_max();
L1.delete_at_specific(max_node);
L1.display_list();

}
OUTPUT
TASK NO 2
Write a program to insert a node at the end of the doubly linked list without using findLast()
function.

INPUT

#include<iostream>
#include<string.h>
using namespace std;
class node{
public:
node* next =NULL;
node* prev =NULL;
int id ;
int salary ;
string name;
void read(){
cout<<"Enter name ";
cin>>name;
cout<<"Enter the id: ";
cin>>id;
cout<<"Enter the salary : ";
cin>>salary;
}
void display(node* curr){
cout<<endl;
cout<<"The id is : ";
cout<<curr->id;
cout<<endl;
cout<<"The name is : "<<curr->name<<endl;
cout<<"The salary is : "<<curr->salary;
cout<<endl;

}
node* create_node(){
node* curr = new node;
curr->read();
return curr;
}
};
class double_link{
public:
node *head =NULL;
void insert_at_end(node *curr){
node* temp = head;
if(head==NULL){
head =curr;
}
else{
while(temp!=NULL){
if(temp->next == NULL){
break;
}
temp = temp->next;
}
temp->next = curr;
curr->prev =temp;
curr->next = NULL;
}

}
void display_list(){
node* temp = head;
node* temp2 =head;
while(temp!=NULL){
temp2->display(temp);
temp =temp->next;
temp2 =temp2->next;
}
}
};
int main(){
node* curr;
node* N;
double_link L1;
N = curr->create_node();
L1.insert_at_end(N);
cout<<"_____Displaying node____"<<endl;
L1.display_list();
}
OUTPUT

You might also like