Web & Social Media Analytics Previous Year Question Paper.pdf
StackArray stack3
1. Implementation of stack using
Array
Struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
}
#define STACKSIZE 100
Struct StackRecord
{ int top;
ElementType
Array[STACKSIZE];
}
2. Implementation of stack using
Array
• Test for empty stack
empty (stack *ps)
{
return (ps → top == -1)
}
3. Implementation using Array
• Pop top element from stack
int pop (stack *ps)
{ int x
if (empty (ps))
printf (“%sn”, “stack underflow!”);
else
{ x = ps → array [ps → top];
(ps → top)--;
return (x); }
}
4. Implementation using Array
• Push an element onto the stack
push (stack *ps, int x)
{ if (ps → top == STACKSIZE – 1)
printf (“%sn”, “stack overflow!”);
else
{ (ps → top)++;
ps → items [ps → top] = x;
}
}
5. Variable types of stack elements
#define STACKSIZE 100
#define INTGR 1
#define FLT 2
#define STRING 3
struct stackelement
{
int etype; /* element type */
union
{ int ival;
float fval;
char *pval;
} element; /* end union */
}
6. Variable Types of Stack Elements
• To access the top element
struct stack s;
struct stackelement se;
se = s.items [s.top];
switch (se.etype)
{
case INTGR: printf (“%dn”, se.ival); break;
case FLT: printf (“%dn”, se.fval); break;
case STRING: printf (“%sn”, se.pval);
}
7. Variable Types of Stack Elements
• To access the top element
struct stack s;
struct stackelement se;
se = s.items [s.top];
switch (se.etype)
{
case INTGR: printf (“%dn”, se.ival); break;
case FLT: printf (“%dn”, se.fval); break;
case STRING: printf (“%sn”, se.pval);
}