Question: Write the Java source code necessary to build a solution for the problem below: The Fibonacci numbers form a sequence where each number is the

Write the Java source code necessary to build a solution for the problem below: The Fibonacci numbers form a sequence where each number is the sum of the previous two numbers. Starting from 0 and 1, the first eight Fibonacci numbers are built in the following list using the equation F n = F n-1 + F n-2:

0, 0 0, 1 1, 1 1, 2 2, 3 3, 5 5, 8 8, 13

The sequence would be the numbers in red (0, 1, 1, 2, 3, 5, 8, 13).

Using the linked list (MyLinkedList) created in Assignment 3.1, create a stack class and a test program to display the first 50 Fibonacci numbers in descending order. Use a stack to store these numbers. Once 50 numbers have been computed, print the numbers in a descending order. You will note that this process is naturally recursive. Therefore, use recursion to find this series of Fibonacci numbers.

My Current Application:

static class Node{

String data; Node next;

Node(String d){ data = d; next =null; }

}

private Node head;

public Assignment_3_1() {

head = null;

}

public void append(String data){

if(head == null) head = new Node(data); else{ Node temp = head; while(temp.next != null) temp = temp.next; temp.next = new Node(data); }

}

public void addFront(String data){

Node newNode = new Node(data); newNode.next = head; head = newNode;

}

public void removeFromLast(){

if(head == null || head.next == null) head = null; else{ Node temp = head; while(temp.next.next != null) temp = temp.next; temp.next = null; }

}

public void removeFirst(){

if(head == null) return; else head = head.next;

}

public boolean search(String data){

Node temp = head;

while(temp != null){ if(temp.data.equals(data)) return true; temp = temp.next; } return false;

}

public void addAtMiddle(String data){

Node slow = head; Node fast = head; Node prev = null; Node newNode = new Node(data);

if(head == null || head.next == null){ newNode.next = head; head = newNode; return; } while(fast != null && fast.next != null){ fast = fast.next.next; prev = slow; slow = slow.next; } if(prev != null) prev.next =newNode; newNode.next = slow; }

public void removeAtMiddle(){

Node slow = head; Node fast = head; Node prev = null;

if(head == null || head.next == null){ head = null; return; }

while(fast != null && fast.next != null){ fast = fast.next.next; prev = slow; slow = slow.next; }

prev.next = slow.next;

}

public void display(){

Node temp = head;

while(temp != null){ System.out.print(temp.data+" "); temp =temp.next; }

System.out.println();

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!