-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressions_stack.c
80 lines (69 loc) · 1.46 KB
/
expressions_stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Project: IFJ21
* @file expressions_stack.c
*
* @brief Implementation of stack for expressions
* @author Dovhalenko Dmytro, xdovha00
*/
#include "expressions_stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void init_stack(Stack *s){
s->top=NULL;
}
void push(Stack *s, char *new_data, char* new_type){
SElement * new_element = (SElement *)malloc(sizeof(SElement));
if(new_element ==NULL){
//Error;
}
else{
strcpy(new_element->data,new_data);
strcpy(new_element->type,new_type);
new_element->next_element=s->top;
s->top=new_element;
}
}
void pop(Stack *s){
if(s->top!=NULL){
SElement* tmp=s->top;
s->top=tmp->next_element;
free(tmp);
}
}
void change_top_type(Stack *s,char*new_type){
if(s->top!=NULL){
strcpy(s->top->type,new_type);
}
}
char* top(Stack *s){
return s->top->data;
}
char* top_type(Stack *s){
return s->top->type;
}
char* top1(Stack *s){
if(s->top!=NULL){
if(s->top->next_element!=NULL){
SElement * tmp = s->top->next_element;
return tmp->data;
}
}
return NULL;
}
char* top1_type(Stack *s){
if(s->top->next_element!=NULL){
SElement * tmp = s->top->next_element;
return tmp->type;
}
return NULL;
}
bool is_empty(Stack *s){
return(s->top==NULL);
}
void destroy(Stack *s){
while (s->top!=NULL){
pop(s);
}
free(s);
}