Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Algoritmi {
- }
- class Node{
- int data;
- Node next;
- public Node(int x)
- {
- data=x;
- next=null;
- }
- }
- class LinkedList{
- Node head;
- public void insertNodeAtStart(int x)
- {
- if(head==null)
- {
- head=new Node(x);
- return;
- }
- Node newNode=new Node(x);
- newNode.next=head;
- head=newNode;
- }
- public void insertNodeAtEnd(int x)
- {
- if(head==null)
- {
- head=new Node(x);
- return;
- }
- Node newNode=new Node(x);
- Node temporaryNode=head;
- while(temporaryNode!=null)
- {
- temporaryNode=temporaryNode.next;
- }
- temporaryNode.next=newNode;
- }
- public void deleteNode(int x)
- {
- Node currentNode=head;
- Node previousNode=null;
- while(currentNode!=null && currentNode.data!=x)
- {
- previousNode=currentNode;
- currentNode=currentNode.next;
- }
- previousNode.next=currentNode.next;
- }
- public void print()
- {
- Node temporaryNode=head;
- while(temporaryNode!=null)
- {
- System.out.println(temporaryNode.data+"-->");
- temporaryNode=temporaryNode.next;
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement