Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty().
my linkedlist class is:
public class Lab8_problem1 {
private Node head, tail;
private int size;
public Lab8_problem1() {
this.head = null;
this.tail = null;
this.size = 0;
}
//Insert a node at specifc Location
public void insertAt(int value, int index)
{
Node newNode = new Node();
newNode.data = value;
if(head == null){
head = newNode;
tail = newNode;
head.next = null;
tail.next = null;
}
else
{
if(index == 0)
insertFront(value);
else if(index == size)
insertLast(value);
else if(index > size)
System.out.println("Cannot insert");
else{
int count = 0;
Node p1 = head.next;
Node p2 = head;
while(count <index)
{
p1 = p1.next;
p2 = p2.next;
count++;
}
newNode.next = p1;
p2.next = newNode;
}
}
size++;
}
//Insert a node at the end of the list
public void insertLast(int value)
{
Node newNode = new Node();
newNode.data = value;
if(head == null){
head = newNode;
tail = newNode;
head.next = null;
tail.next = null;
}
else
{
tail.next = newNode;
tail = newNode;
}
size++;
}
//Insert a node at the front of the list
public void insertFront(int value)
{
Node newNode = new Node();
newNode.data = value;
if(head == null){
head = newNode;
tail = newNode;
head.next = null;
tail.next = null;
}
else
{
newNode.next = head;
head = newNode;
}
size++;
}
//Display all nodes in the list
public void display()
{
if(head == null)
System.out.println("Empty List!");
else
{
Node pointer = head;
//while(pointer!=null)
for (int i = 0; i < size; i++) {
System.out.print(pointer.data + " ");
pointer = pointer.next;
}
System.out.println("");
}
}
//Method to check of the list is empty - isEmpty() (returns a
Boolean)
public boolean isEmpty(){
return (head==null); //
}
//Method to return the size of the list
public int getSize(){
return size;
}
//Delete all nodes in the list
public void clear(){
head = null;
tail = null;
size = 0;
}
//Search List for a node with a certain value (returns the node if
found, null if not found)
public Node search(int value)
{
if(head == null)
System.out.println("Empty List!");
else
{
Node pointer = head;
//while(pointer!=null)
for (int i = 0; i < size; i++) {
if(pointer.data==value) // if zero => they are equal
{
return pointer;
}
pointer = pointer.next;
}
}
return null;
}
//get node of specific index
public Node getNode(int index)
{
if(head == null || index > size-1)
System.out.println("Empty List or invalide index!");
else
{
Node pointer = head;
int count = 0;
while(count != index)
{
count++;
pointer = pointer.next;
}
return pointer;
}
return null;
}
//Delete a node with a specific value in the list if found
public void delete(int value){
if(isEmpty())
System.out.println("Empty List!!");
else if(head.data == value) // if zero => they are equal
{
head = head.next;
}
else{
Node pointer1 = head.next;
Node pointer2 = head;
while(pointer1!=null) {
if(pointer1.data==value) // if zero => they are equal
{
pointer2.next = pointer1.next;
if(tail.data==value)
tail = pointer2;
break;
}
pointer1 = pointer1.next;
pointer2 = pointer2.next;
}
}
size--;
}
//Delete a node with a specific value in the list if found
public void deleteAt(int index){
if(isEmpty() || index >= size)
System.out.println("Empty List or invalid index!!");
else if(index == 0) // if zero => they are equal
head = head.next;
else{
Node pointer1 = head.next;
Node pointer2 = head;
int count = 0;
while(pointer1!=null) {
if(count==index)
{
pointer2.next = pointer1.next;
if(index == size-1)
tail = pointer2;
break;
}
pointer1 = pointer1.next;
pointer2 = pointer2.next;
}
}
size--;
}
class Node {
public int data;
public Node next;
public Node() {
this.data = 0;
this.next = null;
}
public int getData(){
return data;
}
public int count(){
if(head==null)
return 0;
else return countR(head);
}
private int countR(Node p){
if(p==null)
return 0;
else
{
if(p.data<0)
return 1+countR(p.next);
else
return 0+countR(p.next);
}
}
public int maxNum(){
return maxNumR(head, head.data);
}
public int maxNumR(Node p, int max){
if(p==null)
return max;
else{
if(p.data>max)
return maxNumR(p.next, p.data);
else
return maxNumR(p.next, max);
}
}
public void printR(Node p){
if(p==null);
else{
System.out.print(p.data+"\t");
printR(p.next);
}
}
public void printReve(){
printReveR(head);
}
private void printReveR(Node p){
if(p==null);
else{
printReveR(p.next);
System.out.print(p.data+"\t");
}
}
public void searchIndex(int value){
if(head==null)
System.out.println("empty");
else
searchIndexR(value, head, 0);
}
private void searchIndexR(int value, Node p, int i){
if(p==null);
else{
if(value == p.data)
System.out.print(i+"\t");
searchIndexR(value, p.next, ++i);
}
}
}
}
java - netbeans.
The question says to develop a stack using java.util.LinkedList, I don’t think it needs to use the linked list class provided by you. Anyway, I have written two solutions (using both approaches). Choose whichever you want.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: the stack is integer based, not generic, if you want it to be generic, let me know.
// Stack.java using the linked list class you provided
public class Stack {
// creating an object of Lab8_problem1 as instance variable
private Lab8_problem1 list;
// constructor
public Stack() {
// initializing list
list = new Lab8_problem1();
}
// adds an element to the top of stack
public void push(int value) {
// here we consider front of list as top of stack, adding value to front
list.insertFront(value);
}
// removes and returns the top element from stack
public int pop() {
// if empty, throwing an exception
if (isEmpty()) {
throw new RuntimeException("Stack is empty!");
}
// getting element at index 0 from stack (front of list)
int element = list.getNode(0).data;
// removing it
list.deleteAt(0);
// returning removed element
return element;
}
// returns the top element from stack
public int peek() {
if (isEmpty()) {
throw new RuntimeException("Stack is empty!");
}
// returning element at index 0 from stack (front of list)
return list.getNode(0).data;
}
// returns the size of the list
public int size() {
return list.getSize();
}
// returns true if stack is empty
public boolean isEmpty() {
return size() == 0;
}
}
// Stack.java using java.util.LinkedList
import java.util.LinkedList;
public class Stack {
// creating an object of java.util.LinkedList as instance variable
private LinkedList<Integer> list;
// constructor
public Stack() {
// initializing list
list = new LinkedList<Integer>();
}
// adds an element to the top of stack
public void push(int value) {
// here we consider front of list as top of stack, adding value to front
list.addFirst(value);
}
// removes and returns the top element from stack
public int pop() {
// if empty, throwing an exception
if (isEmpty()) {
throw new RuntimeException("Stack is empty!");
}
// getting element at index 0 from stack (front of list)
int element = list.get(0);
// removing it
list.removeFirst();
// returning removed element
return element;
}
// returns the top element from stack
public int peek() {
if (isEmpty()) {
throw new RuntimeException("Stack is empty!");
}
// returning element at index 0 from stack (front of list)
return list.getFirst();
}
// returns the size of the list
public int size() {
return list.size();
}
// returns true if stack is empty
public boolean isEmpty() {
return size() == 0;
}
}
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...
could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...
Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: package lists; import bookdata.*; public class DoublyLinkedList { int size; //Variable que define el tamano de la lista. Node head, tail; //Nodos que definen el Head y Tail en la lista. //Constructor public DoublyLinkedList(){ this.head = null; this.tail = null; this.size = 0; } //Insert a new book in alphabetic order. public void insert(Book nb){ //Creamos uno nuevo nodo con el...
(1) Implement the countKey(T element) method, which should return a count of the number of times that the given key (the element) is found in the list. (2) Implement the indexOf(T element) method, which is similar as the indexOf method in the String class. It returns the index (the position starting from the head node) of the first occurrence of the given element, or -1, if the element does not occur in the list. You will need to track the...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
// 1. Add methods to get and set, Data and Link. Data should be any Comparable object. class Node { Integer data; // Integer is Comparable Node link; public Node(Integer data, Node link) { this.data = data; this.link = link; } public Integer getData() { return data; } public void setData(Integer data) { this.data = data; } public Node getLink() { return link; } public void setLink(Node link) { this.link = link; } } // b. Create MyLinkedList class and...
Here is the IntegerLinkedList_incomplete
class:
public class IntegerLinkedList {
static class Node {
/** The element stored at this node */
private int element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n...
Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable { // ---------------- nested Node class...
Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...
Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...