Re: Stack



On Jan 4, 12:22 pm, Tarique <peo_...@xxxxxxxxx> wrote:
Tarique wrote:
Hello all.I am trying to implement a stack which can store either
integer or float values.

The code is given below:

...snip...

Here is the final code:
Works fine in case of Integer n floats.However i lose track of the
character pointer once i exit the push function (in case of strings).
So in case of *Strings* i cannot display the popped string or the
members of the stack at any given time.
Using an extra global variable to keep track also does not help!
It's confusing!!!

PS:ive not really bothered about scanf..Will change that :)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define STACKSIZE       100
#define INT             1
#define FLOAT           2
#define STRING          3

struct stackelement{
int etype;      /*etype equals INT,FLOAT,STRING*/
                        /*depending on the type of the */
                        /*corresponding element */
union {
        int     iVal;
        float   fVal;
        char    *pVal;

}element;
};

struct stack{
int top;
struct stackelement item[STACKSIZE];

};

void push(struct stack *p,struct stackelement *se)
{
int     test;
int     iValue  = 0;
float   fValue  = 0.0;
char    cValue[10];

printf("Enter the number/character to push");
if(se->etype == INT)
        scanf("%d",&iValue);
else if(se->etype == FLOAT)
        scanf("%f",&fValue);
else if(se->etype == STRING)
{
        while((test=getchar())!='\n')
                ;
        fgets(cValue,10,stdin);

}

if(p->top == STACKSIZE-1 )
     printf("overflow\n");

else
{
        if(se->etype == INT)
                p->item[++p->top].element.iVal=iValue;
        else if(se->etype == FLOAT)
                p->item[++p->top].element.fVal=fValue;
        else if(se->etype == STRING)
                p->item[++p->top].element.pVal=cValue;

}
return;
}

void pop(struct stack *p,struct stackelement *se)
{
if(p->top==-1)
     printf("underflow\n");
else
{
        if(se->etype == INT)
                printf("Deleted Integer is\ %d\n",p->item[p->top].element.iVal);
        else if(se->etype == FLOAT)
                printf("Deleted Float is\ %f\n",p->item[p->top].element.fVal);
        else if(se->etype == STRING)
                printf("Deleted String is\ %s\n",p->item[p->top].element.pVal);

        --p->top;

}
return;
}

void display(struct stack *p,struct stackelement *se)
{
int i;
if(p->top == -1)
     printf("Stack is Empty\n\n");
else
{
        if(se->etype == INT)
        {
                for(i=p->top; i >= 0; i--)
                        printf("%d\n", p->item[i].element.iVal );
        }

        else if(se->etype == FLOAT)
        {
                for(i = p->top; i >= 0; i--)
                        printf("%f\n", p->item[i].element.fVal);
        }

        else if(se->etype == STRING)
        {
                for(i = p->top; i >= 0; i--)
                        printf("%s\n",p->item[p->top].element.pVal);
        }
        /*((((*(p)).item)[0]).element).pVal*/

}
return;
}

void menu(struct stack *s,struct stackelement *s_elem)
{
int num=0;
int             choice;
int             flag=1;

while(flag)
{
        printf("Menu");
        printf("\n1.Push");
        printf("\n2.Pop");
        printf("\n3.Display");
        printf("\n4.Exit");
        printf("\n5.Clear screen\n");
        printf("Enter your choice :");
        scanfs("%d",&choice);

        switch(choice)
        {
            case 1:
                push(s,s_elem);
                system("cls");
                break;
            case 2:
                pop(s,s_elem);
                break;
            case 3:
                display(s,s_elem);
                break;
            case 4:
                flag=0;
                break;
            case 5:
                system("cls");
                break;
            default:
                printf("wrong choice\n");
     }

}
}

int main(void)
{
int     choice_mm ;
struct  stackelement se;
struct  stack s;
s.top = -1;

for(;;)
{
        printf("Choose the type of stack:\n");
        printf("1.Integer\n");
        printf("2.Float\n");
        printf("3.String\n");
        printf("4.Exit\n");
        scanf_s("%i", &choice_mm);

        switch(choice_mm)
        {
                case 1:
                        se.etype=INT;
                        system("cls");
                        menu(&s,&se);
                        system("cls");
                        break;
                case 2:
                        se.etype=FLOAT;
                        menu(&s,&se);
                        break;
                case 3:
                        se.etype=STRING;
                        menu(&s,&se);
                        break;
                case 4:
                        exit(0);
                        break;
                default:
                        printf("wrong input");
        }



}
return 0;
}- Hide quoted text -

