Answers for "how to iterate over a linked list in java"

0

java linked list iterator

// Java code to illustrate listIterator() 
import java.io.*; 
import java.util.LinkedList; 
import java.util.ListIterator; 
  
public class LinkedListDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty LinkedList 
        LinkedList<String> list = new LinkedList<String>(); 
  
        // Use add() method to add elements in the list 
        list.add("Geeks"); 
        list.add("for"); 
        list.add("Geeks"); 
        list.add("10"); 
        list.add("20"); 
  
        // Displaying the linkedlist 
        System.out.println("LinkedList:" + list); 
          
        // Setting the ListIterator at a specified position 
        ListIterator list_Iter = list.listIterator(2); 
  
        // Iterating through the created list from the position 
        System.out.println("The list is as follows:"); 
        while(list_Iter.hasNext()){ 
           System.out.println(list_Iter.next()); 
        } 
    } 
}
Posted by: Guest on January-28-2020
0

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 : \n");
	while(temp != NULL){
		//12
		printf("%d \n",temp->value);
		temp = temp -> next;
	}
}
Posted by: Guest on July-30-2021

Code answers related to "how to iterate over a linked list in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language