Stack ADT application

Using stack ADT similar to undo button, 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.
Eg:
Enter word:I
Enter word:want
Enter word:love
Enter word:u1
Enter word:icecream
Enter word:x
Output: I want icecream.

[b]if number of words to be undo are less than that present in stack.error should be printed.[/b]

[b]The code & stack ADT file I have typed is as follows,I can enter words in the stack but I'm unable to create a error message if number of input words are less than the number or words that have to be undone. I cannot transfer contents from the old stack to a new stack so that stack contents are reversed. The stack contents have to be reversed in the final o/p.can anybody please help?[/b]

[b]StackADT.h[/b][u][/u]
#include
#include


//Stack ADT Type Defintions
typedef struct node2
{
void* dataPtr;
struct node2* link;
} STACK_NODE;

typedef struct
{
int count;
STACK_NODE* top;
} STACK;


/* ADT Prototype Declarations */
STACK* createStack (void);
bool pushStack (STACK* stack, void* dataInPtr);
void* popStack (STACK* stack);
void* stackTop (STACK* stack);
bool emptyStack (STACK* stack);
bool fullStack (STACK* stack);
int stackCount (STACK* stack);
STACK* destroyStack (STACK* stack);

[b]/*========== createStack ==============
This algorithm creates an empty stack.
Pre Nothing
Post Returns pointer to a null stack
-or- NULL if overflow
*/[/b]
STACK* createStack (void)
{
// Local Definitions
STACK* stack;

// Statements
stack = (STACK*) malloc( sizeof (STACK));
if (stack)
{
stack->count = 0;
stack->top = NULL;
} // if
return stack;
} // createStack


[b]/*============ pushStack ================
This function pushes an item onto the stack.
Pre stack is a pointer to the stack
dataPtr pointer to data to be inserted
Post Data inserted into stack
Return true if successful
false if underflow
*/[/b]
bool pushStack (STACK* stack, void* dataInPtr)
{
// Local Definitions
STACK_NODE* newPtr;

// Statements
newPtr = (STACK_NODE* ) malloc(sizeof( STACK_NODE));
if (!newPtr)
return false;

newPtr->dataPtr = dataInPtr;

newPtr->link = stack->top;
stack->top = newPtr;

(stack->count)++;
return true;
} // pushStack


[b]/*============= popStack ==================
This function pops item on the top of the stack.
Pre stack is pointer to a stack
Post Returns pointer to user data if successful
NULL if underflow
*/[/b]
void* popStack (STACK* stack)
{
// Local Definitions
void* dataOutPtr;
STACK_NODE* temp;

// Statements
if (stack->count == 0)
dataOutPtr = NULL;
else
{
temp = stack->top;
dataOutPtr = stack->top->dataPtr;
stack->top = stack->top->link;
free (temp);
(stack->count)--;
} // else
return dataOutPtr;
} // popStack


[b]/*============ stackTop =================
Retrieves data from the top of stack without
changing the stack.
Pre stack is a pointer to the stack
Post Returns data pointer if successful
null pointer if stack empty
*/[/b]
void* stackTop (STACK* stack)
{
// Statements
if (stack->count == 0)
return NULL;
else
return stack->top->dataPtr;
} // stackTop


[b]/*============ emptyStack ================
This function determines if a stack is empty
Pre stack is pointer to a stack
Post returns 1 if empty; 0 if data in stack
*/[/b]
bool emptyStack (STACK* stack)
{
// Statements
return (stack->count == 0);
} // emptyStack


[b]/*============== fullStack =================
This function determines if a stack is full.
Full is defined as heap full.
Pre stack is pointer to a stack head node
Return true if heap full
false if heap has room
*/[/b]
bool fullStack (STACK* stack)
{
// Local Definitions
STACK_NODE* temp;

// Statements
if ((temp =
(STACK_NODE*)malloc (sizeof(*(stack->top)))))
{
free (temp);
return false;
} // if

// malloc failed
return true;
} // fullStack

[b]/*============== stackCount =================
Returns number of elements in stack.
Pre stack is a pointer to the stack
Post count returned
*/[/b]
int stackCount (STACK* stack)
{
// Statements
return stack->count;
} // stackCount

[b][b]/*============= destroyStack =================[/b]
This function releases all nodes to the heap.
Pre A stack
Post returns null pointer
*/[/b]STACK* destroyStack (STACK* stack)
{
// Local Definitions
STACK_NODE* temp;

// Statements
if (stack)
{
// Delete all nodes in stack
while (stack->top != NULL)
{
// Delete data entry
free (stack->top->dataPtr);

temp = stack->top;
stack->top = stack->top->link;
free (temp);
} // while

// Stack now empty. Destroy stack head node.
free (stack);
} // if stack
return NULL;
} // destroyStack

[b]
CODE:[/b]

#include
#include
#include
#include
#include "stacksADT.h"

int main(void)
{

bool done = false;
char* dataPtr,*str;
int i, N;

STACK* stack;

stack = 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);

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

if (strcmp(dataPtr,"/x")==0 || fullStack (stack))//user quits entering
one = 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
{
//we are undoing. Let's get N

N = atoi(dataPtr+2); //convert string to integer

dataPtr=(char*)popStack(stack);//undo of last N words

//print message if N greater than number of words

} //if

else // user wants to enter a word
{

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

//transfer to new stack and print it out
while(!emptyStack(stack))
{
dataPtr=(char*)popStack(stack);
printf("%s
", dataPtr);
free(dataPtr);
}

destroyStack(stack);

return 0;
}



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories