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

Memory Layout of C Programs
A typical memory representation of C program consists of following sections. Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an object...

STUDY TIP
EAT TULSI LEAVES EVERY MORNING AND DRINK WATER AFTER THAT

Review of Thermo
Which of the following process can be made reversible with the help of a regenerator? (A) Constant pressure process (B) Constant volume process (C) Constant pvn process (D) All of these Correct...

Swapping variable contents using C
/* WAP to swap the content of variables using C*/ //Header files #include<stdio.h>#include<conio.h> //Main function void main(){ //Variable declaration of type integer int a,b,c; //function...

Computer Generations
After the invention of electricity in the 20th century, computers began using electric power. This led to the rapid development of computers. As a result computer evolved generation after generation. First...
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