Advertisement
Josif_tepe

Untitled

May 16th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. public class Program {
  2.     public static void main(String[] args) {
  3.      LinkedList list = new LinkedList();
  4.    list.insert_at_end(1);
  5.         list.insert_at_end(2);
  6.         list.insert_at_end(3);
  7.         list.delete_node(2);
  8.         list.print();
  9.     }
  10.  
  11. }
  12.  
  13. class LinkedList {
  14.     Node head = null;
  15.     class Node {
  16.         Node next; // sledbenik
  17.         int info; // informacii zacuvani na linked listata
  18.  
  19.         public Node(int x) {
  20.             info = x;
  21.             next = null;
  22.         }
  23.     }
  24.         public void insert_at_beginning(int x) {
  25.             if(head == null) {
  26.                 head = new Node(x);
  27.                 return; // zavrsi ja funkcijata
  28.             }
  29.             Node new_node = new Node(x);
  30.             new_node.next = head;
  31.             head = new_node;
  32.         }
  33.         public void insert_at_end(int x) {
  34.             if(head == null) {
  35.                 head = new Node(x);
  36.                 return;
  37.             }
  38.             Node tmp = head;
  39.             while(tmp.next != null) {
  40.                 tmp = tmp.next;
  41.             }
  42.             Node new_node = new Node(x);
  43.             tmp.next = new_node;
  44.         }
  45.         public void insert_between(int new_value, int x, int y) {
  46.             Node x_node = head;
  47.             while(x_node.info != x) {
  48.                 x_node = x_node.next;
  49.             }
  50.             Node y_node = head;
  51.             while(y_node.info != y) {
  52.                 y_node = y_node.next;
  53.             }
  54.             Node new_node = new Node(new_value);
  55.             x_node.next = new_node;
  56.             new_node.next = y_node;
  57.         }
  58.         public void delete_node(int x) {
  59.             Node tmp = head;
  60.             Node prethodnik = null;
  61.             while(tmp.info != x) {
  62.                 prethodnik = tmp;
  63.                 tmp = tmp.next;
  64.             }
  65.             prethodnik.next = tmp.next;
  66.         }
  67.         public void print() {
  68.             Node tmp = head;
  69.             while(tmp != null) {
  70.                 System.out.print(tmp.info + " ");
  71.                 tmp = tmp.next;
  72.             }
  73.         }
  74.  
  75.     }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement