-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
70 lines (62 loc) · 1.63 KB
/
stack.c
File metadata and controls
70 lines (62 loc) · 1.63 KB
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
/*---------------------------------------------------
Implementation of stack data structure in C.
Extension for compiling IMPORTANT code to C language.
v1.0
---------------------------------------------------*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1000
// A structure to represent a stack
struct Stack {
int top;
unsigned capacity;
char* array;
};
// Function to create a stack of given capacity. It initializes size of
// stack as 0
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
if(capacity <= MAXSIZE) {
stack->capacity = capacity;
}
else {
stack->capacity = MAXSIZE;
}
stack->top = -1;
stack->array = (char*)malloc(stack->capacity * sizeof(char));
return stack;
}
// Stack is full when top is equal to the last index
int isFull(struct Stack* stack)
{
return stack->top == stack->capacity - 1;
}
// Stack is empty when top is equal to -1
int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}
// Function to add an item to stack. It increases top by 1
void push(struct Stack* stack, char item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
//printf("%d pushed to stack\n", item);
}
// Function to remove an item from stack. It decreases top by 1
char pop(struct Stack* stack)
{
if (isEmpty(stack))
return '';
return stack->array[stack->top--];
}
// Function to return the top from stack without removing it
char peek(struct Stack* stack)
{
if (isEmpty(stack))
return '';
return stack->array[stack->top];
}