- Show quoted text -

/*
This thing still has a long way to go, but it is a little closer to
what you want.
The string you were using was an automatic variable. When a function
exits, all of its automatic variables go away.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STACKSIZE 100
#define CHAR_ELEMENT_SIZE 10
typedef enum element_type {
uninitialized = 0, integer, floating, string
} e_type;

typedef struct stackelement {
e_type etype;
union {
int iVal;
float fVal;
char pVal[CHAR_ELEMENT_SIZE];
} element;
} ste;

typedef struct stack {
int top;
ste item[STACKSIZE];
} Stack;

void push(struct stack * p, struct stackelement * se)
{
int iValue = 0;
float fValue = 0.0;
char cValue[CHAR_ELEMENT_SIZE]={0};
int converted;
char *collected;

printf("Enter the number/string to push: ");
fflush(stdout);
if (se->etype == integer)
converted = scanf("%d", &iValue); /* Check converted and take
appropiate action! */
else if (se->etype == floating)
converted = scanf("%f", &fValue); /* Check converted and take
appropiate action! */
else if (se->etype == string) {
collected = fgets(cValue, sizeof cValue, stdin); /* Check
collected and take appropiate action! */
}
if (p->top == STACKSIZE - 1)
printf("overflow\n");
else {
if (se->etype == integer)
p->item[++p->top].element.iVal = iValue;
else if (se->etype == floating)
p->item[++p->top].element.fVal = fValue;
else if (se->etype == string)
strcpy(p->item[++p->top].element.pVal , cValue);
else
puts("Unexpected element type.");
}
return;
}

void pop(struct stack * p, struct stackelement * se)
{
if (p->top == -1)
printf("underflow\n");
else {
if (se->etype == integer)
printf("Deleted Integer is %d\n", p->item[p-
top].element.iVal);
else if (se->etype == floating)
printf("Deleted Float is %f\n", p->item[p-
top].element.fVal);
else if (se->etype == string)
printf("Deleted String is %s\n", p->item[p-
top].element.pVal);
--p->top;
}
return;
}

void display(struct stack * p, struct stackelement * se)
{
int i;
if (p->top == -1)
printf("Stack is Empty\n\n");
else {
if (se->etype == integer) {
for (i = p->top; i >= 0; i--)
printf("%d\n", p->item[i].element.iVal);
} else if (se->etype == floating) {
for (i = p->top; i >= 0; i--)
printf("%f\n", p->item[i].element.fVal);
} else if (se->etype == string) {
for (i = p->top; i >= 0; i--)
printf("%s\n", p->item[p->top].element.pVal);
}
/* ((((*(p)).item)[0]).element).pVal */
}
return;
}

void menu(struct stack * s, struct stackelement * s_elem)
{
int choice;
int flag = 1;
char *collected;
char st[4];
while (flag) {
printf("Menu");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");
collected = fgets(st, sizeof st, stdin);
choice = atoi(st);
switch (choice) {
case 1:
push(s, s_elem);
#ifdef WIN32
system("cls");
#else
system("clear"); /* Still, not really portable. */
#endif
break;
case 2:
pop(s, s_elem);
break;
case 3:
display(s, s_elem);
break;
case 4:
flag = 0;
break;
case 5:
system("cls");
break;
default:
printf("wrong choice\n");
}
}
}

int main(void)
{
e_type choice_mm;
struct stackelement se = {0};
struct stack s = {0};
int converted;

s.top = -1;
for (;;) {
printf("Choose the type of stack item:\n");
printf("1.Integer\n");
printf("2.Float\n");
printf("3.String\n");
printf("4.Exit\n");
converted = scanf_s("%i", &choice_mm); /* Check converted and
take appropiate action! */
switch (choice_mm) {
case 1:
se.etype = integer;
system("cls");
menu(&s, &se);
system("cls");
break;
case 2:
se.etype = floating;
menu(&s, &se);
break;
case 3:
se.etype = string;
menu(&s, &se);
break;
case 4:
exit(0);
break;
default:
printf("wrong input");
}
}
return 0;
}
.



Relevant Pages