Practical 3:
Create doubly linked list with nodes having information about an employee and
perform Insertion at front of doubly linked list and perform deletion at end of
that doubly linked list.
#incIude <iostream>
using namespace std;
// Employee structure to store employee information
struct Employee(
int employeeld;
string name;
double salary;
Employee* next;
Employee* prev;
class DoublyLL (
private:
Employee* head;
Employee* tail;
public:
DoublyLLOI
head =
nullptr; tail =
nullptr;
// Inseron at the front
void insertAtFront(int employeeld, string name, double salary)
( Employee* newEmployee = new Employee;
newEmployee->employeeId = employeeld;
newEmployee->name = name;
newEmployee->salary = salary;
newEmployee->next = head;
newEmployee->prev = nullptr;
if (head != nullptr) {
head->prev = newEmployee;
} else (
// If the list was empty, set the tail
tail = newEmployee;
head = newEmployee;
// Deleon at the end
void deleteAtEnd0 T
if (tail == nullptr) (
cout << "The list is empty. Deleon is not possible." << endl;
return;
Employee* temp =
tail; if (tail->prev !=
nullptr) ( tail = tail-
>prev;
tail->next = nullptr;
} else (
// Only one element
head = nullptr;
tail = nullptr;
delete temp;
// Display
void displayLL0 T
Employee* curr =
head; while (curr !=
nullptr) {
cout << "Employee ID: " << curr->employeeld << ", Name: " << curr-
>name << ", Salary: " << curr->salary << endl;
curr = curr->next;
cout<<endI;
int main0 T
DoublyLL employeeList;
// Insert employees at the front
employeeList.insertAtFront(1001, "nikita", 40000.0);
employeeList.insertAtFront(3333, "srishti", 50000.0);
employeeList.insertAtFront(1111, "vardh", 60000.0);
employeeList.insertAtFront(2002, "aakash", 70000.0);
cout << "Employee List (Original):" << endI;
empIoyeeList.displayLL0:
// Delete the last employee
empIoyeeList.deIeteAtEnd0:
cout << "Employee List (Aer Deleon):" << endI;
empIoyeeList.displayLL0:
return 0;
Output: "C:\Users\srishti\0neDrive\Documents\
doublylist.exe" Employee List (Original):
Employee ID: 2002, Name: aakash, Salary: 70000
Employee ID: 1111, Name: vardh, Salary: 60000
Employee ID: 3333, Name: srishti, Salary: 50000
Employee ID: 1001, Name: nikita, Salary: 40000
Employee List (Aer Deleon):
Employee ID: 2002, Name: aakash, Salary: 70000
Employee ID: 1111, Name: vardh, Salary: 60000
Employee ID: 3333, Name: srishti, Salary: 50000
Practical 2:
Create a linked list with nodes having informaon about a student and perform a.
Insert a new node at specified posion.
b. Delete of a node with the roll number of student specified.
c. Reversal of that linked list.
#incIude <iostream>
using namespace std;
class Node(
public:
int rollNo;
string name;
Node* next;
Node(int rollNo, string name): rollNo(rolINo), name(name), next(nulIptr) ()
// Insert a new node at kth posion
Node* insertAtPos(Node* head, int k, Node* newNode) (
if (k == 1) (
newNode->next = head;
head = newNode;
return head;
Node* prev = head;
Node* curr = prev-
>next;
for (int i = 1; i < k - 1; i++) {
prev = curr;
curr = curr->next;
prev->next =
newNode; newNode-
>next = curr; return
head;
// Delete of a node with the roll number of student specified.
Node* deleteRollNo(Node* head, int roll) {
if (head->rollNo == roll)
( head = head->next;
return head;
Node* temp = head->next;
Node* prev = head;
while (temp != nullptr) (
if (roll == temp->rollNo)
( prev->next = temp-
>next; return head;
temp = temp->next;
prev = prev->next;
cout<<"Not found"<<endl;
return head;
// Reversal of that linked list
Node* reverseList(Node*head) (
Node* prev = nullptr;
Node* curr = head;
Node* next = nullptr;
while (curr != nullptr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
head = prev;
return head;
int main() (
Node* head = new Node(1, "aakash");
head->next = new Node(2, "vardh");
head->next->next = new Node(4, srishti"); head->next-
>next->next = new Node(5, "nikita"); cout<<"ActuaI List"
<<endI;
Node* temp = head;
while (temp != nullptr) (
cout << temp->roIlNo << " " << temp->name << endI;
temp = temp->next;
cout << endl;
cout<<"List aer inseron at posion 3"<<endI;
head = insertAtPos(head, 3, new Node(3,
"deepika")); Node* temp1 = head;
while (temp1!= nullptr) (
cout << temp1->rolINo << " " << temp1->name <<
endI; temp1 = temp1->next;
cout << endl;
cout<<"List aer deleng roll number 4"<<endl;
head = deleteRollNo(head, 4);
Node* temp2 = head;
while (temp2 != nullptr) (
cout << temp2->rollNo << " " << temp2->name
<< endI;
temp2 = temp2->next;
cout << endl;
cout<<"Reversed List"<<endl;
head = reverseList(head);
Node* temp3 = head;
while (temp3 != nullptr) (
cout << temp3->rollNo << " " << temp3->name << endl;
temp3 = temp3->next;
cout << endl;
return 0;
Output:
"C:\Users\srishti\0neDrive\Documents\linkedlist.exe"
Actual List
1 aakash
2 vardh
4 srishti
5 nikita
List after insertion at position 3
1 aakash
2 vardh
3 deepika
4 srishti
5 nikita
List after deleting roll number 4
1 aakash
2 vardh
3 deepika
5 nikita
Reversed List
5 nikita
3 deepika
2 vardh
1 aakash
Practical 1:
Implement sparse matrix using array. Descripon of program:
a. Read a 2D array from the user.
b. Store it in the sparse matrix form, use array of structures.
c. Print the final
array. #incIude
<iostream> using
namespace std; int
main0I
int matrix 3][4];
cout<<"Enter matrix"<<endI;
for (int i = 0; i<3; i++)
( for (int j = 0; j<4; j+
+) ( cin>>matrix i][j];
cout<<endl;
for (int i = 0; i<3; i++) (
for (int j = 0; j<4; j++)
( if (matrix[i] j] == 0) (
cout<<" ";
} else(
cout<<matrix[i] j]<<" ";
cout<<endl;
return 0;
Output:
"C:\Users\srishti\0neDrive\Documents\sparshmatrix.exe" Enter
matrix:
1007
0010
1300
1 7
1
13__