DATA STRUCTURE CODE-2
CREATING DOUBLY LINKED LIST:
#include <iostream>
using namespace std;
struct node
{
int data{};
node *prev{};
node *next{};
};
node *head{NULL},*temp{NULL};
void insert(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> prev = NULL;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
temp = new_node;
}
else
{
temp -> next = new_node;
new_node -> prev = temp;
temp = new_node;
}
}
void display()
{
temp = head;
while(temp != NULL)
{
cout<<"Elements are: "<<temp -> data<<endl;
temp = temp -> next;
}
}
int main()
{
char ch{};
do
{
int choice{};
cout<<"1. Insert"<<endl;
cout<<"2. Display"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
int value{};
cout<<"Enter the value: ";
cin>>value;
insert(value);
break;
}
case 2:
{
display();
break;
}
default:
cout<<"Do nothing"<<endl;
}
cout<<"Do you want to continue(y/Y): ";
cin>>ch;
cout<<"=============================="<<endl;
}while(ch == 'y' || ch == 'Y');
return 0;
}
INSERTING ELEMENT IN DOUBLY LINKED LIST:
#include <iostream>
using namespace std;
int count{0};
struct node
{
int data{};
node *next{};
node *prev{};
};
node *head{NULL},*temp{NULL},*tail{NULL};
void insert(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> prev = NULL;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
temp = new_node;
tail = new_node;
}
else
{
temp -> next = new_node;
new_node -> prev = temp;
temp = new_node;
tail = new_node;
}
count++;
}
void insert_at_beg(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
new_node -> prev = NULL;
head -> prev = new_node;
new_node -> next = head;
head = new_node;
count++;
}
void insert_at_end(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
new_node -> prev = NULL;
new_node -> prev = tail;
tail -> next = new_node;
tail = new_node;
count++;
}
void insert_at_location(int d)
{
int i{1};
int p{};
cout<<"Enter the position where you want to insert: ";
cin>>p;
if(p > count)
{
cout<<"Invalid position"<<endl;
}
else if(p == 1)
{
insert_at_beg(d);
}
else
{
node *new_node,*temp;
temp = head;
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
new_node -> prev = NULL;
while(i < (p-1))
{
temp = temp + 1;
i++;
}
new_node -> prev = temp;
new_node -> next = temp -> next;
temp -> next = new_node;
new_node -> next -> prev = new_node;
}
count++;
}
void display()
{
temp = head;
while(temp != NULL)
{
cout<<"Elements are: "<<temp -> data<<endl;
temp = temp -> next;
}
}
int main()
{
char ch1{};
do
{
int choice{};
cout<<"1. Insert"<<endl;
cout<<"2. Display"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
int value{};
cout<<"Enter the value: ";
cin>>value;
insert(value);
break;
}
case 2:
{
display();
break;
}
default:
cout<<"Do nothing"<<endl;
}
cout<<"Do you want to continue(y/Y): ";
cin>>ch1;
}while(ch1 == 'y' || ch1 == 'Y');
cout<<"================================================================"<<endl;
char ch2{};
cout<<"You want to continue to enter element(y/Y):";
cin>>ch2;
if(ch2 == 'y' || ch2 == 'Y')
{
do
{
int choice{};
cout<<"1. Insert at begining"<<endl;
cout<<"2. Insert at end"<<endl;
cout<<"3. Insert after given position"<<endl;
cout<<"4. Display"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
int value{};
cout<<"Enter the value: ";
cin>>value;
insert_at_beg(value);
break;
}
case 2:
{
int value{};
cout<<"Enter the value: ";
cin>>value;
insert_at_end(value);
break;
}
case 3:
{
int value{};
cout<<"Enter the value: ";
cin>>value;
insert_at_location(value);
break;
}
case 4:
{
display();
break;
}
default:
cout<<"Do nothing"<<endl;
}
cout<<"Do you want to continue(y/Y): ";
cin>>ch1;
cout<<"=============================="<<endl;
}while(ch1 == 'y' || ch1 == 'Y');
}
return 0;
}
DELETING ELEMENT IN DOUBLY LINKED LIST:
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int count{0};
struct node
{
int data{};
node *next{};
node *prev{};
};
node *head{NULL},*temp{NULL},*tail{NULL};
void insert(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> prev = NULL;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
temp = new_node;
tail = new_node;
}
else
{
temp -> next = new_node;
new_node -> prev = temp;
temp = new_node;
tail = new_node;
}
count++;
}
void delete_from_beg()
{
node *temp{NULL};
if(head == NULL)
{
cout<<"List is empty"<<endl;
}
else
{
temp = head;
head = head -> next;
head -> prev = NULL;
free(temp);
}
}
void delete_from_end()
{
node *temp{NULL};
if(tail == NULL)
{
cout<<"List is empty"<<endl;
}
else
{
temp = tail;
tail = tail -> prev;
tail -> next = NULL;
free(temp);
}
}
void delete_from_location()
{
node *temp{NULL};
int i{1},pos{};
cout<<"Enter the position: ";
cin>>pos;
temp = head;
if(pos == 1)
{
delete_from_beg();
}
else
{
while(i < pos)
{
temp = temp -> next;
i++;
}
temp -> prev -> next = temp -> next;
temp -> next -> prev = temp -> prev;
free(temp);
}
}
void display()
{
temp = head;
while(temp != NULL)
{
cout<<"Elements are: "<<temp -> data<<endl;
temp = temp -> next;
}
}
int main()
{
char ch1{};
do
{
int choice{};
cout<<"1. Insert"<<endl;
cout<<"2. Display"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
int value{};
cout<<"Enter the value: ";
cin>>value;
insert(value);
break;
}
case 2:
{
display();
break;
}
default:
cout<<"Do nothing"<<endl;
}
cout<<"Do you want to continue(y/Y): ";
cin>>ch1;
}while(ch1 == 'y' || ch1 == 'Y');
cout<<"================================================================"<<endl;
char ch2{};
cout<<"You want to continue to enter element(y/Y):";
cin>>ch2;
if(ch2 == 'y' || ch2 == 'Y')
{
do
{
int choice{};
cout<<"1. Delete at begining"<<endl;
cout<<"2. Delete at end"<<endl;
cout<<"3. Delete at given position"<<endl;
cout<<"4. Display"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
delete_from_beg();
break;
}
case 2:
{
delete_from_end();
break;
}
case 3:
{
delete_from_location();
break;
}
case 4:
{
display();
break;
}
default:
cout<<"Do nothing"<<endl;
}
cout<<"Do you want to continue(y/Y): ";
cin>>ch1;
cout<<"=============================="<<endl;
}while(ch1 == 'y' || ch1 == 'Y');
}
return 0;
}
REVERSING ELEMENT IN DOUBLY LINKED LIST:
#include <iostream>
using namespace std;
struct node
{
int data{};
node *prev{};
node *next{};
};
node *head{NULL},*temp{NULL},*tail{NULL};
void insert(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> prev = NULL;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
temp = new_node;
tail = new_node;
}
else
{
temp -> next = new_node;
new_node -> prev = temp;
temp = new_node;
tail -> next = new_node;
new_node -> prev = tail;
tail = new_node;
}
}
void reverse()
{
node *current{NULL},*next_node{NULL};
if(head == NULL)
{
cout<<"List is empty"<<endl;
}
else
{
current = head;
while(current != NULL)
{
next_node = current -> next;
current -> next = current -> prev;
current -> prev = next_node;
current = next_node;
}
current = head;
head = tail;
tail = current;
}
}
void display()
{
temp = head;
while(temp != NULL)
{
cout<<"Elements are: "<<temp -> data<<endl;
temp = temp -> next;
}
}
int main()
{
char ch{};
do
{
int choice{};
cout<<"1. Insert"<<endl;
cout<<"2. Reverse"<<endl;
cout<<"3. Display"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
int value{};
cout<<"Enter the value: ";
cin>>value;
insert(value);
break;
}
case 2:
{
reverse();
break;
}
case 3:
{
display();
break;
}
default:
cout<<"Do nothing"<<endl;
}
cout<<"Do you want to continue(y/Y): ";
cin>>ch;
cout<<"=============================="<<endl;
}while(ch == 'y' || ch == 'Y');
return 0;
}
Comments
Post a Comment