Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latest branch #7288

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions Stack/stacks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include<stdio.h>
#include<mm_malloc.h>

// header for boolean data types true/false
#include <stdbool.h>

#define INT_MIN -9999



// defining a new user-defined
// data type STACK -> mySTACK
typedef struct STACK
{
int *array;
int max_size;
int top;
}mySTACK;


// operation supported on STACK
mySTACK* init_stack(int max_size);
void push(mySTACK *s, int x);
void show_stack(mySTACK *s);
int pop(mySTACK *s);
int get_top(mySTACK *s);
void delete_stack(mySTACK *s);
bool check_stack_overflow(mySTACK *s);
bool check_stack_underflow(mySTACK *s);


int main()
{
mySTACK *s1;
mySTACK *s2;
int x;
// Calling init_stack
s1 = init_stack(10);
//printf("\n Line 28 s1->top=%d",s1->top);

// Adding elelments by calling push function
push(s1, 4);
push(s1, 1);
push(s1, 19);
show_stack(s1);
x = pop(s1);
printf("\nDeleted element is: %d\n", x);
show_stack(s1);

return 0;
}

mySTACK* init_stack(int max_size)
{
mySTACK *s;
//Allocate memory for stack
s = malloc(sizeof(mySTACK));
if(s==NULL)
return NULL;
// allocate memory for array
s->array = malloc(sizeof(int) * max_size);
// free the stack memory if array memory not allocated
if(s->array ==NULL)
{
free(s);
return NULL;
}
// Assigning the max_size and top
s->max_size = max_size;
s->top = -1;
return s;

}

void push(mySTACK *s, int x)
{
if(check_stack_overflow(s))
printf("\t STACK overflow \n");
else
{
s->top +=1;
s->array[s->top] = x;
}
}

void show_stack(mySTACK *s)
{
int i;
printf("\t STACK elements are: ");
for(i=0; i<=s->top; i++)
printf("%d ", s->array[i]);
printf("\n");
}

int pop(mySTACK *s)
{
int x;
if(check_stack_underflow(s))
{
printf("\n STACK Underflow \n");
return INT_MIN;
}
else
{
x = s->array[s->top];
s->top -=1;
return x;
}
}

int get_top(mySTACK *s)
{
int x;
if(check_stack_underflow(s))
{
printf("\n STACK Underflow \n");
return INT_MIN;

}
else
{
x = s->array[s->top];
return x;
}
}

bool check_stack_overflow(mySTACK *s)
{
return (s->max_size -1 == s->top);
}

bool check_stack_underflow(mySTACK *s)
{
return (s->top ==-1);
}

void delete_stack(mySTACK *s)
{
if(s)
{
if(s->array)
free(s->array);
free(s);
}
}
145 changes: 145 additions & 0 deletions Stack/stacks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include<stdio.h>
#include<mm_malloc.h>

// header for boolean data types true/false
#include <stdbool.h>

#define INT_MIN -9999



// defining a new user-defined
// data type STACK -> mySTACK
typedef struct STACK
{
int *array;
int max_size;
int top;
}mySTACK;


// operation supported on STACK
mySTACK* init_stack(int max_size);
void push(mySTACK *s, int x);
void show_stack(mySTACK *s);
int pop(mySTACK *s);
int get_top(mySTACK *s);
void delete_stack(mySTACK *s);
bool check_stack_overflow(mySTACK *s);
bool check_stack_underflow(mySTACK *s);


int main()
{
mySTACK *s1;
mySTACK *s2;
int x;
// Calling init_stack
s1 = init_stack(10);
//printf("\n Line 28 s1->top=%d",s1->top);

// Adding elelments by calling push function
push(s1, 4);
push(s1, 1);
push(s1, 19);
show_stack(s1);
x = pop(s1);
printf("\nDeleted element is: %d\n", x);
show_stack(s1);

return 0;
}

mySTACK* init_stack(int max_size)
{
mySTACK *s;
//Allocate memory for stack
s = malloc(sizeof(mySTACK));
if(s==NULL)
return NULL;
// allocate memory for array
s->array = malloc(sizeof(int) * max_size);
// free the stack memory if array memory not allocated
if(s->array ==NULL)
{
free(s);
return NULL;
}
// Assigning the max_size and top
s->max_size = max_size;
s->top = -1;
return s;

}

void push(mySTACK *s, int x)
{
if(check_stack_overflow(s))
printf("\t STACK overflow \n");
else
{
s->top +=1;
s->array[s->top] = x;
}
}

void show_stack(mySTACK *s)
{
int i;
printf("\t STACK elements are: ");
for(i=0; i<=s->top; i++)
printf("%d ", s->array[i]);
printf("\n");
}

int pop(mySTACK *s)
{
int x;
if(check_stack_underflow(s))
{
printf("\n STACK Underflow \n");
return INT_MIN;
}
else
{
x = s->array[s->top];
s->top -=1;
return x;
}
}

int get_top(mySTACK *s)
{
int x;
if(check_stack_underflow(s))
{
printf("\n STACK Underflow \n");
return INT_MIN;

}
else
{
x = s->array[s->top];
return x;
}
}

bool check_stack_overflow(mySTACK *s)
{
return (s->max_size -1 == s->top);
}

bool check_stack_underflow(mySTACK *s)
{
return (s->top ==-1);
}

void delete_stack(mySTACK *s)
{
if(s)
{
if(s->array)
free(s->array);
free(s);
}
}
Loading