Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

If the Java language lacks pointers, how do I implement classic pointer structures like linked lists?

0
Posted

If the Java language lacks pointers, how do I implement classic pointer structures like linked lists?

0

Ans : Use object references in place of pointers. Object references in Java are like object pointers minus the arithmetic. The Java language lets you point to (refer to) objects; it just doesn’t let you change numbers into references, references into numbers, or otherwise treat references in any numerical way. This property makes it straightforward to implement classic pointer-dependent data structures such as lists and trees. The following example shows a minimal linked-list implementation of a stack: /** A bare-bones stack class that holds objects of any class. */ public class SimpleStack { LinkedObject stackTop = null; public void push(Object obj) { if (obj != null) { stackTop = new LinkedObject(obj, stackTop); } } public Object pop() { if (stackTop != null) { Object oldTop = stackTop.value; stackTop = stackTop.nextObject; return oldTop; } return null; } } /** A bare-bones class for linking objects of any class. */ class LinkedObject { Object value; LinkedObject nextObject; LinkedOb

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.