UrbanPro
true

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.

#include
#include
#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;

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

smart work with patience creates victory
In a village named ramapuram aa unemployee gopi lives. He is in search of job, roaming without food from a week. He decided to sell the wooden sticks by chopping the tree, he started chopping the trunk...

What Is The Difference Between Scope And Lifetime?
Scope of a variable is defined as the block of code from where we can refer or access it. On the other hand the life time of a variable is defined as the time in between allocating memory for it and relinquishing...

How to start writing a program using a Language like C?
Usually the starters get confused of certain things like which proagramming tool they would use and why. Like where should one use "for" loop and where to use "while". However, the most common problem...

How To Write An Assignment?
Instructions for writing the Assignment: It should contain minimum of 7 pages to maximum of 10 pages. Write all the topics with clear headings, sub headings and points with serial numbers 1,2,3,4,5...

Vocabulary Note
CAT Vocabulary:The skills tested in Cat are things more than just English as a language. Cat is an entrance for post graduate management programmes at the premier bschools of India and the test is designed...
X

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