Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

drew s
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
drew s
Python Development Techdegree Graduate 19,491 Points

Need help with his code. It's not compiling

import java.util.Scanner;

//Class Node
class Node {
  protected int data;
  protected Node link;


//Constructor
  public Node() {
    link = null;
    data = 0;
  }

//Constructor
  public Node(int d, Node n) {
    data = d;
    link = n;
  }



//Function to set link to next Node
  public void setLink(Node n) {
    link = n;
  }


//Function to set data to current Node
  public void setData(int d) {
    data = d;
  }


//Function tp get link to next node
  public Node getLink() {
    return link;
  }


//Function to get data from current Node
  public int getData() {
    return data;
  }
}



// Class linkedList
class linkedList {
  protected Node start;
  protected Node end;
  public int size;


// Constructor
  public linkedList() {
    start = null;
    end = null;
    size = 0;
  }



// Function to check if list is empty
  public boolean isEmpty() {
    return start == null;
  }

  public int getSize() {
    return size;
  }


// Function to insert an element at beginning
  public void insertAtStart(int val){
    Node nptr = new Node(val, null);
    size++;
    if(start == null) {
      start = nptr;
      end = start;
    }
    else {
      nptr.setLink(start);
      start = nptr;
    }
  }


//Functions to insert an element at end 
  public void insertAtEnd(int val) {
    Node nptr = new Node(val, null);
    size++;
    if(start == null) {
      start = nptr;
      end = start;
    }
    else {
      end.setLink(nptr);
      end = nptr;
    }
  }

  public void insertAtPos(int val, int pos) {
    Node nptr = new Node(val, null);
    size++;
    if(start == null){
      start = nptr;
      end = start;
    }
    else {
      nptr.setLink(start);
      start = nptr;
    }

  }

  public void insertAtEnd(int val) {
    Node nptr = new Node(val, null);
    size++;
    if(start == null) {
      start = nptr;
      end = start;
  }
    else {
    end.setLink(nptr);
    end = nptr;
    }
  }

  public void insertAtPos(int val, int pos) {
    Node nptr = new Node(val, null);
    Node ptr = start;
    pos = pos - 1;
    for(int i = 1; i < size; i++){
      if(i == pos) {
        Node tmp = ptr.getLink();
        ptr.setLink(tmp);
        nptr.setLink(tmp);
        break;
      }
      ptr = ptr.getLink();
    }
    size++;
  }

  public void deleteAtPos(int pos){
    if (pos == 1){
      start = start.getLink();
      size--;
      return;
    }
    if(pos == size){
      Node s = start;
      Node t = start;
      while (s != end){
        t = s;
        s = s.getLink();
      }
      end = t;
      end.setLink(null);
      size--;
      return;
    }
    Node ptr = start;
    pos = pos - 1;
    for(int i = 1; i < size - 1; i++){
      if (i == pos){
        Node tmp = ptr.getLink();
        tmp = tmp.getLink();
        ptr.setLink(tmp);
        break;
      }
      ptr = ptr.getLink();
    }
    size--;
  }

  public void display() {
    System.out.print("\nSingly Linked List = ");
    if (size = 0){
      System.out.print("empty\n");
      return;
    }
    if(start.getLink() == null){
      System.out.println(start.getData());
      return;
    }

    Node ptr = start;
    System.out.print(start.getData() + "->");
    ptr = start.getLink();
    while (ptr.getLink() != null) {
      System.out.print(ptr.getData() + "->");
      ptr = ptr.getLink();
    }
    System.out.print(ptr.getData() + "\n");
  }
}

public class SinglyLinkedList {
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    linkedList list = new linkedList();
    System.out.println("Singly Linked List Test\n");
    char ch;

    do{
      System.out.println("\nSingly Linked List Operations\n");
      System.out.println("1. insert at the beginning");
      System.out.println("2.insert at the end");
      System.out.println("3.insert at position");
      System.out.println("4.delete at position");
      System.out.println("5.check empty");
      System.out.println("6.get size");

      int choice = scan.nextInt();
      switch(choice){
        case 1:
          System.out.println("Enter integer element to insert");
          list.insertAtStart(scan.nextInt());
          break;
        case 2:
          System.out.println("Enter integer element to insert");
          list.insertAtEnd(scan.nextInt());
          break;
        case 3:
          System.out.println("Enter integer element to insert");
          int num = scan.nextInt();
          System.out.println("Enter position");
          int pos = scan.nextInt();
          if(pos <= 1 || pos > list.getSize())
            System.out.println("Invalid position\n");
          else
            list.insertAtPos(num, pos);
            break;
        case 4:
          System.out.println("Enter position");
          int p = scan.nextInt();
          if(p < 1 || p > list.getSize())
            System.out.println("Invalid position\n");
          else
            list.deleteAtPos(p);
            break;
        case 5:
            System.out.println("Empty status = " + list.isEmpty());
            break;
        case 6:
            System.out.println("Size = " + list.getSize() + "\n");
            break;
        default:
            System.out.println("Wrong Entry\n");
            break;
      }
      list.display();
      System.out.println("\nDo you want to continue (TYPE y or n)\n");
      ch = scan.next().charAt(0);
    }while(ch == 'Y' || ch == 'y');
  }
}