Using any variable and any name for the methods (data structure java eclipse)
1) Find odd average and even number average in a linked list using recursion
2) Remove duplicate from a stack.
1) Find odd average and even number average in a linked list using recursion
// A linked list node
class Node
{
int data;
Node next;
Node(int data, Node next) {
this.data = data;
this.next = next;
}
Node() { }
};
class ListUtils
{
// Helper function to print given linked list
public static void printList(Node head)
{
Node ptr = head;
while (ptr != null)
{
System.out.print(ptr.data + " -> ");
ptr =
ptr.next;
}
System.out.println("null");
}
// Recursive function to rearrange the list
public static Node rearrange(Node head, Node odd, Node
even,
Node oddRef)
{
// we have reached the end of the
list
if (head == null)
{
// null
terminate the list
odd.next =
null;
// join even
sublist and odd sublist
even.next =
oddRef.next;
return
head;
}
// if current node is odd
if ((head.data & 1) != 0)
{
odd.next =
head;
rearrange(head.next, head, even, oddRef);
}
// if current node is even
else
{
even.next =
head;
rearrange(head.next, odd, head, oddRef);
}
return head;
}
// Rearrange the given linked list by separating
odd nodes
// from even ones and maintaining their relative
order.
public static Node rearrangeEvenOdd(Node head)
{
Node odd = new Node();
Node even = new Node();
rearrange(head, odd, even, odd);
return even.next;
}
public static void main(String[] args)
{
// input keys
int[] keys = { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10 };
Node head = null;
for (int i = keys.length - 1; i
>= 0; i--) {
head = new
Node(keys[i], head);
}
head =
rearrangeEvenOdd(head);
printList(head);
}
}
2) Remove duplicate from a stack.
import java.util.*;
/**
* @author Daniel Pitts
*/
public class UniqueQueue<T> implements Queue<T>,
Set<T> {
private final Set<T> data = new
LinkedHashSet<T>();
public int size() {
return data.size();
}
public boolean isEmpty() {
return data.isEmpty();
}
public boolean contains(Object o) {
return data.contains(o);
}
public Iterator<T> iterator() {
return data.iterator();
}
public Object[] toArray() {
return data.toArray();
}
public <T> T[] toArray(T[] a) {
return data.toArray(a);
}
public boolean add(T t) {
return data.add(t);
}
public boolean offer(T t) {
return add(t);
}
public T remove() {
final Iterator<T> iterator = data.iterator();
T t = iterator.next();
iterator.remove();
return t;
}
public T poll() {
final Iterator<T> iterator = data.iterator();
if (iterator.hasNext()) {
T t = iterator.next();
iterator.remove();
return t;
}
return null;
}
public T element() {
return data.iterator().next();
}
public T peek() {
final Iterator<T> iterator = data.iterator();
if (iterator.hasNext()) {
return iterator.next();
}
return null;
}
public boolean remove(Object o) {
return data.remove(o);
}
public boolean containsAll(Collection<?> c) {
return data.containsAll(c);
}
public boolean addAll(Collection<? extends T> c) {
return data.addAll(c);
}
public boolean retainAll(Collection<?> c) {
return data.retainAll(c);
}
public boolean removeAll(Collection<?> c) {
return data.removeAll(c);
}
public void clear() {
data.clear();
}
public boolean equals(Object o) {
return data.equals(o);
}
public int hashCode() {
return data.hashCode();
}
}
Using any variable and any name for the methods (data structure java eclipse) 1) Find odd average and even number average in a linked list using recursion 2) Remove duplicate from a stack.
Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...
Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...
Java 1. Write a method to search for and remove an element from a linked list, thereby returning the data portion of the node. 2. Making use of both Stacks and Queues, write a function that will determine whether or not a String is a palindrome
// Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...
Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions become private in the...
Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...
Using Java coding, complete the following: This program should implement a LinkedList data structure to handle the management of student records. It will need to be able to store any number of students, using a Linked List. LinearNode.java can be used to aid creating this program Student should be a class defined with the following: • String name • int year • A linked list of college classes (college classes are represented using strings) • An...
Homework #1 – Implementing Set with a Linked List Project Objectives 1. Be able to integrate the knowledge of Java Generics, Collection Framework and the Linked List data structure to implement the Set ADT. Components emphasized are: • Allowing parameterized type for handling a general class of values in the collection framework; • Understanding conceptual differences between lists and sets and implementing them with methods; • Implementation of an iterator with functionality that is appropriate for the collection, namely the...
Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers should be within a range (E.g., 1 to 7). The user should be prompted to enter the minimum number and maximum number of the range. Each number should be inserted at the end of the list. Section 7.8 of the textbook covers the random number generator. Examples of how to use the random number generator are in Fig 7.6 and 7.7. Here is a...
Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...