MARKING GUIDE
COM204: COMPUTER DATA STRUCTURES
*1:a
Concept of Data Structure and Implementation*
A data structure is a way to organize, store, and manage data in a computer so that it can be efficiently
accessed, modified, and manipulated. Implementation involves using programming languages to create
and manipulate data structures.
*1b
Common Types of Data Structures*
Common data structures include:
1. Arrays
2. Linked Lists
3. Stacks
4. Queues
5. Trees
6. Graphs
*1c
Explanation of an Array*
An array is a collection of elements of the same data type stored in contiguous memory locations. Each
element is identified by an index or subscript, allowing for efficient access and manipulation.
* 1d:
Explanation of a Linked List*
A linked list is a dynamic collection of elements, where each element points to the next element. This
allows for efficient insertion and deletion of elements at any position in the list.
* 1e:
Designing a Linked List for COSC Departments*
Here's a simple design for a linked list representing the departments in the Computer Science (COSC)
faculty at the University of Ibadan:
```
class Node:
def __init__(self, department):
self.department = department
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, department):
new_node = Node(department)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
# Create a linked list
departments = LinkedList()
# Add departments
departments.append("Artificial Intelligence")
departments.append("Data Science")
departments.append("Software Engineering")
departments.append("Computer Networks")
departments.append("Database Systems")
# Print departments
current = departments.head
while current:
print(current.department)
current = current.next
```
* 2a:
Definition of a Stack with Implementation and Programming in C*
A stack is a Last-In-First-Out (LIFO) data structure, where elements are added and removed from the
top.
Here's an example implementation of a stack in C:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct StackNode {
int data;
struct StackNode* next;
} StackNode;
typedef struct Stack {
StackNode* top;
} Stack;
void push(Stack* stack, int data) {
StackNode* new_node = (StackNode*) malloc(sizeof(StackNode));
new_node->data = data;
new_node->next = stack->top;
stack->top = new_node;
}
int pop(Stack* stack) {
if (stack->top == NULL) {
printf("Stack is empty\n");
return -1;
int data = stack->top->data;
StackNode* temp = stack->top;
stack->top = stack->top->next;
free(temp);
return data;
int main() {
Stack stack;
stack.top = NULL;
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("Popped element: %d\n", pop(&stack));
printf("Popped element: %d\n", pop(&stack));
printf("Popped element: %d\n", pop(&stack));
return 0;
}
```
*2b:
Definition of a Queue without Examples*
A queue is a First-In-First-Out (FIFO) data structure, where elements are added to the end and removed
from the front.
*Step 8: Example of a Tree*
A tree is a hierarchical data structure, where each node has a value and zero or more child nodes.
Example:
``` 1
/|\
2 3 4
```
5 6
What is a Graph?*
A graph is a non-linear data structure consisting of nodes or vertices connected by edges. Each node
may have multiple edges connected to it, and each edge may have a weight or label associated with it.
Graphs are useful for modeling relationships between objects, such as social networks, traffic patterns,
or molecular structures.
3b.
What is a Hash Table?*
A hash table, also known as a hash map, is a data structure that stores key-value pairs in an array using a
hash function to map keys to indices of the array. Hash tables allow for efficient lookup, insertion, and
deletion of elements, with an average time complexity of O(1). They are commonly used for caching,
indexing, and storing configuration data.
4a.
Difference Between a Stack and a Queue*
A stack is a Last-In-First-Out (LIFO) data structure, where elements are added and removed from the
top. A queue, on the other hand, is a First-In-First-Out (FIFO) data structure, where elements are added
to the end and removed from the front. The key difference lies in the order in which elements are
accessed and removed.
4b.
Advantage of Using a Linked List Over an Array*
Linked lists offer several advantages over arrays, including:
- Dynamic memory allocation: Linked lists can grow or shrink dynamically as elements are added or
removed.
- Efficient insertion and deletion: Linked lists allow for efficient insertion and deletion of elements at any
position, without shifting all subsequent elements.
- Flexible data structure: Linked lists can be used to implement various data structures, such as stacks,
queues, and trees.
5:
Purpose of a Hash Function in a Hash Table*
The primary purpose of a hash function in a hash table is to map keys to indices of the underlying array.
A good hash function should have the following properties:
- Deterministic: Given a key, the hash function should always return the same index.
- Non-injective: Different keys should map to different indices.
- Fixed output size: The hash function should produce a fixed-size output, regardless of the input size.
Tree Structure of COSC and Department of Computer Science*
Here's a simple tree structure:
COSC (College of Science)
|-- Department of Computer Science
| |
| |-- Artificial Intelligence
| |-- Data Science
| |-- Software Engineering
| |-- Computer Networks
| |-- Database Systems
|-- Department of Mathematics
|
|-- Department of Statistics
This tree structure represents the College of Science (COSC) with its various departments, including the
Department of Computer Science and its research areas.