Here I have C++ code to implement a stack push()
and pop()
using a linked list. I delete
/ pop()
one data and it works fine, but I want to pop()
/ delete
many data items. Assume the data is 6 5 4 3 2 1
, and I want to delete 6 5 4 3
, what can I do to achieve this?
#include <iostream>
using namespace std;
//Structure of the Node
struct Node
{
int data;
Node *next;
};
// top pointer to keep track of the top of the stack
Node *top = NULL;
//Function to check if stack is empty or not
bool isempty()
{
if (top == NULL){
return true;
} else {
}
return false;
}
void pushStack(int value){
Node *ptr = new Node();
ptr->data = value;
ptr->next = top;
top = ptr;
}
//Function to delete an element from the stack
void pop ( )
{
if ( isempty() )
cout << "Stack is Empty";
else
{
Node *ptr = top;
top = top->next;
delete(ptr);
}
}
// Function to Display the stack
void displayStack()
{
if ( isempty() )
cout << "Stack is Empty";
else
{
Node *temp = top;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << "\n";
}
}
int main()
{
pushStack(1);
pushStack(2);
pushStack(3);
pushStack(4);
pushStack(5);
pushStack(6);
pop();
displayStack();
return 0;
}
You have 6 items pushed on the stack, and you are asking to remove the last 4 items that were pushed. Simply call pop()
4 times, eg:
int main()
{
pushStack(1);
pushStack(2);
pushStack(3);
pushStack(4);
pushStack(5);
pushStack(6);
displayStack(); // displays "6 5 4 3 2 1"
pop();
pop();
pop();
pop();
displayStack(); // displays "2 1"
return 0;
}
UPDATE:
In comments, you are asking how to remove specific nodes from the middle of the stack. While that is easy to accomplish with a linked list, that is not how a stack is meant to operate. A stack is a First-In-Last-Out (FILO) container, meaning the first value pushed is the last value popped. So, you should only be pushing and popping values from the top of the stack, nowhere else.
But, if you really want to do this, it would look something like this:
#include <iostream>
using namespace std;
//Structure of the Node
struct Node
{
int data;
Node *next;
};
// top pointer to keep track of the top of the stack
Node *top = NULL;
//Function to check if stack is empty or not
bool isEmpty()
{
return (top == NULL);
}
//Function to add an element to the top of the stack
void pushStack(int value){
Node *ptr = new Node();
ptr->data = value;
ptr->next = top;
top = ptr;
}
//Function to delete the top element from the stack
void pop()
{
if ( isEmpty() )
cout << "Stack is Empty\n";
else
{
Node *ptr = top;
top = top->next;
delete ptr;
}
}
void removeValue(int value)
{
if ( isEmpty() )
cout << "Stack is Empty\n";
else
{
Node *temp = top, **ptr = ⊤
while (temp != NULL)
{
if (temp->data == value)
{
*ptr = temp->next;
delete temp;
return;
}
ptr = &(temp->next);
temp = temp->next;
}
cout << "Value not found\n";
}
}
// Function to display the stack
void displayStack()
{
if ( isEmpty() )
cout << "Stack is Empty\n";
else
{
Node *temp = top;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << "\n";
}
}
int main()
{
pushStack(1);
pushStack(2);
pushStack(3);
pushStack(4);
pushStack(5);
pushStack(6);
displayStack(); // displays "6 5 4 3 2 1"
removeValue(4);
removeValue(5);
displayStack(); // displays "6 3 2 1"
return 0;
}