Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, We have studied stack implementation using ADT of linked lists, and also the

ID: 3636615 • Letter: H

Question

Hi, We have studied stack implementation using ADT of linked lists, and also the pointer based implementation of stack operations(PREFERRED)...

Please dont post the solution to this that is available on the web. After the question i have posted an answer that i received to this question earlier, but you need to please modify that a bit, as i will explain below the answer!


QUESTION :

Use a stack ADT similar to undo button, where each word can be typed in as long as words are not the undo or exit sequence.

The undo sequence can be /uN where /u specifies undo operation, followed by N, number of previous words to undo. The exit sequence can be /x.Assume only one word is entered at a time.

[Hint: Consider a stack of strings]


Eg:
Enter word:I
Enter word:want
Enter word:love
Enter word:u1
Enter word:icecream
Enter word:x
Output: I want icecream.

If number of words to be undo are more than that present in stack, error should be printed.

//////////////////////
YOU ARE AVAILABLE WITH THE FOLLOWING STACK OPERATIONS IN SIMPLE POINTER BASED IMPLEMENTATION,

bool StackisEmpty();
void push(stackItemType Item, bool & Success);
void pop(bool & Success);
void pop(StackItemType & StackTop, bool & Success);
void GetStackTop(stackItemType & StackTop, bool & Success);

//////////////////////////////////////////

THE ANSWER THAT I RECEIVED IS (FORGIVE ME FOR NOT ALIGNING IT PROPERLY) :

#include #include #include using namespace std; int main(){ stack words; bool hResult; string str; int undoNum=0; NewStart: do{ cout << "Enter word:"; cin >>str; if (str.compare("\x")==0) break; if (str.length()>1){ //just to avoid exceptions if (str[0] =='\' && str[1] =='u'){ undoNum = atoi(str.substr( 2,str.length()-2 ).c_str() );//get the number } //delete time for (int i=0 ; i< undoNum ; i++) { if (words.StackisEmpty()) { cout<< "Error you can't undo " << undoNum << " steps! "; goto NewStart; } words.pop(&hResult); } }else{ words.push(str,&hResult); } }else{ words.push(str,&hResult); } }while (true); //now time to print the words stack reverse; //int size= words.size(); while(!words.StackisEmpty() ){ words.GetStackTop(&str, &hResult); reverse.push(str,&hResult); words.pop(&hResult); } cout << "Output: "; while (!reverse.StackisEmpty() ){ reverse.GetStackTop(&str, &hResult); cout << str << " "; reverse.pop(&hResult); } cout << endl; return 0; }

WHAT YOU NEED TO CHANGE IS THAT PLEASE DO IT WITHOUT DOING THE INTEGER - STRING CONVERSION (i.e Without atoi).

Explanation / Answer

//Hi I'm very sorry for my late response. i think the only part confused you

//is how to convert from string to number , so i wrote my own function

//to show how that is happened

//the modifications appears in red

//good luck in the exam ^_^

#include<iostream>

#include<string>

#include<stack>

using namespace std;

int convertToInteger(string str){

int i,n=0;

//strated from 2 to leave u out from the calculations

for (i=2;i<str.size();i++)

n=10*n + (str[i]- '0');

//the multiply by 10 comes to give each number it's weight

//and i substracted 0 from the string to convert each character to it's numeric value

return n;

}

int main(){

stack words;

bool hResult;

string str;

int undoNum=0;

NewStart:

do{

cout << "Enter word:";

cin >>str;

if (str.compare("\x")==0) break;

if (str.length()>1){ //just to avoid exceptions

if (str[0] =='\' && str[1] =='u'){

undoNum = convertToInteger(str);

//get the number

}

//delete time

for (int i=0 ; i< undoNum ; i++) {

if (words.StackisEmpty()) {

cout<< "Error you can't undo " << undoNum << " steps! ";

goto NewStart;

}

words.pop(&hResult);

}

}else{

words.push(str,&hResult);

}

}else{

words.push(str,&hResult); }

}while (true);

//now time to print the words stack reverse;

//int size= words.size();

while(!words.StackisEmpty() ){

words.GetStackTop(&str, &hResult);

reverse.push(str,&hResult);

words.pop(&hResult);

}

cout << "Output: ";

while (!reverse.StackisEmpty() ){

reverse.GetStackTop(&str, &hResult);

cout << str << " "; reverse.pop(&hResult);

}

cout << endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote