Computing.Net > Forums > Programming > stack ADT application

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

stack ADT application

Reply to Message Icon

Name: gunzem
Date: March 1, 2009 at 19:29:03 Pacific
OS: Windows XP
CPU/Ram: 1.73MHz
Subcategory: C/C++
Comment:

#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
#include "stacksADT.h"


int main(void)
{
bool done = false;
char* dataPtr;
int N;

STACK* stack;
STACK* newStack;

stack = createStack();
newStack = createStack();

while (!done)
{
dataPtr = (char*)malloc(sizeof(char));
printf ("Enter a word or /uN for undoing N words or /x to exit:");
scanf("%s", dataPtr);

if(strcmp(dataPtr,"/x")==0 || fullStack (stack))//user quits entering
done = true;

else //user wants to enter more words or undo
{
//undo part
if(strncmp(dataPtr,"/u",2)==0)//check first two entered characters if undo
{
N= atoi(dataPtr+2);

while(N--)
{
popStack(stack);
}

} //if

else
{
pushStack(stack, dataPtr); // push in to stack
}

} //else

} // while

while (!emptyStack(stack))
{
dataPtr = (char*)popStack(stack);
pushStack(newStack, dataPtr);
} // while

while (!emptyStack(newStack))
{
dataPtr = (char*)popStack(newStack);
printf("%s\t", dataPtr);

free (dataPtr);
} // while

return 0;
}

This is how the output looks like:
Eg:
Enter word: I
Enter word: love
Enter word: icecream
Enter word: /u2
Enter word: /x
Output: I love

I'm not getting this part right.

Eg:
Enter word : I
Enter word: love
Enter word: /u3
can undo only 2 words.
Enter word: icecream
Enter word: /x
Output: icecream

I need to add a printf statement to above code.this printf statement should be printed incase I input less words and try to undo more words.

printf("Can undo only %d words\n", ??);

what do I put inplace of ?? and where should this statement go??



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


How do i change the backg... Batch file Help



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: stack ADT application

Stacks, Queues and Lists www.computing.net/answers/programming/stacks-queues-and-lists/6498.html

C++ stack program www.computing.net/answers/programming/c-stack-program/8589.html

Stack/Heap (C++) www.computing.net/answers/programming/stackheap-c/2293.html