Stack implementation with array - c

I am trying to implement stack with array in C. But I guess my push function is not correct.(Maybe there are some other mistakes) Because when I run the code, it prints "Stack is empty!" two times.
How can I solve this problem and is this implementation logic is true?
#include <stdio.h>
#define SIZE 10
typedef struct stack
{
int top;
int items[SIZE];
}stack;
void push(int a, stack st)
{
if((st.top + 1) != SIZE)
{
st.top++;
st.items[st.top] = a;
}
else
{
printf("\nStack is full!");
}
}
void pop(stack st)
{
if(st.top != -1)
{
st.top--;
}
else
{
printf("\nStack is empty!");
}
}
void printList(stack st)
{
int i;
for(i = 0; i < st.top + 1; i++)
{
printf("%d -> ", st.items[i]);
}
puts("");
}
int main(void)
{
stack stack1;
stack1.top = -1;
stack stack2;
stack2.top = -1;
push(3, stack1);
push(5, stack1);
push(7, stack1);
printList(stack1);
pop(stack1);
printList(stack1);
pop(stack1);
printList(stack1);
}

Hi your stack implementation is wrong.Using gdb you can verify this.You are passing structure as value you should pass as address.
On gdb you can see
In main
gdb) p &stack1
$4 = (stack *) 0x7fffffffddf0
In push fn
(gdb) p &st
$3 = (stack *) 0x7fffffffdd90
both are different.
correct code is given below.
#include <stdio.h>
#define SIZE 10
typedef struct stack
{
int top;
int items[SIZE];
}stack;
void push(int a, stack *st)
{
if((st->top + 1) != SIZE)
{
st->top++;
st->items[st->top] = a;
}
else
{
printf("\nStack is full!");
}
}
void pop(stack *st)
{
if(st->top != -1)
{
st->top--;
}
else
{
printf("\nStack is empty!");
}
}
void printList(stack *st)
{
int i;
for(i = 0; i < st->top + 1; i++)
{
printf("%d -> ", st->items[i]);
}
puts("");
}
int main(void)
{
stack stack1;
stack1.top = -1;
stack stack2;
stack2.top = -1;
push(3, &stack1);
push(5, &stack1);
push(7, &stack1);
printList(&stack1);
pop(&stack1);
printList(&stack1);
pop(&stack1);
printList(&stack1);
}

Related

Stack balanced Parentheses build log show :- Process terminated with status -1073741510 (0 minute(s), 2 second(s))

