0% found this document useful (0 votes)
4 views4 pages

CS12201 Lab Assignment 3 - 10-04-2025

Uploaded by

fischerdavid1357
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

CS12201 Lab Assignment 3 - 10-04-2025

Uploaded by

fischerdavid1357
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

NATIONAL INSTITUTE OF TECHNOLOGY SIKKIM

Department of Computer Science and Engineering


CS12201 Computing Laboratory
January–June 2025

Laboratory Assignment #3
NB: Solve the following programming problems according to instructions given during the laboratory sessions. Along with the
requirements mentioned in the text of the questions, additional instructions may be given by the course instructor to produce the
desired output. All programs should be written in the C programming language.

1. Write a program to define a self-referential structure and then create a linked list of 5 nodes using your
defined structure. Declare two members for the structure: one to store a key (an int, float, or double value),
and another to make a node point to the next node in the implementation of the linked list. You may store
the values of the nodes by writing the values explicitly, i.e. hardcoding them, in your source code. Display
the list elegantly in a single line as the output of your program.

2. Write a program to create a singly linked list of 5 nodes as in Question no. 1 above, and then display the list
in a single line in the reverse order, i.e. if the linked list is implemented as (1 → 2 → 3 → 4 → 5), then the
values should be displayed as (5, 4, 3, 2, 1).

3. Write a function called get_node_count to count the number of nodes in a singly linked list.
get_node_count will take only one parameter which is a pointer to the first node of a linked list L, and will
return the number of nodes in L. Take a linked list L as input from the user, call get_node_count from your
main function with the first node of L as the argument, and then print the number of nodes in L as the final
output of your program.

4. Write a program to search for a given value in a singly linked list by linear search with a function called
search_list. search_list will take two parameters: i) a pointer to the first node of a linked list L, and
ii) a value k. search_list will then return a pointer to the node of L containing k if k is present in L,
otherwise will return NULL if k is not present in L. Take L and k as input from the user, and print your final
output with an appropriate message.

5. Write a program to create a singly linked list where each node stores the length and the breadth of a
rectangle, and then find out the node representing the largest rectangle (the rectangle with the largest area).
Make a linked list of n nodes (n to be taken as input from the user) by taking the values of length and
breadth of each rectangle as input from the user. Print the position number of the node representing the
largest rectangle as the final output of your program.

6. Write a program to add new nodes at the beginning and at the end of a singly linked list. Write two
functions called add_at_head, which will add a new node at the beginning of a singly linked list, and
add_at_tail, which will add a new node at the end of a singly linked list. The prototypes of the two said
functions are left to the discretion of the programmer. Your main function will create a singly linked list
with numbers given by the user as input, and then call add_at_head and add_at_tail to add two new nodes
to the list. Values of the two new nodes also will be taken as input from the user. Display the list both
before modification and after modification as the final output of your program.

7. Write a program to delete nodes from the beginning and from the end of a singly linked list. Write two
functions called delete_from_head, which will delete a node from the beginning of a singly linked list, and
delete_from_tail, which will delete a node from the end of a singly linked list. The prototypes of the two
said functions are left to the discretion of the programmer. Your main function will create a singly linked
list with numbers given by the user as input, and then call delete_from_head and delete_from_tail to
delete nodes from the list. Display the list both before modification and after modification as the final
output of your program.

Page 1 of 4
8. Write a program to create a singly linked list and perform the operations of insert, delete, and search using a
menu-based interface. The search operation is to be performed by linear search. The menu should be
displayed repeatedly until the user chooses to quit.
List of functions to implement:
▪ create_node: Takes one argument: a value to store in the new node. Action: Creates a new node with
the given value as its key, and sets the next pointer (the link) of the new node to null. Return value:
The pointer to the newly created node if node was created successfully; null otherwise.
▪ get_node_count: Takes one argument: the pointer to the list (head/start/begin). Action: Counts the
number of nodes in the list. Return value: The number of nodes present in the list.
▪ insert: Takes three arguments: the pointer to the list (head/start/begin), the value to insert in the list,
and the position in the list to insert the value into. Action: Inserts a given value into a given position in
the list. Return value: 1 if insertion is successful; 0 if insertion is unsuccessful.
▪ delete_by_position: Takes two arguments: the pointer to the list (head/start/begin), and the position
from which value is to be deleted. Action: Deletes the value from a given position in the list. Return
value: 1 if delete is successful; 0 otherwise.
▪ delete_by_value: Takes two arguments: the pointer to the list (head/start/begin), and the value to be
deleted. Action: Deletes all instances of the given value from the list. Return value: Number (count) of
positions from which the given value was deleted.
▪ search: Takes two arguments: the pointer to the list (head/start/begin), and the value to search for.
Action: Finds out, by linear search, whether the given value exists in the list. Return value: The first
position number where the value was found, if the value was found; -1 if the value was not found.
▪ display: Takes one argument: the pointer to the list (head/start/begin). Action: Prints the keys of the list
from beginning to end in one line. Return value: None (void type function).

9. Write a program to join two singly linked lists using a function called join. The function join takes two
arguments: head1 pointing to the first list, and head2 pointing to the second list. Action: Appends the
second list onto the end of the first list. Return value: Left to the programmer’s discretion.

10. Write a program to maintain a sorted singly linked list, wherein you need to define two functions as
described below. Create a menu-based interface for your program. The menu will be displayed repeatedly
until the user chooses to quit. The list should always remain sorted in ascending order (or descending
order).
▪ create_node: This function creates a new node with a given value, similar to the create_node function
of Question no. 8.
▪ insert: Takes two arguments: the head of the list, and the value to insert in the list. Action: Inserts the
given value at the correct position in the list such that the list remains sorted in the ascending order.

