I wrote code for a linked list, adding a node. I use VS Code, but when I am want to run the code it’s not working. This is my code:
package Linked_List;
import java.util.*;
public class Insertion{
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
Node head=null;
public void creation(){
int data,n;
Scanner sc=new Scanner(System.in);
do{
System.out.println("enter data");
data=sc.nextInt();
Node newnode=new Node(data);
if(head==null){
head=newnode;
}
else{
newnode.next=head;
head=newnode;
}
System.out.println("do you want to continue press y for yes");
n=sc.next().charAt(0);
}while(n=='y'|| n=='Y');
}
public void traverse(){
Node temp=head;
if(head==null){
System.out.println("linled list dont exist");
}
else{
while(temp!=null){
System.out.println(temp.data);
temp=temp.next;
}
}
}
public static void main(String[] args) {
Insertion list = new Insertion();
list.creation();
list.traverse();
}
}
Here the code is right but the problem is I have to write it over and over again:
1-javac Linked_List/Insertion.java
2-java Linked_List/Insertion
This file (Insertion.java
) is in a folder named Linked_List
I don’t want to write these two lines over and over again; when I click on run code it want it just to run. What can I do for that to happen?
Priyansh Sindhwani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You can create a script (.bat or .sh) to run couple commands.