Created
February 22, 2026 09:20
-
-
Save raiyansarker/5486159be02eb30c4a4d6ab7abcae03d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #define MAX_SIZE 100 | |
| typedef struct Stack { | |
| int arr[MAX_SIZE]; | |
| int top; | |
| } Stack; | |
| Stack* init(); | |
| void push(Stack *s, int data); | |
| int pop(Stack *s); | |
| int peak(Stack *s); | |
| int main() { | |
| Stack *stack = init(); | |
| push(stack, 1); | |
| push(stack, 2); | |
| push(stack, 5); | |
| push(stack, 3); | |
| printf("Peak %d\n", peak(stack)); | |
| printf("%d\n", pop(stack)); | |
| printf("%d\n", pop(stack)); | |
| printf("Peak %d\n", peak(stack)); | |
| printf("%d\n", pop(stack)); | |
| printf("%d\n", pop(stack)); | |
| printf("%d\n", pop(stack)); | |
| return 0; | |
| } | |
| Stack* init() { | |
| Stack *s = (Stack*)malloc(sizeof(Stack)); | |
| s->top = -1; | |
| return s; | |
| } | |
| void push(Stack *s, int data) { | |
| s->arr[++(s->top)] = data; | |
| } | |
| int pop(Stack *s) { | |
| if (s->top == -1) return -1; | |
| return s->arr[s->top--]; | |
| } | |
| int peak(Stack *s) { | |
| if (s->top == -1) return -1; | |
| return s->arr[s->top]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment