0% found this document useful (0 votes)
12 views12 pages

LAB 7 - 90 - Merged

The document discusses implementing a singly linked list in C++. It includes functions to insert nodes at the beginning, end, and a specified location of the linked list. A menu driven program is provided to demonstrate different insertion options using a switch case statement.

Uploaded by

Hemant Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

LAB 7 - 90 - Merged

The document discusses implementing a singly linked list in C++. It includes functions to insert nodes at the beginning, end, and a specified location of the linked list. A menu driven program is provided to demonstrate different insertion options using a switch case statement.

Uploaded by

Hemant Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

‭Lab 7 : Singly Linked List‬

‭Q1. Write a function Insert_Beginning() to insert a new node at the‬


‭beginning of singly linked list. Call this function N time to create a‬
‭linked list with N nodes. Also write display function to print the linked‬
‭list.‬
‭/*********************************************************************‬
‭// This program is developed by NARENDRA OJHA (221B250)‬
‭/*********************************************************************‬


‭#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

‭START = InsertBeg(START, info);‬


‭DisplayList(START);‬
A

‭}‬
‭return 0;‬
R

‭}‬
D

‭Node* InsertBeg(Node* START, int info) {‬


‭Node* New_Node = new Node;‬
EN

‭New_Node->INFO = info;‬
‭New_Node->NEXT = START;‬
‭START = New_Node;‬
AR

‭return START;‬
‭}‬
‭void DisplayList(Node* START) {‬
‭N

‭Node* current = START;‬


‭while (current != NULL) {‬
‭cout << current->INFO << "->";‬
‭current = current->NEXT;‬

‭}‬
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

‭Node* InsertBeg(Node* START, int info);‬


‭Node* InsertEnd(Node* START, int info);‬
‭Node* InsertAtGivenLoc(Node* START, int info, int Loc);‬
O

‭void DisplayList(Node* START);‬


‭int main() {‬
A

‭Node* START = NULL;‬


‭int choice, info, loc;‬
R

‭do {‬
D

‭cout << "\nMenu:\n";‬


‭cout << "1. Insert at the beginning\n";‬
EN

‭cout << "2. Insert at the end\n";‬


‭cout << "3. Insert at a given location\n";‬
‭cout << "4. Exit\n";‬
AR

‭cout << "Enter your choice: ";‬


‭cin >> choice;‬
‭switch (choice) {‬
‭N

‭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

‭Node* InsertBeg(Node* START, int info) {‬


‭Node* New_Node = new Node;‬
‭New_Node->INFO = info;‬
O

‭New_Node->NEXT = START;‬
‭START = New_Node;‬
A

‭return START;‬
R

‭}‬
‭Node* InsertEnd(Node* START, int info) {‬
D

‭Node* New_Node = new Node;‬


EN

‭New_Node->INFO = info;‬
‭New_Node->NEXT = NULL;‬
‭if (START == NULL) {‬
AR

‭START = New_Node;‬
‭} else {‬
‭Node* Temp = START;‬
‭N

‭while (Temp->NEXT != NULL) {‬


‭Temp = Temp->NEXT;‬
‭}‬
‭Temp->NEXT = New_Node;‬
‭}‬
‭return START;‬
‭}‬
‭Node* InsertAtGivenLoc(Node* START, int info, int Loc) {‬
‭Node* Temp = START;‬
‭if (Loc < 1) {‬
‭cout << "Invalid location!" << endl;‬
‭3‬
r‭ eturn START;‬
‭}‬
‭Node* New_Node = new Node;‬
‭New_Node->INFO = info;‬
‭if (Loc == 1) {‬
‭New_Node->NEXT = START;‬
‭START = New_Node;‬
‭return START;‬
‭}‬
‭for (int i = 1; i < Loc - 1 && Temp != NULL; i++) {‬
‭Temp = Temp->NEXT;‬


‭}‬

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

‭cout << current->INFO << "->";‬


‭current = current->NEXT;‬
‭}‬
O

‭cout <<"NULL"<< endl;‬


‭}‬
A

‭OUTPUT:‬
R
D
EN
AR
‭N

‭4‬

5 0)
B2
21
(2
A
JH
O
A
R
D
EN

‭ 3. Write a menu driven program to delete the node from the‬


Q
‭beginning, from from specified position and from the end of linked list.‬
AR

‭/********************************************************************* //‬
‭This program is developed by NARENDRA OJHA (221B250)‬
‭/*********************************************************************‬
‭N

‭#include<iostream>‬
‭using namespace std;‬
‭struct Node {‬
‭int INFO;‬
‭Node* NEXT;‬
‭};‬

‭ ode* InsertBeg(Node* START, int info);‬


N
‭Node* DeleteBeg_List(Node* START);‬
‭Node* DeleteEnd_List(Node* START);‬

‭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

‭START = DeleteFromLoc_List(START, loc);‬


‭DisplayList(START);‬
‭break;‬
AR

‭case 4:‬
‭cout << "Exiting the program.\n"; break;‬
‭default:‬
‭N

‭cout << "Invalid choice. Please try again.\n"; break;}‬


‭} while (choice != 4);‬
‭Node* current = START;‬
‭while (current != NULL) {‬
‭Node* temp = current;‬
‭current = current->NEXT;‬
‭delete temp; }‬
‭return 0;‬
‭}‬
‭Node* InsertBeg(Node* START, int info) {‬
‭Node* New_Node = new Node;‬
‭6‬
‭ ew_Node->INFO = info;‬
N
‭New_Node->NEXT = START;‬
‭START = New_Node;‬
‭return START;‬
‭}‬
‭Node* DeleteBeg_List(Node* START){‬
‭Node* Temp=START;‬
‭if (START==NULL){‬
‭cout<<"Empty linked kist!";‬
‭return START;‬
‭}‬


‭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

‭Node* Temp = START;‬


‭if (START == NULL|| Loc <= 0) {‬
‭cout << "Empty Linked List or invalid location" << endl;‬
‭N

‭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‬
i‭f (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

‭Q4. WAP to reverse the singly linked list.‬


/‭********************************************************************* //‬
‭This program is developed by NARENDRA OJHA (221B250)‬
‭/*********************************************************************‬
‭#include<iostream>‬
‭using namespace std;‬
‭class node{‬

‭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

‭Node* Searching_list(Node* START,int info);‬


‭void DisplayList(Node* START);‬
O

‭int main(){‬
‭Node* START = NULL;‬
‭int info,n;‬
A

‭cout<<"enter the size:";‬


R

‭cin>>n;‬
‭for(int i=0;i<n;i++){‬
D

‭cout << "Enter the value to insert: ";‬


EN

‭cin >> info;‬


‭START = InsertBeg(START, info);‬
‭DisplayList(START);‬
AR

‭}‬
i‭nt key;‬
‭N

‭cout<<"enter the info:";‬


‭cin>>info;‬
‭if(Searching_list(START,info))‬
‭cout<<"element is present!";‬
‭else‬
‭cout<<"not present!";‬
‭return 0; }‬
‭Node* InsertBeg(Node* START, int info) {‬
‭Node* New_Node = new Node;‬
‭New_Node->INFO = info;‬

‭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‬

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy