Lab 06
Lab 06
Lahore
Lab Manual: Data Campus
Structures and Algorithm
Lab- 06 Manual
Lab Instructor: Sameer Ahmed
Department of Software Engineering
Email: [email protected]
Array Processing:
Elements of the array are dealt as normal variables, the only difference here is to specify
the index desired. These statements are examples of using array elements:
Lab Manual: Data Structures and Algorithm
1D dynamic array
#include <iostream>
int main() {
int size;
std::cout << "Enter the size of the array: ";
std::cin >> size;
// Input elements
std::cout << "Enter " << size << " elements: ";
for(int i = 0; i < size; i++) {
std::cin >> arr[i];
}
// Display elements
std::cout << "Array elements: ";
for(int i = 0; i < size; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
Searching in 1D Arrays
#include <iostream>
int main() {
int size, key;
std::cout << "Enter the size of the array: ";
std::cin >> size;
return 0;
}
3. Useful Concepts:
Or
b[0][0] = 51; b[0][1] = 52; b[0][2] = 53; b[1][0] =
54; b[1][1] = 55; b[1][2] = 56;
or
int b[][3] = {{51, 52, 53}, {54, 55, 56}};
• The output of two-dimensional arrays should be in the form of rows and columns for
readability. Nested for loops are used to print the rows and columns in row and column
order.
• By increasing the index value of the array the elements stored at that index value are printed
on the output screen.
int array[10][10];
passFunc(array)
;
// Function Definition
2D dynamic Arrays
#include <iostream>
int main() {
int rows, cols;
std::cout << "Enter the number of rows and columns: ";
std::cin >> rows >> cols;
// Input elements
std::cout << "Enter elements for the 2D array: \n";
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
std::cin >> arr[i][j];
}
}
Lab Manual: Data Structures and Algorithm
// Display elements
std::cout << "2D Array elements:\n";
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
int main() {
int rows, cols, key;
std::cout << "Enter the number of rows and columns: ";
std::cin >> rows >> cols;
return 0;
}
Linked list:
1. Introduction
Purpose: This lab manual aims to provide an in-depth understanding of singly linked lists, their
structure, and their operations. By completing this lab, students will learn how to implement,
manipulate, and utilize singly linked lists in programming.
Unlike arrays, linked lists do not store elements in contiguous memory locations. Instead,
elements (nodes) are linked using pointers, providing flexibility in memory usage and easy
insertion and deletion operations.
Dynamic Size: Linked lists can grow or shrink in size as needed, unlike arrays which have a fixed
size.
Ease of Insertion and Deletion: Inserting and deleting nodes in a linked list is more efficient than
in an array, as it does not require shifting elements.
Memory Management: Linked lists allocate memory as needed, which can lead to better
memory utilization.
Doubly Linked List: Each node points to both the next and previous nodes.
Circular Linked List: The last node points back to the head, forming a circular structure.
For this lab manual, we will focus on the singly linked list.
The first node in the list is called the "head." The last node points to null, indicating the end of
the list.
#include <iostream>
using namespace std;
Node Class:
class Node {
public:
int data;
Node* next;
Node(int value) {
Lab Manual: Data Structures and Algorithm
data = value;
next = nullptr;
}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = nullptr;
}
if (index == 0) {
// Insert at the beginning
newNode->next = head;
head = newNode;
} else {
Node* temp = head;
int currentIndex = 0;
if (temp != nullptr) {
// Insert at the specified index
newNode->next = temp->next;
temp->next = newNode;
} else {
// Index is out of bounds
std::cout << "Index is out of bounds." << std::endl;
delete newNode;
}
}
}
// Function to delete a node from the beginning
void deleteFromBeginning() {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
delete temp;
Lab Manual: Data Structures and Algorithm
void deleteList() {
Node* current = head;
Node* nextNode = nullptr;
int main() {
LinkedList list;
list.insertAtBeginning(3);
list.insertAtBeginning(2);
list.insertAtBeginning(1);
list.insertAtEnd(4);
list.insertAtEnd(5);
list.deleteFromBeginning();
cout << "Linked List after deleteFromBeginning operation: ";
list.printList();
list.deletelist();
cout << "Linked List after deleteFromEnd operation: ";
list.printList();
return 0;
}
#include <iostream>
class Node {
public:
int data;
Node* next;
Lab Manual: Data Structures and Algorithm
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
class CircularLinkedList {
private:
Node* head;
public:
CircularLinkedList() {
head = nullptr;
}
if (head->next == head) {
delete head;
head = nullptr;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
Node* nodeToDelete = head;
head = head->next;
temp->next = head;
delete nodeToDelete;
}
}
};
int main() {
CircularLinkedList list;
list.insertAtTail(10);
list.insertAtTail(20);
list.insertAtTail(30);
list.insertAtHead(5);
std::cout << "Total nodes: " << list.countNodes() << std::endl; // Output: Total
nodes: 4
list.deleteAtHead();
std::cout << "List elements after deleting head: ";
list.printList(); // Output: 10 -> 20 -> 30 -> (head)
return 0;
}
Lab Manual: Data Structures and Algorithm
Instructions:
Add nodes to the linked list, delete a node from the beginning, and print the list to verify.
Instructions:
Add nodes to the linked list, delete the entire list, and verify that the list is empty.
Instructions:
Implement a function to insert a node at the head (beginning) of a singly circular linked list.
Lab Manual: Data Structures and Algorithm
Instructions:
Objective: Implement a function to insert a node at the tail (end) of a singly circular linked list.
Instructions:
Objective: Implement a function to delete the node at the head of a singly circular linked list.
Instructions:
Objective: Implement a function to count the number of nodes in a singly circular linked list.
Instructions: