Commit d53f98b1 authored by Paffenholz, Andreas's avatar Paffenholz, Andreas

programs app A

parent 5fe4aa7e
/*************************************
* Beispielprogramm zur Vorlesung
* Einfuehrung in die Programmierung I
* Andreas Paffenholz
* TU Darmstadt, Wintersemester 2020/21
* (c) 2020-
*
* stack, Implementierung
*
**************************************/
#include<stdio.h>
#include<stdlib.h>
#include <limits.h>
#include "stack.h"
/*
* Initialisieren und Löschen des Stapels
*/
static struct node* init_node(int value) {
struct node * node = (struct node *)malloc(sizeof(struct node));
node->value = value;
node->next = NULL;
return node;
}
static void free_node(struct node * node) {
free(node);
}
void free_stack(struct node * node ) {
struct node* keep = NULL;
while( node != NULL ) {
keep = node;
node = node->next;
free_node(keep);
}
}
/*
* Standardfunktionen
*/
void push(struct node **head_ptr, int value) {
struct node* new_node = init_node(value);
new_node->next = *head_ptr;
*head_ptr = new_node;
}
int pop(struct node **head_ptr) {
int value = INT_MIN;
if( *head_ptr ) {
struct node * node = *head_ptr;
value = node->value;
*head_ptr = node->next;
free(node);
}
return value;
}
int stack_is_empty(struct node **head_ptr) {
return ( *head_ptr == NULL );
}
int peek_stack(struct node **head_ptr) {
int value = INT_MIN;
if( *head_ptr ) {
value = (*head_ptr)->value;
}
return value;
}
/*
* I/O
*/
void print_stack(const struct node * node) {
static int count = 1;
printf("Aufruf %d, die Liste enthaelt: ", count++);
while (node != NULL ) {
printf("%d ", node->value);
node = node->next;
}
printf("\n");
}
/*************************************
* Beispielprogramm zur Vorlesung
* Einfuehrung in die Programmierung I
* Andreas Paffenholz
* TU Darmstadt, Wintersemester 2020/21
* (c) 2020-
*
* stack, Header
*
**************************************/
#ifndef STACK_H
#define STACK_H
#ifndef NODE_STRUCT
#define NODE_STRUCT
// Element
struct node {
int value;
struct node * next;
};
#endif
/* Initialisieren und Löschen des Stapels */
void free_stack(struct node *);
/* Standardfunktionen */
void push(struct node **, int);
int pop(struct node **);
int stack_is_empty(struct node **);
int peek_stack(struct node **);
/* I/O */
void print_stack(const struct node *);
#endif
/*************************************
* Beispielprogramm zur Vorlesung
* Einfuehrung in die Programmierung I
* Andreas Paffenholz
* TU Darmstadt, Wintersemester 2020/21
* (c) 2020-
*
* stack, main
*
* Uebersetzen mit
* gcc stack_main.c stack.c -o stack
* Aufruf mit
* ./stack
**************************************/
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main() {
// Initialisierung
struct node * head = NULL;
push(&head, 17);
push(&head, 22);
push(&head, 30);
print_stack(head);
int value = pop(&head);
printf("Wert %d zurueckbekommen\n",value);
print_stack(head);
printf("Liste ist leer: %s\n", stack_is_empty(&head)?"ja":"nein" );
printf("Das naechste Element ist %d\n", peek_stack(&head));
pop(&head);
value = pop(&head);
printf("Wert %d zurueckbekommen\n",value);
printf("Liste ist leer: %s\n", stack_is_empty(&head)?"ja":"nein" );
free_stack(head);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment