forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
150 lines (116 loc) · 3.25 KB
/
stack.h
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// this header file is for C lang, try not to use with c++ as it might generate some errors
#ifndef __STACK_LINKEDLIST__
#define __STACK_LINKEDLIST__ 1
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack // outer struct to use as Stack
{
size_t length; // var to store length of stack
struct Internal
{ // inner struct to use as stack nodes
int data;
struct Internal *prev, *next;
} * top, *temp;
} Stack;
typedef enum
{
false,
true
} bool;
// enum for boolean variable, to maintain backward compatibilty
// with c98 and c89, i skipped <stdbool.h>, feel free to
// include it if you use c99 or later
// function declaration
Stack *getNewStack(void);
// function to allocate memory for new stack
// takes nothing
// returns a pointer of type Stack
Stack *push(Stack *stk, const int value);
// function to add an element in stack
// takes a poiner of type Stack and int value
// returns pointer to updated stack
Stack *pop(Stack *stk);
// function to remove last elemment of stack
// takes a pointer to stack
// return a pointer to updated stack
const int peek(const Stack *stk);
// function to get last element of stack
// takes a constant pointer to stack
// return a constant inteager value
const int length(const Stack *stk);
// function to get length of stack
// takes a constant pointer to stack
// return a constant inteager value
const bool stackNull(const Stack *stk);
// function to check if given stack pointer is null
// takes a constant pointer of Stack type
// return boolean, or it's integer equivalent
// function defination
Stack *getNewStack(void)
{
Stack *temp = (Stack *)malloc(sizeof(Stack));
if (temp == NULL)
{
printf("Error : Memory allocation failed !\n");
return NULL;
}
// assigning default value to it's internal element
temp->length = 0;
temp->temp = temp->top = NULL;
// no more wild pointer
return temp;
}
Stack *push(Stack *stk, const int value)
{
stk->temp = (struct Internal *)malloc(sizeof(struct Internal));
// allocating memory for internal structure
stk->temp->data = value;
stk->temp->next = stk->temp->prev = NULL;
// putting value and nullifying wild pointers
if (stk->top == NULL) // if no element exists
stk->top = stk->temp; // then this is first and top element
else
{
stk->top->next = stk->temp;
stk->temp->prev = stk->top;
stk->top = stk->temp;
}
/* this part can be optimised by and reduced by 2 or 3 lines,
but doing so will reduce readability for beginners
but if you can, feel free to optimise it and send pull request
*/
++stk->length;
return stk;
}
Stack *pop(Stack *stk)
{
if (stackNull(stk))
return stk;
stk->temp = stk->top;
stk->top = stk->top->prev;
free(stk->temp);
--stk->length;
return stk;
}
const int peek(const Stack *stk)
{
if (stackNull(stk))
return -1;
return stk->top->data;
}
const int length(const Stack *stk)
{
if (stackNull(stk))
return -1;
return stk->length;
}
const bool stackNull(const Stack *stk)
{
if (stk == NULL)
{
printf("Error : Stack empty or doesn't exist!\n");
return true;
}
return false;
}
#endif