From 6dc883e568ed48f8d87667225ed999615d3c3579 Mon Sep 17 00:00:00 2001 From: karangarg218 <42265308+karangarg218@users.noreply.github.com> Date: Sat, 3 Oct 2020 14:31:57 +0530 Subject: [PATCH] Created a stack using linked list This is the linked list implementation of stack in c so that the student may be familiar with both the implementation --- Source Code/Stack.c | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Source Code/Stack.c diff --git a/Source Code/Stack.c b/Source Code/Stack.c new file mode 100644 index 0000000..d74fe97 --- /dev/null +++ b/Source Code/Stack.c @@ -0,0 +1,73 @@ +// C program for array implementation of stack +#include +#include +#include + +// A structure to represent a stack +struct Stack { + int top; + unsigned capacity; + int* 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)); + stack->capacity = capacity; + stack->top = -1; + stack->array = (int*)malloc(stack->capacity * sizeof(int)); + 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, int 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 +int pop(struct Stack* stack) +{ + if (isEmpty(stack)) + return INT_MIN; + return stack->array[stack->top--]; +} + +// Function to return the top from stack without removing it +int peek(struct Stack* stack) +{ + if (isEmpty(stack)) + return INT_MIN; + return stack->array[stack->top]; +} + +// Driver program to test above functions +int main() +{ + struct Stack* stack = createStack(100); + + push(stack, 10); + push(stack, 20); + push(stack, 30); + + printf("%d popped from stack\n", pop(stack)); + + return 0; +}