Data structures exam 2

Ace your homework & exams now with Quizwiz!

What is binary recursion?

void toBinary(int num) { if (num > 0) { toBinary(num/2); cout<< num % 2;}}

How to apply the evaluation algorithm on an expression in postfix notation?

#include <iostream> #include <stack> using namespace std; int prec(char ch) { if (ch == '*' or ch == '/') return 2; else if (ch == '+' or ch == '-') return 1; else if (ch == '(' or ch == ')') return 0; } int main() { char ch; stack<char> oper; cin.get(ch); while (!cin.eof()) { if((ch>='0' and ch<='9')or (ch >='a' and ch <'z')or (ch >'A' and ch <='Z')) // checking for operand cout << ch; else { if (ch == '(') { oper.push(ch); } else if (ch==')'){ while(!oper.empty() && (oper.top() !='(')) { cout << oper.top(); oper.pop(); } if (!oper.empty()) { oper.pop(); } else cout << "Error no matching ("; } else if (ch == '*' or ch =='/' or ch == '+' or ch == '-') { if (oper.empty() or (prec(oper.top()) < prec(ch))){ // lower stack has lowest precedence oper.push(ch); } else { while (!oper.empty() && (prec(oper.top()) >= prec(ch))) { cout << oper.top(); oper.pop(); } oper.push(ch); } } else { cout << " illegal character"; } } cin.get(ch); } while (!oper.empty()) { cout << oper.top(); oper.pop(); } }

What is the balancing brackets algorithm?

#include <iostream> #include<fstream> #include<stack> using namespace std; bool balance(string file){ ifstream inFile; inFile.open(file); stack<char> s; bool balanced = true; char my_char; while (inFile.get(my_char)) { if (my_char=='('||my_char=='['||my_char=='{') { s.push(my_char); } else if(my_char==')'||my_char==']'||my_char=='}') { if (s.empty()) balanced = false; else { char x = s.top(); s.pop(); switch(my_char) { case ')': if (x=='{' || x=='[') balanced = false; break; case '}': if (x=='(' || x=='[') balanced = false; break; case ']': if (x=='{' || x=='(') balanced = false; break; } } } } if(!s.empty()) balanced = false; return balanced; } int main() { if(balance("balance.txt")) cout<<"Balanced"; else cout<<"Not Balanced"; return 0; }

What is a recursive function?

A recursive function calls back to itself // This function returns the sum of 1 to num int sum (int num) { int result; if (num == 1) //base case (the smallest problem) result = 1; else result = num + sum(num-1); return result; }

What is the main concept of backtracking?

Backtracking is an algorithmic-technique for solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem at any point of time

How to print a linked list in reverse order using recursion?

