-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked list.java
More file actions
128 lines (115 loc) · 3.28 KB
/
Linked list.java
File metadata and controls
128 lines (115 loc) · 3.28 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.company;
import java.util.*;
public class Main {
Node head; //declaring head node
private int size; //used to get size of list
Main()
{
this.size=0;
}
// Node class/ blue-print
class Node{
String data;//value inside linkedlist
Node next;//next node's pointer
Node(String data) //Node Constructor
{
this.data=data;
this.next=null;
size++;
}
}
//add node/data first/last
//add first
public void addFirst(String data)
{
Node newNode= new Node(data); //creation of new node
if(head==null) //checking if head is present or not
{
head=newNode;// if not , creating a new node as head
return;
}
newNode.next=head; //if head is present, the node added points toward head node & becomes the new head node
head=newNode; //the prev head node becomes next node after new head node
}
//add Last
public void addLast(String data)
{
Node newNode=new Node(data);
if(head==null)
{
head=newNode;
return;
}
Node currNode= head;//creating a new node for traversal as head cannot be trifled with to avoid data loss
while(currNode.next!=null)//traversing till the next doesn't point to null
{
currNode=currNode.next;
}
currNode.next=newNode;
}
//printing the list
public void printList()
{
if(head==null)//checking if list is empty
{
System.out.println("list is empty");
return;
}
Node currNode= head;//creating a new node for traversal as head cannot be trifled with to avoid data loss
while(currNode !=null)//traversing till the next doesn't point to null
{
System.out.print(currNode.data+ "-> ");
currNode=currNode.next;
}
System.out.println("NULL"); //end point / null point reached
}
//delete first
public void deleteFirst()
{
if (head==null)
{
System.out.println("list is empty");
return;
}
size--;
head=head.next;
}
//delete last
public void deleteLast()
{
if (head==null) // corner case
{
System.out.println("list is empty");
return;
}
size--;
if (head.next==null) //corner case if only 1 node present
{
head=null;
}
Node secondLast=head; // to traverse
Node lastNode=head.next;//to traverse & delete last node
while (lastNode.next != null) //condition for traversing total list
{
lastNode=lastNode.next;
secondLast=secondLast.next;
}
secondLast.next=null;
}
//getting size of list
public int sizeOflList()
{
return size;
}
public static void main(String[] args) {
Main list=new Main();// linked list object of class
list.addFirst("yo");
list.addLast("hi");
list.addLast("hello");
list.addLast("bonjour");
list.addLast("bella ciao");
list.addLast("namaste");
list.printList();
System.out.println(list.sizeOflList());
}
}