Sei sulla pagina 1di 20

Singly Linked List

public static void insertNode(String item, int location) {


Node x = new Node();
Node p = new Node();
x.data = item;
x.link = null;
if(head==null) {
head=x;
} else {
p = head;
if (location==0){
x.link = head;
head = x;
} else {
for (int i=1;i<=location -1;i++) {
p = p.link;
}
x.link = p.link;
p.link = x;
}
}
listSize++;
}
Ex. insertNode(15, 0)
5 b
head
10 c 20 /
a b
15 /
x
c
d
public static void insertNode(String item, int location) {
Node x = new Node();
Node p = new Node();
x.data = item;
x.link = null;
if(head==null) {
head=x;
} else {
p = head;
if (location==0){
x.link = head;
head = x;
} else {
for (int i=1;i<=location -1;i++) {
p = p.link;
}
x.link = p.link;
p.link = x;
}
}
listSize++;
}
Ex. insertNode(15, 0)
5 b
head
10 c 20 /
a b
15 /
x
c
p
d
public static void insertNode(String item, int location) {
Node x = new Node();
Node p = new Node();
x.data = item;
x.link = null;
if(head==null) {
head=x;
} else {
p = head;
if (location==0){
x.link = head;
head = x;
} else {
for (int i=1;i<=location -1;i++) {
p = p.link;
}
x.link = p.link;
p.link = x;
}
}
listSize++;
}
Ex. insertNode(15, 0)
5 b
head
10 c 20 /
a b
15 a
x
c
p
d
public static void insertNode(String item, int location) {
Node x = new Node();
Node p = new Node();
x.data = item;
x.link = null;
if(head==null) {
head=x;
} else {
p = head;
if (location==0){
x.link = head;
head = x;
} else {
for (int i=1;i<=location -1;i++) {
p = p.link;
}
x.link = p.link;
p.link = x;
}
}
listSize++;
}
Ex. insertNode(15, 0)
5 b
head
10 c 20 /
a b
15 a
x
c
p
d
public static void insertNode(String item, int location) {
Node x = new Node();
Node p = new Node();
x.data = item;
x.link = null;
if(head==null) {
head=x;
} else {
p = head;
if (location==0){
x.link = head;
head = x;
} else {
for (int i=1;i<=location -1;i++) {
p = p.link;
}
x.link = p.link;
p.link = x;
}
}
listSize++;
}
Ex. insertNode(15, 2)
5 b
head
10 c 20 /
a b
15 /
x
c
p
d
public static void insertNode(String item, int location) {
Node x = new Node();
Node p = new Node();
x.data = item;
x.link = null;
if(head==null) {
head=x;
} else {
p = head;
if (location==0){
x.link = head;
head = x;
} else {
for (int i=1;i<=location -1;i++) {
p = p.link;
}
x.link = p.link;
p.link = x;
}
}
listSize++;
}
Ex. insertNode(15, 2)
5 b
head
10 c 20 /
a b
15 /
x
c
p
i=1
d
public static void insertNode(String item, int location) {
Node x = new Node();
Node p = new Node();
x.data = item;
x.link = null;
if(head==null) {
head=x;
} else {
p = head;
if (location==0){
x.link = head;
head = x;
} else {
for (int i=1;i<=location -1;i++) {
p = p.link;
}
x.link = p.link;
p.link = x;
}
}
listSize++;
}
Ex. insertNode(15, 2)
5 b
head
10 c 20 /
a b
15 /
x
c
p
i=2
d
public static void insertNode(String item, int location) {
Node x = new Node();
Node p = new Node();
x.data = item;
x.link = null;
if(head==null) {
head=x;
} else {
p = head;
if (location==0){
x.link = head;
head = x;
} else {
for (int i=1;i<=location -1;i++) {
p = p.link;
}
x.link = p.link;
p.link = x;
}
}
listSize++;
}
Ex. insertNode(15, 2)
5 b
head
10 c 20 /
a b
15 c
x
c
p
i=2
d
public static void insertNode(String item, int location) {
Node x = new Node();
Node p = new Node();
x.data = item;
x.link = null;
if(head==null) {
head=x;
} else {
p = head;
if (location==0){
x.link = head;
head = x;
} else {
for (int i=1;i<=location -1;i++) {
p = p.link;
}
x.link = p.link;
p.link = x;
}
}
listSize++;
}
Ex. insertNode(15, 2)
5 b
head
10 d 20 /
a b
15 c
x
c
p
i=2
d
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(1)
5 b
head
10 d 20 /
a b
15 c
c d
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(1)
5 b
head
10 d 20 /
a b
15 c
c d
x
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(1)
5 b
head
10 d 20 /
a b
15 c
c d
x
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(1)
5 b
head
10 d 20 /
a b
15 c
c d
x
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(3)
5 b
head
10 d 20 /
a b
15 c
c d
p
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(3)
5 b
head
10 d 20 /
a b
15 c
c d
p
i=1
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(3)
5 b
head
10 d 20 /
a b
15 c
c d
p
i=2
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(3)
5 b
head
10 d 20 /
a b
15 c
c d
p
i=2
x
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(3)
5 b
head
10 c 20 /
a b
15 c
c d
p
i=2
x
public static void deleteNode(int nodelocation) {
Node x = new Node();
Node p = new Node();
if(nodelocation==1) {
x = head;
head = x.link;
x = null;
} else {
p = head;
for (int i=1;i<nodelocation -1; i++) {
p = p.link;
}
x = p.link;
p.link = x.link;
x = null;
}
listSize--;
}
Ex. deleteNode(3)
5 b
head
10 c 20 /
a b
15 c
c d
p
i=2
x

Potrebbero piacerti anche