Data Structures Exam 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Evaluate the postfix expression: 3,5,4,x,+,12,-,8,2,/,+,=

15

Write the equivalent infix expression for the following postfix expression: ABxC+

A*B+C

Convert the following infix expression to postfix notation: A-(B+C)xD+E/F

ABC+Dx-EF/+

In a linked list, memory allocated for the nodes is sequential. T/F

False

In a linked list, nodes are always inserted either at the beginning or the end because a linked link is not a random-access data structure. T/F

False

In a linked list, the order of the elements is determined by the order in which the nodes were created to store the elements. T/F

False

A stack is a FIFO data structure. T/F

False (LIFO) or (FILO)

A single linked list can be traversed in either direction. T/F

False, Doubly linked lists are.

complete the definition of the reverse print funtion shown belw. template <class Type> void ListType<Type>::reversePrint2(NodeType<Type> *head){ if(head != NULL) { reversePrint2(_____________); cout << ___________________<< endl; } }

Template <class Type> void ListType<Type>::reversePrint2(NodeType<Type> *head){ if(head != NULL) { reversePrint2(__head->next___); cout << __head->info______<< endl; } }

An iterator for a linked list is an object that produces each element of the linked list. T/F

True

Middle elements of a queue should not be accessed directly. T/F

True

Postfix notation does not require the use of parentheses to enforce operator precedence. T/F

True

The Search on a linked list is sequential. T/F

True

complete the following client code that prints the contents of the list a using an iterator. for(IteratorType i = ____; _______; _______;) cout << __________ << endl;

for(IteratorType i = a.begin(); i != a.end(); ++i;) cout << _*i___ << endl;

Complete the dereferencing and increment operators of IteratorType shown below. int IteratorType::operator*() { return ____________________; } IteratorType IteratorType::operator++() { _____________________; return ________________; }

int IteratorType::operator*() { return _current->info_____; } IteratorType IteratorType::operator++() { __current = current->next______; return __*this_____; }

Complete the follwoing code that derives a stack class using Deque as the base class. Note that the stack class should only provide "push" and "pop" operations. (This means functions such as push_back should not be available for stacks) template <class Type> class Stack:____________________{ public: Stack() {}; void push(Type info); Type pop(); }; template <class Type> void Stack<Type>::push(Type info) { _______________________________; } template <class Type> Type Stack<Type>::pop() { ___________________________; }

template <class Type> class Stack:____private Deque<Type>_____{ public: Stack() {}; void push(Type info); Type pop(); }; template <class Type> void Stack<Type>::push(Type info) { ___Deque<Type>::pop_front(info)__________; } template <class Type> Type Stack<Type>::pop() { _____return Deque<Type>::pop_front()___________; }

Complete the definition of the insert function shown below, which should perform an "ordered list" insert operation. The list should be sorted in ascending order. template <class Type> Void ListType<Type>::insert(Type x) {//ordered list insert NodeType<Type> *curr, *prev, *p; curr = prev =_______________; while(curr != NULL && curr->info <= x) { prev = curr; __________________________; } p = new NodeType<Type>; p->info = x; p->next = curr; if(curr = head) head = p; else prev->next = ________________; }

template <class Type> Void ListType<Type>::insert(Type x) {//ordered list insert NodeType<Type> *curr, *prev, *p; curr = prev =____head_____; while(curr != NULL && curr->info <= x) { prev = curr; ____curr = cur->next__________; } p = new NodeType<Type>; p->info = x; p->next = curr; if(curr = head) head = p; else prev->next = ___p_____; }

complete enqueue and dequeue functions for the following queue class. template <class Type> class Queue { public: Queue(); void enqueue(const Type x); Type dequeue(); bool is Empty() const; private: Type *queue; int max; int front; int rear; }; template <class Type> Queue<Type>::Queue() { max = 100; queue = new Type [Type]; front = rear = max - 1; } template <class Type> void Queue<Type>::enqueue(const Type x) { rear = (rear + 1) % max; if(rear == front) { cout << "Error: Overflow" << endl; exit(1); } else { _______________________; } } template <class Type> Type Queue<Type>::dequeue() { if(front == rear) { cout << "Error: Underflow" << endl; exit(1); } else { _________________; __________________; } } template <class Type> bool Queue<Type>::isEmpty() const { return front == rear;}

template <class Type> class Queue { public: Queue(); void enqueue(const Type x); Type dequeue(); bool is Empty() const; private: Type *queue; int max; int front; int rear; }; template <class Type> Queue<Type>::Queue() { max = 100; queue = new Type [Type]; front = rear = max - 1; } template <class Type> void Queue<Type>::enqueue(const Type x) { rear = (rear + 1) % max; if(rear == front) { cout << "Error: Overflow" << endl; exit(1); } else { _queue[rear] = x_________; } } template <class Type> Type Queue<Type>::dequeue() { if(front == rear) { cout << "Error: Underflow" << endl; exit(1); } else { _front = (front + 1) % max______; __return queue[front]_____; } } template <class Type> bool Queue<Type>::isEmpty() const { return front == rear;}

Consider Deque shown below. template <class Type> struct NodeType { Type info; NodeType<Type> *next, *prev; }; template <class Type> class Deque { public: Deque(); void push_front(Type info); void push_back(Type info); Type pop_front(); Type pop_back(); bool isEmpty(); private: NodeType<Type> *head, *tail; }; complete the definition of the push_front function shown below. It performs a stack push operation. template <class Type> void Deque<Type>::push_front(Type info) { Node Type<Type> *p = new NodeType<Type>; p->info = info; p->next = __________________; p->prev = __________________; if(head != NULL) { head->prev = _________________; head = __________________; } else head = tail = p; }

template <class Type> void Deque<Type>::push_front(Type info) { Node Type<Type> *p = new NodeType<Type>; p->info = info; p->next = ___p->next = head_____; p->prev = ___p->prev = NULL____; if(head != NULL) { head->prev = ___head->prev = p_____; head = __p_________; } else head = tail = p; }

Consider simulating the recursion in the following function using stacks. void proc(int n) { if(n>0) { proc(n-1); cout << n << endl; proc(n-1); } } rewrite the function using the stack class shown below. (Private members not shown.) The new function should not contain recursive calls. Recursion should be simulated using stack operation. template <class Type> class Stack { public: Stack(); Void push(const Type x); Type pop(); bool isEmpty() const; private: ...... }

void proc(int n) { stack<int> s; stack< void *> retaddr; L1: if(n>0){ //proc(n-1) s.push(n); retaddr.push(&&L2); n = n-1; goto L1; L2: cout << n << endl; //proc(n-1) s.push(n); retaddr.push(&&L3); n = n-1; goto L1; } L3: if(!s.isEmpty()){ n = s.pop(); void *addr = retaddr.pop(); goto *addr; } }


Ensembles d'études connexes

les douze mois de l'année et les quatre saisons

View Set

Chapter 8 consumer strategies and Legal protection

View Set

Financial Accounting Exam 1 misc.

View Set

Casualty Insurance Terms and Related Concepts

View Set

Study Guide House Taken Over and The Fall of the House of Usher

View Set