Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Zaara

  • one year ago

what is the difference between ++top and top++in data structures and algorithms in java?

  • This Question is Closed
  1. ajprincess
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Guess u r referring the one that comes in stack @Zaara. Right?

  2. Zaara
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes u r ryt.... im littl confused with dat.....

  3. ajprincess
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    when u initialize the top u give the value -1 which means that there is no element in the stack.If there is one element the index of top will be 0. when u insert the first element the index shoud be iterated from -1 to 0. In this case u use ++top. The iteration has to be done before the insertion. Am I clear?

  4. Zaara
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    just compare ++top and top++... i like to knw the difference...

  5. asnaseer
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ++top means "first increment top, then read its new value" top++ means "read the current value of top, then increment it" e.g. top = -1; x = ++top; will result in x=0, top=0 whereas: top = -1; x = top++; will result in x=-1, top=0

  6. hduff
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    asnaseer has nailed it... Java has two increment/decrement operators: ++ and --. These operators can be used both before and after the operand they are applied to. When used before they carry out prefix incrementing or decrementing. When used after the operand they carry out postfix incrementing or decrementing. The results are different in each case. For example top= 10; newTop = ++top; System.out.println(newTop); prints 11, but top = 10; newTop = top++; System.out.println(newTop); prints 10 In the second line of the first case top is incremented by 1 and then newTop is initialized with top. In the second line of the second case newTop is incremented to the value of Top and then Top is incremented. Hope this helps...

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.