Results 1 to 8 of 8
  1. #1

    Default need help about stacks!


    Write a complete program that will perform the following:

    1. Push the following sequence of numbers (in order) onto the STACK

    1 4 9 16 25 36 49 64 81 100

    1. After inserting the above items, your program should modify all the elements in the Stack. The modification of the element is based on two rules: (1) if an element is an even number, get its square; (2) if an element is an odd number, multiply it by 3 and add 1.
    2. After modifying all elements in the Stack, your program should remove the top 3 items.
    3. Lastly, print Stack elements.

    To solve this problem, you should implement the following functions:
    /*This function will push each given item onto the Stack*/
    void push(int item)
    /*This function will remove an item from the Stack. The function will return the value of the popped (removed) item.*/
    int pop()
    /*This function will modify each element in the Stack. The modification of the element is based on two rules: (1) if an element is an even number, get its square; (2) if an element is an odd number, multiply it by 3 and add 1.*/
    void modify()
    /*This function will display the elements in the Stack.*/
    void printStack()

    The output of your program should be:

    Original Stack elements:
    100-81-64-49-36-25-16-9-4-1-NULL
    Modified Stack elements:
    10000-244-4096-148-1296-76-256-28-16-4-NULL
    Pop(): 10000
    Pop(): 244
    Pop(): 4096
    Remaining Stack elements:
    148-1296-76-256-28-16-4-NULL

    ************************************************** *******
    This is my code:

    class Stack
    {
    private int tos;
    private int stak[];

    Stack(int size)
    {
    tos=-1;
    stak=new int[size];
    }

    void push(int item)
    {
    if(tos==stak.length-1)
    System.out.println("Stack is Full");
    else
    {
    stak[++tos]=item;
    System.out.println(stak[tos]);
    }
    }

    int pop()
    {
    if(tos<0)
    {
    System.out.println("Stack Underflow");
    return 0;
    }
    else
    return stak[tos--];
    }

    }

    class stack1
    {
    public static void main(String[] args)
    {
    Stack s1 = new Stack(10);
    s1.push(1);
    s1.push(4);
    s1.push(9);
    s1.push(16);
    s1.push(25);
    s1.push(36);
    s1.push(49);
    s1.push(64);
    s1.push(81);
    s1.push(100);


    System.out.println("\nOriginal Stack Elements: ");
    for(int i=0; i<10; i++)
    System.out.print(s1.pop()+ " ");

    }
    }


    and ang iya output is this ..

    1
    4
    9
    16
    25
    36
    49
    64
    81
    100

    Original Stack Elements:
    100 81 64 49 36 25 16 9 4 1

    ***
    i need this output. unsaon?
    Modified Stack elements:
    10000-244-4096-148-1296-76-256-28-16-4-NULL
    Pop(): 10000
    Pop(): 244
    Pop(): 4096
    Remaining Stack elements:
    148-1296-76-256-28-16-4-NULL

  2. #2

    Default Re: need help about stacks!

    unsa ni assignment? seatwork? project? machine problem?

  3. #3

    Default Re: need help about stacks!

    You can try the following, I haven't tested it though:

    Code:
    import java.util.ArrayList;
    import java.lang.Math;
    
    public class Stack
    {
        private ArrayList st;
    
        public Stack()
        {
            this.st = new ArrayList();
        }
    
        public void push(int item)
        {
            this.st.add(item);
        }
    
        public int pop()
        {
            int i = this.st.size() - 1;
            int v = this.st.get(i);
    
            this.st.remove(i);
    
            return v;
        }
    
        public void modify()
        {
            int v, i;
    
            for(i = 0; i < this.st.size(); i++) {
                v = this.st.get(i);
    
                if((v % 2) == 0) {
                    this.st.set(i, Math.floor(Math.sqrt(v)));
                } else {
                    this.st.set(i, (v * 3) + 1);
                }
            }
        }
    }
    Then gamiton na lang nimo na sila didto sa imong main nga function.

  4. #4

    Default Re: need help about stacks!

    TS.. gihimo sa akong amigo

    public class Stack {
    private int size = 0;
    private static final int INITIAL_SIZE = 10;
    private Integer[] contents;
    public Stack() {
    contents = new Integer[INITIAL_SIZE];
    }
    public void push(Integer number) {
    increaseArraySize();
    contents[incrementSize()] = number;
    }
    public Integer pop() {
    if (isEmpty()) {
    throw new RuntimeException("Stack is empty");
    }
    return contents[decrementSize()];
    }
    public boolean isEmpty() {
    return size == 0;
    }
    public int getSize() {
    return size;
    }
    private int incrementSize() {
    return size++;
    }
    private int decrementSize() {
    return --size;
    }
    private void increaseArraySize() {
    if (contents.length == getSize()) {
    contents = Arrays.copyOf(contents, 2 * getSize() + 1);
    }
    }
    }

    pero ikaw nalang daw himo sah third function

  5. #5

    Default Re: need help about stacks!

    Quote Originally Posted by moodsey211 View Post
    You can try the following, I haven't tested it though:

    Code:
    import java.util.ArrayList;
    import java.lang.Math;
    
    public class Stack
    {
        private ArrayList st;
    
        public Stack()
        {
            this.st = new ArrayList();
        }
    
        public void push(int item)
        {
            this.st.add(item);
        }
    
        public int pop()
        {
            int i = this.st.size() - 1;
            int v = this.st.get(i);
    
            this.st.remove(i);
    
            return v;
        }
    
        public void modify()
        {
            int v, i;
    
            for(i = 0; i < this.st.size(); i++) {
                v = this.st.get(i);
    
                if((v % 2) == 0) {
                    this.st.set(i, Math.floor(Math.sqrt(v)));
                } else {
                    this.st.set(i, (v * 3) + 1);
                }
            }
        }
    }
    Then gamiton na lang nimo na sila didto sa imong main nga function.
    Ingon akong amigo nindot sad himo ni brother moodsey211 .. Simple and effective daw and gamay ra daw code ingon akong amigo

  6. #6

    Default Re: need help about stacks!

    thx kau mga bro. ako ni try. assigment man gud ni. hehehe

  7. #7

    Default Re: need help about stacks!

    Quote Originally Posted by mediaskare View Post
    thx kau mga bro. ako ni try. assigment man gud ni. hehehe
    maau kai assignment ni...

  8. #8

    Default Re: need help about stacks!

    Quote Originally Posted by mediaskare View Post
    thx kau mga bro. ako ni try. assigment man gud ni. hehehe
    nice... nindut ni inyu assignment bro..

    btw, kasabut ka?

  9.    Advertisement

Similar Threads

 
  1. need help about cabling
    By MisterSuave in forum Networking & Internet
    Replies: 33
    Last Post: 06-05-2009, 12:11 AM
  2. GUYS NEED HELP!!! ABOUT FLASH MP3 PLAYERs!!
    By tsunamibomb in forum Software & Games (Old)
    Replies: 0
    Last Post: 04-22-2006, 06:51 PM
  3. Need Help about DFA
    By darkdevil in forum Politics & Current Events
    Replies: 15
    Last Post: 04-07-2006, 10:57 PM
  4. need help about my system units.
    By junaxM in forum Computer Hardware
    Replies: 8
    Last Post: 03-01-2006, 09:01 AM
  5. pls. i need help about gunzonline
    By yanbupipers in forum Software & Games (Old)
    Replies: 2
    Last Post: 10-15-2005, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top