In: Computer Science
Create a program in C++ implementing the LinkedStack
7. Write a client that removes all negative numbers from a stack of int objects. If the original stack contained the integers 30, –15, 20, –25 (top of stack), the new stack should contain the integers 30, 20.
#include <iostream>
utilizing namespace sexually transmitted disease;
/struct hub for connect
struct Node
{
int information;
Hub *next;
};
/single myStack
class LinkedStack
{
private:
Hub *head;
public:
LinkedStack()
{
head=NULL;
}
/adding hub to interface
void push(int esteem)
{
Hub *temp=new Node;
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
temp=NULL;
}else{
temp->next = head;
head = temp;
}
}
/show interface
void print()
{
Hub *temp=new Node;
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
/show interface
void removeAllNegative()
{
Hub *temp=new Node;
temp=head;
/eliminate from first
while(temp->data<0){
head = temp->next;
temp = temp->next;
}
Hub *prev=temp;
while(temp!=NULL)
{
if(temp->data<0){
prev->next = temp->next;
}
prev = temp;
temp = temp->next;
}
cout<<endl;
}
};
int main(){
LinkedStack myStack;
myStack.push(- 25);
myStack.push(20);
myStack.push(- 15);
myStack.push(30);
cout<<"Original myStack"<<endl;
myStack.print();
myStack.removeAllNegative();
cout<<"After eliminating negative myStack"<<endl;
myStack.print();
bring 0 back;
}
/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/