add initial c, cpp files
This commit is contained in:
47
c/isp-ws25/a2-vowels.c
Normal file
47
c/isp-ws25/a2-vowels.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define STRING_SIZE 30
|
||||
|
||||
void inputString(char user_input[])
|
||||
{
|
||||
printf("Please enter the string: ");
|
||||
fgets(user_input, STRING_SIZE, stdin);
|
||||
}
|
||||
|
||||
void countVowels(char user_input[], int vowels[])
|
||||
{
|
||||
int position = 0;
|
||||
while (user_input[position] != '\n')
|
||||
{
|
||||
if (user_input[position] == 'a' || user_input[position] == 'A') vowels[0]++;
|
||||
if (user_input[position] == 'e' || user_input[position] == 'E') vowels[1]++;
|
||||
if (user_input[position] == 'i' || user_input[position] == 'I') vowels[2]++;
|
||||
if (user_input[position] == 'o' || user_input[position] == 'O') vowels[3]++;
|
||||
if (user_input[position] == 'u' || user_input[position] == 'U') vowels[4]++;
|
||||
position++;
|
||||
}
|
||||
}
|
||||
|
||||
void printVowels(int vowels[])
|
||||
{
|
||||
printf("a: %d\n", vowels[0]);
|
||||
printf("e: %d\n", vowels[1]);
|
||||
printf("i: %d\n", vowels[2]);
|
||||
printf("o: %d\n", vowels[3]);
|
||||
printf("u: %d\n", vowels[4]);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char user_input[STRING_SIZE] = {'\0'};
|
||||
int vowels[5] = {0};
|
||||
|
||||
inputString(user_input);
|
||||
|
||||
countVowels(user_input, vowels);
|
||||
|
||||
printVowels(vowels);
|
||||
|
||||
return 0;
|
||||
}
|
||||
163
c/isp-ws25/a4-insert-in-sorted-list.c
Normal file
163
c/isp-ws25/a4-insert-in-sorted-list.c
Normal file
@@ -0,0 +1,163 @@
|
||||
/******************************************************************************
|
||||
Student Management Using Linked Lists
|
||||
|
||||
Task Description
|
||||
In this coding task, you will extend a student management system. The program already creates students and stores them in a sorted linked list. Your task is to implement the functionality of adding additional students into the sorted linked list. Some parts of the program are already implemented.
|
||||
|
||||
Key points:
|
||||
Already Implemented: The student struct is defined. Students are created in the main function and added manually into a linked list.
|
||||
Your task: The function insertSorted should insert a student into the existing linked list at the correct position.
|
||||
The linked list is sorted alphabetically by student names.
|
||||
|
||||
Hints:
|
||||
How to find the position to insert?
|
||||
Stop iterating over the linked list at the element before the one you want to insert.
|
||||
In the visualization above, we insert "Benedikt" after "Andreas" and before "Daniel". Stop at "Andreas" and change the pointers accordingly.
|
||||
Consider the case of adding the student in the middle of the linked list, but also at the end and at the head (index to insert = 0)
|
||||
Have a look at the testcases
|
||||
|
||||
Implement the following functions:
|
||||
|
||||
-> insertSorted(Student *list, Student *new_student)
|
||||
|
||||
This function takes two arguments:
|
||||
i) a pointer to the current list of students (list),
|
||||
ii) a pointer to the students to insert into the list.
|
||||
Add the student into the existing linked list at the correct position.
|
||||
The linked list is sorted alphabetically by name.
|
||||
The function should return the head of the linked list.
|
||||
|
||||
-> freeStudents(Student *list)
|
||||
|
||||
Free all students in the linked list.
|
||||
This is tested with valgrind checks at testcase 2 and 3.
|
||||
The function should not return anything.
|
||||
|
||||
Specifications
|
||||
Use dynamic memory allocation
|
||||
The program must compile and run without memory leaks/errors.
|
||||
Use the existing Linked List.
|
||||
You don't need to modify the structs or the print function.
|
||||
You don't have to consider the case where no more memory is available.
|
||||
*******************************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct Student
|
||||
{
|
||||
char name_[32];
|
||||
struct Student *next_;
|
||||
} Student;
|
||||
|
||||
Student* createStudent(char* name)
|
||||
{
|
||||
Student *s = malloc(sizeof(Student));
|
||||
if (!s) return NULL;
|
||||
strcpy(s->name_, name);
|
||||
s->next_ = NULL;
|
||||
return s;
|
||||
}
|
||||
|
||||
void printStudents(Student *list)
|
||||
{
|
||||
Student *cur = list;
|
||||
int index = 0;
|
||||
while (cur != NULL)
|
||||
{
|
||||
printf("%d: %s\n", index++, cur->name_);
|
||||
cur = cur->next_;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: write your code here
|
||||
Student *insertSorted(Student *list, Student *new_student)
|
||||
{
|
||||
Student *current = list;
|
||||
Student *previous = NULL;
|
||||
|
||||
while(current != NULL && strcmp(current->name_, new_student->name_) <= 0)
|
||||
{
|
||||
previous = current;
|
||||
current = current->next_;
|
||||
}
|
||||
|
||||
if(previous == NULL)
|
||||
{
|
||||
new_student->next_ = current;
|
||||
list = new_student;
|
||||
}
|
||||
else
|
||||
{
|
||||
new_student->next_ = current;
|
||||
previous->next_ = new_student;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
void freeStudents(Student *list)
|
||||
{
|
||||
if (!list) return;
|
||||
|
||||
freeStudents(list->next_);
|
||||
free(list);
|
||||
list = NULL;
|
||||
}
|
||||
// TODO: write your code here
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int test;
|
||||
printf("Enter tc: ");
|
||||
scanf("%d", &test);
|
||||
if(test == 1)
|
||||
{
|
||||
// test adding at the second and third position
|
||||
Student* s1 = createStudent("Andreas");
|
||||
Student* s2 = createStudent("Daniel");
|
||||
s1->next_ = s2;
|
||||
|
||||
Student* to_add1 = createStudent("Benedikt");
|
||||
Student* to_add2 = createStudent("Clemens");
|
||||
|
||||
s1 = insertSorted(s1, to_add1);
|
||||
s1 = insertSorted(s1, to_add2);
|
||||
printStudents(s1);
|
||||
//freeStudents(s1); // uncomment that after you implemented the function
|
||||
}
|
||||
else if(test == 2)
|
||||
{
|
||||
// test adding at same first character (T)
|
||||
Student* s1 = createStudent("Benjamin");
|
||||
Student* s2 = createStudent("Tanja");
|
||||
Student* s3 = createStudent("Thomas");
|
||||
s1->next_ = s2;
|
||||
s2->next_ = s3;
|
||||
|
||||
Student* to_add1 = createStudent("Toni");
|
||||
Student* to_add2 = createStudent("Tamim");
|
||||
|
||||
s1 = insertSorted(s1, to_add1);
|
||||
s1 = insertSorted(s1, to_add2);
|
||||
printStudents(s1);
|
||||
//freeStudents(s1); // uncomment that after you implemented the function
|
||||
}
|
||||
else if(test == 3)
|
||||
{
|
||||
// test adding in the beginning and end of the LL
|
||||
Student* s1 = createStudent("Lenon");
|
||||
Student* s2 = createStudent("Margarita");
|
||||
s1->next_ = s2;
|
||||
|
||||
Student* to_add1 = createStudent("Julia");
|
||||
Student* to_add2 = createStudent("Kilian");
|
||||
|
||||
s1 = insertSorted(s1, to_add1);
|
||||
s1 = insertSorted(s1, to_add2);
|
||||
printStudents(s1);
|
||||
//freeStudents(s1); // uncomment that after you implemented the function
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user