Answers to Selected Exercises Chapter 1



Download 0.49 Mb.
Page3/7
Date18.07.2017
Size0.49 Mb.
#23634
1   2   3   4   5   6   7

Chapter 5


1.

(a) Yes


(b) No

(c) Yes


(d) No

(e) Yes


(f) No

(g) No


(h) Yes

3. (a)


3 5 4 (on one line)

5 16 1 0 (each on a separate line)

(b)

0 5 6 5 (each on a separate line)



5 4 0 (on one line)

5. stack.Push(letter);



letter

X













letter

X

















































stack.top

4













stack.top

4













.overFlow
















.overFlow

T













.underFlow
















.underFlow
















.items

A

B

C

D

E

.items

A

B

C

D

E




[0]

[1]

[2]

[3]

[4]




[0]

[1]

[2]

[3]

[4]

8. (a) Set secondElement to the second element in the stack, leaving the stack without its original top two elements.

{

stack.Pop(secondElement);



stack.Pop(secondElement);

}

(b) Set bottom equal to the bottom element in the stack, leaving the stack empty.



{

while (!stack.IsEmpty())

stack.Pop(bottom);

}

(c) Set bottom equal to the bottom element in the stack, leaving the stack unchanged.



{

StackType tempStack;

ItemType tempItem;
while (!stack.IsEmpty())

{

stack.Pop(tempItem);



tempStack.Push(tempItem);

}

bottom = tempItem;



// restore stack

while (!tempStack.IsEmpty())

{

tempStack.Pop(tempItem);



stack.Push(tempItem);

}

}



(d) Make a copy of the stack, leaving the stack unchanged.

{

StackType tempStack;



ItemType tempItem;

while (!stack.IsEmpty())

{

stack.Pop(tempItem);



tempStack.Push(tempItem);

}
// restore stack

while (!stack.IsEmpty())

{

tempStack.Pop(tempItem);



stack.Push(tempItem);

copy.Push(tempItem)

}

}

12. (a) Draw a diagram of how the stack might look.



smallTop: top for the stack of small values, initialized to -1 and incremented.

largeTop: top for the stack of large values, initialized to 200 and decremented.

























[0]

[1]

[2]

[3]

....

[197]

[198]

[199]

(b)


class DStack

{

public:



DStack();

void Push(int item);

void PopLarge(int& item);

void PopSmall(int& item);

private:

int smallTop;

int largeTop;

int items[200];

}

(c)


void Push(int item)

{

if (item <= 1000)



{

small++;


items[small] = item;

}

else



{

large--;


items[large] = item;

}

}



15.

void (StackType& stack, ItemType oldItem, ItemType newItem)

{

StackType tempStack;



ItemType tempItem;

while (!stack.IsEmpty())

{

stack.Pop(tempItem);



if (tempItem.ComparedTo(oldItem) == EQUAL)

tempStack.Push(newItem);

else

tempStack.Push(tempItem);



}

// restore stack

while (!stack.IsEmpty())

{

tempStack.Pop(tempItem);



stack.Push(tempItem);

}

}



17.

Stack ADT Specification

Structure: Elements are added to and removed from the top of the stack.
Definitions (provided by user):

MAX_ITEMS: Maximum number of items that might be on the stack.

ItemType: Data type of the items on the stack.
Operations (provided by the ADT):

MakeEmpty

Function: Sets stack to an empty state.

Precondition: None

Postcondition: Stack is empty.
Boolean IsEmpty

Function: Determines whether the stack is empty.

Precondition: Stack has been initialized.

Postcondition: Function value = (stack is empty)
Boolean IsFull

Function: Determines whether the stack is full.

Precondition: Stack has been initialized.

Postcondition: Function value = (stack is full)
Push(ItemType newItem, Boolean& error)

Function: Adds newItem to the top of the stack.

Precondition: Stack has been initialized.

Postcondition: If stack is not full, newItem is at the top of the stack and error is false;

otherwise, error is true and the stack is unchanged.


Pop(ItemType& item, Boolean& error)

Function: Removes top item from Stack and returns it in item.

Preconditions: Stack has been initialized and is not empty.

Postcondition: If the stack is not empty, the top element has been removed from stack, item is a copy of removed item, and error is false; otherwise, error is true and the stack is unchanged.

(b) None


(c) None

An alternative implementation would add an error data member to the class. This error flag would be set if underflow or overflow occurred and the operation would not be performed. A member function would have to be added to the class to allow the user to check the error flag.