11. Write a program to find out the largest key in a singly linked list. Take the list as input from the user.

12. Write a program to find out the sum of all the keys in a singly linked list. Take the list as input from the
user.

13. Write a program to display the keys of a singly linked list in the reverse order, i.e. starting from the tail and
ending at the head, using a void type function called display_in_reverse which takes the head of the list as
its argument.

14. Write a program to break a singly linked list into two separate lists. Write a function called break_list
which takes two arguments: the head of a list and the position of breaking. After breaking the list into two
lists, the function returns the head of the second list, i.e. the pointer to the first node after the point of
breaking.
Page 2 of 4
15. Write a program to reverse a singly linked list, i.e. the nodes of the list will be rearranged in the reverse
order. The head will become the tail, the second node will become the second last node, and so on.
Implement the operation with a function called reverse_list which takes the head of a singly linked list as
its argument and returns the new head of the list after reversing the order of the nodes.

16. Write a program to interchange or swap two nodes of a singly linked list. Create the list by taking the keys
from the user as input. Write a function called swap_nodes which takes three arguments: the head of a list,
and the position numbers of two nodes that need to be interchanged. After interchanging the two nodes,
swap_nodes returns the head of the modified list. Print the modified list as the final output of your
program.

17. Write a program to create a singly linked list L containing integers, and then extract from L the odd integers
and even integers, and store them in two separate lists. Create L by taking the values of L as input from the
user, and then store the odd values of L into a new singly linked list L1 and the even values of L into another
new singly linked list L2. Display L1 and L2 as the final output of your program.

18. Write a program to delete every alternate node of a singly linked list, starting with the first node, i.e. delete
the first node, third node, fifth node, and so on. Take the values of the list as input from the user and then
display the modified list as the final output of your program.

19. Given a singly linked list with each node containing an integer, write a program to delete from the list the
nodes which contain prime numbers. Take the linked list as input from the user, perform the said delete
operation, and then print the modified list as the final output of your program.

20. Write a program to swap the first node, called the head, and the last node, called the tail, of a singly linked
list with a function called swapHeadAndTail. swapHeadAndTail will take a pointer to the head of a
singly linked list as a parameter, will interchange the positions of the head and the tail, and will return a
pointer to the head of the modified linked list. Note that the two nodes need to be swapped by modifying
their links; merely swapping the key values is not acceptable. Take the linked list as input from the user,
and print the modified linked list as the final output of your program.

21. Write a program to implement a stack with a singly linked list, taking the maximum size of the stack as
input from the user. Define functions as described below to implement the stack operations. The exact
prototypes of the functions are left to the programmer’s discretion.
i) push: Pushes a given element to the stack.
ii) pop: Pops an element from the stack and returns it.
iii) print_stack: Displays the elements of the stack in the correct order beginning from the top.
Create a menu-based interface where the menu contains the following options which are self-explanatory:
i) Create new empty stack, ii) Push, iii) Pop, iv) Count elements, iv) Print stack, v) Quit. Note that
appropriate messages should be printed for “underflow” and “overflow” conditions.

22. Write a program to implement a queue with a singly linked list, taking the maximum size of the queue as
input from the user. Define functions as described below to implement the queue operations. The exact
prototypes of the function are left to the programmer’s discretion.
i) add: Adds a given element to the queue.
ii) delete: Removes an element from the queue and returns it.
iii) print_queue: Displays the elements of the queue in the correct order from front to rear.
Create a menu-based interface where the menu contains the following options which are self-explanatory:
i) Create new empty queue, ii) Add, iii) Delete, iv) Count elements, iv) Print queue, v) Quit. Note that
appropriate messages should be printed for “underflow” and “overflow” conditions.

Page 3 of 4
23. Write a program to add two polynomials by representing the polynomials with singly linked lists, as
described by [Horowitz et al.]1. Take the coefficients and exponents of two polynomials P1 and P2 as input
from the user, and create two linked lists L1 and L2 to represent P1 and P2 respectively. Then, add P1 and P2
to get a third polynomial P3 (= P1 + P2) which should be represented by a linked list L3. Print P1, P2, and P3
elegantly as the final output of your program by reading the nodes of L1, L2, and L3 respectively.

24. Write a program to represent a polynomial P with a singly linked list L1, and multiply P with a given real
number k to obtain another polynomial Q which is the product of P and k, where Q is represented with
another singly linked list L2. Take the coefficients and exponents of P and the value k as input from the user.
Print P and Q as the final output of your program by reading the nodes of L1 and L2.

25. Write a program to implement a priority queue with a singly linked list. Each element of the queue will be
represented by a node with three fields: i) a key, ii) a priority number, and iii) a link to the next node in the
linked list. Assume that a node with a lower priority number has a higher priority than a node with a higher
priority number in the queue. If two nodes have the same priority, the node that was added to the queue
earlier will be placed before the node that was added to the queue later. Create a menu-based user interface.
Define the following three functions representing the priority queue operations.
i) delete: Removes a node from the front of the queue and returns a pointer to the removed node.
ii) add: Adds a new node to the queue at the correct position based on the priority of the node.
iii) display: Prints all the nodes of the queue in the correct order from front to rear in an elegant format.

Reference:
1
Ellis Horowitz, Sartaj Sahni, Susan Anderson-Freed, “Fundamentals of Data Structures in C”, Second Edition, 2008

---------------------------------------x---------------------------------------

Page 4 of 4

You might also like