0% found this document useful (0 votes)
4 views

Lab 10 Stack,Class,Constructor,Object,Pointer

Uploaded by

Ishaq Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 10 Stack,Class,Constructor,Object,Pointer

Uploaded by

Ishaq Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab 10 Stack, Class, Constructor, Object and pointer to Object

Stack
A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has
one end, whereas the Queue has two ends (front and rear). It contains only one pointer top
pointer pointing to the topmost element of the stack. Whenever an element is added in the stack,
it is added on the top of the stack, and the element can be deleted only from the stack. In other
words, a stack can be defined as a container in which insertion and deletion can be done from
the one end known as the top of the stack.

Working of Stack

Implementations
A stack can be implemented using:
• Array: Fixed size and faster access, but limited in capacity.

Operations
• PUSH operation
1. Before inserting an element in a stack, we check whether the stack is full.
2. If we try to insert the element in a stack, and the stack is full, then
the overflow condition occurs.
3. When we initialize a stack, we set the value of top as -1 to check that the
stack is empty.
4. When the new element is pushed in a stack, first, the value of the top gets
incremented, i.e., top=top+1, and the element will be placed at the new
position of the top.
5. The elements will be inserted until we reach the max size of the stack.

• POP operation
1. Before deleting the element from the stack, we check whether the stack is
empty.
2. If we try to delete the element from the empty stack, then
the underflow condition occurs.
3. If the stack is not empty, we first access the element which is pointed by
the top
4. Once the pop operation is performed, the top is decremented by 1,
i.e., top=top-1.

Code

#include <iostream>
#define MAX 5 // Maximum size of the stack

using namespace std;

class Stack {
private:
int arr[MAX]; // Array to hold stack elements
int top; // Index of the top element

public:
// Constructor to initialize the stack
Stack() { top = -1; }

// Function to check if the stack is full


bool isFull() {
return top == MAX - 1;
}
// Function to check if the stack is empty
bool isEmpty() {
return top == -1;
}

// Function to add an element to the stack


void push(int value) {
if (isFull()) {
cout << "Stack Overflow! Cannot push " << value << endl;
} else {
arr[++top] = value;
cout << "Pushed " << value << " onto the stack" << endl;
}
}

// Function to remove the top element from the stack


void pop() {
if (isEmpty()) {
cout << "Stack Underflow! Cannot pop" << endl;
} else {
cout << "Popped " << arr[top--] << " from the stack" << endl;
}
}

// Function to get the top element of the stack without removing it


int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return -1; // Indicating an empty stack
} else {
return arr[top];
}
}

// Function to display all elements in the stack


void display() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
} else {
cout << "Stack elements: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};

int main() {
Stack stack;
int choice, value;

while (true) {
cout << "\n*** Stack Operations ***\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "4. Display\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
cout << "Top element is: " << stack.peek() << endl;
break;
case 4:
stack.display();
break;
case 5:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid Choice! Please try again." << endl;
}
}
}
Class
In C++, a class is a blueprint or a template for creating objects that share common
properties and behaviors. It is a user-defined data type that encapsulates data and
functions, providing a structured way to represent real-world entities or concepts. Classes
in C++ can also be used to implement inheritance, which is a mechanism for creating new
classes that inherit the properties and behaviors of existing classes. Inheritance allows for
the reuse of code, as well as the creation of hierarchies of related classes that reflect the
real-world relationships between entities or concepts.

Implementation of Class:
Code
#include <iostream>

using namespace std;

// Class definition

class Student {

private:

string name;

int age;

float grade;

public:

// Constructor

Student(string n, int a, float g) : name(n), age(a), grade(g) {

cout << "Constructor called for student: " << name << endl;

// Method to display student information

void displayInfo() {

cout << "Name: " << name << endl;

cout << "Age: " << age << endl;

cout << "Grade: " << grade << endl;

}
};

int main() {

// Creating an object of class Student

Student student1("John", 20, 85.5);

// Displaying information of student1 using the object

cout << "Displaying student1 information:" << endl;

student1.displayInfo();

// Creating a pointer to an object of class Student

Student *studentPtr = &student1;

// Accessing the object through pointer

cout << "\nDisplaying student1 information through pointer:" << endl;

studentPtr->displayInfo();

return 0;

}
Lab Tasks

After going through Lab tasks, submit a separate Word document which should

include all the results of the tasks Code should be in word and output should be

screenshot.

First page should contain your Full name, Registration #, Course name and Date

The File Should be saved with your name only.

No copied or AI Should be in Code or Explanation.

Customer Support System for an Online Service Platform


Scenario:
Imagine you are building the core of a customer support system for an online service
platform, such as an e-commerce website or a social media network. The platform
receives two types of customer support requests:
1. General Support Requests: These are routine inquiries or lower-priority requests
submitted by regular users, such as account issues or payment queries.
2. Priority Support Requests: These come from VIP users or customers who are
experiencing critical issues, like security concerns or payment failures, and they
need immediate attention.
In this assignment, you'll develop a system that simulates handling both types of support
requests, using queues for general support requests and stacks for priority requests. The
system will need to balance these requests effectively, processing them according to a
specified ratio to ensure fair handling of both routine and critical requests.
Objective:
The goal of this assignment is to design a CustomerSupportCenter class that models the
queue and stack behaviors required to manage both types of support requests effectively.
You will:
1. Implement a queue to handle general support requests (FIFO order).
2. Implement a stack to handle priority support requests (LIFO order).
3. Write logic to process requests in a balanced way, ensuring that priority requests are
processed in between general requests based on a configurable ratio.
4. Provide functionality for viewing processed requests, pending requests, and
adjusting the priority processing ratio.
Requirements
1. Class Structure:
o Define a CustomerSupportCenter class.
o Define two private data structures:
▪ A queue (generalSupportQueue) for regular support requests.
▪ A stack (prioritySupportStack) for priority support requests.
2. Methods to Implement:
o enqueueGeneralRequest(int requestId, string userName, string
issueDescription): Adds a general support request to the
generalSupportQueue.
o pushPriorityRequest(int requestId, string userName, string
issueDescription): Adds a priority support request to the
prioritySupportStack.
o setProcessingRatio(int generalToPriorityRatio): Sets the processing ratio
between general and priority requests. For example, if the ratio is set to 3, the
system will process 3 general requests for every 1 priority request.
o processRequests():
▪ Processes requests in the specified ratio, handling general requests
first, then interspersing priority requests.
▪ If there are no priority requests, it continues processing general
requests until the queue is empty.
o displayProcessedRequests(): Shows all processed requests in the order
they were handled, displaying whether each was a general or priority request.
o displayRemainingRequests(): Shows all remaining requests in both the
queue and stack that were not yet processed.
3. Data Structure Choices:
o Queue for General Requests: Use an array-based or linked-list based queue
to ensure FIFO order for regular requests.
o Stack for Priority Requests: Use an array-based or linked-list based stack to
handle priority requests in LIFO order, as the most recent issue might need to
be handled first.

You might also like