20. Yes, this sequence is possible.

22. (a)


1 0 4

5 16 0 3

(b)

6 4 6 0


6 5 5

24. queue.Enqueue(letter);




letter

J













letter

J

















































queue.front

4













queue.front

4













.rear

3













.rear

3













.overFlow
















.overFlow

T













.underFlow
















.underFlow
















.items

V

W

X

Y

Z

.items

V

W

X

Y

Z




[0]

[1]

[2]

[3]

[4]




[0]

[1]

[2]

[3]

[4]

27. queue.Dequeue(letter);


letter
















letter

V

















































queue.front

4













queue.front

0













.rear

2













.rear

2













.overFlow
















.overFlow
















.underFlow
















.underFlow

F













.items

V

W

X

Y

Z

.items

V

W

X

Y

Z




[0]

[1]

[2]

[3]

[4]




[0]

[1]

[2]

[3]

[4]

30. (a) Set secondElement to the second element in the queue, leaving the queue without its original front two elements.

{

queue.Dequeue(secondElement);



queue.Dequeue(secondElement);

}

(b) Set last equal to the rear element in the queue, leaving the queue empty.



{

while (!queue.IsEmpty())

queue.Dequeue(last);

}

(c) Set last equal to the rear element in the queue, leaving the queue unchanged.



{

QueType tempQ;

ItemType item;
while (!queue.IsEmpty())

{

queue.Dequeue(last);



tempQ.Enqueue(last);

}

while (!tempQ.IsEmpty())



{

tempQ.Dequeue(item);

ueue.Enqueue(item);

}

}



(d) A copy of the queue, leaving the queue unchanged.

{

QueType tempQ;



ItemType item;
while (!queue.IsEmpty())

{

queue.Dequeue(item);



tempQ.Enqueue(item);

}

while (!tempQ.IsEmpty())



{

tempQ.Dequeue(item);

queue.Enqueue(item);

copy.Enqueue(item);

}

}

32. The correct answer for the first statement is (d); the correct answer for the second statement is (a).



35. (a) No

(b) Yes


(c) No

(d) No


(e) Yes

(f) Yes


(g) No

(h) Yes


(i) No

37.


int Length(QueType queue)

{

QueType tempQ;



ItemType item;

int length = 0;


while (!queue.IsEmpty())

{

queue.Dequeue(item);



tempQ.Enqueue(item);

}

while (!tempQ.IsEmpty())



{

tempQ.Dequeue(item);

queue.Enqueue(item);

length++;

}

return length;



}

39. The second else-clause is only executed if both queues are not empty, but one had to be empty for the loop to be exited, so only one can be empty. Therefore, string1 can only be assigned a value from one queue or the other but not both.

42. No, this sequence is not possible.

44.


#include "StackType.h"

#include

bool IsOpen(char symbol);

bool IsClosed(char symbol);

bool Matches(char symbol, char openSymbol);
int main()

{

using namespace std;



char symbol;

StackType stack;

bool balanced = true;

char openSymbol;

try

{

cout << "Enter an expression and press return."



<< endl;

cin.get(symbol);

while (symbol != '\n' && balanced)

{

if (IsOpen(symbol))



stack.Push(symbol);
else if (IsClosed(symbol))

{

if (stack.IsEmpty())



balanced = false;

else


{

openSymbol = stack.Top();

stack.Pop();

balanced = Matches(symbol, openSymbol);

}

}

cin.get(symbol);



}

}

catch (FullStack error)



{

cout << "Push called when the stack is full." << endl;

return 1;

}

catch (EmptyStack error)



{

cout << "Top or Pop called when stack is empty."



<< endl;

return 1;

}
if (balanced)

cout << "Expression is well formed." << endl;

else

cout << "Expression is not well formed." << endl;



return 0;

}

bool IsOpen(char symbol)



{

if ((symbol == '(') || (symbol == '{') || (symbol == '['))

return true;

else


return false;

}
bool IsClosed(char symbol)

{

if ((symbol == ')') || (symbol == '}') || (symbol == ']'))



return true;

else


return false;

}
bool Matches(char symbol, char openSymbol)

{

return (((openSymbol == '(') && symbol == ')')



|| ((openSymbol == '{') && symbol == '}')

|| ((openSymbol == '[') && symbol == ']'));

}



Download 0.49 Mb.

Share with your friends:
1   2   3   4   5   6   7




The database is protected by copyright ©ininet.org 2024
send message

    Main page