Take BTech Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Can we store different data types in a stack?

Shiladitya Munshi
25/11/2016 0 0

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.

#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....");
        p->topVal = p->topVal + 1;
        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....");
        p->topVal = p->topVal + 1;

0 Dislike
Follow 0

Please Enter a comment


Other Lessons for You

RCM Not Applicable To Intra-State Supply Of Goods & Services Below Value Of Rs. 5000
RCM not applicable to Intra-State Supply of Goods & Services below value of Rs. 5000 RCM not applicable to Intra-State (within one state) Supply of Goods & Services below the value of Rs. 5000...

Some formulas of algebra
Algebra: 1. (a + b) = a + 2ab + b 2. (a – b) = a – 2ab + b 3. (a + b) (a – b) = a – b 4. (x + a)(x + b) = x + (a + b)x + ab 5. (x + a)(x – b) = x + (a – b)x –...

Engineering Mechanics Preparation
1.Engineering Mechanics subject becomes easy if Fundamental cocepts are learnt correctly. 2.Take time to learn basics. Then any problem can be solved easily. 3.Practicing number of problems gives us...

Pointers Concept
Every variable has a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following...

Try to clear up the basics, if basics are clear then you can go ahead with any difficult problem
Hey guys, To all the students i just want to convey that just clear up your basics so that they can help you solve anyu problem and you would achieve a great success. Regards, Ishani Chakraborty

Looking for BTech Tuition Classes?

The best tutors for BTech Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BTech Tuition with the Best Tutors

The best Tutors for BTech Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more