LinkedList

Run Settings
LanguageJava
Language Version
Run Command
class Node{ private String value; private Node next; public Node(String value){ this.value= value; } public void setNext(Node next){ this.next= next; } public Node getNext(){ return this.next; } public void setValue(String value){ this.value= value; } public String getValue(){ return this.value; } } class MyStack{ private Node bottom; private Node top; private int length; public MyStack(String value){ top = new Node(value); bottom= top; this.length++; } public String peek(){ if (this.length ==0){ System.out.println("The stack is empty, cannot pop"); return; } return this.top.getValue(); } public String pop(){ if (this.length ==0){ System.out.println("The stack is empty, cannot pop"); return; } Node removedTop =top; top= removedTop.getNext(); this.length--; if (removedTop == bottom){ bottom=null; } return removedTop.getValue(); } public void push(String value){ Node newTop = new Node(value); newTop.setNext(top); top=newTop; this.length++; } public boolean isEmpty(){ return this.length==0; } /*public static void main(String args[]){ MyStack stack = new MyStack("uno"); stack.push("dos"); stack.push("tres"); stack.push("cuatro"); stack.push("cinco"); System.out.println(stack.peek()); System.out.println(stack.pop()); System.out.println(stack.pop()); while(!stack.isEmpty()){ System.out.println(stack.pop()); } }*/ } class Main { private Node head; private Node tail; private int length=0; public Main(String value){ this.head = new Node(value); this.tail = head; length++; } public void prepend(String value){ Node newHead = new Node(value); newHead.setNext(head); head= newHead; length++; } public void append(String value){ Node newTail= new Node(value); tail.setNext(newTail); tail=newTail; length++; } public void insert(int index, String value){ Node currentNode = head; Node previousNode=head; int currentIndex=0; if (index ==0){ prepend(value); return; } while (currentIndex < index && currentNode != null){ previousNode = currentNode; currentNode = currentNode.getNext(); currentIndex++; } Node newNode = new Node(value); previousNode.setNext(newNode); newNode.setNext(currentNode); } public void delete(int index){ Node currentNode = head; Node previousNode=head; int currentIndex=0; while (currentIndex < index && currentNode != null){ previousNode = currentNode; currentNode = currentNode.getNext(); currentIndex++; } if (index ==0){ head= head.getNext(); } else { previousNode.setNext(currentNode.getNext()); } } public void printAll(){ Node node = head; while (node != null){ System.out.println(""+node.getValue()); node = node.getNext(); } } public static void main(String[] args) { /*Main linkedList = new Main("1"); linkedList.append("2"); linkedList.append("3"); linkedList.append("4"); linkedList.prepend("0"); linkedList.prepend("-1"); linkedList.insert(1, "-0.5"); linkedList.insert(3, "0.5"); linkedList.insert(7, "3.5"); linkedList.insert(0, "-2"); linkedList.printAll(); linkedList.delete(9); linkedList.printAll();*/ MyStack stack = new MyStack("uno"); //System.out.println(stack.peek()); stack.push("dos"); //System.out.println(stack.peek()); stack.push("tres"); //System.out.println(stack.peek()); stack.push("cuatro"); //System.out.println(stack.peek()); stack.push("cinco"); //System.out.println(stack.peek()); //System.out.println(stack.pop()); //System.out.println(stack.pop()); while(!stack.isEmpty()){ System.out.println(stack.pop()); } // System.out.println(stack.pop()); } }
/*class Node{ private String value; private String next; public Node(String value){ this.value = value; } public void setNext(String next){ this.next = next; } public String getNext(String next){ return this.next; } public String getValue(String value){ return this.value; } }*/ class Stack{ private Node bottom; private Node top; private int length; public Stack(String value){ top = new Node(value); bottom= top; } public String peek(){ return this.top.getValue(); } public String pop(){ Node removedTop =top.getNext(); top= removedTop.getNext(); this.length--; if (removedTop == bottom){ bottom=null; } return removedTop.getValue(); } public void push(String value){ Node newTop = new Node(value); newTop.setNext(top); top=newTop; this.length++; } public void isEmpty(){ return this.length==0; } public static void main(String args[]){ MyStack stack = new MyStack("uno"); stack.push("dos"); stack.push("tres"); stack.push("cuatro"); stack.push("cinco"); System.out.println(stack.peek()); System.out.println(stack.pop()); System.out.println(stack.pop()); while(!stack.isEmpty()){ System.out.println(stack.pop()); } } }
Editor Settings
Theme
Key bindings
Full width
Lines