Java Data structures practice programs
/Write a program to count the number of nodes in a tree
Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.
Browse Courses/Write a program to count the number of nodes in a tree
Binary Tree: –>Binary tree is a hierarchical data structure where each node has at most 2 children left child and right child Tree Terminology: Node: An element in the tree. It contains data and links Root: The topmost node in the tree(No parent) Parent: A Node that has children Child: […]
TCS Codings – 2 def find_equilibrium_index(arr): total_sum = sum(arr) left_sum = 0 for i in range(len(arr)): # right_sum = total_sum – left_sum – arr[i] if left_sum == (total_sum – left_sum – arr[i]): return […]
Count the number of vowels in the given string by using queue import java.util.*; class Count { public static void main(String args[]) { String input=”queue”;//4 Queue<Character> q=new LinkedList<>(); int count=0; for(char ch:input.toCharArray())//e { q.add(ch);//e } while(!q.isEmpty())//true { char ch=q.remove(); if(“aeiouAEIOU”.indexOf(ch)!=-1)//indexof returns -1 if value is not found { count++; } […]
//Checking the string is palindrome or not by using stack and queue class Palindrome { public static void main(String args[]) { String str=”madam”; Queue<Charcter> q=new LinkedList<>(); Stack<Character> s=new Stack(); for(char ch:str.toCharArray())//ch=m { q.add(ch);//m s.push(ch);//m } boolean isPalindrome=true; while(!q.isEmpty())//false { if(q.remove()!=s.pop()) { isPalindrome=false; break; } } if(isPalindrome)//true System.out.println(str +”is […]
import java.util.*; class ReverseQueue { public static void main(String args[]) { Queue<Integer> q=new LinkedList<>(); q.add(1); q.add(2); q.add(3); System.out.println(“Size of the queue=”+q.size());//3 } }
//reverse a elements of queue by using stack import java.util.*; class ReverseQueue { public static void main(String args[]) { Queue<Integer> q=new LinkedList<>(); q.add(1); q.add(2); q.add(3); Stack<Integer> s=new Stack<>(); while(!q.isEmpty()) { s.push(q.remove()); } while(!s.isEmpty()) { q.add(s.pop()); } System.out.println(“Reverse queue=” +q); } } output: Reverse queue=[3, 2, 1]
import java.util.*; class QueueExample { public static void main(String args[]) { //Queue<String> q=new Queue<>();//queue is not a class Queue<Integer> q=new LinkedList<>();//dynamic binding q.add(10); q.add(20); q.add(30); int removed=q.remove(); System.out.println(“removed element is=” +removed); System.out.println(“Queue elements are=” +q); }}
create a linkedlist and add 5 integer elements to that.calculate and print the sum of odd elements of the list 1 2 3 4 5 class Main { public static void main(String[] args) { LinkedList<Integer> list = new LinkedList<>(); for(int i = 1; i<=5; i++){ list.add(i);//1 2 3 4 5 } int sum […]
import java.util.*; class Maindsa { public static void main(String args[]) { LinkedList<String> list=new LinkedList<>(); //Add the elements to the list list.add(“apple”); //add elements to the list list.addFirst(“banana”); //add node to the front list.addLast(“cherry”); //add node at the end System.out.println(“Adding elements=” +list); list.remove();//removes the first element(banana) System.out.println(“After remove method=” +list); […]
Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.
Browse CoursesLearn the fundamentals of programming with Python in this beginner-friendly course.
Master the basics of data analysis, visualization, and machine learning.
Build modern websites with HTML, CSS, JavaScript and popular frameworks.
Learn SEO, social media marketing, email campaigns and analytics.