COMP1020 Summer 2018 Exam 1- Computing II Name: 3. STACK A) (12 points) You are
ID: 3904059 • Letter: C
Question
COMP1020 Summer 2018 Exam 1- Computing II Name: 3. STACK A) (12 points) You are given the following requirements for a stack abstract data type: a. It must be able to set up enough memory to accommodate the stack b. It must be possible to make a stack empty c. It must be possible to push a given element on to a stack. d. It must be possible to pop the topmost element from a stack. e. It must be possible to free all the memory used by the stack Write a contract for a stack abstract data type. Express your contract in the form of an application programming interface (what we called API in class) using an opaque object, with a comment specifying the expected behavior of each function. Note: You shall need to specify the opaque object before using it. B) (3 points) Briefly describe a possible representation for a stackExplanation / Answer
class Stack{
private:
int *data;
int max;
int len;
public:
Stack(int a){
data = new int[a];
len = 0;
max = a;
}
void empty(){
len = 0;
}
void push(int a){
if (len < max){
data[len] = a;
len++;
}
else {
cout << "Stack is full ";
}
}
void pop(){
if (len > 0){
int a = data[len-1];
len--;
return a;
}
else {
cout << "Stack is empty ";
}
}
void destroy(){
len = 0;
max = 0;
delete [] data;
}
}
QNode *MakeNode(int k){
QNode *q = (QNode *)malloc(sizeof(QNode));
q->data = k;
q->next = NULL;
return q;
}
Status enqueue(Queue q,int k){
QNode *m = MakeNode(k);
if (lnode == NULL){
lnode = m;
rnode = m;
}
else {
rnode.next = m;
rnode = m;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.