LP 5
TOKEN RING
import java.util.Scanner;
public class TokenRing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of nodes you want :");
int n = sc.nextInt();
System.out.println("Ring formed is as below :");
for (int i = 0; i < n; i++) {
System.out.println(i);
}
System.out.println("0");
int choice = 1;
int token = 0;
do {
System.out.println("\nEnter Sender :");
int sender = sc.nextInt();
System.out.println("Enter Receiver :");
int receiver = sc.nextInt();
System.out.println("Enter Data to send :");
int data = sc.nextInt();
System.out.println("Token Passing :");
// Pass the token to the sender
for (int i = token; i < sender; i = (i + 1) % n) {
System.out.println(i + " -> " + sender);
}
// Sender sends the data
System.out.println("Sender " + sender + " sending data " + data);
for (int i = sender; i != token; i = (i + 1) % n) {
System.out.println("Data :" + data + " Forwarded By :" + i);
}
// Pass the token to the receiver
System.out.println("Receiver :" + receiver + " received the data " + data);
token = sender;
System.out.println("\nDo you want to send data again? If yes, enter 1, if no, enter 0");
choice = sc.nextInt();
} while (choice == 1);
sc.close();
}
}
Comments
Post a Comment