Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node{
- private String payload;
- private Node next;
- public Node (String payload){
- this.payload = payload;
- this.next = null;
- }
- public Node (String payload, Node next){
- this.payload = payload;
- this.next = next;
- }
- public String getPayload (){
- return this.payload;
- }
- public Node getNext(){
- return next;
- }
- public void setPayload (String payload){
- this.payload = payload;
- }
- public void setNext(Node next){
- this.next = next;
- }
- }
- class LinkedList2{
- Node head;
- public LinkedList2(){
- head = null;
- }
- public void printList(){
- Node currentNode = head;
- while(currentNode.getNext() != null){
- System.out.println(currentNode.getPayload());
- currentNode = currentNode.getNext();
- }
- System.out.println(currentNode.getPayload());
- }
- public void addHead(String value){
- head = new Node (value, head);
- }
- public void removeHead(){
- if (head != null){
- head = head.getNext();
- }
- }
- public static void main (String [] args){
- LinkedList2 list = new LinkedList2();
- list.addHead("test1");
- list.addHead("test2");
- list.addHead("test3");
- list.removeHead();
- list.printList();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement