-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStacks.c
103 lines (93 loc) · 1.95 KB
/
Stacks.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Stacks.h"
typedef struct StackNode
{
int data;
struct StackNode *below;
}StackNode;
typedef struct Stack
{
int count;
StackNode* top;
}Stack;
StackLink stack_initialize()
{
StackLink pointer;
pointer = malloc(sizeof(StackLink));
pointer->count = 0;
pointer->top = NULL;
return pointer;
}
void stack_push(StackLink pointer, int value)
{
StackNodeLink temp;
temp = malloc(sizeof(StackNode));
temp->data = value;
temp->below = pointer->top; // Making the previous top the below
pointer->top = temp;
pointer->count++;
}
int stack_pop(StackLink pointer)
{
if(pointer->count != 0)
{
StackNodeLink temp;
temp = pointer->top;
int n = pointer->top->data;
pointer->top = pointer->top->below;
free(temp);
pointer->count--;
return n;
} else
{
printf("The Stack is empty.\n");
return -1;
}
}
Array_Int stack_to_array(StackLink pointer)
{
int* arr = malloc(pointer->count * sizeof(int*) );
StackNodeLink temp = pointer->top;
int n = pointer->count - 1;
while ( temp != NULL )
{
arr[n] = temp->data;
temp = temp->below;
n--;
}
return arr;
}
StackLink stack_copy(StackLink pointer)
{
Array_Int array = stack_to_array(pointer);
StackLink new = stack_initialize();
for(int i = 0 ; i < pointer->count ; i++)
{
stack_push(new, array[i]);
}
free(array);
return new;
}
void stack_clear(StackLink pointer)
{
printf("Clearing the stack:\n");
int i = pointer->count;
while(i > 0)
{
printf("Poping -> ( %d )\n", stack_pop(pointer));
i--;
}
}
void stack_print(StackLink pointer)
{
printf("The stack is:\n");
StackLink new = stack_copy(pointer);
int i = pointer->count;
while(i > 0)
{
printf("( %d )\n", stack_pop(new));
i--;
}
}