Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Lab Programs

Uploaded by

Mehwish 9474
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Programs

Uploaded by

Mehwish 9474
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Array Operations

Problem: Implement basic operations (insertion, deletion, traversal) on an array.

Solution:

#include <iostream>

using namespace std;

void display(int arr[], int n) {

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";

cout << endl;

int main() {

int arr[100] = {1, 2, 3, 4, 5};

int n = 5;

// Insert element

int pos = 2, value = 10;

for (int i = n; i > pos; i--) arr[i] = arr[i - 1];

arr[pos] = value;

n++;

// Delete element

pos = 3;

for (int i = pos; i < n - 1; i++) arr[i] = arr[i + 1];

n--;
display(arr, n);

return 0;

2. Stack Implementation

Problem: Implement stack operations using an array.

Solution:

#include <iostream>

#define MAX 100

using namespace std;

class Stack {

int top;

int arr[MAX];

public:

Stack() { top = -1; }

bool push(int x);

int pop();

int peek();

bool isEmpty();

};

bool Stack::push(int x) {

if (top >= (MAX - 1)) {


cout << "Stack Overflow\n";

return false;

arr[++top] = x;

return true;

int Stack::pop() {

if (top < 0) {

cout << "Stack Underflow\n";

return 0;

return arr[top--];

int Stack::peek() {

if (top < 0) {

cout << "Stack is Empty\n";

return 0;

return arr[top];

bool Stack::isEmpty() { return (top < 0); }

int main() {

Stack s;

s.push(10);

s.push(20);
cout << "Top element: " << s.peek() << endl;

s.pop();

cout << "Top element after pop: " << s.peek() << endl;

return 0;

3. Queue Implementation

Problem: Implement a queue using arrays.

Solution:

#include <iostream>

#define SIZE 5

using namespace std;

class Queue {

int front, rear;

int arr[SIZE];

public:

Queue() {

front = -1;

rear = -1;

void enqueue(int x);

int dequeue();

bool isEmpty();

bool isFull();
};

void Queue::enqueue(int x) {

if (isFull()) {

cout << "Queue Overflow\n";

return;

if (front == -1) front = 0;

arr[++rear] = x;

int Queue::dequeue() {

if (isEmpty()) {

cout << "Queue Underflow\n";

return 0;

int data = arr[front];

if (front == rear) {

front = -1;

rear = -1;

} else {

front++;

return data;

bool Queue::isEmpty() { return (front == -1); }

bool Queue::isFull() { return (rear == SIZE - 1); }


int main() {

Queue q;

q.enqueue(10);

q.enqueue(20);

cout << "Dequeued: " << q.dequeue() << endl;

return 0;

4. Singly Linked List

Problem: Implement insertion and deletion in a singly linked list.

Solution:

#include <iostream>

using namespace std;

struct Node {

int data;

Node* next;

};

void insert(Node*& head, int data) {

Node* newNode = new Node();

newNode->data = data;

newNode->next = head;

head = newNode;

void deleteNode(Node*& head, int key) {


Node* temp = head, *prev = nullptr;

if (temp != nullptr && temp->data == key) {

head = temp->next;

delete temp;

return;

while (temp != nullptr && temp->data != key) {

prev = temp;

temp = temp->next;

if (temp == nullptr) return;

prev->next = temp->next;

delete temp;

void display(Node* head) {

while (head != nullptr) {

cout << head->data << " ";

head = head->next;

cout << endl;

int main() {

Node* head = nullptr;


insert(head, 10);

insert(head, 20);

insert(head, 30);

cout << "Linked List: ";

display(head);

deleteNode(head, 20);

cout << "After Deletion: ";

display(head);

return 0;

5. Binary Search

Problem: Implement binary search on a sorted array.

Solution:

#include <iostream>

using namespace std;

int binarySearch(int arr[], int low, int high, int key) {

while (low <= high) {

int mid = low + (high - low) / 2;

if (arr[mid] == key)

return mid;
else if (arr[mid] < key)

low = mid + 1;

else

high = mid - 1;

return -1;

int main() {

int arr[] = {10, 20, 30, 40, 50};

int n = sizeof(arr) / sizeof(arr[0]);

int key = 30;

int result = binarySearch(arr, 0, n - 1, key);

if (result != -1)

cout << "Element found at index " << result << endl;

else

cout << "Element not found\n";

return 0;

You might also like