-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.c
132 lines (114 loc) · 2.8 KB
/
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Stack.h"
struct Number{
char no[101];
int length;
int length_float;
int isNegative;
};
struct StackChar{
int top;
int capacity;
char *array;
};
struct StackNumber{
int top;
int capacity;
Number *array;
};
/// -----------------------------------------------------------------------
/// The following stack functions were taken from
/// https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
/// Function for creating a stack:
StackChar * createStack(int capacity)
{
StackChar * stack = (StackChar*)malloc(sizeof(StackChar));
stack->capacity = capacity;
stack->top = -1;
stack->array = (char*)malloc(stack->capacity * sizeof(char));
return stack;
}
/// Function for checking if a stack is full:
int isFull(StackChar *stack)
{
return stack->top == stack->capacity - 1;
}
/// Function for checking if a stack is empty:
int isEmpty(StackChar *stack)
{
return stack->top == -1;
}
/// Function to add an item to the top of the stack:
void push(StackChar *stack, char item)
{
if(isFull(stack)){
printf("\nERROR! Stack is full\n");
return;
}
stack->top++;
stack->array[stack->top] = item;
}
/// Function to take an item from the top of the stack:
char pop(StackChar *stack)
{
if(isEmpty(stack)){
printf("\ERROR! Stack is empty\n");
return 'y';
}
stack->top--;
return stack->array[stack->top+1];
}
/// Function to see the item on the top of the stack:
char peek(StackChar *stack)
{
if(isEmpty(stack))
return 'y';
return stack->array[stack->top];
}
StackNumber * createStackNumber(int capacity)
{
StackNumber * stack = (StackNumber*)malloc(sizeof(StackNumber));
stack->capacity = capacity;
stack->top = -1;
stack->array = (Number*)malloc(stack->capacity * sizeof(Number));
return stack;
}
/// Function for checking if a stack is full:
int isFullNumber(StackNumber *stack)
{
return stack->top == stack->capacity - 1;
}
/// Function for checking if a stack is empty:
int isEmptyNumber(StackNumber *stack)
{
return stack->top == -1;
}
/// Function to add an item to the top of the stack:
void pushNumber(StackNumber *stack, Number item)
{
if(isFull(stack)){
printf("\nERROR! Stack is full\n");
return;
}
stack->top++;
stack->array[stack->top] = item;
}
/// Function to take an item from the top of the stack:
Number popNumber(StackNumber *stack)
{
if(isEmpty(stack)){
printf("\ERROR! Stack is empty\n");
return;
}
stack->top--;
return stack->array[stack->top+1];
}
/// Function to see the item on the top of the stack:
Number peekNumber(StackNumber *stack)
{
if(isEmpty(stack))
return;
return stack->array[stack->top];
}