Java Each node maintains a vector of distances and next hops to all destinations
ID: 3703804 • Letter: J
Question
JavaEach node maintains a vector of distances and next hops to all destinations
1. Initialize vector with 0 cost to self, infinity to other destinations
2. Periodically send vector to neighbors
3. Update vector for each destination by selecting the shorted distance heard,
after adding cost of neighbor link
4. Use the best neighbor for forwarding
Link State Algorithm 1. Nodes flood topology with link state packets Each node learns full topology 2. Each node computes its own forwarding table By running Dijkstra algorithm
Java
Each node maintains a vector of distances and next hops to all destinations
1. Initialize vector with 0 cost to self, infinity to other destinations
2. Periodically send vector to neighbors
3. Update vector for each destination by selecting the shorted distance heard,
after adding cost of neighbor link
4. Use the best neighbor for forwarding
Link State Algorithm 1. Nodes flood topology with link state packets Each node learns full topology 2. Each node computes its own forwarding table By running Dijkstra algorithm
Java
Each node maintains a vector of distances and next hops to all destinations
1. Initialize vector with 0 cost to self, infinity to other destinations
2. Periodically send vector to neighbors
3. Update vector for each destination by selecting the shorted distance heard,
after adding cost of neighbor link
4. Use the best neighbor for forwarding
Link State Algorithm 1. Nodes flood topology with link state packets Each node learns full topology 2. Each node computes its own forwarding table By running Dijkstra algorithm
Explanation / Answer
As per your question
import java.util.*;
class Node
{
protected int info;
protected Node later, before;
public Node()
{
later = null;
before = null;
info = 0;
}
public Node(int d, Node n, Node p)
{
info = d;
later = n;
before = p;
}
public void setLinklater(Node n)
{
later = n;
}
public void setLinkbefore(Node p)
{
before = p;
}
public Node getLinklater()
{
return later;
}
public Node getLinkbefore()
{
return before;
}
public void setinfo(int d)
{
info = d;
}
public int getinfo()
{
return info;
}
}
class linkedList
{
protected Node begin;
protected Node last ;
public int size;
public linkedList()
{
begin = null;
last = null;
size = 0;
}
public boolean isEmpty()
{
return begin == null;
}
public int getSize()
{
return size;
}
public void insertAtbegin(int val)
{
Node ntest = new Node(val, null, null);
if (begin == null)
{
ntest.setLinklater(ntest);
ntest.setLinkbefore(ntest);
begin = ntest;
last = begin;
}
else
{
ntest.setLinkbefore(last);
last.setLinklater(ntest);
begin.setLinkbefore(ntest);
ntest.setLinklater(begin);
begin = ntest;
}
size++ ;
}
public void insertAtlast(int val)
{
Node ntest = new Node(val, null, null);
if (begin == null)
{
ntest.setLinklater(ntest);
ntest.setLinkbefore(ntest);
begin = ntest;
last = begin;
}
else
{
ntest.setLinkbefore(last);
last.setLinklater(ntest);
begin.setLinkbefore(ntest);
ntest.setLinklater(begin);
last = ntest;
}
size++;
}
public void insertAtstate(int val , int state)
{
Node ntest = new Node(val, null, null);
if (state == 1)
{
insertAtbegin(val);
return;
}
Node test = begin;
for (int i = 2; i <= size; i++)
{
if (i == state)
{
Node tmp = test.getLinklater();
test.setLinklater(ntest);
ntest.setLinkbefore(test);
ntest.setLinklater(tmp);
tmp.setLinkbefore(ntest);
}
test = test.getLinklater();
}
size++ ;
}
public void deleteAtstate(int state)
{
if (state == 1)
{
if (size == 1)
{
begin = null;
last = null;
size = 0;
return;
}
begin = begin.getLinklater();
begin.setLinkbefore(last);
last.setLinklater(begin);
size--;
return ;
}
if (state == size)
{
last = last.getLinkbefore();
last.setLinklater(begin);
begin.setLinkbefore(last);
size-- ;
}
Node test = begin.getLinklater();
for (int i = 2; i <= size; i++)
{
if (i == state)
{
Node p = test.getLinkbefore();
Node n = test.getLinklater();
p.setLinklater(n);
n.setLinkbefore(p);
size-- ;
return;
}
test = test.getLinklater();
}
}
public void display()
{
System.out.print(" Circular Doubly Linked List = ");
Node test = begin;
if (size == 0)
{
System.out.print("empty ");
return;
}
if (begin.getLinklater() == begin)
{
System.out.print(begin.getinfo()+ " <-> "+test.getinfo()+ " ");
return;
}
System.out.print(begin.getinfo()+ " <-> ");
test = begin.getLinklater();
while (test.getLinklater() != begin)
{
System.out.print(test.getinfo()+ " <-> ");
test = test.getLinklater();
}
System.out.print(test.getinfo()+ " <-> ");
test = test.getLinklater();
System.out.print(test.getinfo()+ " ");
}
}
public class CircularDoublyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
linkedList list = new linkedList();
System.out.println("Circular Doubly Linked List Test ");
char ch;
do
{
System.out.println(" Circular Doubly Linked List Operations ");
System.out.println("1. insert at begining");
System.out.println("2. insert at last");
System.out.println("3. insert at stateition");
System.out.println("4. delete at stateition");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.laterInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtbegin( scan.laterInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtlast( scan.laterInt() );
break;
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.laterInt() ;
System.out.println("Enter stateition");
int state = scan.laterInt() ;
if (state < 1 || state > list.getSize() )
System.out.println("Invalid stateition ");
else
list.insertAtstate(num, state);
break;
case 4 :
System.out.println("Enter position");
int p = scan.laterInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position ");
else
list.deleteAtstate(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" ");
break;
default :
System.out.println("Wrong Entry ");
break;
}
list.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.later().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.