0% found this document useful (0 votes)
8 views9 pages

Ejaz

The document contains two programming assignments in C++. The first assignment involves creating a C++ program for basic array operations such as insertion, deletion, searching, and displaying elements, while handling edge cases. The second assignment focuses on implementing a singly linked list with operations for inserting, deleting, searching, and displaying nodes.

Uploaded by

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

Ejaz

The document contains two programming assignments in C++. The first assignment involves creating a C++ program for basic array operations such as insertion, deletion, searching, and displaying elements, while handling edge cases. The second assignment focuses on implementing a singly linked list with operations for inserting, deleting, searching, and displaying nodes.

Uploaded by

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

Assignment no:1

Ejaz-ur-Rehman
F23…07
Question 1: Array Operations Write a C++ program that performs basic operations on an array of
integers. Requirements: a. Create an array of integers with a maximum capacity of 10 elements. b.
Implement the following operations: • Insert an element at a specific position in the array • Delete an
element from a specific position • Search for an element and return its position • Display all elements in
the array c. Make sure to handle the following edge cases: • Array is full when attempting to insert •
Position is out of bounds when inserting or deleting • Element not found when searching

#include <iostream>
using namespace std;

class ArrayOperations {
private:
int arr[10]; // Fixed array size
int size; // Current number of elements

public:
ArrayOperations() { size = 0; }

// Insert element at a specific position


void insert(int element, int position) {
if (size >= 10) {
cout << "Error: Array is full!" << endl;
return;
}
if (position < 0 || position > size) {
cout << "Error: Invalid position!" << endl;
return;
}
for (int i = size; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
size++;
display();
}

// Delete element from a specific position


void remove(int position) {
if (position < 0 || position >= size) {
cout << "Error: Invalid position!" << endl;
return;
}
for (int i = position; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
display();
}

// Search for an element and return its position


void search(int element) {
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
cout << "Element " << element << " found at position " << i << endl;
return;
}
}
cout << "Element " << element << " not found" << endl;
}

// Display all elements in the array


void display() {
if (size == 0) {
cout << "Array is empty" << endl;
return;
}
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};

int main() {
ArrayOperations arrOps;

// Test cases
arrOps.insert(10, 0);
arrOps.insert(20, 1);
arrOps.insert(30, 2);
arrOps.insert(40, 3);
arrOps.insert(50, 4);

arrOps.insert(25, 2);
arrOps.search(30);
arrOps.search(100);

arrOps.remove(1);
arrOps.remove(3);

arrOps.insert(60, 10);
arrOps.remove(8);

return 0;
}

Question 2: Simple Singly Linked List Implement a singly linked list in C++ to store integers and perform
basic operations. Requirements: a. Create a Node structure containing: • An integer value • A pointer to
the next node b. Implement a LinkedList class with the following methods: • insertAtBeginning: Insert a
new node at the beginning of the list • insertAtEnd: Insert a new node at the end of the list •
deleteNode: Delete the first occurrence of a node with a given value • display: Display all elements in
the linked list • search: Search for a value and return true if found, false otherwise

#include <iostream>
using namespace std;

// Node structure
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};

// Singly Linked List class


class LinkedList {
private:
Node* head;

public:
LinkedList() : head(nullptr) {}

// Insert at the beginning


void insertAtBeginning(int val) {
Node* newNode = new Node(val);
newNode->next = head;
head = newNode;
display();
}
// Insert at the end
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
display();
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
display();
}

// Delete a node with a specific value


void deleteNode(int val) {
if (!head) {
cout << "Error: List is empty!" << endl;
return;
}
if (head->data == val) {
Node* temp = head;
head = head->next;
delete temp;
display();
return;
}
Node* temp = head;
while (temp->next && temp->next->data != val) {
temp = temp->next;
}
if (!temp->next) {
cout << "Element not found" << endl;
return;
}
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
display();
}

// Search for a value


bool search(int val) {
Node* temp = head;
while (temp) {
if (temp->data == val) {
cout << "Element " << val << " found" << endl;
return true;
}
temp = temp->next;
}
cout << "Element " << val << " not found" << endl;
return false;
}

// Display the list


void display() {
if (!head) {
cout << "List is empty" << endl;
return;
}
Node* temp = head;
while (temp) {
cout << temp->data;
if (temp->next) cout << " -> ";
temp = temp->next;
}
cout << endl;
}
};

int main() {
LinkedList list;

// Test cases
list.insertAtBeginning(30);
list.insertAtBeginning(20);
list.insertAtBeginning(10);
list.insertAtEnd(40);
list.insertAtEnd(50);

list.search(30);
list.search(100);

list.deleteNode(20);
list.deleteNode(10);
list.deleteNode(50);
list.deleteNode(100);

return 0;
}

You might also like