DATA STRUCTURE CODE-1
CREATING A LINKED LIST:
#include <iostream>
using namespace std;
struct node
{
int data{};
node *next{};
};
node *head{NULL},*temp{NULL};
void insert(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
temp = new_node;
}
else
{
temp -> next = new_node;
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 ELEMENTS IN LINKED LIST:
#include <iostream>
using namespace std;
int count{0};
struct node
{
int data{};
node *next{};
};
node *head{NULL},*temp{NULL};
void insert(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
temp = new_node;
}
else
{
temp -> next = new_node;
temp = new_node;
}
count++;
}
void insert_at_beg(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
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;
temp = head;
while(temp -> next != NULL)
{
temp = temp -> next;
}
temp -> next = new_node;
count++;
}
void insert_after_location(int d,int p)
{
node *new_node;
int i{1};
if(p > count)
{
cout<<"Inavlid Position"<<endl;
}
else
{
temp = head;
while(i < p)
{
temp = temp -> next;
}
new_node = new node();
new_node -> data = d;
new_node -> next = temp -> next;
temp -> next = 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{},position{};
cout<<"Enter the position after which you want to insert the value: ";
cin>>position;
cout<<"Enter the value: ";
cin>>value;
insert_after_location(value,position);
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 ELEMENTS IN LINKED LIST:
#include <iostream>
using namespace std;
int count{0};
struct node
{
int data{};
node *next{};
};
node *head{NULL},*temp{NULL};;
void insert(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
temp = new_node;
}
else
{
temp -> next = new_node;
temp = new_node;
}
count++;
}
void delete_at_beg()
{
node *temp{NULL};
temp = head;
head = head -> next;
free(temp);
}
void delete_at_end()
{
node *temp{NULL},*prev{NULL};
temp = head;
while(temp -> next != NULL)
{
prev = temp;
temp = temp -> next;
}
prev -> next = NULL;
free(temp);
}
void delete_at_pos()
{
int i{1},position;
cout<<"Enter the position from were you want to delete: ";
cin>>position;
node *next_node{NULL},*temp{NULL};
if(position > count)
{
cout<<"Invalid position";
}
else
{
while(i < position-1)
{
temp = temp -> next;
i++;
}
next_node = temp -> next;
temp -> next = next_node -> next;
free(next_node);
}
}
void display()
{
node *temp{NULL};
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. Delete at beg"<<endl;
cout<<"3. Delete at end"<<endl;
cout<<"4. Delete at specific position"<<endl;
cout<<"5. 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:
{
delete_at_beg();
}
case 3:
{
delete_at_end();
}
case 4:
{
delete_at_pos();
}
case 5:
{
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;
}
LENGTH OF LINKED LIST:
#include <iostream>
using namespace std;
int count{0};
struct node
{
int data{};
node *next{};
};
node *head{NULL},*temp{NULL};
void insert(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
temp = new_node;
}
else
{
temp -> next = new_node;
temp = new_node;
}
count++;
}
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<<"3. Display length of linked list"<<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;
}
case 3:
{
cout<<"Length of linked list is: "<<count<<endl;
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;
}
REVERSING A LINKED LIST:
#include <iostream>
using namespace std;
struct node
{
int data{};
node *next{};
};
node *head{NULL},*temp{NULL};
void insert(int d)
{
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
temp = new_node;
}
else
{
temp -> next = new_node;
temp = new_node;
}
}
void reverse()
{
node *prev_node{NULL},*next_node{NULL},*temp{NULL};
temp = head;
next_node = head;
while(next_node != NULL)
{
next_node = next_node -> next;
temp -> next = prev_node;
prev_node = temp;
temp = next_node;
}
head = prev_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. Reversing Linked list"<<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