diff --git a/algorithms/C/README.md b/algorithms/C/README.md index cddb7614..37bd98e3 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -69,3 +69,8 @@ - [Jump Search](searching/Jump-search.c) - [Ternary Search](searching/Ternary-search.c) - [Interpolation Search](searching/Interpolation-search.c) + +## Stacks + +- [Stack using arrays](stacks/stack-using-arrays.c) +- [Stack using Linked List](stacks/stack-using-linked-list.c) \ No newline at end of file diff --git a/algorithms/C/stacks/stack-using-arrays.c b/algorithms/C/stacks/stack-using-arrays.c new file mode 100644 index 00000000..1ea1695f --- /dev/null +++ b/algorithms/C/stacks/stack-using-arrays.c @@ -0,0 +1,191 @@ +// Implementation of stack using array + +/* +Implement the following stack operations : +1. Push +2. Pop +3. Peek +4. Display +*/ + +#include +#include + +#define N 50 + +int stack[N]; +int top = -1; + +// Push Element at the top of stack +void push() +{ + int x; + printf("Enter The Element to push: "); + scanf("%d", &x); + if (top == N - 1) + { + printf("Stack overflow\n"); + } + else + { + top++; + stack[top] = x; + printf("%d pushed to Stack\n", x); + } +} + +// Pop the top-element from the stack +void pop() +{ + if (top == -1) + { + printf("Stack underflow\n"); + } + else + { + int item = stack[top]; + top--; + printf("Popped: %d\n", item); + } +} + +// Prints the top-most element +void peek() +{ + if (top == -1) + { + printf("Stack is empty\n"); + } + else + { + printf("Top Element: %d\n", stack[top]); + } +} + +// Prints the stack +void display() +{ + printf("Displaying Stack....\n"); + for (int i = top; i >= 0; i--) + { + printf("%d ", stack[i]); + } + printf("\n"); +} + +int main() +{ + int ch; + + while (1) + { + + printf("\n*** Stack Menu ***"); + printf("\n\n1.Insert\n2.Delete\n3.Display\n4.Peek\n5.Exit"); + printf("\n\nEnter your choice(1-4):"); + scanf("%d", &ch); + switch (ch) + { + case 1: + push(); + break; + case 2: + pop(); + break; + case 3: + display(); + break; + case 4: + peek(); + break; + case 5: + exit(0); + default: + printf("\nWrong Choice!!"); + } + } + return 0; +} + +/* +Output: +*** Stack Menu *** + +1.Insert +2.Delete +3.Display +4.Peek +5.Exit + +Enter your choice(1-4):1 +Enter The Element to push: 4 +4 pushed to Stack + +*** Stack Menu *** + +1.Insert +2.Delete +3.Display +4.Peek +5.Exit + +Enter your choice(1-4):1 +Enter The Element to push: 3 +3 pushed to Stack + +*** Stack Menu *** + +1.Insert +2.Delete +3.Display +4.Peek +5.Exit + +Enter your choice(1-4):1 +Enter The Element to push: 2 +2 pushed to Stack + +*** Stack Menu *** + +1.Insert +2.Delete +3.Display +4.Peek +5.Exit + +Enter your choice(1-4):2 +Popped: 2 + +*** Stack Menu *** + +1.Insert +2.Delete +3.Display +4.Peek +5.Exit + +Enter your choice(1-4):4 +Top Element: 3 + +*** Stack Menu *** + +1.Insert +2.Delete +3.Display +4.Peek +5.Exit + +Enter your choice(1-4):3 +Displaying Stack.... +3 4 + +*** Stack Menu *** + +1.Insert +2.Delete +3.Display +4.Peek +5.Exit + +Enter your choice(1-4):5 +*/ diff --git a/algorithms/C/stacks/stack-using-linked-list.c b/algorithms/C/stacks/stack-using-linked-list.c new file mode 100644 index 00000000..4d4a17a7 --- /dev/null +++ b/algorithms/C/stacks/stack-using-linked-list.c @@ -0,0 +1,221 @@ +// Implementation of stack using Linked List + +/* +Implement the following stack operations : +1. Push +2. Pop +3. Peek +4. Display +*/ + +#include +#include + +struct Stack +{ + int data; + struct Stack *next; +}; + +struct Stack *top = NULL; + +// Push Element at the top of stack +void push() +{ + int x; + printf("Enter Element to be pushed\n"); + scanf("%d", &x); + struct Stack *newNode = (struct Stack *)malloc(sizeof(struct Stack)); + newNode->data = x; + newNode->next = top; + top = newNode; +} + +// Pop the top-element from the stack +void pop() +{ + struct Stack *temp = (struct Stack *)malloc(sizeof(struct Stack)); + temp = top; + if (top == NULL) + { + printf("Stack is empty\n"); + } + else + { + printf("Popped Element is %d\n", top->data); + top = top->next; + free(temp); + } +} + +// Prints the top-most element +void peek() +{ + if (top == NULL) + { + printf("Stack is empty\n"); + } + else + { + printf("Top element is %d\n", top->data); + } +} + +// Prints The Stack +void display() +{ + struct Stack *temp = top; + if (top == NULL) + { + printf("Stack is empty\n"); + } + else + { + while (temp != NULL) + { + printf("%d ", temp->data); + temp = temp->next; + } + } + printf("\n"); +} + +// Delete Entire Stack +void deleteStack() +{ + struct Stack *temp = top; + if (top == NULL) + { + printf("Stack is empty\n"); + } + else + { + while (temp != NULL) + { + top = top->next; + free(temp); + temp = top; + } + } +} + +int main() +{ + int opt; + while (1) + { + + printf("which operation do you want to perform?\n"); + printf("1.Push\n"); + printf("2.Pop\n"); + printf("3.Peek\n"); + printf("4.Display\n"); + printf("5.Delete Entire Stack\n"); + printf("6.Exit\n"); + + scanf("%d", &opt); + + switch (opt) + { + case 1: + push(); + break; + case 2: + pop(); + break; + case 3: + peek(); + break; + case 4: + display(); + break; + case 5: + deleteStack(); + break; + case 6: + exit(0); + break; + default: + printf("Unknown operation\n"); + } + } + return 0; +} + +/* +Output: +which operation do you want to perform? +1.Push +2.Pop +3.Peek +4.Display +5.Delete Entire Stack +6.Exit +1 +Enter Element to be pushed +10 +which operation do you want to perform? +1.Push +2.Pop +3.Peek +4.Display +5.Delete Entire Stack +6.Exit +1 +Enter Element to be pushed +5 +which operation do you want to perform? +1.Push +2.Pop +3.Peek +4.Display +5.Delete Entire Stack +6.Exit +1 +Enter Element to be pushed +3 +which operation do you want to perform? +1.Push +2.Pop +3.Peek +4.Display +5.Delete Entire Stack +6.Exit +4 +3 5 10 +which operation do you want to perform? +1.Push +2.Pop +3.Peek +4.Display +5.Delete Entire Stack +6.Exit +2 +Popped Element is 3 +which operation do you want to perform? +1.Push +2.Pop +3.Peek +4.Display +5.Delete Entire Stack +6.Exit +5 +which operation do you want to perform? +1.Push +2.Pop +3.Peek +4.Display +5.Delete Entire Stack +6.Exit +4 +Stack is empty + +which operation do you want to perform? +1.Push +2.Pop +3.Peek +4.Display +5.Delete Entire Stack +6.Exit +6 +*/ \ No newline at end of file