25-Comp-A4 Program Design and Data Structures · December 2017
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Paper format. 98-Comp-A4 Program Design and Data Structures, December 2017 — 3 hours, closed book, no calculator permitted. Nine questions of equal weight (20 marks each: 1 and 7 split as (a) 10 + (b) 10, 8 split as (a) 15 + (b) 5); candidates answer any six, so a complete paper is 120 marks. Pseudocode or any high-level language is accepted, and the examiner's note states explicitly that marking emphasises the operation of the program, not syntactic details. All nine questions are answered below, because the whole set is the more useful revision resource. Answers are given in C or C++ as the question dictates; each is compilable as written (or corrected where the printed paper itself has a slip), but a clear, correctly reasoned pseudocode answer would earn the same marks.
Reference texts for this subject.
The Computer Engineering citation list is built around architecture and networking texts (Patterson & Hennessy, Tanenbaum, Mano); this subject is programming and data structures, so the works above are cited instead.
Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.
Given. (a) The ELEMENT doubly-linked-node
type (data/prev/next) and the function signature
void del_dupl(ELEMENT *head). (b) A stack module interface
(make_empty, is_empty, push,
pop) over a linked list of nodes with a single top
pointer.
Find. (a) A function removing every later occurrence of a value already seen earlier in the list, safe on an empty list. (b) A complete linked-list-backed implementation of the four stack operations.
Approach. (a) For each node in turn, scan the remainder
of the list and unlink (relink prev/next, free) every later node whose
data matches it, so only the first occurrence of each value
survives. (b) Push/pop only ever touch the single top pointer,
so a classic singly-linked "insert/remove at head" pattern gives $O(1)$ push
and pop.
del_dupl on a doubly linked list (10 marks)p from head onward, walk every node q
after p; whenever q->data == p->data, unlink
q by pointing q->prev->next at
q->next and (if it exists) q->next->prev
at q->prev, then free q and continue from the
node that used to follow it.#include <stdlib.h>
typedef struct element {
int data;
struct element *prev;
struct element *next;
} ELEMENT;
/* delete duplicate valued elements in the list pointed to by head */
void del_dupl(ELEMENT *head)
{
ELEMENT *p, *q, *next_q;
if (head == NULL) return; /* empty list: nothing to do */
for (p = head; p != NULL; p = p->next) {
q = p->next;
while (q != NULL) {
next_q = q->next;
if (q->data == p->data) {
q->prev->next = q->next;
if (q->next != NULL)
q->next->prev = q->prev;
free(q);
}
q = next_q;
}
}
}
del_dupl(NULL) returns immediately via the guard
clause, satisfying the "must work correctly for empty lists" requirement.
$$\boxed{3,1,2,3,1 \ \longrightarrow\ 3,1,2}$$typedef struct { int data; node *next; } node; does
not compile — node is used as a member type before the
typedef that introduces the name has completed, so the type
must be given a tag (struct node) and referenced by that tag
inside itself. The header guard's closing #end if is also a slip for #endif (a two-word "#end if" is not
valid C preprocessor syntax). Both are flagged and corrected below.stack.h / stack.c).
/* stack.h */
#ifndef STACK_H
#define STACK_H
void make_empty(void);
int is_empty(void);
void push(int i);
int pop(void);
#endif
/* stack.c */
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
struct node {
int data;
struct node *next;
};
static struct node *top = NULL;
void make_empty(void)
{
while (!is_empty()) pop(); /* free any existing nodes */
}
int is_empty(void)
{
return top == NULL;
}
void push(int i)
{
struct node *n = malloc(sizeof(struct node));
n->data = i;
n->next = top;
top = n;
}
int pop(void)
{
struct node *old_top = top;
int val = old_top->data; /* caller must ensure !is_empty() first */
top = old_top->next;
free(old_top);
return val;
}
push(5) → top=5; push(3) → top=3, then 5;
pop() returns 3 (top=5); push(7) → top=7, then
5; pop() returns 7; pop() returns 5;
is_empty() now returns true — matching LIFO order
throughout.
$$\boxed{\text{push }5,3\ \Rightarrow\ \text{pop}=3;\ \text{push }7\ \Rightarrow\ \text{pop}=7;\ \text{pop}=5}$$| Case | Result |
|---|---|
del_dupl on $3,1,2,3,1$ | $3,1,2$ |
del_dupl(NULL) | returns immediately, no crash |
| push(5), push(3), push(7), then pop three times | 7, 3, 5 (LIFO) |
both are corrected above using
a tagged struct node and a proper #endif.