Lab 10 Stack,Class,Constructor,Object,Pointer
Lab 10 Stack,Class,Constructor,Object,Pointer
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
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; }
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>
// Class definition
class Student {
private:
string name;
int age;
float grade;
public:
// Constructor
cout << "Constructor called for student: " << name << endl;
void displayInfo() {
}
};
int main() {
student1.displayInfo();
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