Assignment 1 BS (CS)
Assignment 1 BS (CS)
Lahore Campus
Course: Data Structures Course CS 218
Code:
Program: BS (CS) Semester: Fall 2023
Due Date: Total Marks: 250
Section: 3E,3F Page(s): 13
Type: Assignment 1
Important Instructions:
Submit separate .cpp files for each question. The naming format of each file
Should be: 22L-RollNo._Q_No.cpp i.e. 22L-XXX_Q_X.cpp. The files violating the
Format won’t be considered for grading. DO NOT COMMENT THE CODE.
Commented codes will be marked 0. Do not submit .zip files. Late Submissions won’t
be accepted.
Given a singly linked list and a positive integer k, write a function to rearrange
the nodes in the list such that the nodes are grouped by k nodes into sub-lists.
Within each sub-list, the nodes should be in their original relative order.
Example:
Input:
Output:
If the list cannot be divided exactly on k, the last sub-list will be of the
remaining nodes that are less than k.
Note: The output should be a linked list of linked lists, where each sub-list is
represented as a separate linked list node.
Question # 2: [50 Marks]
Luck is the second most important thing to reach what you’re looking for.
Hard work and following the clues is the first. Your task is to reach the elite
node in the network of linked lists.
int data;
node* up;
node* down;
node* left;
node* right;
DA
TA
12,11,10
1,2,3
6,8,9
You wil start from the top left corner node and reach the next node using the
data as the Clue.
Decode the clue as following:
Elite Node: The node whose clue will bring you to itself will be the elite node.
Functionalities:
Read(string filename)
12,34,56,78,90
44,76,34,87,99
88,65,12,19,50
This data shows that the maze will consist of 3 rows and 5
columns.
Print(node* head)
ClueRow(int data)
ClueColumn(int data)
EliteNode(node* elite)
Input:
Output: 3401->18->7017->2->118->101
Elite Node = 101
Traverse the maze (left / right / top / bottom) to reach the next node.
Question # 3: [100 Marks]
In a computer system the main memory (RAM) is a shared resource that all running
programs share. The operating systems serves as a resource manager. The main job of the
operating system for memory management includes: allocation of free memory to the
programs whenever they request it, reclaim the memory of the programs after finishing
their execution and keeping record of free and allocated memory.
Programs can claim memory at the start of the program and during its execution. So the
memory reserved by one program may not be contiguous. Similarly, due to dynamic nature
of the programs, new programs are added to the system and existing programs are removed
after finishing their execution. This may cause memory fragmentation. As processes are
loaded and removed from memory, the free memory space is broken into little pieces
is called fragmentation. In the figure below shaded areas are free memory scattered in the
main memory.
Whenever a program needs memory, the memory manger asks the total number of bytes
required to store the data. The memory manager finds a chunk of contiguous memory in
RAM and allocate that memory to the program. If the program asking memory is a new
program a new program is added to the list of current programs. Otherwise the information
of the block of bytes allocated to the existing program is kept with the program information.
A block maintains the Id of starting byte and total number of bytes. So a program in a
memory management system has a collection of blocks where each block maintains the
address of next block of the program and program stores the address of first block. In order
to make the whole process efficient, the memory management system maintains the pool
of available blocks instead of available bytes.
Your task is to simulate the memory management system. The Memory management system
includes: RAM, list of available blocks (pool) and list of Programs currently in execution.
We define the RAM as a one-dimensional array of bytes of size numOfBytes. A pool of
available blocks is kept in a linked list of block. A block has two fields: start_byte_ID, and
total_bytes. Programs are kept in a linked list Progs where each node has a program as data.
Each program has an ID, and a linked list of blocks allocated to that program. Asking
memory requires claiming a sufficient number of bytes from pool, if available and update
the pool accordingly. Removal of a program requires removing the list of blocks allocated to
the program and transferring the bytes assigned to this program back to pool.
Suppose we have a RAM of size 100KB. This means we will have byte ID’s from 0 to
102400-1.
Figure 1 shows a link list of blocks called pool. The first available block is of 30 bytes
starting from byte ID 420 and last block is of 9900 bytes starting at byte ID 4000. Figure 2
shows a sample link list of Programs. There are 3 programs in the memory. The Id of
program1 is P1 currently has acquired 260 bytes. It also tells that the data of the program is
stored in 3 blocks. First block starts at byte ID 0 and has 40 bytes, second block has 20 bytes
that starts from byte ID 70 and last block also has 200 bytes starting from byte ID 120.
In order to implement the memory management system you need to implement the
following classes:
Data members:
data
next
Data member:
head
tail
size
Class of block
Data member:
start_byte_ID
total_bytes
Class of program
Data members:
Id
Size (memory)
Link list of blocks
Class MemoryManagementSystem
Data members:
pool (Sorted Linked list of block)
Progs (Linked list of Programs)
sizeOfMemory
strategy (a Boolean variable)
Member functions:
GetMem: The GetMem function must take program Id and size of the memory required.
as parameters and should work as follows:
Find the memory of required size from the pool of free memory using strategy s (will be
explained later). If the requested memory is not available then return false. Otherwise, Id
is searched in the list of programs. If the program Id already exists then a new block of
memory is removed from the pool of free memory and added to the existing program at the
end of the program linked list of blocks. If no such program already exists then first a new
program is inserted at the end of Progs and then this block is added to the list of blocks of
the newly inserted program.
The memory management system implements one of the two strategies:
For example a new program P4 requests a block of 200 size. According to the first fit
strategy, the memory will be allocated starting at 980 byte. This block has 400 bytes. The
first 200 bytes will be allocated to P4 and remaining 200 resides in the pool. The updated
pool and Progs list is shown the figure 3 and figure 4.
This function must take program Id as parameter and delete the program with the given Id
from Progs. Also move all the blocks of the memory to pool in sorted order. If two
consecutive block become one single contiguous block then you must also merge them into
one single block. For example if program P3 is deleted then the block starting at 450 must
be merged with the block in pool starting at byte position 420 and so on. The updated pool
will be as follows
Constructor:
Initially Progs is empty and pool has only one block. SizeofMemory and Strategy will be
initialized to user provided values and will remain the same throughout the program.
Provide a driver function that creates a memory management system object and a menu
that prompts the user to perform different operations of the memory management.
Question # 4: [80 Marks]
You all may have noticed the digits displayed on calculators and digital clocks. Every digit
is derived from the digit ‘8’.
Consider that every point where two or more lines meet in a node. The structure of a single
digit will be as following where each black dots represents a node.
Each node has 4 pointers (node* left, node* right, node* up, node* down). Any digit can be
written using these 6 nodes, by establishing essential links.
Sample Input:
16:26
Output:
Internal Structure:
You need to make sure that all the pointeres are properly handled. Every pointor that is not
pointing to any of the node should be pointing to null.
FUNCTIONAL IMPLEMENTATIONS:
1. Addition of Minutes
The function should take the minutes input from the user, add it in
the time and print the time after addition of respective minutes.
NOTE: Addition of minutes may affect hours.
2. Subtraction of Minutes
The function should take the minutes input from the user, subtract
it from the time and print the time after subtaction of respective
minutes.
NOTE: subtraction of minutes may affect hours.
3. Addition of Hours
The function should take the hours input from the user, add it in
the time and print the time after addition of respective hours.
4. Subtraction of Hours
The function should take the hours input from the user, subtract it
from the time and print the time after subtaction of respective
hours.
6. Change of Date
The program should display a date changed message if any of the
functionalities above, when performed on time, results in the
changing of date.
Departmentof Computer Science, FASTSchoolof Computing FAST-NU, Lahore Page
14