icse-promo

Question

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(LastInFirstOut).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:

Classname: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 top most 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 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”
int popmarks():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 algorithm need not be written.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class Array_to_Stack
{   
    int m[],st[];
    int cap,top;
    Scanner sc=new Scanner(System.in);
    Array_to_Stack(int n)
    {   
        cap=n;
        top=-1;
        st=new int[cap];
        m=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)
        {
            top=top+1;
            st[top]=v;
        }
        else
        {
            System.out.println("stack is full");
        }
    }

    int popmarks()
    {   
        if(top>=0)
        {
            return st[top--];
        }
        else
        {
            return -999;
        }
    }
    
    void display()
    {    
        for(int i=top;i>=0;i--)
        {
            System.out.println(st[i]);
        }
    }
}



				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *