In: Computer Science
You have already implemented a Queue before that works
on First Come First served basis.
In this assignment you are to implement a class that works on Last
In First Out. Such a structure is called a Stack. It only allows
two operations add (named push) and remove (named pop).
Push only allows adding any data item to the last entry
place.
Pop only allows you to remove the last added entry from the
stack.
For example if your stack has items (means values
where added in the order 12 then 30 then 15)
12 30 15
After Push(45) the stack will look like
12 30 15 45
After call to Pop() the stack must look like
12 30 15
After another call to Pop() the stack must look
like
12 30
You must implement a Stack as a class.
This should have
three methods
constructor: That will declare an empty array of 5 elements.
Pop: This function will just remove a value from the last of the
Stack and return it to the user.
Push: This method will take an item as argument to be added to the
stack.
If the space in the stack is not full the item will be added to the
end of the stack
If the stack is full with items then a new array must be declared
that is double the size of the last array and all values must be
copied to the new array before the new item is added to the last of
these values.
NOTE: The code must be in "C LANGUAGE"
THANK YOU
As C doesnt supports class(oop) i have written the code with structure. If you need the code with class we can implement it with c++. Also the below code could be modified to work with class.
Code :
#include <stdio.h>
typedef struct
{
int *a,tos,n;
}stack;
void init(stack *,int);
int isempty(stack *);
int isfull(stack *);
void push(stack *,int);
int pop(stack *);
void copy(stack *,int);
void display(stack *);
void init(stack *s,int x)
{
s->tos=-1;
s->n=x;
s->a=(int *)malloc(s->n*sizeof(int));
}
int isempty(stack *s)
{
if(s->tos==-1)
return 1;
else
return 0;
}
int isfull(stack *s)
{
if(s->n-1==s->tos)
return 1;
else
return 0;
}
void push(stack *s,int info)
{
if(isfull(s))
{
copy(s,2*(s->n));
}
s->tos++;
s->a[s->tos]=info;
}
int pop(stack *s)
{
int info;
if(isempty(s))
{
printf("\n\tStack Is Empty");
return 0;
}
else
{
info=s->a[s->tos];
s->tos--;
return info;
}
}
void display(stack *s)
{
int i;
for (i=0; i <= s->tos; i++)
printf("%d ",s->a[i]);
printf("\n");
}
void copy(stack *s,int x)
{
stack s1;
init(&s1,x);
int i;
for (i=0; i <= s->tos; i++)
s1.a[s1.tos++] = s->a[i];
s = &s1;
return;
}
int main()
{
stack s;
init(&s,5);
push(&s,1);
push(&s,5);
push(&s,4);
push(&s,3);
display(&s);
push(&s,1);
push(&s,5);
push(&s,4);
push(&s,3);
display(&s);
printf("Element popped : %d\n",pop(&s));
printf("Element popped : %d\n",pop(&s));
display(&s);
return 0;
}
Output :
C++
#include <stdio.h>
class stack
{
int *a,tos,n;
public :
stack(int s)
{
tos = -1;
n = s;
a = new int[s];
};
int isempty()
{
if(tos==-1)
return 1;
else
return 0;
}
int isfull()
{
if(tos<n)
return 0;
else
return 1;
}
void push(int info)
{
stack *s1 = this;
if (isfull())
copy(s1,2*n);
tos++;
a[tos]=info;
}
int pop()
{
int info;
if(isempty())
{
printf("\n\tStack Is Empty");
return 0;
}
else
{
info=a[tos];
tos--;
return info;
}
}
void display()
{
for (int i=0; i <= tos; i++)
printf("%d ",a[i]);
printf("\n");
}
void copy(stack *stk,int s)
{
stack s1(s);
for(int i=0; i <= tos;
i++)
s1.a[s1.tos++] = a[i];
stk = &s1;
}
};
int main()
{
stack s(5);
s.push(1);
s.push(5);
s.push(4);
s.push(3);
s.display();
s.push(1);
s.push(5);
s.push(4);
s.push(3);
s.display();
printf("Element popped : %d\n",s.pop());
printf("Element popped : %d\n",s.pop());
s.display();
return 0;
}