linked list head and tail

import java.util.Iterator; 

// Custom Linked List class using Generics 
class List<T> implements Iterable<T> { 
	Node<T> head, tail; 
	
	// add new Element at tail of the linked list in O(1) 
	public void add(T data) 
	{ 
		Node<T> node = new Node<>(data, null); 
		if (head == null) 
			tail = head = node; 
		else { 
			tail.setNext(node); 
			tail = node; 
		} 
	} 
	
	// return Head 
	public Node<T> getHead() 
	{ 
		return head; 
	} 
	
	// return Tail 
	public Node<T> getTail() 
	{ 
		return tail; 
	} 
	
	// return Iterator instance 
	public Iterator<T> iterator() 
	{ 
		return new ListIterator<T>(this); 
	} 
} 

class ListIterator<T> implements Iterator<T> { 
	Node<T> current; 
	
	// initialize pointer to head of the list for iteration 
	public ListIterator(List<T> list) 
	{ 
		current = list.getHead(); 
	} 
	
	// returns false if next element does not exist 
	public boolean hasNext() 
	{ 
		return current != null; 
	} 
	
	// return current data and update pointer 
	public T next() 
	{ 
		T data = current.getData(); 
		current = current.getNext(); 
		return data; 
	} 
	
	// implement if needed 
	public void remove() 
	{ 
		throw new UnsupportedOperationException(); 
	} 
} 

// Constituent Node of Linked List 
class Node<T> { 
	T data; 
	Node<T> next; 
	public Node(T data, Node<T> next) 
	{ 
		this.data = data; 
		this.next = next; 
	} 
	
	// Setter getter methods for Data and Next Pointer 
	public void setData(T data) 
	{ 
		this.data = data; 
	} 
	
	public void setNext(Node<T> next) 
	{ 
		this.next = next; 
	} 
	
	public T getData() 
	{ 
		return data; 
	} 
	
	public Node<T> getNext() 
	{ 
		return next; 
	} 
} 

// Driver class 
class Main { 
	public static void main(String[] args) 
	{ 
		// Create Linked List 
		List<String> myList = new List<>(); 
		
		// Add Elements 
		myList.add("abc"); 
		myList.add("mno"); 
		myList.add("pqr"); 
		myList.add("xyz"); 
		
		// Iterate through the list using For Each Loop 
		for (String string : myList) 
			System.out.println(string); 
	} 
} 

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source