December
1998 QUESTION 5 Total Marks: 20 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS
|
(a) | Give the following definition of a linear
linked-list type as the representation of the elements of a stack, typedef struct stack_element { typedef struct { The following questions require the definition of a series of functions that form an interface to a stack. You can assume that the stack argumnet to each of the functions is a non-null pointer, and points to a valid StackType.
|
[2] |
(a) | Write a function isempty, that has
the function prototype shown below, that returns a one if stack is empt, and zero
otherwise. int isempty(StackType *stack);
|
[2] |
A definition
of isempty follows: int
isempty(StackType *stack) { and the following marking scheme should be used:
|
||
(b) | Write a procedure push, that has the
procedure prototype shown below, that pushes the element x x onto the stack stack. You
assume that stack will always be a non-null pointer to a valid StackType. void push(StackType *stack, int x);
|
[4] |
A definition
of push follows: void
push(StackType *stack, int x) { temp = (StackElement*)
and the following marking scheme should be used:
|
||
(c) | Write a function pop, that has the
function prototype shown below, that returns and then removes the top element from the
stack stack. You should call the functino error (with prototype below), if it is not
possible to remove an element from the stack. int
pop(StackType *stack);
|
[5] |
A definition
of pop follows: int
pop(StackType *stack) { and the following marking scheme should be used:
|
||
(d) | Write a function concatenate, that
has the function prototype shown below, that joins the two stacks left and right together.
i.e., if left contained the sequence [1,2,3] and right [4,5], then the function should
alter left so that it represents the sequence [1,2,3,4,5]. void concatenate(StackType *left, StackType *right)
|
[5] |
A definition
of concatenate follows: void
concatenate(StackType *left, StackType *right) {
ptr=left->top_of_stack; and the following marking scheme should be used:
|
||
(e) | Using the function pop and isempty
defined below, define a procedure popmany, that has the functino prototype shown below,
that removes the top n items from the stack, if the stack contains at least n items;
otherwise all items are removed from the stack. void popmany(StackType *stack, int n)
|
[4] |
Two possible
definitions of popmany follows: void popmany(StackType *stack, int n) { void popmany(StackType *stack, int
n) { for(i=0;i<n;i++) and the following marking scheme should be used:
|