Question

========================Data Structure C++============================ 1. Write a CLIENT program that creates a stacks fo type int (so...

========================Data Structure C++============================

1. Write a CLIENT program that creates a stacks fo type int (so one stack instance):  aStack. Ask the user for 5 ints and push them onto aStack. Find a way to print the values in aStack in from the bottom to the top (so display the top LAST) - so if aStack was 1,2,3,4,5 with 5 at the top and 1 at the bottom you want it to display 1,2,3,4,5 (you don't want it to display 5,4,3,2,1 like normal). You can use ArrayStack.h or LinkedStack.h for your stacks but do NOT change the code for ArrayStack or LinkedStack. Submit ONLY your main method for this program. Preferably copy and paste but I set it up so you can submit files if need be.

2. Write a FUNCTION (not a member function just a regular old function for a client program) that uses a stack to test whether or not a given string is a palindrome (remember a palindrome reads the same backward and forward: so a, aba, a_ _a, abba etc). Test your function in a client program. You can use ArrayStack.h or LinkedStack.h for your stacks but do NOT change the code for ArrayStack or LinkedStack. Turn in ONLY your function code but it should compile without alternation when I test it (I should only need to add the function prototype to my main). Preferably copy and paste but I set it up so you can submit files if need be.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

#include <iostream>
#include "LinkedStack.h"

using namespace std;

int main()
{
   LinkedStack<int> aStack; // assuming LinkedStack is a templated stack
   int num;
   cout<<"Enter 5 integers to push : ";
   // loop to push 5 ints
   for(int i=0;i<5;i++)
   {
       cin>>num;
       aStack.push(num);
   }
  
   LinkedStack<int> tempStack; // create a temporary stack
   // isEmpty() is a function of LinkedStack that returns if the stack if empty or not
   while(!aStack.isEmpty())
   {
       tempStack.push(aStack.peek());
       aStack.pop();
   }
  
   cout<<"Stack from bottom to top : "<<endl;
   while(!tempStack.isEmpty())
   {
       cout<<tempStack.peek();
       tempStack.pop();
   }
   return 0;
}

//end of program

2.

1.

#include <iostream>
#include "LinkedStack.h"

using namespace std;

bool isPalindrome(string str)
{
   LinkedStack<char> aStack;
   // loop over the string and push the characters to the stack
   for(int i=0;i<str.length();i++)
       aStack.push(str.at(i));
  
   int i=0;
   // loop over the string and stack
   while((!aStack.isEmpty()) && (i <str.length()))
   {
       char strChr = str.at(i); // get the ith character of the str
       char stkChr = aStack.peek(); // get the top character from stack
       if(strChr != stkChr) // if the characters do not match, then string is not a palindrome
           return false;
       aStack.pop(); // remove the top element from stack
       i++;   // advance to next index of str
   }
  
   return true; // str is palindrome
}

//end of function

int main()

{

// test the function

return 0;

}

Add a comment
Know the answer?
Add Answer to:
========================Data Structure C++============================ 1. Write a CLIENT program that creates a stacks fo type int (so...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • Stacks There are two main operations associated with stacks; 1) putting things on the stack which...

    Stacks There are two main operations associated with stacks; 1) putting things on the stack which is referred to as push, 2) taking things from the stack which is referred to as pop.   We can create a stack using linked lists if we force ourselves to insert and remove nodes only at the top of the list. One use of a stack is when you want to write a word backward. In that case, you will read the letters of...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • Write a program that takes a file as input and checks whether or not the content...

    Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples:             [ ( ) ] { } à balanced.             [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...

  • #19 with the following instructions 19. A palindrome is a string that can be read backward...

    #19 with the following instructions 19. A palindrome is a string that can be read backward and forward with the same result. For example, the following is a palindrome: Able was I ere I saw Elba. unction to test if a string is a palindrome using a stack. You can push characters in the stack one by one. When you reach the en string, you can pop the characters and form a new string. If the two strings are exactly...

  • PLEASE COMPLETE IN C++ LANGUAGE Location row : int = -1 col : int = -1...

    PLEASE COMPLETE IN C++ LANGUAGE Location row : int = -1 col : int = -1 setLocation(row : int, col : int) getRow(): int getColl): int isEmpty(): bool Details Write all the code necessary to implement the Location class as shown in the UML Class Diagram to the right. Do this in a project named snake (we'll continue adding files to this project as the term progresses). Your class definition and implementation should be in separate files. When complete, you...

  • Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise...

    Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise as a practice for c programming, could someone do this as well so i could compare if my code makes makes sense and to see and help correct the errors im getting right now.. Any helpful help would be appreciated.. Pointers &Pointer arithmetic Memory allocation and freeing Main topics: Exercise This lab is designed to give you practice working with pointers and memory allocation...

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT