Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

implementing iterator for linked list java

public class BasicLinkedList<T> implements Iterable<T> {
    public int size;

    private class Node {
        private T data;
        private Node next;

        private Node(T data) {
            this.data = data;
            next = null;
        }
    }

    private Node head;
    private Node tail;

    public BasicLinkedList() {
        head = tail = null;
    }
//Add, remove method 

public Iterator<T> iterator() {
        return new Iterator<T>() {

            Node current = head;

            @Override
            public boolean hasNext() {
                return current != null;
            }

            @Override
            public T next() {
                if(hasNext()){
                    T data = current.data;
                    current = current.next;
                    return data;
                }
                return null;
            }

            @Override
            public void remove(){
                throw new UnsupportedOperationException("Remove not implemented.");
            }

        };
Comment

iterate trough linked list java

#Enhances For Loop
for (String temp : linkedList) {
    System.out.println(temp);
}
Comment

Java Iterating through LinkedList

import java.util.LinkedList;

class Main {
    public static void main(String[] args) {
        // Creating a linked list
        LinkedList<String> animals = new LinkedList<>();
        animals.add("Cow");
        animals.add("Cat");
        animals.add("Dog");
        System.out.println("LinkedList: " + animals);

        // Using forEach loop
        System.out.println("Accessing linked list elements:");
        for(String animal: animals) {
            System.out.print(animal);
            System.out.print(", ");
        }
    }
}
Comment

Iterating through a linked list

#include<stdio.h>
#include<stdlib.h>

//1
typedef struct node{
	int value;
	struct node *next;
}node;


int main(){
	int length,i;

	//2
	printf("Enter size of the list : ");
	scanf("%d",&length);

	//3
	struct node *headNode, *currentNode, *temp;

	//4
	for(i=0 ; i<length; i++){ //5 currentNode = (node *)malloc(sizeof(node)); //6 printf("Enter element %d : ",(i+1)); scanf("%d", ¤tNode->value);

		//7
		if(i==0){
			headNode = temp = currentNode;
		}else{
			//8
			temp->next = currentNode;
			temp = currentNode;
		}
	}

	//9
	temp->next = NULL;

	//10
	temp = headNode;

	//11
	printf("Iterating through the elements of the linked list : 
");
	while(temp != NULL){
		//12
		printf("%d 
",temp->value);
		temp = temp -> next;
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: java enum in switch statement 
Java :: recursion java fibonacci 
Java :: c++ vs java 
Java :: android activity transition 
Java :: enum class in java 
Java :: valid parentheses leetcode solution java 
Java :: method overloading 
Java :: Java Abstract Class and Method 
Java :: buy and sell stock 
Java :: camunda 
Java :: join strings in java 
Java :: pass data to layout from navigation android studio 
Java :: Print the string after the specified character java 
Java :: how to Compile the source code in ./src folder with libraries in ./lib folder using JavaSE-1.7 
Java :: java exception handling 
Java :: Unrolling java - from 
Java :: first none duplicated char 
Java :: location.href webview android studio 
Java :: open cv for java 
Java :: how to display an integer in a textfield in java 
Java :: thread dump 
Java :: spigot bukkit self cancelling task timer example repeat times 
Java :: validate list of objects in javax validation 
Java :: create object of hashMap with integer key and String value 
Java :: java stack declaration 
Java :: find node from pos linkedlist java 
Java :: lcd initialize arduino 
Java :: anulom vilom in english 
Java :: matrix program 
Java :: closable resources java 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =