You are on page 1of 4

CS221 Data Structures

Assignment 03: Stacks and Queues.


Due : 23h55 Sunday 18 April, 2021.
FCSE - GIKI
Fall 2021

1 Objectives
(a) Test the student’s ability to implement stacks and queues.
(b) Test the student’s ability to follow written instructions.

2 Description
You will be using the C programming language to do this assignment. You will need to use C
functions related to dynamic memory allocation, taking input from user, and using the C library
built-in functions for File IO.

3 Task 1
T1. Two stacks in an array.
In class you have studied how to implement a stack using an array; the array contained only
one stack. In this implementations you will use the array to store two different stacks in the same
array: the odd indexes of the array will contain values for one stack and the even indexes
will contain element of the second stack. This way the same array will contain elements of both
the stacks:

• your stack structure will contain only one array.


• in your stack structure you will need to maintain two top variables, one for each stack.
• you will make functions push1() and pop1() to push and pop values from one stack.
• you will make functions push1() and pop2() to push and pop values from the second stack.

• both stacks, even though residing in the same array, will function independently; in each
push() and pop() function you will implement whether that particular stack is full or empty
respectively.
• you will make functions print1() and print2() to print the first and second stack respectively.

1
3.1 Input
Your program will receive an input file as a command line argument. That input file will contain
the commands to push and pop values from a stack in the following format:

• the first line will always contain a positive integer telling you how many commands are to
follow (let’s call it N)
• the next N lines will contain command in the following format: CMD STACK ID OP-
TIONAL VALUE

– CMD will be an integer: 1 stands for push(), 2 for pop(), and 3 for print()
– STACK ID will be an integer: 1 would mean that the command applies to stack 1
and 2 will mean that this command is for stack 2.
– OPTIONAL VALUE will only be present for push commands (CMD==1) and will
be an integer denoting the value to be pushed. For pop and print commands this
value will not exist.
You program shall open the file whose path the user has passed via the command line and
read its contents. It should read the first integer whose value will tell your program the number
of commands to follow.

Next your program should read each command and call the appropriate function to perform
the action demanded by that command.

All the work should happen in separate functions. The main function should only read file
input and call these functions.

4 Task 2
T2. Queue using a singly-linked list.
Implement a queue using a singly-linked list. You should implement:
• the enqueue() function to insert an element into the queue.
• the dequeue() function to remove an element from the queue.

• the printq() function to print all the values in the queue in a single line separated by spaces.

4.1 Input
Your program will receive an input file as a command line argument. That input file will contain
the commands to enqueue and dequeue values from the queue in the following format:

• the first line will always contain a positive integer telling you how many commands are to
follow (let’s call it N)

• the next N lines will contain command in the following format: CMD OPTIONAL VALUE
– CMD will be an integer: 1 stands for enqueue(), 2 for dequeue(), and 3 for print()
– OPTIONAL VALUE will only be present for enqueue() commands (CMD==1) and
will be an integer denoting the value to be inserted in the queue. For dequeue and
print commands this value will not exist.

2
5 Error Checking and Clean Code instructions
It is extremely important that your program handles errors. You shall always insert code for error
checking where ever there is a possibility of things going wrong. In case of any errors, the pro-
gram should output a helpful error message. All error messages should be displayed on stderr. 1

Things that can go wrong may include, but are not limited to, :
• invalid number of command line arguments
• file path is not valid, i.e., file does not exist
• being passed a -ve value where a +ve one is expected

• failure to allocate memory


• failure to open(), read() file
You would lose marks if your program crashes during use.

It is the programmer’s responsibility to free any system resources i.e. memory , file de-
scriptors, etc., they have acquired from the system. Your program should always free system
resources once it is done using them.

Code should be properly indented, readable and commented.

You should always your compile your code using the -Wall option to check for warnings.
Your program should compile without any warnings.

If your program does not compile you will get a 0.

If your program has memory leaks, you’ll lose half the marks in that task.

6 Submission Instructions
1. You submission should consist of .c files only.
2. Each of you will submit two .c files which you shall name as u2019xxx a3 t1.c and u2019xxx a3 t2.c
where xxx are the last three digits of your registration number.
3. You will submit on CMS. Failure to submit on CMS would make you lose marks (50%).
Get in touch with your advisor to register yourself on CMS.

4. Missing submission deadline on will cost you marks (50%). Submissions received more
than 24 hours after submission deadline will get a 0.

7 Rubric
This is an individual assignment. Any form of collaboration, cheating, plagiarism will get
you a 0. Giving your code to somebody else, even if it is for their understanding only, is not
allowed. You may be called for a viva; if you are unable to explain any line of the submitted
code, you’ll get a 0.

1 You might also want to read about the use of errno, perror(), and strerror().

3
Any form of plagiarism or collusion will get you at least a 0 in the assignment and, poten-
tially, an F in the course.

To discourage plagiarism and encourage academic honesty, if you’ve been unable to do any
thing you can submit a program saying Hello World before the deadline by following submission
instructions (name your file u2019xxx a3 hw.c), and get the submission marks. This way you
are sure to get at least 25% of the marks.

Category Marks
Followed submission insns 10 marks
Code was readable +
Compiled without warnings +
Does not crash +
Program handles errors well 10 marks
T1 working properly 20 marks
T2 working properly 20 marks
Total 60 marks

You might also like