QUESTION 1 (a) Given an array called A provide pseudocode to insert an d by the
ID: 3873995 • Letter: Q
Question
QUESTION 1 (a) Given an array called A provide pseudocode to insert an d by the element x in location loc. Is this operations Stack ADT? Justify your answer. (4 marks) (b) Given the Stack: 01234 where 0 represents an empty index and not a value. Write the output produced by the following series of operations. push(3), push(5), push(4), pop), push(2), top0, popO, isEmptyO, push(1), push(6), top0, push(7), isFullO. (6 marks) (c) Povide pseudocode for the following operations pertaining to a Stack: isFull(S), isEmpty(S), push(S. x), pop(S). S is an array and x is an element. Why does each operation except push(S, x) only require the single argument S? (15 marks) Total (25 marks)Explanation / Answer
(a) let n be the length of array, we need to shift elements before we put a new element at loc
for(int i = n;i<loc;i--)
A[i+1] = A[i];
A[loc] = x;
No, such kind of operation is not supported, we cannot insert element directly at some location
(b) 4
5
3
pop() : will give us 4
2
5
3
top() will give us 2 but 2 will not be popped
pop() : will give us 2
isEmpty() : false
6
1
5
3
top() will give us 6 but 6 will not be popped
isFull() : true
7
6
1
5
3
(c)
int isempty() {
if(top == -1)
return 1;
else
return 0;
}
int isfull() {
if(top == MAX_LENGTH)
return 1;
else
return 0;
}
int pop() {
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}else {
printf("Could not retrieve data, Stack is empty. ");
}
}
int push(int data) {
if(!isfull()) {
top = top + 1;
printf("push %d, top is %d ", data, top);
stack[top] = data;
//printf("push stack[top] = data, top is %d ", top);
}else {
printf("Could not insert data, Stack is full, top is %d. ", top);
}
}
BECAUSE We dont need any other as top is a global variable and that will determine whether stack is full, empty, or which element we need to pop.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.