Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Program {
- public static void main(String[] args) {
- LinkedList list = new LinkedList();
- list.insert_at_end(1);
- list.insert_at_end(2);
- list.insert_at_end(3);
- list.delete_node(2);
- list.print();
- }
- }
- class LinkedList {
- Node head = null;
- class Node {
- Node next; // sledbenik
- int info; // informacii zacuvani na linked listata
- public Node(int x) {
- info = x;
- next = null;
- }
- }
- public void insert_at_beginning(int x) {
- if(head == null) {
- head = new Node(x);
- return; // zavrsi ja funkcijata
- }
- Node new_node = new Node(x);
- new_node.next = head;
- head = new_node;
- }
- public void insert_at_end(int x) {
- if(head == null) {
- head = new Node(x);
- return;
- }
- Node tmp = head;
- while(tmp.next != null) {
- tmp = tmp.next;
- }
- Node new_node = new Node(x);
- tmp.next = new_node;
- }
- public void insert_between(int new_value, int x, int y) {
- Node x_node = head;
- while(x_node.info != x) {
- x_node = x_node.next;
- }
- Node y_node = head;
- while(y_node.info != y) {
- y_node = y_node.next;
- }
- Node new_node = new Node(new_value);
- x_node.next = new_node;
- new_node.next = y_node;
- }
- public void delete_node(int x) {
- Node tmp = head;
- Node prethodnik = null;
- while(tmp.info != x) {
- prethodnik = tmp;
- tmp = tmp.next;
- }
- prethodnik.next = tmp.next;
- }
- public void print() {
- Node tmp = head;
- while(tmp != null) {
- System.out.print(tmp.info + " ");
- tmp = tmp.next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement