-1

I have a problem with my homework. And I need all the help that you can give me.

i need to create a combination of a stack that can store any type of data and a Stack that works with elements of type Stack. I´m very confused.

The methods that I was supposed to implement were:

initializeStack() isEmpty() isFull() Push() Pop() showStack() countElements()

And this is what i have so far:

public class pilita {
    Object vectorPila[];
    int tope;
    public pilita(int tam){
        vectorPila=new Object[tam];
        tope=-1;
    }
    public void inicializarPila(){
        tope=-1;
    }
    public void push(Object dato){
        tope++;
        vectorPila[tope]=dato;
    }
    public Object pop(){
        Object fuera=vectorPila[tope];
        tope--;
        return fuera;
    }
    public boolean pilaVacia(){
        return tope==-1;
    }
    public boolean pilaLlena(){
        return vectorPila.length-1==tope;
    }
    public Object cima(){
        return vectorPila[tope];
    }
    public Object contar(){
        return tope+1;
    } }

All the methods are well implemented (Using my logic).

But how can i make an Stack using Stack data type using those methods?. I would be very grateful if anyone could help me.

Also here is the original problem.

Stack of little Stacks: The elements of the Data Structure Little Stack are of any type of data. The elements of the Data Structure Stack are of Stack type.

  • "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." https://www.tutorialspoint.com/javaexamples/data_stack.htm – MikeJRamsey56 Oct 26 '16 at 01:29

1 Answers1

0
// Java code for stack implementation

import java.io.*;
import java.util.*;

class Test
{   
    // Pushing element on the top of the stack
    static void stack_push(Stack<Integer> stack)
    {
        for(int i = 0; i < 5; i++)
        {
            stack.push(i);
        }
    }

    // Popping element from the top of the stack
    static void stack_pop(Stack<Integer> stack)
    {
        System.out.println("Pop :");

        for(int i = 0; i < 5; i++)
        {
            Integer y = (Integer) stack.pop();
            System.out.println(y);
        }
    }

    // Displaying element on the top of the stack
    static void stack_peek(Stack<Integer> stack)
    {
        Integer element = (Integer) stack.peek();
        System.out.println("Element on stack top : " + element);
    }

    // Searching element in the stack
    static void stack_search(Stack<Integer> stack, int element)
    {
        Integer pos = (Integer) stack.search(element);

        if(pos == -1)
            System.out.println("Element not found");
        else
            System.out.println("Element is found at position " + pos);
    }


    public static void main (String[] args)
    {
        Stack<Integer> stack = new Stack<Integer>();

        stack_push(stack);
        stack_pop(stack);
        stack_push(stack);
        stack_peek(stack);
        stack_search(stack, 2);
        stack_search(stack, 6);
    }
}

In Java, they are called Collections and the are analogous to the standard template library(STL) of C++.Now, what you can do is define a void type stack and typecast it for various type of elements.

In c, stack.h has the following stack structure

    typedef struct{
        void *elems;
        int elemSize;
        int allocLength;
        int logLength;
        void (*freefnc)(void *);
    } Stack;

void stackNew(Stack *s, int elemSize, void (*freefnc)(void *));
void stackDispose(Stack *s);
void stackPush(Stack *s, void *elemAddr);
void stackPop(Stack *s, void *target);

You can study more about them here:-

http://www.geeksforgeeks.org/java/ (see collections in java)

http://www.geeksforgeeks.org/c-plus-plus/#STL (see standard template library(STL))

for defining void type generics in java refer:-

What's the difference between Void and no parameter?

https://coderanch.com/t/450693/java/pass-void-generic-parameter-type

Community
  • 1
  • 1
NeoR
  • 353
  • 2
  • 9
  • 27