Advertisements
Advertisements
प्रश्न
A Stack is a kind of data structure which can store elements with the restriction that an element can be added or removed from the top end only.
The details of the class Stack is given below:
| Class name: | Stack |
| Data members/instance variables: | |
| Chat[]: | array to hold the characters |
| size: | stores the maximum capacity of the stack |
| top: | to point the index of the topmost element of the stack |
| Member functions/methods: | |
| Stack(int mm): | constructor to initialize the data member size = mm, top = −1 and create the character array |
| void push_char(char v): | to add characters from the top end if possible else display the message("Stack full") |
| char pop_char(): | to remove and return characters from the top end, if any, else return '$' |
| void display( ): | to display elements of the stack |
Specify the class Stack giving the details of void push_char(char) and char pop_char( ). Assume that the other functions have been defined.
The main( ) function and algorithm need NOT be written.
कोड लेखन
Advertisements
उत्तर
class Stack
{
char cha[];
int top;
int size;
void push_char(char v)
{
if (top==size-1)
System.out.println("Stack full !!!!");
else
cha[++top]=v;
}
char pop()
{
If (top==-1)
return ("$");
else
{
char c= cha[top];
top=top-1;
return c;
}
}
}shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
