Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
dynamic_stack.c File Reference

Dynamic Stack, just like Dynamic Array, is a stack data structure whose the length or capacity (maximum number of elements that can be stored) increases or decreases in real time based on the operations (like insertion or deletion) performed on it. More...

#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for dynamic_stack.c:

Data Structures

struct  DArrayStack
 to verify assumptions made by the program and print a diagnostic message if this assumption is false. More...
 

Typedefs

typedef struct DArrayStack DArrayStack
 to verify assumptions made by the program and print a diagnostic message if this assumption is false.
 

Functions

DArrayStackcreate_stack (int cap)
 Create a Stack object.
 
DArrayStackdouble_array (DArrayStack *ptr, int cap)
 As this is stack implementation using dynamic array this function will expand the size of the stack by twice as soon as the stack is full.
 
DArrayStackshrink_array (DArrayStack *ptr, int cap)
 As this is stack implementation using dynamic array this function will shrink the size of stack by twice as soon as the stack's capacity and size has significant difference.
 
int push (DArrayStack *ptr, int data)
 The push function pushes the element onto the stack.
 
int pop (DArrayStack *ptr)
 The pop function to pop an element from the stack.
 
int peek (DArrayStack *ptr)
 To retrieve or fetch the first element of the Stack or the element present at the top of the Stack.
 
int show_capacity (DArrayStack *ptr)
 To display the current capacity of the stack.
 
int isempty (DArrayStack *ptr)
 The function is used to check whether the stack is empty or not and return true or false accordingly.
 
int stack_size (DArrayStack *ptr)
 Used to get the size of the Stack or the number of elements present in the Stack.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Dynamic Stack, just like Dynamic Array, is a stack data structure whose the length or capacity (maximum number of elements that can be stored) increases or decreases in real time based on the operations (like insertion or deletion) performed on it.

In this implementation, functions such as PUSH, POP, PEEK, show_capacity, isempty, and stack_size are coded to implement dynamic stack.

Author
SahilK-027

Typedef Documentation

◆ DArrayStack

typedef struct DArrayStack DArrayStack

to verify assumptions made by the program and print a diagnostic message if this assumption is false.

to provide a set of integer types with universally consistent definitions that are operating system-independent for IO operations for including functions involving memory allocation such as malloc

DArrayStack Structure of stack.

Function Documentation

◆ create_stack()

DArrayStack * create_stack ( int  cap)

Create a Stack object.

Parameters
capCapacity of stack
Returns
DArrayStack* Newly created stack object pointer
37{
38 DArrayStack *ptr;
39 ptr = (DArrayStack *)malloc(sizeof(DArrayStack));
40 ptr->capacity = cap;
41 ptr->top = -1;
42 ptr->arrPtr = (int *)malloc(sizeof(int) * cap);
43 printf("\nStack of capacity %d is successfully created.\n", ptr->capacity);
44 return (ptr);
45}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
to verify assumptions made by the program and print a diagnostic message if this assumption is false.
Definition dynamic_stack.c:25
int top
to store capacity and top of the stack
Definition dynamic_stack.c:26
int * arrPtr
array pointer
Definition dynamic_stack.c:27

◆ double_array()

DArrayStack * double_array ( DArrayStack ptr,
int  cap 
)

As this is stack implementation using dynamic array this function will expand the size of the stack by twice as soon as the stack is full.

Parameters
ptrStack pointer
capCapacity of stack
Returns
DArrayStack*: Modified stack
56{
57 int newCap = 2 * cap;
58 int *temp;
59 temp = (int *)malloc(sizeof(int) * newCap);
60 for (int i = 0; i < (ptr->top) + 1; i++)
61 {
62 temp[i] = ptr->arrPtr[i];
63 }
64 free(ptr->arrPtr);
65 ptr->arrPtr = temp;
66 ptr->capacity = newCap;
67 return ptr;
68}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26

◆ isempty()

int isempty ( DArrayStack ptr)

The function is used to check whether the stack is empty or not and return true or false accordingly.

Parameters
ptrStack pointer
Returns
int returns 1 -> true OR returns 0 -> false
178{
179 if (ptr->top == -1)
180 {
181 return 1;
182 }
183 return 0;
184}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
247{
248 test(); // run self-test implementations
249 return 0;
250}
static void test()
Self-test implementations.
Definition dynamic_stack.c:199
Here is the call graph for this function:

◆ peek()

int peek ( DArrayStack ptr)

To retrieve or fetch the first element of the Stack or the element present at the top of the Stack.

Parameters
ptrStack pointer
Returns
int Top of the stack
153{
154 if (ptr->top == -1)
155 {
156 printf("Stack is empty UNDERFLOW \n");
157 return -1;
158 }
159 return ptr->arrPtr[ptr->top];
160}

◆ pop()

int pop ( DArrayStack ptr)

The pop function to pop an element from the stack.

