PDSlab 2019S Sec6 Asgn09

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Programming and Data Structures Laboratory, 2018-19 Spring semester, Section 6

April 2, 2019: Tutorial and Assignment 9 (Linked lists, Stack, Files)

Tutorial

Learn about GDB Debugger (links to some tutorials given on course webpage)

Assignment (for evaluation – write on machine and submit to Moodle before end of class)

1. [20 marks] Recall Assignment 6 where you maintained a dynamic set of integers using an array.
Now implement the same dynamic set of integers using a sorted linked list. The program should
start with an empty linked list. The user should be given options to (i) insert a number into the set,
(ii) delete a number from the set, (iii) see the elements in the set, and (iv) exit. When the user wants
to insert a number, the program should check whether the number entered already exists in the set.
If yes, the program should inform the user; otherwise the element should be inserted into the set.
Again, when the user wants to delete a number, the program should check whether the number
exists in the set. If not, the user should be informed; otherwise the said number should be deleted
from the set. Throughout the operations, the linked list should be maintained sorted in ascending
order of the integers. While searching for a number n, the search should terminate if a number
larger than n is encountered in the sorted linked list.
An example interaction between the user (U) and the program (P):
P: Press 1 to insert numbers, 2 to delete numbers, 3 to display the set, and 0 to exit.
U: 1
P: Enter number to insert:
U: 43
P: 43 inserted [Note: 43 should be placed in the first node of the linked list]
P: Press 1 to insert numbers, 2 to delete numbers, 3 to display the set, and 0 to exit.
U: 1
P: Enter number to insert:
U: 21
P: 21 inserted [Note: 21 should be placed in the first node of the linked list, and 43 should be shifted
to the second node.]
P: Press 1 to insert numbers, 2 to delete numbers, 3 to display the set, and 0 to exit.
U: 1
P: Enter number to insert:
U: 73
P: 73 inserted [Note: 73 should be placed in the third node of the linked list]
P: Press 1 to insert numbers, 2 to delete numbers, 3 to display the set, and 0 to exit.
U: 1
P: Enter number to insert:
U: 43
P: Number already exists
P: Press 1 to insert numbers, 2 to delete numbers, 3 to display the set, and 0 to exit.
U: 3
P: {21, 43, 73}
P: Press 1 to insert numbers, 2 to delete numbers, 3 to display the set, and 0 to exit.
U: 2
P: Enter number to delete
U: 43
P: 43 deleted [Note: 73 should become the second node of the linked list]
P: Press 1 to insert numbers, 2 to delete numbers, 3 to display the set, and 0 to exit.
U: 2
P: Enter number to delete
U: 50
P: This number does not exist in the set
P: Press 1 to insert numbers, 2 to delete numbers, 3 to display the set, and 0 to exit.
U: 0

2. [20 marks] Assume that the user has written a two-dimensional matrix in a file. The first line of the file
will contain the dimensions of the matrix (number of rows, followed by number of columns, separated by
whitespace). Each of the subsequent lines will contain the elements of a row of the matrix, separated by
whitespace, i.e., the second line in the file will contain the elements of the first row of the matrix, and so on.
For instance, a file can contain a 3x4 matrix written as follows:

3 4

12 31 43 89

21 86 -31 41

42 -1 45 -8

Write a program that reads such a file and stores the matrix using a dynamically allocated array of the said
dimensions. Your program should compute the transpose of the matrix, and write the transpose into another
file in a similar format. The name of the input file and the name of the output file should be taken as inputs
from the user. You can assume that all elements of the array will be integers.

3. [20 marks] Implement a stack using singly linked list, each node of which holds a character, and a pointer
to the next node. Write functions Push() and Pop() with appropriate prototype. Pop() should check whether
the stack is empty. While pushing every new item, a node should be dyanmically allocated. Then use this
stack to solve the following problem.
Take as input through the keyboard a string s consisting of characters ‘{’, ‘}’, ‘[’, ‘]’. Check whether s is a
balanced parenthesisation.

Example of a string that is a balanced parenthesisation: [{[{}]}{{}}]

Example of a string that is not a balanced parenthesisation: ][{]}]{

Use the following algorithm to solve this problem: if the current symbol is an opening brace or third bracket
(i.e., ‘{’ or ‘[’) push it onto the stack. If the current symbol is a closing brace or third bracket, pop the stack
and check if the popped symbol is the matching closed counterpart of the current symbol. If the check fails at
any step, print “Not balanced”. If the check does not fail while processing the whole string, then print
“Balanced”. Use the stack to implement this algorithm.

Submission instructions:

Submit one compressed file, named as <roll number>_A9.tar.gz or <roll number>_A9.zip

The compressed file should contain four source files:

<roll number>_A9_1.c, <roll number>_A9_2.c, <roll number>_A9_3.c

You might also like