Question

In: Computer Science

Create a program in C++ implementing the LinkedStack 7. Write a client that removes all negative...

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.

Solutions

Expert Solution

#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!!*/


Related Solutions

Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk...
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk to a server program in two separate terminals. Write the server program that can handle multiple clients (so threads will be needed) and with fork() and exec()
Please write program in C++ format: Write a program to accept five negative numbers from the...
Please write program in C++ format: Write a program to accept five negative numbers from the user. (1) Find the average of the five numbers and display the answer to the standard output. Keep the answer two decimal points - 5 points (2) Output the numbers in ascending order and display the answer to the standard output. - 5 points
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
write a c++ program an expression that determines if an integer, n is a negative four...
write a c++ program an expression that determines if an integer, n is a negative four digit number. write a c++ program an expression that determines if a string, wd, equals "so" ignoring case.
Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
Create a schematic of a circuit using and write C program to run on a PIC...
Create a schematic of a circuit using and write C program to run on a PIC 16F88 that will flash between a red and green LED at 5Hz with the green on 75% of the time and the red on 25% of the time.
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT