Difference of two stacks
hello here below there is an implementation of stack class by using singly linked list that contains operations such as constructor, destructor, pop, push, top, displaying the stacks and checking if the stack is empty or not. Additionally, there is a part for intersection, union and difference of two stacks by using these operations. While intersection and union parts works well, there is a problem with difference of two stacks that I could not figure it out. At this part, I tried to put elements into a stack that s1 has and s2 has not. I would appreciate it if you could help me out. Thanks in advance. #include #include using namespace std; // Node structure for singly linked list struct Node { string data; Node* next; }; // Stack class implementation using singly linked list class Stack { private: Node* top; public: // Constructor Stack() : top(nullptr) {} // Destructor ~Stack() { while (!isEmpty()) { pop(); } } // Push operation void push(const string& data) { Node* newNode = new Node{data, top}; top = newNode; } // Pop operation string pop() { if (isEmpty()) { cout next; delete temp; return data; } // Top operation string getTop() const { if (isEmpty()) { return ""; } return top->data; } // Display all elements in the stack void displayStack() const { Node* current = top; while (current) { cout data next; } cout data == data) { return true; } current = current->next; } return false; } }; // Function to compute intersection of two stacks Stack intersection(Stack& s1, Stack& s2) { Stack result; Stack temp; // Temporary stack to preserve s1 during traversal while (!s1.isEmpty()) { string element = s1.pop(); temp.push(element); if (s2.find(element)) { result.push(element); } } // Restore original stack s1 while (!temp.isEmpty()) { s1.push(temp.pop()); } return result; } // Function to compute union of two stacks Stack unionStacks(Stack& s1, Stack& s2) { Stack result; Stack temp; // Temporary stack to preserve s1 during traversal while (!s1.isEmpty()) { string element = s1.pop(); temp.push(element); result.push(element); } // Restore original stack s1 while (!temp.isEmpty()) { s1.push(temp.pop()); } while (!s2.isEmpty()) { string element = s2.pop(); if (!result.find(element)) { // Avoid duplicate entries result.push(element); } } return result; } Stack difference(Stack& s1, Stack& s2) { Stack result; Stack temp; // Temporary stack to preserve s1 during traversal while (!s1.isEmpty()) { string element = s1.pop(); temp.push(element); if (s2.find(element)) { continue; } else { result.push(element); } } // Restore original stack s1 while (!temp.isEmpty()) { s1.push(temp.pop()); } return result; } // Main function to test functionality int main() { Stack s1, s2; // Push elements into stacks S1 and S2 s1.push("apple"); s1.push("banana"); s1.push("cherry"); s2.push("banana"); s2.push("cherry"); s2.push("date"); cout

hello here below there is an implementation of stack class by using singly linked list that contains operations such as constructor, destructor, pop, push, top, displaying the stacks and checking if the stack is empty or not. Additionally, there is a part for intersection, union and difference of two stacks by using these operations. While intersection and union parts works well, there is a problem with difference of two stacks that I could not figure it out. At this part, I tried to put elements into a stack that s1 has and s2 has not. I would appreciate it if you could help me out. Thanks in advance.
#include
#include
using namespace std;
// Node structure for singly linked list
struct Node {
string data;
Node* next;
};
// Stack class implementation using singly linked list
class Stack {
private:
Node* top;
public:
// Constructor
Stack() : top(nullptr) {}
// Destructor
~Stack() {
while (!isEmpty()) {
pop();
}
}
// Push operation
void push(const string& data) {
Node* newNode = new Node{data, top};
top = newNode;
}
// Pop operation
string pop() {
if (isEmpty()) {
cout << "Stack is empty!" << endl;
return "";
}
string data = top->data;
Node* temp = top;
top = top->next;
delete temp;
return data;
}
// Top operation
string getTop() const {
if (isEmpty()) {
return "";
}
return top->data;
}
// Display all elements in the stack
void displayStack() const {
Node* current = top;
while (current) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
// Check if stack is empty
bool isEmpty() const {
return top == nullptr;
}
// Find operation
bool find(const string& data) const {
Node* current = top;
while (current) {
if (current->data == data) {
return true;
}
current = current->next;
}
return false;
}
};
// Function to compute intersection of two stacks
Stack intersection(Stack& s1, Stack& s2) {
Stack result;
Stack temp; // Temporary stack to preserve s1 during traversal
while (!s1.isEmpty()) {
string element = s1.pop();
temp.push(element);
if (s2.find(element)) {
result.push(element);
}
}
// Restore original stack s1
while (!temp.isEmpty()) {
s1.push(temp.pop());
}
return result;
}
// Function to compute union of two stacks
Stack unionStacks(Stack& s1, Stack& s2) {
Stack result;
Stack temp; // Temporary stack to preserve s1 during traversal
while (!s1.isEmpty()) {
string element = s1.pop();
temp.push(element);
result.push(element);
}
// Restore original stack s1
while (!temp.isEmpty()) {
s1.push(temp.pop());
}
while (!s2.isEmpty()) {
string element = s2.pop();
if (!result.find(element)) { // Avoid duplicate entries
result.push(element);
}
}
return result;
}
Stack difference(Stack& s1, Stack& s2) {
Stack result;
Stack temp; // Temporary stack to preserve s1 during traversal
while (!s1.isEmpty()) {
string element = s1.pop();
temp.push(element);
if (s2.find(element)) {
continue;
}
else {
result.push(element);
}
}
// Restore original stack s1
while (!temp.isEmpty()) {
s1.push(temp.pop());
}
return result;
}
// Main function to test functionality
int main() {
Stack s1, s2;
// Push elements into stacks S1 and S2
s1.push("apple");
s1.push("banana");
s1.push("cherry");
s2.push("banana");
s2.push("cherry");
s2.push("date");
cout << "Stack S1: ";
s1.displayStack();
cout << "Stack S2: ";
s2.displayStack();
// Intersection of S1 and S2
Stack s3 = intersection(s1, s2);
cout << "Intersection (S3): ";
s3.displayStack();
// Union of S1 and S2
Stack s4 = unionStacks(s1, s2);
cout << "Union (S4): ";
s4.displayStack();
// Difference of S1 and S2
Stack s5 = difference(s1, s2);
cout << "Difference (S5): ";
s5.displayStack();
return 0;
}