Parameters
ptrStack pointer
Returns
int Popped value
125{
126 if (ptr->top == -1)
127 {
128 printf("Stack is empty UNDERFLOW \n");
129 return -1;
130 }
131 int ele = ptr->arrPtr[ptr->top];
132 ptr->arrPtr[ptr->top] = 0;
133 ptr->top = (ptr->top - 1);
134 if ((ptr->capacity) % 2 == 0)
135 {
136 if (ptr->top <= (ptr->capacity / 2) - 1)
137 {
138 ptr = shrink_array(ptr, ptr->capacity);
139 }
140 }
141 printf("Successfully popped: %d\n", ele);
142 return ele;
143}
DArrayStack * shrink_array(DArrayStack *ptr, int cap)
As this is stack implementation using dynamic array this function will shrink the size of stack by tw...
Definition dynamic_stack.c:79
Here is the call graph for this function:

◆ push()

int push ( DArrayStack ptr,
int  data 
)

The push function pushes the element onto the stack.

Parameters
ptrStack pointer
dataValue to be pushed onto stack
Returns
int Position of top pointer
102{
103 if (ptr->top == (ptr->capacity) - 1)
104 {
105 ptr = double_array(ptr, ptr->capacity);
106 ptr->top++;
107 ptr->arrPtr[ptr->top] = data;
108 }
109 else
110 {
111 ptr->top++;
112 ptr->arrPtr[ptr->top] = data;
113 }
114 printf("Successfully pushed : %d\n", data);
115 return ptr->top;
116}
DArrayStack * double_array(DArrayStack *ptr, int cap)
As this is stack implementation using dynamic array this function will expand the size of the stack b...
Definition dynamic_stack.c:55
Definition prime_factoriziation.c:25
Here is the call graph for this function:

◆ show_capacity()

int show_capacity ( DArrayStack ptr)

To display the current capacity of the stack.

Parameters
ptrStack pointer
Returns
int Current capacity of the stack
168{ return ptr->capacity; }

◆ shrink_array()

DArrayStack * shrink_array ( DArrayStack ptr,
int  cap 
)

As this is stack implementation using dynamic array this function will shrink the size of stack by twice as soon as the stack's capacity and size has significant difference.

Parameters
ptrStack pointer
capCapacity of stack
Returns
DArrayStack*: Modified stack
80{
81 int newCap = cap / 2;
82 int *temp;
83 temp = (int *)malloc(sizeof(int) * newCap);
84 for (int i = 0; i < (ptr->top) + 1; i++)
85 {
86 temp[i] = ptr->arrPtr[i];
87 }
88 free(ptr->arrPtr);
89 ptr->arrPtr = temp;
90 ptr->capacity = newCap;
91 return ptr;
92}

◆ stack_size()

int stack_size ( DArrayStack ptr)

Used to get the size of the Stack or the number of elements present in the Stack.

Parameters
ptrStack pointer
Returns
int size of stack
193{ return ptr->top + 1; }

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
200{
201 DArrayStack *NewStack;
202 int capacity = 1;
203 NewStack = create_stack(capacity);
204 uint64_t arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
205
206 printf("\nTesting Empty stack: ");
207 assert(stack_size(NewStack) == 0);
208 assert(isempty(NewStack) == 1);
209 printf("Size of an empty stack is %d\n", stack_size(NewStack));
210
211 printf("\nTesting PUSH operation:\n");
212 for (int i = 0; i < 12; ++i)
213 {
214 int topVal = push(NewStack, arr[i]);
215 printf("Size: %d, Capacity: %d\n\n", stack_size(NewStack),
216 show_capacity(NewStack));
217 assert(topVal == i);
218 assert(peek(NewStack) == arr[i]);
219 assert(stack_size(NewStack) == i + 1);
220 assert(isempty(NewStack) == 0);
221 }
222
223 printf("\nTesting POP operation:\n");
224 for (int i = 11; i > -1; --i)
225 {
226 peek(NewStack);
227 assert(peek(NewStack) == arr[i]);
228 int ele = pop(NewStack);
229 assert(ele == arr[i]);
230 assert(stack_size(NewStack) == i);
231 }
232
233 printf("\nTesting Empty stack size: ");
234 assert(stack_size(NewStack) == 0);
235 assert(isempty(NewStack) == 1);
236 printf("Size of an empty stack is %d\n", stack_size(NewStack));
237
238 printf("\nTesting POP operation on empty stack: ");
239 assert(pop(NewStack) == -1);
240}
int isempty(DArrayStack *ptr)
The function is used to check whether the stack is empty or not and return true or false accordingly.
Definition dynamic_stack.c:177
int show_capacity(DArrayStack *ptr)
To display the current capacity of the stack.
Definition dynamic_stack.c:168
DArrayStack * create_stack(int cap)
Create a Stack object.
Definition dynamic_stack.c:36
int stack_size(DArrayStack *ptr)
Used to get the size of the Stack or the number of elements present in the Stack.
Definition dynamic_stack.c:193
char pop()
Function to pop from the stack.
Definition infix_to_postfix2.c:45
void push(struct Stack *p, char ch)
push function
Definition infix_to_postfix.c:55
Here is the call graph for this function: