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

For B.Tech Students: Applied Chemistry - First Year.
The dispersity index, or PDI/ polydispersity index is measure of the distributon of molecular mass in a given polymer sample. Lets try an example:

Few Operating System Question - Answers for Fresher Level Interview
What is kernel? How it is different from shell? Kernel is the core portion of the Operating system which gets loaded into the primary memory at the time of bootstrapping. It allocates the memory for the...

Java 9 , the new beginning
Java 9 is here! A major feature release in the Java Platform Standard Edition is Java 9 Lets see what more it offers more than its previous versions Java platform module JEP 223 : New version...
G

GCC

0 0
0

Classification of Electrical Machines
Electric machine is a general term for electric motors and generators. They are electromechanical energy converters i.e. an electric motor converts electrical energy in to mechanical energy whereas a generator...

Electronics MADE easy
As everyone knows for understanding any subject in detail we have go through the basics of that subject. But sometimes we tend to forget the basics which are required for the advanced subjects. So knowing...
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