Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class People{
  4. public static void main (String[] args) {
  5. Scanner scnr = new Scanner(System.in);
  6.  
  7. PeopleNode headNode;
  8. PeopleNode currNode;
  9. PeopleNode lastNode;
  10.  
  11. String people;
  12. int numberOfpeople;
  13. int i;
  14.  
  15. // Front of nodes list
  16. headNode = new PeopleNode();
  17. lastNode = headNode;
  18.  
  19. int input = scnr.nextInt();
  20.  
  21. for(i = 0; i < input; i++ ) {
  22. people = scnr.next();
  23. numberOfpeople=scnr.nextInt();
  24. currNode = new PeopleNode(people, numberOfpeople);
  25. currNode.insertAtFront(headNode, currNode);
  26. lastNode = currNode;
  27. }
  28.  
  29. // Print linked list
  30. currNode = headNode.getNext();
  31. while (currNode != null) {
  32. currNode.printNodeData();
  33. currNode = currNode.getNext();
  34. }
  35. }
  36. }
  37.  
  38. public class PeopleNode {
  39. private String People;
  40. private int numberOfPeople;
  41. private PeopleNode nextNodeRef; // Reference to the next node
  42.  
  43. public PeopleNode() {
  44. People = "";
  45. numberOfPeople = 0;
  46. nextNodeRef = null;
  47. }
  48.  
  49. // Constructor
  50. public PeopleNode(String people, int numberOfPeople) {
  51. this.People= people;
  52. this.numberOfPeople = numberOfPeople;
  53. this.nextNodeRef = null;
  54. }
  55.  
  56. // Constructor
  57. public PeopleNode(String People, int numberOfPeople, PeopleNode nextLoc) {
  58. this.People= People;
  59. this.numberOfPeople = numberOfPeople;
  60. this.PeopleNode= nextLoc;
  61. }
  62.  
  63.  
  64. //Define an insertAtFront() method that inserts a node at the
  65. //front of the linked list
  66. public void insertAtFront(PeopleNode headNode,PeopleNode currNode) {
  67. PeopleNode tmpNext;
  68. tmpNext = this.nextNodeRef;
  69. this.nextNodeRef = currNode;
  70. currNode.nextNodeRef = tmpNext;
  71. }
  72.  
  73. // Get location pointed by nextNodeRef
  74. public PeopleNode getNext() {
  75. return this.nextNodeRef;
  76. }
  77.  
  78. // Print node data
  79. public void printNodeData() {
  80. System.out.println(this.numberOfPeople + " " + this.People);
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement