mirror of
https://github.com/Manoj-HV30/ds-lab-codes.git
synced 2026-05-16 19:35:22 +00:00
init
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
typedef struct {
|
||||
int top;
|
||||
int capacity;
|
||||
char *array;
|
||||
} stack;
|
||||
|
||||
stack *createStack(int c) {
|
||||
stack *Stack = (stack *)malloc(sizeof(stack));
|
||||
if (!Stack) {
|
||||
return NULL;
|
||||
}
|
||||
Stack->capacity = c;
|
||||
Stack->top = -1;
|
||||
Stack->array = (char *)malloc(Stack->capacity);
|
||||
if (!Stack->array) {
|
||||
free(Stack);
|
||||
return NULL;
|
||||
}
|
||||
return Stack;
|
||||
}
|
||||
|
||||
bool isfull(stack *s) { return s->top == s->capacity - 1; }
|
||||
bool isempty(stack *s) { return s->top == -1; }
|
||||
|
||||
int push(stack *s, char v) {
|
||||
if (isfull(s)) {
|
||||
printf("stack overflow");
|
||||
return -1;
|
||||
}
|
||||
s->array[++s->top] = v;
|
||||
return s->top;
|
||||
}
|
||||
int pop(stack *s) {
|
||||
if (isempty(s)) {
|
||||
printf("empty");
|
||||
return -1;
|
||||
}
|
||||
return s->array[s->top--];
|
||||
}
|
||||
|
||||
void display(stack *s) {
|
||||
for (int i = 0; i <= s->top; i++) {
|
||||
printf("%c", s->array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
stack *ms = createStack(3);
|
||||
|
||||
push(ms, 'a');
|
||||
push(ms, 'b');
|
||||
display(ms);
|
||||
}
|
||||
Reference in New Issue
Block a user