Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
using namespace std;
struct Stack {
    int top;
    int capacity;
    int *array;
};
struct Stack *createStack(int capacity){
    struct Stack *S=new struct Stack;
    if(!S)
        return NULL;
    S->capacity=capacity;
    S->top=-1;
    S->array=new int[capacity];
    if (!S->array)
        return NULL;
    return S;
}
int isEmpty(struct Stack *S){
    return(S->top==-1);
}
int size(struct Stack *S){
    return(S->top+1);
}
int isFull(struct Stack *S){
    return(S->top==S->capacity-1);
}
void doubleStack(struct Stack *S){
    S->capacity*=2;
    S->array=realloc(S->array,S->capacity * sizeof(int));
}
void push(struct Stack *S,int data){
    if (isFull(S))
        doubleStack(S);
    else
        S->array[++S->top]=data;
}
int pop(struct Stack *S){
    if (isEmpty(S)){
        cout<<"Stack is Empty"<<endl;
        return INT16_MIN;
    }
    else
        return(S->array[S->top--]);
}
int peek(struct Stack *S){
    if (isEmpty(S)){
        cout<<"Stack is Empty"<<endl;
        return INT16_MIN;
    }
    else
        return(S->array[S->top]);
}
void deleteStack(struct Stack *S){
    if (S){
        if (S->array){
            delete S->array;
            delete S;
        }
    }
}
int main() {
    int capacity=5;
    struct Stack *stack= createStack(capacity);
    for (int i = 0; i < 2*capacity; ++i) {
        push(stack,i);
    }
    cout<<"top:"<<peek(stack)<<endl;
    cout<<"size:"<<size(stack)<<endl;
    for (int i = 0; i < capacity; ++i) {
        cout<<pop(stack)<<" ";
    }
    cout<<endl;
    if (isEmpty(stack))
        cout<<"Stack is Empty"<<endl;
    else
        cout<<"Stack is not Empty"<<endl;
    deleteStack(stack);
    return 0;
}


What I have tried:

void doubleStack(struct Stack *S){
    S->capacity*=2;
    S->array=realloc(S->array,S->capacity * sizeof(int));
}
Posted
Updated 24-Apr-22 12:10pm
v2
Comments
Tony Hill 24-Apr-22 14:51pm    
As you have not told us what the error is or where it occurs it is very unlikely anyone can help.
KarstenK 24-Apr-22 14:52pm    
Where is the problem? Have you tried some debugging?
Rishabh Vishwakarma 2022 24-Apr-22 15:52pm    
void doubleStack(struct Stack *S){
S->capacity*=2;
S->array=realloc(S->array,S->capacity * sizeof(int));
}
how i can fix this code?
OriginalGriff 24-Apr-22 16:30pm    
:sigh:
How do you expect us to know how to fix code when we have no idea what it should do, what it actually does do, what you feed it to make it do that, or what you have tried to fix it?

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.

1 solution

Presumably the compiler would say that realloc returns a void*. But since an int* is expected here, a cast will probably be necessary.

I would advise against mixing new and malloc/realloc.
If this is C, new would not be an option.

If it's C++, why not use a class for the stack?

In addition, a std::vector would be much more useful in C++ than an array, then it doesn't matter the std library will take care of that or you can use resize.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900