ellie0414
ellie0414

Reputation: 81

Can I push string and int values onto a stack?

I need my stack to take in both int and char arrays (strings). any thoughts? I know currently I have the pop and push commands operating with integers. would a different type of structure work better? I seem to recall certain structures being able to take in different arguments. would it help if i defined different functions to handle the char array? I know you can not overload in c. my other thought was to have the stack take in strings and convert between strings and int as needed but that seems a little dicey constant switching between two variable types.

typedef struct Stack
{
int capacity;       // max # of elements the stack can hold
int size;           // current size of the stack
int *elements;      // the array of elements
}Stack;

Stack * createStack(int maxElements)
{        

Stack *S;        
S = (Stack *)malloc(sizeof(Stack));     
S->elements = (int *)malloc(sizeof(int)*maxElements);        
S->size = 0;        
S->capacity = maxElements;      
return S;
}


// STACK COMMANDS


void pop(Stack *S)
{               
if(S->size==0)        
{                
    printf("Stack is Empty\n");                
return;        
}        

else        
{                
    S->size--;        
}        

return;
}

int top(Stack *S)
{        
if(S->size==0)        
{                
    printf("Stack is Empty\n");               
    exit(0);        
}               
return S->elements[S->size-1];
}

void push(Stack *S,int element)
{                
if(S->size == S->capacity)        
{                
    printf("Stack is Full\n");        
}        
else        
{                               
    S->elements[S->size++] = element;        
}        
return;
}

Upvotes: 0

Views: 3192

Answers (2)

paddy
paddy

Reputation: 63481

You can use unions if you want. Effectively what you are after is a variant datatype. So I'll call it Variant:

typedef enum VariantType {
    v_int,
    v_string
} EVariantType;

typedef struct Variant
{
    EVariantType type;
    union {
        int m_int;
        char * m_string;
    };
} SVariant;

From memory, I'm pretty sure this anonymous union is okay. That means you can store integers:

SVariant v;
v.type = v_int;
v.m_int = 42;

and strings:

SVariant v;
v.type = v_string;
v.m_string = strdup( "Hello, World!" );

I used enums here for generality, because you can easily expand to other types later (as opposed to having a flag that indicates whether it is an int or not an int).

When you come to use the data, of course, you can handle it in a switch statement:

switch( v.type )
{
    case v_int:  printf( "Integer value: %d\n", v.m_int ); break;
    case v_string: printf( "String value: %s\n", v.m_string ); break;
}

Your stack is now declared as:

typedef struct Stack
{
    int capacity;       // max # of elements the stack can hold
    int size;           // current size of the stack
    SVariant *elements; // the array of elements
}Stack;

Hope that helps.

Upvotes: 2

imreal
imreal

Reputation: 10378

You could create a union type:

union mType
{
    int integer;
    char *string;
};

Then you can just make mType your elements type.

EDIT:

unions are similar to structs in the sense that they have data elements that can be accessed by the . and -> operators. But the memory for each member is overlapped.

Upvotes: 0

Related Questions