Exercise 1 from Chapter 6 of the textbook (p.294)
(to do with using an ADT for a stack)
1. Suppose that you have a stack S and an empty auxiliary stack T. Show how you can do each of the following tasks by using only the ADT stack operations:
ItemType i;
while (!EmptyStack (S))
{
Pop (S, i);
Push (i, T);
}
while (!EmptyStack (T))
{
Pop (T, i);
i.Display ();
Push (i, S);
}
ItemType i;
int Count = 0;
while (!EmptyStack (S))
{
Pop (S, i);
Push (i, T);
}
while (!EmptyStack (T))
{
Pop (T, i);
Count++;
Push (i, S);
}
Delete ( ItemType ElementToDelete )
{
ItemType i;
while (!EmptyStack (S))
{
Pop (S, i);
if (!(i == ElementToDelete))
Push (i, T);
}
while (!EmptyStack (T))
{
Pop (T, i);
Push (i, S);
}
}