April 2000
CA208 : 'C' PROGRAMMING

QUESTION 3

Total Marks: 15 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 3

You are to implement a stack which can be used for job control.When a job is created by the system,a stack element is created which is used to store the details of the job.Jobs are always added to and removed from the top of the stack,thereby making it a first in last out data structure,and each element added to the stack also contains a ointer which is used to point to the next element down the stack.
Consider the following type definition of a Stack in C,and the definition of elements which can be placed on and removed from the stack:
typedef struct {
  stack_element* top_of_stack;
} Stack;
typedef struct node {
  node* next;
  Job JobInfo;
} stack_element;

(a)Define a suitable enumerated type,called JobType which can take on any of the values SystemJob ClientJob or ServerJob [1mark ]

(b)Give a definition of the structure Job which contains the members type which is a JobType and data which is a pointer with an undefined type.[3 marks ]

(c)Define a function,IsEmpty the signature of which is given below,which takes a stack s and returns 1 if the stack is empty,and zero otherwise.A stack is defined to be empty if there are no elements on the stack.
int IsEmpty(const stack* s); [2 marks ]

(d)Define a procedure Push the signature of which is given below,which takes a reference to a stack s and a reference to a stack element e and will add the element to the top of the stack.Your procedure should ensure that both the stack pointer and the reference to the next item in the stack are updated,should it be necessary.
void Push(stack* s, stack element* e); [4 marks ]

(e)Define a procedure Pop the signature of which is given below,which takes a reference to a stack s anda stack element e The procedure should remove the to element from the stack,returning a reference to the element removed,and should return the number of items successfully removed from the stack.
int Pop(stack* s, stack element* e); [5 marks ]