Stock Generator


SUBMITTED BY: Gummzag3400

DATE: Oct. 3, 2023, 12:25 p.m.

FORMAT: Text only

SIZE: 7.9 kB

HITS: 13384

  1. import java.util.*;
  2. class Node {
  3. int data;
  4. Node prev;
  5. Node next;
  6. public Node(int data) {
  7. this.data = data;
  8. this.prev = null;
  9. this.next = null;
  10. }
  11. }
  12. class DoublyLinkedListCalaycay {
  13. Node head;
  14. Node tail;
  15. // Constructor to initialize an empty list.
  16. public DoublyLinkedListCalaycay() {
  17. head = null;
  18. tail = null;
  19. }
  20. // Add a new node to the head of the list.
  21. public void addToHead(int data) {
  22. Node newNode = new Node(data);
  23. if (head == null) {
  24. head = newNode;
  25. tail = newNode;
  26. } else {
  27. newNode.next = head;
  28. head.prev = newNode;
  29. head = newNode;
  30. }
  31. System.out.println("Added " + data + " to the head.");
  32. }
  33. // Add a new node to the tail of the list.
  34. public void addToTail(int data) {
  35. Node newNode = new Node(data);
  36. if (tail == null) {
  37. head = newNode;
  38. tail = newNode;
  39. } else {
  40. tail.next = newNode;
  41. newNode.prev = tail;
  42. tail = newNode;
  43. }
  44. System.out.println("Added " + data + " to the tail.");
  45. }
  46. // Print the data in the head node.
  47. public void printHead() {
  48. if (head != null) {
  49. System.out.println("Head: " + head.data);
  50. } else {
  51. System.out.println("List is empty.");
  52. }
  53. }
  54. // Print the data in the tail node.
  55. public void printTail() {
  56. if (tail != null) {
  57. System.out.println("Tail: " + tail.data);
  58. } else {
  59. System.out.println("List is empty.");
  60. }
  61. }
  62. // Print the data in the list from head to tail.
  63. public void printForward() {
  64. Node current = head;
  65. System.out.print("Forward: ");
  66. while (current != null) {
  67. System.out.print(current.data + " ");
  68. current = current.next;
  69. }
  70. System.out.println();
  71. }
  72. // Print the data in the list from tail to head.
  73. public void printBackward() {
  74. Node current = tail;
  75. System.out.print("Backward: ");
  76. while (current != null) {
  77. System.out.print(current.data + " ");
  78. current = current.prev;
  79. }
  80. System.out.println();
  81. }
  82. // Remove the node from the head of the list.
  83. public void removeFromHead() {
  84. if (head != null) {
  85. if (head == tail) {
  86. head = null;
  87. tail = null;
  88. } else {
  89. head = head.next;
  90. head.prev = null;
  91. }
  92. System.out.println("Removed from head.");
  93. } else {
  94. System.out.println("List is empty.");
  95. }
  96. }
  97. // Remove the node from the tail of the list.
  98. public void removeFromTail() {
  99. if (tail != null) {
  100. if (head == tail) {
  101. head = null;
  102. tail = null;
  103. } else {
  104. tail = tail.prev;
  105. tail.next = null;
  106. }
  107. System.out.println("Removed from tail.");
  108. } else {
  109. System.out.println("List is empty.");
  110. }
  111. }
  112. // Insert a new node after a specific item.
  113. public void insertNodeAfter(int searchItem, int itemToInsert) {
  114. Node newNode = new Node(itemToInsert);
  115. Node current = head;
  116. while (current != null) {
  117. if (current.data == searchItem) {
  118. newNode.prev = current;
  119. newNode.next = current.next;
  120. if (current.next != null) {
  121. current.next.prev = newNode;
  122. } else {
  123. tail = newNode;
  124. }
  125. current.next = newNode;
  126. System.out.println("Inserted " + itemToInsert + " after " + searchItem + ".");
  127. return;
  128. }
  129. current = current.next;
  130. }
  131. System.out.println(searchItem + " not found in the list.");
  132. }
  133. // Remove a specific node from the list.
  134. public void removeNode(int itemToRemove) {
  135. Node current = head;
  136. while (current != null) {
  137. if (current.data == itemToRemove) {
  138. if (current == head) {
  139. removeFromHead();
  140. } else if (current == tail) {
  141. removeFromTail();
  142. } else {
  143. current.prev.next = current.next;
  144. current.next.prev = current.prev;
  145. System.out.println("Removed " + itemToRemove + " from the list.");
  146. }
  147. return;
  148. }
  149. current = current.next;
  150. }
  151. System.out.println(itemToRemove + " not found in the list.");
  152. }
  153. }
  154. import java.util.*;
  155. public class DoublyLinkedListDemo {
  156. public static void main(String[] args) {
  157. DoublyLinkedListCalaycay list = new DoublyLinkedListCalaycay();
  158. Scanner scanner = new Scanner(System.in);
  159. while (true) {
  160. System.out.println("Doubly Linked List Operations:");
  161. System.out.println("1 - Add to Head");
  162. System.out.println("2 - Add to Tail");
  163. System.out.println("3 - Print Head");
  164. System.out.println("4 - Print Tail");
  165. System.out.println("5 - Print Forward");
  166. System.out.println("6 - Print Backward");
  167. System.out.println("7 - Remove from Head");
  168. System.out.println("8 - Remove from Tail");
  169. System.out.println("9 - Insert Node After");
  170. System.out.println("10 - Remove Node");
  171. System.out.println("11 - Quit");
  172. System.out.print("Enter your choice (1-11): ");
  173. int choice = scanner.nextInt();
  174. switch (choice) {
  175. case 1:
  176. System.out.print("Enter value to add to the head: ");
  177. int data = scanner.nextInt();
  178. list.addToHead(data);
  179. break;
  180. case 2:
  181. System.out.print("Enter value to add to the tail: ");
  182. data = scanner.nextInt();
  183. list.addToTail(data);
  184. break;
  185. case 3:
  186. list.printHead();
  187. break;
  188. case 4:
  189. list.printTail();
  190. break;
  191. case 5:
  192. list.printForward();
  193. break;
  194. case 6:
  195. list.printBackward();
  196. break;
  197. case 7:
  198. list.removeFromHead();
  199. break;
  200. case 8:
  201. list.removeFromTail();
  202. break;
  203. case 9:
  204. System.out.print("Enter the item to search for: ");
  205. int searchItem = scanner.nextInt();
  206. System.out.print("Enter the item to insert after: ");
  207. int itemToInsert = scanner.nextInt();
  208. list.insertNodeAfter(searchItem, itemToInsert);
  209. break;
  210. case 10:
  211. System.out.print("Enter the item to remove: ");
  212. int itemToRemove = scanner.nextInt();
  213. list.removeNode(itemToRemove);
  214. break;
  215. case 11:
  216. System.out.println("Exiting...");
  217. scanner.close();
  218. System.exit(0);
  219. default:
  220. System.out.println("Invalid choice. Please enter a valid option.");
  221. }
  222. }
  223. }
  224. }

comments powered by Disqus