Wednesday, September 18, 2013

Can we store different data types in a stack?

Yesterday, one of my Facebook friend asked me this question. My answer is "yes", and in this post I will discuss how could we do this.

I am a great supporter of working with unions and I will be using union for it.

If you are to implement the stack with arrays, then within the stack array you need to maintain a union so that you can store different data types. Along with this you need to have stack top variable and an array to keep track of data type of each array position.

Here is the code. I have written it in great hurry and therefore the code is not fully optimized..but it is running obviously.

#include<stdio.h>
#include<stdlib.h>
#define CHARACTER 1
#define INTEGER 2
#define UNKNOWN 3
#define INVALID_INT -99
#define INVALID_CHAR '~'

union Data
{
    int ival;
    char cval;
};
struct STACK
{
    union Data* arr;
    int topVal;
    int* topType;        
};

struct POPVAL
{
    int pival;
    char pcval;
};
struct STACK* initStack(int size)
{
    int i;
    struct STACK* p;
    p =(struct STACK*)malloc(sizeof(struct STACK));
    p->arr = (union Data*)malloc (size*sizeof(union Data));
   
    p->topVal = -1;
   
    p->topType = (int *)malloc (size*sizeof(int));
    for(i = 0; i<size; i++)
        p->topType[i] = UNKNOWN;
    printf("\n Stack is initialized...");
    return p;    
}

void pushChar(struct STACK* p, int size)
{
   
    if(p->topVal == size - 1)
        printf("\n Overflow Condition Appears.... No Push Posible....");
    else
    {
        p->topVal = p->topVal + 1;
        fflush(stdin);
        printf("\n Enter the character to push:");
        scanf("%c", &p->arr[p->topVal].cval);
        p->topType[p->topVal] = CHARACTER;
        printf("\n Character Push Done....");
    }
}

void pushInt(struct STACK* p, int size)
{
    if(p->topVal == size - 1)
        printf("\n Overflow Condition Appears.... No Push Posible....");
    else
    {
        p->topVal = p->topVal + 1;
        printf("\n Enter the integer to push:");
        scanf("%d", &p->arr[p->topVal].ival);
        p->topType[p->topVal] = INTEGER;
        printf("\n Integer Push Done....");
    }    
}

struct POPVAL* pop(struct STACK* p)
{
    struct POPVAL* poppedVal = NULL;
    if(p->topVal != -1)
    {
        poppedVal = (struct POPVAL*)malloc(sizeof(struct POPVAL));
        poppedVal->pival = INVALID_INT;
        poppedVal->pcval = INVALID_CHAR;
        if(p->topType[p->topVal] == CHARACTER)
        {
            poppedVal->pcval = p->arr[p->topVal].cval;
        }
        if(p->topType[p->topVal] == INTEGER)
        {
            poppedVal->pival = p->arr[p->topVal].ival;
        }
        p->topVal = p->topVal - 1;
    }
    return poppedVal;
            
}
void displayStack(struct STACK* p, int size)
{
    int i;
    printf("\n Displaying Stack ...\n");
    for(i = 0; i<=p->topVal; i++)
    {
        if(p->topType[i] == CHARACTER)
            printf(" %c ", p->arr[i].cval);
        if(p->topType[i] == INTEGER)
            printf(" %d ", p->arr[i].ival);        
    }
}

int main(void)
{
    int maxSize,choice;
    struct STACK* stack;
    struct POPVAL* popstruct;
    printf("What is the max size of the array ?");
    scanf("%d", &maxSize);
    stack = initStack(maxSize);
    do
    {
        printf("\n *************** Menu ***************\n");
        printf("\n 1. Push Character to the Stack");
        printf("\n 2. Push Integer to the Stack");
        printf("\n 3. Pop From the Stack");
        printf("\n 4. Display Stack");
        printf("\n 5. Exit");
        printf("\n\n Enter Your Choice::");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                    pushChar(stack,maxSize);
                    break;
            case 2:
                    pushInt(stack,maxSize);
                    break;
            case 3:
                    popstruct = pop(stack);
                    if(popstruct == NULL)
                        printf("\n Underflow Condition Appears... No Pop Possible....");
                    else
                    {
                        if(popstruct->pival != INVALID_INT)
                            printf("\n Integer Pop Done With Value = %d",popstruct->pival);
                        else
                            printf("\n Character Pop Done With Value = %c",popstruct->pcval);    
                    }
                    break;                                                                
            case 4:
                    displayStack(stack, maxSize);
                    break;
            
            case 5:
                    printf("\n Exiting From The Program...");
                    break;            
            default:
                    printf("\n Enter A Valid Choice...");
        }
        
    }while(choice!=5);
    printf("\n Thanks for using BunksAllowed Code....\n\n");
    return 0;
}

The same thing you need to do if you are to implement the stack with a linked list. In this case, the nodes of the linked list must contain a union to store different data types, along with a typical next pointer and a special variable to inform which node is containing which data type.

Happy Learning!
   

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.