added ws202425 courses
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.29)
|
||||
project(uebung_0_stack C)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
include_directories(.)
|
||||
|
||||
add_executable(uebung_0_stack
|
||||
stack.c
|
||||
stack.h)
|
||||
@@ -0,0 +1,30 @@
|
||||
# Compiler
|
||||
CC = gcc
|
||||
|
||||
# Compiler flags
|
||||
CFLAGS = -Wall -g
|
||||
|
||||
# Target executable
|
||||
TARGET = prog
|
||||
|
||||
# Object files
|
||||
OBJS = stack.o prog.o
|
||||
|
||||
# Default rule to build the target executable
|
||||
all: $(TARGET)
|
||||
|
||||
# Rule to build the target executable from object files
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS)
|
||||
|
||||
# Rule to compile stack.c to object file
|
||||
stack.o: stack.c stack.h
|
||||
$(CC) $(CFLAGS) -c stack.c
|
||||
|
||||
# Rule to compile prog.c to object file
|
||||
prog.o: prog.c stack.h
|
||||
$(CC) $(CFLAGS) -c prog.c
|
||||
|
||||
# Clean up object files and the executable
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
@@ -0,0 +1,12 @@
|
||||
# Übung 0 Stack
|
||||
|
||||
Dies ist die Vorlage für die Live Aufgabe "Stack".
|
||||
|
||||
Bitte forken Sie diese Vorlage über den Button Fork und wählen Sie ihre Gruppe als URL aus.
|
||||
Anschließend können Sie in Ihrer Version des Projektes arbeiten :)
|
||||
Forderns Sie kein Zugriff auf dieses Projekt an!
|
||||
|
||||
1. Implementieren Sie den Datentyp stack_t.
|
||||
2. Implementieren Sie die auskommentierten Methoden in stack.c.
|
||||
3. Implementieren Sie ein Programm, welches Ihren Stack nutzt in prog.c
|
||||
4. Erstellen Sie ein Makefile, welches die Executable prog erstellt.
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include "stack.h"
|
||||
|
||||
int main() {
|
||||
|
||||
stack_t* stack = stack_create(5);
|
||||
|
||||
if(stack == NULL){
|
||||
printf("Failed to create stack\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (stack_push(stack, 'A') != 0) {
|
||||
printf("Error: failed to push onto stack\n");
|
||||
stack_delete(stack);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (stack_push(stack, 'B') != 0) {
|
||||
printf("Error: failed to push onto stack\n");
|
||||
stack_delete(stack);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (stack_push(stack, 'C') != 0) {
|
||||
printf("Error: failed to push onto stack\n");
|
||||
stack_delete(stack);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Stack length is %zu\n", stack_length(stack));
|
||||
|
||||
char popped;
|
||||
if (stack_pop(stack, &popped) != 0){
|
||||
printf("Error: failed to pop from stack\n");
|
||||
stack_delete(stack);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Popped element: %c\n", popped);
|
||||
|
||||
if (stack_pop(stack, &popped) != 0){
|
||||
printf("Error: failed to pop from stack\n");
|
||||
stack_delete(stack);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Popped element: %c\n", popped);
|
||||
|
||||
printf("Stack length is %zu\n", stack_length(stack));
|
||||
|
||||
stack_delete(stack);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "stack.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
stack_t *stack_create(size_t size) {
|
||||
stack_t* stack = (stack_t*) malloc(sizeof(stack_t));
|
||||
if (stack == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
stack->array = (char*) malloc(size * sizeof(char));
|
||||
if (stack->array == NULL) {
|
||||
free(stack);
|
||||
return NULL;
|
||||
}
|
||||
stack->size = size;
|
||||
stack->length = 0;
|
||||
return stack;
|
||||
}
|
||||
|
||||
void stack_delete(stack_t *stack) {
|
||||
if (stack != NULL) {
|
||||
free(stack->array);
|
||||
free(stack);
|
||||
}
|
||||
}
|
||||
|
||||
size_t stack_length(stack_t *stack) {
|
||||
return stack->length;
|
||||
}
|
||||
|
||||
int stack_push(stack_t *stack, char c) {
|
||||
if (stack->length >= stack->size) {
|
||||
return -1;
|
||||
}
|
||||
stack->array[stack->length] = c;
|
||||
stack->length++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stack_pop(stack_t *stack, char *dest) {
|
||||
if (stack->length == 0) {
|
||||
return -1;
|
||||
}
|
||||
*dest = stack->array[stack->length - 1];
|
||||
stack->length--;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct stack{
|
||||
int size;
|
||||
char* array;
|
||||
int length;
|
||||
} stack_t;
|
||||
|
||||
stack_t* stack_create(size_t size);
|
||||
|
||||
void stack_delete(stack_t* stack);
|
||||
|
||||
size_t stack_length(stack_t* stack);
|
||||
|
||||
int stack_push(stack_t* stack,char c);
|
||||
|
||||
int stack_pop(stack_t* stack, char* dest);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user