How to Recursively Traverse in a Linked List

The Linked List data structure is a powerful alternative to simple arrays. Unlike arrays, data can be quickly added to and removed from a linked list without recreating the list one element at a time. However, unlike arrays, data in a linked list can only be accessed in order. You can do this with a simple loop or with a recursive (or self-calling) function. This will be written in Java, but the code can be implemented in any language with only minor modifications to suit the syntax differences.

Instructions

    • 1

      Open a text editor.

    • 2

      Paste the following Java code:

      public class RecursiveLLTraverser {

      public static void traverseList(LinkedList l) {

      }

      }

      All the code will go within the "traverseList" method.

    • 3

      Paste the following inside the "traverseList" method:

      if (l.size() == 0) return;

      if (l.size() > 0) {

      LinkedList n = l.clone();

      Object o = n.removeFirst();

      o.doSomething();

      traverseList(n);

      }

      This takes a Linked List and makes a shallow clone of it with the first element removed (and some processing performed upon it.) That clone is then run through the traverse List itself. Eventually, the clone will be empty, in which case the traverse List method will simply return.

Tips & Warnings

  • All recursive algorithms require at least two cases: a base case, which must return, and a recursive case, which reduces the data size to something more easily managed. One common bug in recursive methods is forgetting the base case. This causes an infinite loop that eventually crashes when the computer runs out of stack space.

  • Recursive methods depend a system setting called "stack size" which changes with the operating system and language. This section of memory keeps track of all currently running methods. Attempting a recursive algorithm on an extremely large list can produce stack errors.

Related Searches:

References

Comments

You May Also Like

  • How to Sort a Linked List in Java

    A linked list is one of the primary types of data structures in the programming world. It's an arrangement of nodes which...

  • How to Create a Linked List in C

    In C, a linked list allows you to create a list without deciding ahead of time how long it might be, and...

  • Binary Tree Traversal Methods

    Binary trees (BTs) are data structures used by computer programmers whose software must represent medium to large data sets in an organized,...

  • Java Recursion Tutorial

    In computer science, a recursive method call is a method that calls itself during the process of computation. The Java programming language...

  • How to Do Inorder Traversal in Binary Tree in Java

    Java doesn't have a binary tree class, though it's simple to present a version of the data structure to do an inorder...

  • How to Do Postorder Traversal in a Binary Tree in Java

    Though Java doesn't provide a binary tree class in the default libraries, a basic binary tree class is simple enough to be...

  • How to Create a Binary Tree in C

    You May Also Like. Binary Tree Traversal Methods. Binary trees (BTs) are data structures used by computer programmers whose software must represent...

  • What Is a Traverse Link?

    A traverse is a measured series of linked lines whose angles are used mathematically to represent relationships between objects. Traverse links start...

  • How to Use a Skew Heap in Java

    A skew heap is an abstract data structure. Though Java doesn't provide for a binary tree class, the skew heap can be...

  • LLTD Networking Protocol

    LLTD Networking Protocol. The Link Layer Topology Discovery protocol is a proprietary protocol of Microsoft for its Windows operating system. Its function...

  • How to Store a Binary Search Tree to a File

    A binary search tree is a data structure where records of data, called "nodes," have pointers to other nodes, called "children." This...

  • How to Do Preorder Traversal in Binary Tree in Java

    Create a simple binary search tree class that has a basic constructor which initializes the node's value. Also included should be an...

  • List of CAD Symbols

    List of CAD Symbols. Computer-aided design applies technology to the art and craft of draftsmanship. Drafting tables have given way to large-screen...

  • Chevy Traverse Specifications

    Chevy Traverse Specifications. The Chevrolet Traverse is a crossover-style full-size sport utility vehicle with a 3.6-liter six-cylinder engine. The manufacturer ...

  • How to Reinstall the Taskbar in Ubuntu Gnome

    Ubuntu is a Linux-based open-source operating system that is freely available for download. The Ubuntu desktop is similar to the Windows desktop...

  • How to Write a Method in Java

    Java is the most popular programming language, primarily because of its portability. The same Java program can be used on virtually any...

  • What is a Traverse Juror?

    A traverse juror, also known as a petit juror, is a citizen who has been selected to serve on a trial jury...

Related Ads

Featured