0% found this document useful (0 votes)
3 views5 pages

DSA Lab 09

The document contains code implementations for linear search, stack using an array, and stack using a linked list in C++. It includes examples of how to perform operations such as push, pop, and peek on the stack data structure. The code is structured with main functions demonstrating the functionality of each task.

Uploaded by

40096
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)
3 views5 pages

DSA Lab 09

The document contains code implementations for linear search, stack using an array, and stack using a linked list in C++. It includes examples of how to perform operations such as push, pop, and peek on the stack data structure. The code is structured with main functions demonstrating the functionality of each task.

Uploaded by

40096
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/ 5

NAME : ANEES YAMEEN

ID : 40096
DATE : 22/04/2025
LAB : DSA
INSTRUCTOR : SIR NOMAN UL HASSAN
LAB : 09 Online Lab Tasks

Task : 1
#include <iostream>
using namespace std;

// Function to perform linear search


int linearSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i; // Return index if found
}
}
return -1; // Return -1 if not found
}

int main() {
int arr[] = {2, 3, 4, 10, 40}; // Example array
int x = 10; // Element to search
int n = sizeof(arr) / sizeof(arr[0]); // Calculate array size

int result = linearSearch(arr, n, x); // Call linear search

// Display result
if (result == -1) {
cout << "Element not found in the array." << endl;
} else {
cout << "Element found at index: " << result << endl;
}

return 0;
}

Screenshot :

Task : 2

Using Array:

#include <iostream>
using namespace std;

#define MAX_SIZE 100

class StackArray {
private:
int top;
int arr[MAX_SIZE];

public:
StackArray() : top(-1) {}

// Push element onto stack


void push(int x) {
if (top >= MAX_SIZE - 1) {
cout << "Stack Overflow!" << endl;
return;
}
arr[++top] = x;
}
// Pop element from stack
int pop() {
if (top < 0) {
cout << "Stack Underflow!" << endl;
return -1;
}
return arr[top--];
}

// Check if stack is empty


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

// Get top element


int peek() {
return arr[top];
}
};

int main() {
StackArray s;
s.push(10);
s.push(20);
s.push(30);
cout << "Top element: " << s.peek() << endl; // Output: 30
cout << "Popped: " << s.pop() << endl; // Output: 30
return 0;
}

Screenshot :
Using Linked list :
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};

class StackLinkedList {
private:
Node* top;

public:
StackLinkedList() : top(nullptr) {}

// Push element onto stack


void push(int x) {
Node* newNode = new Node(x);
newNode->next = top;
top = newNode;
}

// Pop element from stack


int pop() {
if (!top) {
cout << "Stack Underflow!" << endl;
return -1;
}
Node* temp = top;
int val = temp->data;
top = top->next;
delete temp;
return val;
}

// Check if stack is empty


bool isEmpty() {
return (top == nullptr);
}

// Get top element


int peek() {
return top->data;
}
};
int main() {
StackLinkedList s;
s.push(100);
s.push(200);
cout << "Top element: " << s.peek() << endl; // Output: 200
cout << "Popped: " << s.pop() << endl; // Output: 200
return 0;
}

You might also like