LAB 7 - 90 - Merged
LAB 7 - 90 - Merged
#include <iostream>
0)
using namespace std;
struct Node {
5
int INFO;
B2
Node* NEXT;
};
Node* InsertBeg(Node* START, int info);
21
void DisplayList(Node* START);
int main() {
Node* START = NULL;
int info,n;
cout<<"enter the size:";
(2
A
cin>>n;
JH
for(int i=0;i<n;i++){
cout << "Enter the value to insert: ";
cin >> info;
O
}
return 0;
R
}
D
New_Node->INFO = info;
New_Node->NEXT = START;
START = New_Node;
AR
return START;
}
void DisplayList(Node* START) {
N
}
c out <<"NULL"<< endl;
}
OUTPUT:
1
0)
Q2. Write a menu driven program using switch-case to insert the node at
5
beginning, from specified position and at the end of linked list.
B2
/*********************************************************************
// This program is developed by NARENDRA OJHA (221B250)
/*********************************************************************
21
#include <iostream>
using namespace std;
struct Node {
int INFO;
Node* NEXT;
(2
A
};
JH
do {
D
case 1:
cout << "Enter the value to insert: ";
cin >> info;
START = InsertBeg(START, info);
DisplayList(START);
break;
case 2:
cout << "Enter the value to insert: "; cin >>
info;
START = InsertEnd(START, info);
2
DisplayList(START);
break;
case 3:
cout << "Enter the value to insert: "; cin >>
info;
cout << "Enter the location: ";
cin >> loc;
START = InsertAtGivenLoc(START, info, loc);
DisplayList(START);
break;
case 4:
cout << "Exiting the program.\n";
0)
break;
default:
5
cout << "Invalid choice. Please try again.\n"; break;
B2
}
} while (choice != 4);
Node* current = START;
21
while (current != NULL) {
Node* temp = current;
current = current->NEXT;
delete temp; }
(2
A
return 0;
}
JH
New_Node->NEXT = START;
START = New_Node;
A
return START;
R
}
Node* InsertEnd(Node* START, int info) {
D
New_Node->INFO = info;
New_Node->NEXT = NULL;
if (START == NULL) {
AR
START = New_Node;
} else {
Node* Temp = START;
N
}
0)
if (Temp == NULL) {
cout << "Location is greater than the number of nodes + 1" << endl;
5
delete New_Node;
B2
return START;
}
New_Node->NEXT = Temp->NEXT;
21
Temp->NEXT = New_Node;
return START;
}
void DisplayList(Node* START) {
(2
A
Node* current = START;
while (current != NULL) {
JH
OUTPUT:
R
D
EN
AR
N
4
5 0)
B2
21
(2
A
JH
O
A
R
D
EN
/********************************************************************* //
This program is developed by NARENDRA OJHA (221B250)
/*********************************************************************
N
#include<iostream>
using namespace std;
struct Node {
int INFO;
Node* NEXT;
};
5
ode* DeleteFromLoc_List(Node* START,int Loc);
N
void DisplayList(Node* START);
int main(){
Node* START = NULL;
int info,n;
cout<<"enter the size:";
cin>>n;
for(int i=0;i<n;i++){
cout << "Enter the value to insert: "; cin >>
info;
START = InsertBeg(START, info);
DisplayList(START);}
0)
int choice,loc;
do {
5
cout << "\nMenu:\n";
B2
cout << "1. Delete from beginning\n";
cout << "2. Delete from end\n";
cout << "3. Delete from a specified location\n";
21
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
(2
A
case 1:
START = DeleteBeg_List(START);
JH
DisplayList(START);
break;
case 2:
O
START = DeleteEnd_List(START);
DisplayList(START);
A
break;
R
case 3:
cout << "Enter the location to delete: "; cin >>
D
loc;
EN
case 4:
cout << "Exiting the program.\n"; break;
default:
N
START=START->NEXT;
0)
delete Temp;
return START;
5
}
B2
Node* DeleteEnd_List(Node* START){
Node* Prev=START,* Temp=START;
if(START==NULL){
21
cout<<"Empty Linked List!";
return START ;
}
if(START->NEXT==NULL){
(2
A
START=NULL;
delete Temp;
JH
return START;
}
while(Temp->NEXT!=NULL){
O
Prev=Temp;
Temp=Temp->NEXT;
A
}
R
Prev->NEXT=NULL;
delete Temp;
D
return START;
EN
}
Node* DeleteFromLoc_List(Node* START, int Loc) {
Node* Prev = START;
AR
return START;
}
if (Loc == 1) {
START = START->NEXT;
delete Temp;
return START;
}
for (int i = 1; i <= Loc - 1 && Temp != NULL; i++) {
Prev = Temp;
Temp = Temp->NEXT;
}
7
if (Temp == NULL) {
cout << "Location is greater than the total number of nodes" << endl;
return START;
}
Prev->NEXT = Temp->NEXT;
delete Temp;
return START;
}
void DisplayList(Node* START) {
Node* current = START;
while (current != NULL) {
cout << current->INFO << "->";
0)
current = current->NEXT;
}
5
cout <<"NULL"<< endl;
B2
}
OUTPUT:
21
(2
A
JH
O
A
R
D
EN
AR
N
8
p ublic:
int data;
node * next;
node(int data){
this->data=data;
this->next=NULL;
}
} ;
void print(node *&head){
node * temp=head;
while(temp!=NULL){
cout<< temp->data <<" ";
0)
temp=temp->next;
}
5
cout<<"-> NULL";
B2
return;
}
void insertathead (node *&head,int data){
21
node * temp=new node(data);
temp->next=head;
}
head=temp;
(2
A
node * reverse (node *&head){
node *forw=NULL;
JH
node *prev=NULL;
node *curr=head;
while(curr!=NULL){
O
forw=curr->next;
curr->next=prev;
A
prev=curr;
R
curr=forw;
}
D
return prev;
EN
}
int main(){
node *node1=new node(10);
AR
node *head=node1;
node *tail=node1;
print(head);
N
cout<<endl;
insertathead(head,20);
print(head);
cout<<endl;
insertathead(head,50);
print(head);
cout<<endl;
insertathead(head,90);
print(head);
cout<<endl;
cout<<"Reverse of linked list : \n";
9
n ode *ptr=reverse(head);
print(ptr);
return 0;
}
UTPUT:
O
0)
Q5. WAP to search an element in the linked list.
5
/*********************************************************************
B2
// This program is developed by NARENDRA OJHA (221B250)
/*********************************************************************
21
#include<iostream>
using namespace std;
struct Node {
int INFO;
Node* NEXT;
(2
A
};
Node* InsertBeg(Node* START, int info);
JH
int main(){
Node* START = NULL;
int info,n;
A
cin>>n;
for(int i=0;i<n;i++){
D
}
int key;
N
10
ew_Node->NEXT = START;
N
START = New_Node;
return START;
}
Node* Searching_list(Node* START,int info){
Node* Temp=START;
while(Temp!=NULL){
if(Temp->INFO==info){
return Temp;
}
Temp=Temp->NEXT;
}
0)
return NULL;
}
5
void DisplayList(Node* START) {
B2
Node* current = START;
while (current != NULL) {
cout << current->INFO << "->";
21
current = current->NEXT;
}
}
cout <<"NULL"<< endl;
(2
A
OUTPUT:
JH
O
A
R
D
Q6. Assume you have given a start pointer of a singly linked list. Write a
program to find the middle node in the given linked list.
EN
/*********************************************************************
// This program is developed by NARENDRA OJHA (221B250)
AR
/*********************************************************************
#include <iostream>
using namespace std;
N
struct Node {
public:
int data;
Node* next;
};
void printMiddle(Node* head) {
Node* slow_ptr = head;
Node* fast_ptr = head;
if (head != NULL) {
while (fast_ptr != NULL && fast_ptr->next != NULL) {
fast_ptr = fast_ptr->next->next;
11
s low_ptr = slow_ptr->next;
}
cout << "The middle element is: " << slow_ptr->data << endl; }
}
void push(Node** head_ref, int new_data) {
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main() {
Node* head = NULL;
0)
for (int i = 5; i > 0; i--) {
push(&head, i);
5
}
B2
printMiddle(head);
return 0;
}
21
OUTPUT:
(2
A
JH
O
A
R
D
EN
AR
N
12