Repository navigation
Expand file tree
/
Copy pathsll.java
More file actions
114 lines (102 loc) · 2.3 KB
/
Copy pathsll.java
File metadata and controls
114 lines (102 loc) · 2.3 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
import java.util.Scanner;
class sll {
class node {
int element;
node next;
node(int element) {
this.element = element;
this.next = null;
}
}
node head = null;
node tail = null;
int counter = 0;
public node createsll() {
Scanner sc = new Scanner(System.in);
int cho = 1;
while (cho != 0) {
System.out.print("enter element: ");
int num = sc.nextInt();
node newnode = new node(num);
insertlast(newnode);
System.out.print("enter 0 to stop: ");
cho = sc.nextInt();
}
return head;
}
public node insertlast(node newnode) {
if (head == null) {
head = newnode;
tail = newnode;
}
else {
tail.next = newnode;
tail = tail.next;
}
counter++;
return head;
}
public node insertfirst (node newnode){
if (head == null) {
head = newnode;
tail = newnode;
}
else {
newnode.next = head;
head = newnode;
}
counter++;
return head;
}
node deletefirst() {
if (head == null)
return null;
head = head.next;
if (head == null)
tail = null;
counter--;
return head;
}
node deletelast() {
if (head == null) {
return null;
}
if (head.next == null) {
head = null;
tail = null;
counter--;
return null;
}
node temp = head;
while (temp.next != tail) {
temp = temp.next;
}
temp.next = null;
tail = temp;
counter--;
return head;
}
int getsize() {
return counter;
}
boolean isempty() {
if (head == null)
return true;
else
return false;
}
int findnode(int val) {
node temp = head;
int pos = 0;
while (temp != null) {
if (temp.element != val) {
temp = temp.next;
pos++;
}
else {
return pos;
}
}
return pos;
}
}