2) This question is about the implementation of a singly linked list in Java. Th
ID: 3633520 • Letter: 2
Question
2) This question is about the implementation of a singly linked list in Java. This implementation will use a Node data structure whose Java definition is given as follows:Public class Node{
Public Object element=null;
Public Object Node next=null,
Public Node(Object o, Node n){
Element= 0;
Next = n;
}
The following code which defines a Singly Linked List class is partly completed:
Public class Singly Linked List{
Public Node head = null; // refers to the first node in the list
Public Node last=null; // refers to the last node in the list
Public int size=0; // the number of elements in the list
Public SinglyLinkedList (){}
// insert an element e into the linked list at position i.
Public void add(int i, Object e){
……..
}
// remove and return the last element in the linked list
Public Object removeLast(){
……………
}
// record the singly linked list so that the ordering of the nodes
// becomes reverse of what it was before.
Public void reverse(){
}
……
}
How to complete the methods of this program and give its' worst case running time? index of the list starts at 0
Explanation / Answer
How to implement Stack using Linked list in Java? This example shows how to implement the stack data structure using singly linked list. The Stack has following functions: push(Object) - used to insert the element into the stack. pop() - returns the top element from the stack. isEmpty() - checks whether stack is empty or not. makeEmpty() - makes the empty stack. size()- returns the size of the stack. Stack Interface view plainprint? package datastruct; public interface Stack { void push(Object a); Object pop(); boolean isEmpty(); void makeEmpty(); int size(); } Node Node is the important part of stack implementation. Node holds the information about the element and connected node. view plainprint? package datastruct; public class Node { //element where the object is stored private Object element; //represents the next node private Node next; //Constructors public Node(){ this(null, null); } /** * Node which stores the given Object * and next node information * @param element * @param next */ public Node(Object element, Node next){ this.element = element; this.next = next; } //returns the Object containing in it. public Object getElement(){ return element; } /** * returns the information of previous node. * @return */ public Node getNode(){ return next; } /** * Update the value of Node * @param obj */ public void setElement(Object obj){ this.element = obj; } /** * Update the value of previous node. * @param node */ public void setNode(Node node){ this.next = node; } } Implementation of Stack view plainprint? package datastruct; /** * This example is implementation of Stack using singly Linked List. * @author jegan * */ public class LinkedStack implements Stack { private Node top; private int size; /** * Checks weather the Stack is empty or not */ @Override public boolean isEmpty() { //if value of size is zero then stack is empty. return (size == 0); } /** * Makes the Stack empty */ @Override public void makeEmpty() { //make reference of top to null and makes size to zero. top = null; size =0; } /* * pops the element in the top of stack. */ @Override public Object pop() { //if top element is null returns null if (top == null) return null; //TO-DO throw new StackEmptyException //returns the top element in stack //and set node to previous one. Object val = top.getElement(); top = top.getNode(); size -= 1; return val; } /** * insert new element into the Stack */ @Override public void push(Object obj) { //Creates the new Node and inserts into stack. Node v = new Node(obj, top); size += 1; top = v; } /** * returns the size of the Stack. */ @Override public int size() { return size; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.