REVERSE: template void LinkedList::printListReverse(ListNode *current) const { if (current != NULL) { printListReverse(current->next); //print the tail cout << current->value << " "; //print the node } } GIVES HEAD: template void LinkedList::printListReverse() const { printListReverse(head); cout << endl; }

What are infix and postfix expressions and how do you convert between the two?

Regular expressions are written in "infix" notation, i.e., operator between two operands, e.g., (A+B) * (C- (D+E)) -The expression (A+B) * (C- (D+E)) becomes: A B + C D E + - * ○ Postfix expressions like A B + are evaluated as A + B

How to find the largest element in an array using recursion?

int largest(const int list[], int lowerIndex, int upperIndex) { int max; if (lowerIndex == upperIndex) //size of the sublist is one return list[lowerIndex]; else { max = largest(list, lowerIndex + 1, upperIndex); if (list[lowerIndex] >= max) return list[lowerIndex]; else return max; } }

How to implement circular Array Queue?

queueRear = (queueRear + 1) % maxQueueSize;

How to check if stack is empty?

template <class T> bool stackType<T>::isEmptyStack() const{ return(stackTop == 0);}

How to check if stack is full?

template <class T> bool stackType<T>::isFullStack() const { return(stackTop == maxStackSize);}

How to overload operator function?

template <class T> const stackType<T>& stackType<T>::operator=(const stackType<T<

Write a constructor for array stack-

template <class T> stakType::stackType(int stackSize) { if(stackSize <=0) {cout<<"Size of the array must be postitive"; maxStackSize = 100;} else maxStackSize = stackSIze; stackTop = 0; list = new T[maxStackSize];}

How to initialize stack array?

template <class T> void stackType<T>::initializeStack(){ stackTop = 0;}

How to write a copy constructor?

template <class Type> LinkedStack<Type>::LinekdStack(const LinkedStack<Type>& otherStack) {stackTop = NULL; copyStack(otherStack);}

write a linked stack construcutor?

template <class Type> LinkedStack<Type>::LinkedStack() {stackTop = NULL;}

How to writ the top function?

template <class Type> Type LinkedStack<Type>::top() const { assert(stackTop != NULL); return stackTop->info;}

How to see if linked stack if full?

template <class Type> bool LinkedStack::isFullStack() {return false;}

How to see if linked stack is empty?

template <class Type> bool LinkedStack<Type>::isEmptyStack() const {return(stackTop == NULL);}

What is a stack ADT(Abstract class)?

template <class Type> class StackADT { public: virtual bool isEmptyStack() const = 0; virtual bool isFullStack) const = 0; virtual void initalizeStack() const = 0; push(const T& newItem) = 0; virtual T top() const = 0; virtual void pop() = 0; };

How to see if linked stack is initialized?

template <class Type> void LinkedStack::initializedStack() { nodeType *temp while(stackTop != NULL) {temp = stackTop; stackTop = stackTop->next; delete temp;}

How to write a copy linked stack function?

template <class Type> void LinkedStack<Type>::copyStack (const LinkedStack<Type>& otherStack) { nodeType *newNode, *current, *last; if(stackTop != NULL) initializeStack(); if(otherStack.stackTop == NULL) stackTop = NULL; else { current = otherstack.stacTop; stackTop = new nodeType; stackTop->info = current->info; stackTop->next = NULL; last = stackTop; current = current->next; while(current != NULL) {newNode = new nodeType; newNode->info = current->info; last->next = newNode; last = newNode; current = current->next;}}}

How to pop a linked stack?

template <class Type> void LinkedStack<Type>::push(const Type& newE) { nodeType *temp; if(stackTop != NULL) { temp = stackTop; stackTop = stackTop->next; delete temp;} else cout<<"Cannot remove from an empty stack."<<endl;}

How to push a linked stack?

template <class Type> void linkedStack<Type>::push(const Type& newE) { nodeType *newNode; newNode = new nodeType; newNode->info = newE; newNode-> stackTop; stackTop = newNode;}

How to add to array-queue?

template <class Type> void queueType::addQueue(const Type& newElement) { if (!isFullQueue()) { queueRear = (queueRear + 1) % maxQueueSize; count++; list[queueRear] = newElement; } else cout << "Cannot add to a full queue." << endl; }

How to delete from array-queue?

template <class Type> void queueType::deleteQueue() { if (!isEmptyQueue()) { count--; queueFront = (queueFront + 1) % maxQueueSize;} else cout << "Cannot remove from an empty queue" << endl; }

How to write constructor Linked Queue?

template <class Type> LinkedQueue::LinkedQueue() { queueFront = NULL; //set front to null queueRear = NULL ; //set rear to null }

How to do a back function Linked queue?

template <class Type> LinkedQueue::back() const { assert(queueRear!= NULL); return queueRear ->info; }

How to check if linked queue is empty?

template <class Type> bool LinkedQueue::isEmptyQueue() const { return (queueFront == NULL); }

How to overload operator linked stack?

template <class Type> const stackType<T>::operator=(const stackType& otherStack) { if (this != &otherStack) //avoid self-copy copyStack(otherStack); return *this; }

How to do a back function array-queue?

template <class Type> queueType::back() const { assert(!isEmptyQueue()); return list[queueRear]; }

How to write constructor array- Queue?

template <class Type> queueType::queueType(int queueSize) { if (queueSize <= 0) maxQueueSize = 100; } else maxQueueSize = queueSize; queueFront = 0; queueRear = maxQueueSize - 1;count = 0; list = new Type[maxQueueSize]; }

How to initialize Linked Queue?

template <class Type> void LinkedQueue::initializeQueue() { nodeType *temp; while(queueFront!= NULL) { temp = queueFront; queueFront = queueFront->next; delete temp; occupied by temp } queueRear = NULL;}

How to write deconstructor Linked Stack?

template LinkedStack::~LinkedStack() { initializeStack(); }

How to do front function Linked queue?

template Type LinkedQueue::front() const { assert(queueFront != NULL); return queueFront ->info; } //end front

How to do front function array-queue?

template Type queueType::front() const { assert(!isEmptyQueue()); return list[queueFront]; }

How to check if array-queue is empty?

template bool queueType::isEmptyQueue() const { return (count == 0); }

How to check if array-queue is full?

template bool queueType::isFullQueue() const { return (count == maxQueueSize);}

How to implement a linked list stack?

template class LinkedStack: public stackADT { private: struct nodeType { Type info; nodeType *next; }; nodeType *stackTop; //pointer to the stack (head) void copyStack(const LinkedStack& otherStack);

What are the structure and operations of a queue

template class queueADT { public: virtual bool isEmptyQueue() const = 0; //Returns true if queue is empty virtual bool isFullQueue() const = 0; //Returns true if the queue is full virtual void initializeQueue() = 0; //Function to initialize the queue to an empty state. virtual Type front() const = 0; //Function to return the first element of the queue. virtual Type back() const = 0; //Function to return the last element of the queue. virtual void addQueue(const Type& queueElement) = 0; //Function to add queueElement to the queue. virtual void deleteQueue() = 0; //Function to remove the first element of the queue. };

What is a Queue?

template class queueType: public queueADT { private: int maxQueueSize; //to store the maximum queue size int count; //number of elements in the queue int queueFront; //points to the first element of the queue int queueRear; //points to the last element of the queue Type *list; //pointer to the array that holds the queue elements public: bool isEmptyQueue() const; bool isFullQueue() const; void initializeQueue(); Type front() const; Type back() const; void addQueue(const Type& queueElement); void deleteQueue(); queueType(int queueSize = 100); queueType(const queueType& otherQueue); ~queueType(); const queueType& operator=(const queueType&); };

How to implement an array-based stack?

template stackType class stackType: public stackADT { private: int maxStackSize; //variable to store the maximum stack size int stackTop; //variable to point to the top of the stack T *list; //pointer to the array that holds the stack elements void copyStack(const stackType& otherStack); public: stackType(int stackSize = 100); void initializeStack(); bool isEmptyStack() const; void push(const T& newItem); T top() const; void pop(); void copyStack(const stackType<T>& otherStack) stackType<T>::stackType(const stackType<T>& otherStack) const operator=(const stackType<T>& otherStack)};

How to add to Linked queue?

template void LinkedQueue::addQueue(const Type& newElement) { nodeType *newNode; newNode = new nodeType; newNode->info = newElement; newNode->next = NULL; if (queueFront == NULL) { queueFront = newNode; queueRear = newNode; } else { queueRear->next = newNode; queueRear = newNode; } }

How to delete from Linked queue?

template void LinkedQueue::deleteQueue() { nodeType *temp; if (!isEmptyQueue()) { temp = queueFront; queueFront = queueFront->next; delete temp; if (queueFront == NULL) queueRear = NULL; } else cerr << "Cannot remove from an empty queue" << endl; }

How to initialize array-Queue?

template void queueType::initializeQueue() { queueFront = 0; queueRear = maxQueueSize - 1; count = 0; }

What are operation of the stack?

▷initializeStack—Initializes the stack to an empty state. (Also, reinitializes) ▷ isEmptyStack—Returns True if stack is empty ▷ isFullStack—Returns True if stack is full ▷ push—Adds a new element to the top of the stack. ▷ top—Returns(retrieves) the top element of the stack. ▷ pop—Removes the top element of the stack.


Related study sets

Western Civilization 1 CLEP: Kingdoms

View Set

exam 2 2107 ch 6 mindful listening

View Set

When ya code on paper and can't look at Stack Overflow... [PYTHON EDITION]

View Set

Econ Macro principles final practice

View Set

AP English Midterms (LOTF and Rhetorical Devices)

View Set

DATABASES: IS 3100 (chapter four)

View Set

¿Quiéne es?/¿Quienes son? ¿De dónde es?/¿De dónde son?

View Set

Deleon Chemistry- Chapter 7 test answers

View Set