When I try to implement parenthesis problem using stack (array representation) it showing above problem. Here I use dynamic memory allocation in array. When I try to compile the above program it appear built log like : process terminated with status -1073741510 (0 minute(s), 2 second(s))
#include<stdlib.h>
struct stack
{
int size;
int top;
char *arr;
};
int parenthematch(char *pt)
{
struct stack *st;
st->size = 100;
st->top = -1;
st->arr = (char *)malloc(st->size * sizeof(char)); //create array of st->size
for(int i=0; pt[i]!='\0'; i++)
{
if(pt[i]=='(')
{
push(st,'(');
}
else if(pt[i]==')')
{
if(isEmpty(st))
{
return 0;
}
pop(st);
}
}
int main()
{
char *p ="(34)(4(5+6))";
if(parenthematch(p))
{
printf("parenthesis match \n");
}
else
{
printf("Not match");
}
return 0;
} ```
#include <stdio.h>
#include <stdlib.h>
struct stack
{
size_t size;
int top;
char *arr;
};
void push(struct stack *st, char ch)
{
st->top += 1;
st->arr[st->top] = ch;
}
int isEmpty(struct stack *st)
{
return st->top == -1;
}
void pop(struct stack *st)
{
st->top -= 1;
}
int parenthematch(char *pt)
{
struct stack *st = (struct stack *)malloc(sizeof(struct stack));
st->size = 100;
st->top = -1;
st->arr = (char *)malloc(st->size * sizeof(char)); //create array of st->size
for (int i = 0; pt[i] != '\0'; i++)
{
if (pt[i] == '(')
{
push(st, '(');
}
else if (pt[i] == ')')
{
if (isEmpty(st) || st->arr[st->top] != '(')
{
return 0;
}
pop(st);
}
}
return isEmpty(st);
}
int main()
{
char p[] = "(34)(4(5+6))";
if (parenthematch(p))
{
printf("parenthesis match \n");
}
else
{
printf("Not match");
}
return 0;
}

Sorting Stack by implementing as Arrays

I saw a problem on the web,i.e, I am trying to sort a stack using another reference stack in C. I tried to do it by implementing it as an array but it is not working.
(condition is that you have to use only arrays)
#include <stdio.h>
int MAXSIZE = 5;
int stack[5];
int tmpstack[5];
int tmp;
int top = -1;
int top2=-1;
int isempty(int A[],int B) {
if(B == -1)
return 1;
else
return 0;
}
int isfull(int A[],int B) {
if(B == MAXSIZE)
return 1;
else
return 0;
}
int pop(int A[],int B) {
int data;
if(!isempty(A,B)) {
data = A[B];
B = B - 1;
return data;
}
else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
int push(int data,int A[],int B) {
if(!isfull(A,B)) {
B = B + 1;
A[B] = data;
}
else {
printf("Could not insert data, Stack is full.\n");
}
}
int sortStack(int stack[])
{
int tmpStack[MAXSIZE];
while (!isempty(stack,top))
{
int tmp = stack[top];
pop(stack,top);
while (!isempty(tmpstack,top2) && tmpstack[top2] > tmp)
{
push(pop(tmpstack,top2),stack,top);
}
push(tmp,tmpstack,top2);
}
return 0;
}
int main() {
int n ,a;
n=5;
for(int i = 0;i < n ; ++i){
scanf("%d",&a);
push(a,stack,top);
}
sortStack(stack);
while (!isempty(tmpstack,top2))
{
printf("%d ",tmpstack[top2]);
pop(tmpstack,top2);
}
return 0;
}
What I did here is that I made similar functions in arrays which we used in stacks only and we are supposed to use them only for implementing our stacks.

When I run the stackAry, the main part doesn't print

How should I print StackArray in C?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct
{
void** stackAry;
int count;
int stackMax;
int top;
}STACK;
STACK* createStack(int maxSize)
{
STACK* stack;
stack = (STACK*)malloc(sizeof(STACK));
if(!stack)
return NULL;
stack->count = 0;
stack->top = -1;
stack->stackMax = maxSize;
stack->stackAry = (void**)calloc(stack->stackMax, sizeof(void*));
if(!stack->stackAry)
{
free(stack);
return NULL;
}
return stack;
}
bool pushStack(STACK* stack, void* itemPtr)
{
if(stack->count == stack->stackMax)
return false;
(stack->count)++;
(stack->top)++;
stack->stackAry[stack->top] = itemPtr;
return true;
}
void* popStack(STACK* stack)
{
void* dataPtrOut;
if(stack->count == 0)
dataPtrOut = NULL;
else
{
dataPtrOut = stack->stackAry[stack->top];
(stack->count)--;
(stack->top)--;
}
return dataPtrOut;
}
void* stackTop(STACK* stack)
{
if(stack->count == 0)
return NULL;
else
return stack->stackAry[stack->top];
}
bool emptyStack(STACK* stack)
{
return(stack->top == -1);
}
bool fullStack(STACK* stack)
{
return (stack->count == stack->stackMax);
}
int stackCount(STACK* stack)
{
return stack->count;
}
STACK* destroyStack(STACK* stack)
{
int i;
if(stack)
{
for(i=0 ; i<stack->count ; i++)
free(stack->stackAry[i]);
free(stack->stackAry);
free(stack);
}
return NULL;
}
int main(void)
{
STACK* myStack = createStack(100);
double* ptr;
while(1)
{
ptr = (double*)malloc(sizeof(double));
if(ptr == NULL)
return 0;
else
{
printf("Enter a positive real number: <-1> to stop: \n");
scanf("%lf", ptr);
if(*ptr == -1.0 || fullStack(myStack))
{
break;
}
else
pushStack(myStack, ptr);
}
}
printf("\n\nThe list of numbers reversed: \n");
while(!emptyStack(myStack))
{
ptr = (double*)popStack(myStack);
printf("%lf\n", *ptr);
free(ptr);
}
myStack = destroyStack(myStack);
return 0;
}
When I run the stackAry, the main part doesn't print.
The stack part does not seem to be a problem, but if you print it, "Enter a positive real number: <-1> to stop:" is not printed. If you enter numbers on a blank screen and press -1, it will be printed.
I'm not sure which part is wrong. Please help me

Problems with implementing a stack in C

I tried to implement to stack in C. Here is my code, I ran the code, the code ended with:
Thread 1: EXC_BAD_ACCESS error
I am so confused, don't know what went wrong, can anyone debug y code? Thanks!
And I have one more question, why command+k key didn't work for me? I have to indent line by line.
#include <stdio.h>
#include <stdlib.h>
#define init_size 10
#define increment 1
typedef struct sqStack
{
int* top;
int* base;
int stack_size;
} sqStack;
int init_stack(sqStack* sq)
{
if(sq->base==NULL)
{
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
}
if(sq->base==NULL) exit(-1);
sq->stack_size=init_size;
sq->top=sq->base;
return 1;
}
int push(sqStack* sq, int e)
{
if(sq==NULL) exit(-1);
if(sq->top-sq->base==sq->stack_size)
{
int* q = (int*)realloc(sq->base,
(init_size+increment)*sizeof(int));
if(q==NULL) exit(-1);
else
{
sq->base=q;
sq->stack_size += increment;
sq->top=sq->base+sq->stack_size;
}
*sq->top++=e;
}
return 1;
}
int pop(sqStack* sq,int*e)
{
if(sq==NULL) exit(-1);
if(sq->base==sq->top) exit(-1);
sq->top-=1;
*e=*sq->top;
return 1;
}
int empty(sqStack* sq)
{
if(sq->base==sq->top) return 1;
else return 0;
}
int main()
{
sqStack* sq=NULL;
init_stack(sq);
push(sq,1);
int *e=(int*)malloc(sizeof(int));
pop(sq,e);
printf("%d\n",*e);
return 0;
}
In function int init_stack(sqStack* sq)
the
if(sq->base==NULL)
{
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
}
should be:
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
In function int push(sqStack* sq, int e)
the
}
*sq->top++=e;
}
return 1;
}
should be
}
}
*sq->top++=e;
return 1;
}
In function int main()
the is not need use int *e=(int*)malloc(sizeof(int)); just use int e;
the
sqStack* sq=NULL;
init_stack(sq);
push(sq,1);
int *e=(int*)malloc(sizeof(int));
pop(sq,e);
printf("%d\n",*e);
should be
sqStack sq;
init_stack(&sq);
push(&sq,1);
int e;
pop(sq,&e);
printf("%d\n",e);
The following code could work:
#include <stdio.h>
#include <stdlib.h>
#define init_size 10
#define increment 1
typedef struct sqStack {
int* top;
int* base;
int stack_size;
} sqStack;
int init_stack(sqStack* sq) {
sq->base = (int*)malloc(init_size * sizeof(int));
if (sq->base == NULL) exit(-1);
sq->stack_size = init_size;
sq->top = sq->base;
return 1;
}
int push(sqStack* sq, int e) {
if (sq == NULL) exit(-1);
if (sq->top - sq->base == sq->stack_size) {
int* q = (int*)realloc(sq->base, (init_size + increment) * sizeof(int));
if (q == NULL)
exit(-1);
else {
sq->base = q;
sq->stack_size += increment;
sq->top = sq->base + sq->stack_size;
}
}
*sq->top++ = e;
return 1;
}
int pop(sqStack* sq, int* e) {
if (sq == NULL) exit(-1);
if (sq->base == sq->top) exit(-1);
sq->top -= 1;
*e = *sq->top;
return 1;
}
int empty(sqStack* sq) {
if (sq->base == sq->top)
return 1;
else
return 0;
}
int main() {
sqStack sq;
init_stack(&sq);
push(&sq, 1);
int e;
pop(&sq, &e);
printf("%d\n", e);
return 0;
}

Implementing Stack using Dynamic Array

I have implemented a Stack using dynamic array(implementing array doubling) but when the doubling happens for the second time, I am getting runtime error! What's going wrong with the implementation? Please help.
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int *arr;
int top,size,capacity;
};
struct Stack *createStack(int capacity) {
struct Stack *s = (struct Stack*)malloc(sizeof(struct Stack));
s->arr = (int*)malloc(sizeof(int)*capacity);
s->top = -1;
s->capacity = capacity;
s->size = 0;
return s;
}
void doubleStack(struct Stack *s) {
s->capacity = s->capacity*2;
s->arr = realloc(s->arr,s->capacity);
printf("Array doubling happened successfully!\n");
}
int isFull(struct Stack *s) {
return s->size == s->capacity;
}
void push(struct Stack *s, int item) {
if(isFull(s))
doubleStack(s);
printf("%d pushed!\n",item);
s->arr[++(s->top)] = item;
s->size++;
}
int isEmpty(struct Stack *s) {
return s->size == 0;
}
void pop(struct Stack *s) {
if(isEmpty(s)) {
printf("Empty stack!\n");
return;
}
int item = s->arr[(s->top)--];
s->size--;
printf("%d popped!\n",item);
}
int main(void) {
struct Stack *s = createStack(2);
push(s,1);
push(s,2);
push(s,3);
push(s,4);
push(s,5);
pop(s);
pop(s);
return 0;
}
You failed to multiply the size to allocate via realloc() by sizeof(int).
Try this with some other improvement:
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int *arr;
int top,size,capacity;
};
struct Stack *createStack(int capacity) {
struct Stack *s = malloc(sizeof(struct Stack));
s->arr = malloc(sizeof(int)*capacity);
s->top = -1;
s->capacity = capacity;
s->size = 0;
return s;
}
void destroyStack(struct Stack *s) {
if(s != NULL) free(s->arr);
free(s);
}
void doubleStack(struct Stack *s) {
s->capacity = s->capacity*2;
s->arr = realloc(s->arr,sizeof(int)*s->capacity);
if(s->arr != NULL) {
printf("Array doubling happened successfully!\n");
} else {
perror("realloc");
}
}
int isFull(struct Stack *s) {
return s->size == s->capacity;
}
void push(struct Stack *s, int item) {
if(isFull(s))
doubleStack(s);
printf("%d pushed!\n",item);
s->arr[++(s->top)] = item;
s->size++;
}
int isEmpty(struct Stack *s) {
return s->size == 0;
}
void pop(struct Stack *s) {
if(isEmpty(s)) {
printf("Empty stack!\n");
return;
}
int item = s->arr[(s->top)--];
s->size--;
printf("%d popped!\n",item);
}
int main(void) {
struct Stack *s = createStack(2);
push(s,1);
push(s,2);
push(s,3);
push(s,4);
push(s,5);
pop(s);
pop(s);
destroyStack(s);
return 0;
}

Resources