mirror of
https://github.com/theoleuthardt/learningC.git
synced 2026-06-13 09:37:53 +00:00
Initial commit
This commit is contained in:
commit
826d4c8c9d
81 changed files with 7268 additions and 0 deletions
49
examTemplate/examTemp_modules/stack.c
Normal file
49
examTemplate/examTemp_modules/stack.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "stack.h"
|
||||
|
||||
struct Stack *top = NULL;
|
||||
|
||||
int isEmpty(){
|
||||
return (top == NULL);
|
||||
}
|
||||
|
||||
void Push(int item){
|
||||
struct Stack *temp = (struct Stack *) malloc(sizeof(struct Stack));
|
||||
temp->ptr = top;
|
||||
temp->data = item;
|
||||
top = temp;
|
||||
}
|
||||
|
||||
int Pop(){
|
||||
if (top == NULL){
|
||||
printf("Stack ist leer!\n");
|
||||
return -1;
|
||||
}
|
||||
struct Stack *temp = top->ptr;
|
||||
int popped = top->data;
|
||||
free(top);
|
||||
top = temp;
|
||||
return popped;
|
||||
}
|
||||
|
||||
int Top(){
|
||||
if(isEmpty()) {
|
||||
printf("Stack ist leer!\n");
|
||||
return -1;
|
||||
}
|
||||
return top->data;
|
||||
}
|
||||
|
||||
int Size(){
|
||||
if(isEmpty())
|
||||
return 0;
|
||||
|
||||
struct Stack *next = top->ptr;
|
||||
int size = 1;
|
||||
for (; next != NULL; next = next->ptr) {
|
||||
size++;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue