0% found this document useful (0 votes)
4 views2 pages

C++ Stack Implementation with Products

Uploaded by

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

C++ Stack Implementation with Products

Uploaded by

shoaibmeo401
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

using namespace std;

// Product class
class Product {
private:
int data; // To hold the digit
Product* next; // Pointer to the next Product
int productId; // Unique identifier for the product

public:
// Getters and Setters
int get() { return data; }
void set(int data) { this->data = data; }
Product* getNext() { return next; }
void setNext(Product* nextNode) { this->next = nextNode; }
int getProductId() { return productId; }
void setProductId(int pid) { this->productId = pid; }
};

// Stack class
class Stack {
private:
Product* head; // Head pointer for the stack

public:
Stack() : head(NULL) {} // Constructor to initialize the head to nullptr

// Push method to add an element to the stack


void push(int x) {
Product* newProduct = new Product(); // Create a new Product object
newProduct->set(x); // Set the value
newProduct->setNext(head); // Point to the previous head
head = newProduct; // Update head to new Product
cout << "Pushed digit " << x << " onto the stack." << endl;
}

// Method to return the top element of the stack


int top() {
if (head != NULL) {
return head->get(); // Return the value of the head Product
}
return -1; // Indicate empty stack
}

// Method to pop the top element from the stack


int pop() {
if (head == NULL) {
cout << "Stack is empty, cannot pop." << endl;
return -1; // Indicate empty stack
}
int topData = head->get(); // Get value of top Product
Product* temp = head; // Temporary pointer to hold the head
head = head->getNext(); // Move head to next Product
delete temp; // Delete the popped Product
return topData; // Return the popped value
}
};
int main() {
Product product; // Create Product object
Stack stack;

// Set product ID with the last two digits of the student ID


int productId = 2356; // e.g., 2356
[Link](productId);

cout << "------------------------------------------" << endl;


cout << "The product id is: " << [Link]() << endl;
cout << "-------------------------------------------" << endl;

// Get the product ID and convert it to individual digits


int pid = [Link]();

int digits[4];
for (int i = 3; i >= 0; --i) {
digits[i] = pid % 10;
pid /= 10;
}

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


[Link](digits[i]);
}

cout << "------------------------------------------" << endl;


cout << "The Top element of the stack is: " << [Link]() << endl;
cout << "------------------------------------------" << endl;

cout << "The First element popped from the stack is: " << [Link]() << endl;
cout << "The Second element popped from the stack is: " << [Link]() << endl;

cout << "------------------------------------------" << endl;


cout << "The Top element of the stack is: " << [Link]() << endl;

return 0;
}

You might also like