queue using linkedlist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import java.util.*; class Node { public static int data; public static Node next; public Node(int x) { data = x; } public void displayNode() { System.out.print(data + " "); } } class qeueu_using_linkedlist { private static Node front; private static Node rear; public qeueu_using_linkedlist() { front = null; rear = null; } public static void enqueue(int value) { Node newnode = new Node(value); newnode.next = null; if (isempty()) { front = newnode; } else { rear.next = newnode; rear = newnode; } } public static void dequeue() { int temp = front.data; if (front.next == null) { rear = null; } else { front = front.next; } } public static int peek() { return (front.data); } public static boolean isempty() { if (front == null) { return true; } return false; } public static void displayList() { Node current = front; while (current != null) { System.out.println(current.data + " last "); //current.displayNode(); // System.out.println(current.data+"1"); current = current.next; // System.out.println(current.data+" last "); } } public static void main(String[] args) { qeueu_using_linkedlist object = new qeueu_using_linkedlist(); Scanner vinnu = new Scanner(System.in); while (true) { System.out.println("1.Insert"); System.out.println("2.Delete"); System.out.println("3.Peek"); System.out.println("4.isempty"); System.out.println("5.Display"); System.out.println("6.exit"); System.out.println("Enter your choice = "); int option = vinnu.nextInt(); System.out.println("--------------------------------------------------"); switch (option) { case 1: System.out.print("enter the elememt to push = "); int value = vinnu.nextInt(); enqueue(value); break; case 2: dequeue(); break; case 3: int result = peek(); System.out.println(result + " is the peek element . "); break; case 4: isempty(); break; case 5: displayList(); break; case 6: System.exit(1); } System.out.println("--------------------------------------------------"); } } } |
queue using linkedlist
Reviewed by Unknown
on
09:51
Rating:
No comments: