Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Getting Elements of a LinkedList

Java Example – Getting Elements of a LinkedList

Java Examples Java Examples

The following example demonstrates using the top() and pop() methods to get the elements of the linked list:

Main.java File

import

java

.

util

.*;

public

class

Main

{

private

LinkedList

list

=

new

LinkedList

(

)

;

public

void

push

(

Object

v

)

{

list

.

addFirst

(

v

)

;

}

public

Object

top

(

)

{

return

list

.

getFirst

(

)

;

}

public

Object

pop

(

)

{

return

list

.

removeFirst

(

)

;

}

public

static

void

main

(

String

[

]

args

)

{

Main

stack

=

new

Main

(

)

;

for

(

int

i

=

30

;

i

<

40

;

i

++

)

stack

.

push

(

new

Integer

(

i

)

)

;

System

.

out

.

println

(

stack

.

top

(

)

)

;

System

.

out

.

println

(

stack

.

pop

(

)

)

;

System

.

out

.

println

(

stack

.

pop

(

)

)

;

System

.

out

.

println

(

stack

.

pop

(

)

)

;

}

}

The output of the above code is:


39
39
38
37

Java Examples Java Examples