Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
4.8k views
in Computer by (48.9k points)
closed by

A stack is a linear data structure which enables the user to add and remove integers from one end only, using the concept of LIFO(Last In First Out). An array containing the marks of 50 students in ascending order is to be pushed into the stack. 

Define a class Array_to_Stack with the following details:

Class name: Array to Stack Data members/instance variables: 

m[]: to store the marks 

st[ ]: to store the stack elements 

cap: maximum capacity of the array and stack 

top: to point the index of the topmost element of the stack 

Methods/Member functions:

Array_to_Stack(int n): parameterized constructor to initialize cap = n and top = -1 

void input_marks(): to input the marks from the user and store it in the array m[ ] in ascending order and simultaneously push the marks into the stack st[ ] by invoking the function pushmarks() 

void pushmarks(int v): to push the marks into the stack at top location if possible, otherwise, display “not possible” 

intpopmarks(): to return marks from the stack if possible, otherwise, return-999 

void display(): To display the stack elements 

Specify the class Array_to_Stack, giving the details of the constructor(int), void input_marks(), void pushmarks(int), int popmarks() and void display(). 

The main function and the algorithm need not be written.

1 Answer

+1 vote
by (49.9k points)
selected by
 
Best answer

importjava.util.*; 

class Array to Stack 

{ int m[], stD; 

int cap, top; 

static Scanner sc=new Scanner(System.in); 

Array_to_Stack(int n) 

cap = n; 

top = -1; 

m=newint[cap]; 

st=new int[cap]; 

void input_marks() 

System.out.println("Enter "+cap+" elements in ascending order"); 

for(int i=0;  i< cap; i++)

{

m[i]=sc.nextInt(); 

pushmarks(m[i]); 

void pushmarks(int v) 

if (top=< cap -1)

else 

System.out.println("stack is full"); 

int popmarks() 

if(top>=0)

retumst[top--]; 

else 

return-999; 

void display() 

for(int i=top;i>=0 ;i--) 

System.out.println(st[i]); 

}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...