# Assignment 3 – Linked Lists and Multiple Sources solved

\$35.00

## Description

5/5 - (1 vote)

Problem 3.1 A linked list of characters (1 point)
Presence assignment, due by 18:30 h today
Using the example from the slides (Lecture 3 & 4, pages 22 − 25), write a program that uses a
linked list. Your program should wait for input from the keyboard. Entering from the keyboard
a ’1’ will just add the following character (read as next from the keyboard) to the end of the list,
while a ’2’ inserts at the beginning of the list. Entering a ’3’ will remove the first element from
the list, a ’4’ will print the list while a ’5’ will empty the list and free the memory used by the list
and quit the execution of the program.
Use a switch-case statement to decide which action to take.
You can assume that the input will be valid.
Testcase 3.1: input
2
a
2
c
1
b
4
3
4
5
Testcase 3.1: output
c a b
a b
Problem 3.2 An enhanced linked list of characters (2 points)
Extend your program for Problem 3.1 by writing a function for inserting a new element into the
list at a given position and a function for reversing the order of the elements in the list. Your
program should wait for input from the keyboard. A ’6’ followed by a number and a character
(the position and the element to be inserted) should insert the character at the position of the
number (the first element in the list has position 0). You can assume that the input does not
contain any logical errors (e.g., ’6’ is always followed by a position and a character, and ’2’ and ’1’
are followed by one character). However, if the position for inserting is negative or is greater than
the number of elements in the list then print on the standard output “Invalid position!”. A
’7’ should reverse the order of the characters in the list without allocating new nodes or using a
doubly linked list (i.e., only with the use of pointers).
Use a switch-case statement to decide which action to take.
Testcase 3.2: input
2
a
2
c
1
b
4
3
4
6
1
x
4
6
11
b
7
4
5
Testcase 3.2: output
c a b
a b
a x b
Invalid position!
b x a
Problem 3.3 A doubly linked list of numbers (4 points)
Create a data type that implements a doubly linked list of integer numbers. Write a program that
tests your doubly linked list with the testcase given below. Entering from the keyboard an ’a’
will just add the following number (also entered from the keyboard) to the list at the beginning,
while an ’r’ will remove all elements with the given number from the list, a ’p’ will print the
current list, while a ’b’ will print the elements of list backwards. A ’q’ will empty the list, free the
memory used by the doubly linked list and quit the execution of the program.
You can assume that the input does not contain logical errors (i.e., if the command is ’r’, you can
assume that a number will follow). However, a number to be deleted may not be currently in the
list. In this case, the message “The element is not in the list!” should be printed on
the standard output.
Use a switch-case statement to decide which action to take.
Testcase 3.3: input
a
7
a
2
a
5
p
a
2
a
11
a
7
a
11
p
r
11
p
b
r
18
q
Testcase 3.3: output
5 2 7
11 7 11 2 5 2 7
7 2 5 2 7
7 2 5 2 7
The element is not in the list!
Problem 3.4 Multiple sources (1 point)
Modify your solution for Problem 3.1 such that you separate your source code into three files:
struct declaration and function declarations in linked_list.h, function definitions in
You can assume that the input will be valid.
Testcase 3.4: input
2
a
2
c
1
b
4
3
4
5
Testcase 3.4: output
c a b
a b
• Your source code should be properly indented and compile with gcc without any warnings (You
can use gcc -Wall -o program program.c). Insert suitable comments (not on every line . . . ) to
• Please name the programs according to the suggested filenames (they should match the description
of the problem) in Grader. Otherwise you might have problems with the inclusion of header files.
Each program must include a comment on the top like the following:
/*
JTSK-320112
a3 p1.c
Firstname Lastname
myemail@jacobs-university.de
*/