0% found this document useful (0 votes)
11 views11 pages

Class Running Notes 14th Nov

The document discusses three Java collection classes: LinkedList<E>, Vector<E>, and Stack<E>. LinkedList<E> is a non-synchronized class that organizes elements in nodes, while Vector<E> is a synchronized legacy class that maintains elements in sequence. Stack<E> is a child class of Vector<E> that follows a Last-In-First-Out (LIFO) structure and includes methods for pushing, popping, and searching elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Class Running Notes 14th Nov

The document discusses three Java collection classes: LinkedList<E>, Vector<E>, and Stack<E>. LinkedList<E> is a non-synchronized class that organizes elements in nodes, while Vector<E> is a synchronized legacy class that maintains elements in sequence. Stack<E> is a child class of Vector<E> that follows a Last-In-First-Out (LIFO) structure and includes methods for pushing, popping, and searching elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dt : 14/11/2022

(b)LinkedList<E>:

=>LinkedList<E> organizes elements in NonSequence and which is also

NonSynchronized class.

=>The elements in LinkedList<E> are available in the form of "nodes".

=>The LinkedList<E> node internally divided into the following partitions:

ii
(i)Previous Node Address

ath
(ii)Data

(iii)Next Node Address

aip
Diagram:
hM
a tes
nk
Ve
ii
ath
aip
hM
tes

Ex : [Link]

package maccess;
import [Link].*;
public class DemoList2 {
a

@SuppressWarnings("removal")
nk

public static void main(String[] args) {


LinkedList<Integer> ob = new LinkedList<Integer>();
for(int i=25;i<=29;i++)
{
Ve

[Link](new Integer(i));
}//end of loop
[Link]([Link]());
}
}

=====================================================================

*imp
(c)Vector<E>:

=>Vector<E> organizes elements in Sequence and which is Synchronized class.

(Thread-Safe class)

=>Vector<E> is known as Legacy class,which means replacement not available for

Vector<E> class.

=>The following are some important methods of Vector<E>:

ii
public synchronized E elementAt(int);

ath
public synchronized E firstElement();

public synchronized E lastElement();

aip
public synchronized void setElementAt(E, int);

public synchronized void removeElementAt(int);


hM
public synchronized void insertElementAt(E, int);

public synchronized void addElement(E);

public synchronized boolean removeElement([Link]);


tes

public synchronized void removeAllElements();


a

Ex : [Link]
nk

package maccess;
import [Link].*;
public class DemoList3 {
Ve

@SuppressWarnings("removal")
public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
for(int i=1;i<=5;i++)
{
[Link](new Integer(i));
}//end of loop
[Link]([Link]());
[Link]("ele at index 2:"+[Link](2));
[Link]("First Element : "+[Link]());
[Link]("Last Element : "+[Link]());
[Link](new Integer(400), 2);
[Link]([Link]());
[Link](2);
[Link]([Link]());
[Link](new Integer(13), 2);
[Link]([Link]());
[Link](new Integer(13));
[Link]([Link]());
[Link]("size:"+[Link]());
[Link]();
[Link]("size:"+[Link]());

ii
}

ath
}

o/p:

aip
[1, 2, 3, 4, 5]

ele at index 2:3


hM
First Element : 1

Last Element : 5

[1, 2, 400, 4, 5]
tes

[1, 2, 4, 5]

[1, 2, 13, 4, 5]
a

[1, 2, 4, 5]
nk

size:4

size:0
Ve

==========================================================================

Note:

=>In realtime Vector<E> will hold multiple pre-initialized Database connections

in Connection Pooling Concept.

==========================================================================
faq:

define Stack<E>?

=>Stack<E> is a ChildClass of Vector<E> and which organizes elements based on

the algorithm First-In-Last-Out or Last-In-First-Out

=>The following are some important methods of Stack<E>:

public E push(E);

ii
public synchronized E pop();

ath
public synchronized E peek();

public boolean empty();

aip
public synchronized int search([Link]);
hM
Ex : [Link]

package maccess;
import [Link].*;
public class DemoStack {
@SuppressWarnings("removal")
tes

public static void main(String[] args) {


Stack<Integer> ob = new Stack<Integer>();
Scanner s = new Scanner([Link]);
try(s;){
a

try {
while(true) {
nk

[Link]("*****Choice*****");
Ve

[Link]("[Link](E)\[Link]()\[Link]()\[Link]()\n5.
exit");
[Link]("Enter the Choice:");
switch([Link]()) {
case 1:
[Link]("Enter the ele:");
[Link](new Integer([Link]()));
[Link]([Link]());
break;
case 2:
if([Link]()) {
[Link]("Stack is
empty...");
}else {
[Link]();
[Link]("Ele deleted
from top of stack");
[Link]([Link]());
}
break;
case 3:

ii
if([Link]()) {
[Link]("Stack is

ath
empty...");
}else {
[Link]("peek
ele:"+[Link]());
[Link]([Link]());

aip
}
break;
case 4:
if([Link]()) {
hM
[Link]("Stack is
empty...");
}else {
[Link]("Enter the ele
to searched:");
tes

Integer ele = new


Integer([Link]());
int pos = [Link](ele);
if(pos>0) {
[Link]("Ele found at
a

position :"+pos);
nk

}else {
[Link]("Ele not
found...");
}
Ve

}
break;
case 5:
[Link]("Stack operation
Stopped...");
[Link](0);
default:
[Link]("Invalid
Choice...");
}//end of switch
}//end of loop
}catch(Exception e) {[Link]();}
}//end of try
}
}

o/P:

*****Choice*****

ii
[Link](E)

ath
[Link]()

[Link]()

[Link]()

[Link]

Enter the Choice:


aip
hM
4

Stack is empty...

*****Choice*****
tes

[Link](E)

[Link]()
a
nk

[Link]()

[Link]()
Ve

[Link]

Enter the Choice:

Enter the ele:

11

[11]
*****Choice*****

[Link](E)

[Link]()

[Link]()

[Link]()

[Link]

ii
Enter the Choice:

ath
1

Enter the ele:

aip
12

[11, 12]
hM
*****Choice*****

[Link](E)

[Link]()
tes

[Link]()

[Link]()
a

[Link]
nk

Enter the Choice:

1
Ve

Enter the ele:

13

[11, 12, 13]

*****Choice*****

[Link](E)
[Link]()

[Link]()

[Link]()

[Link]

Enter the Choice:

ii
Enter the ele:

ath
14

[11, 12, 13, 14]

aip
*****Choice*****

[Link](E)
hM
[Link]()

[Link]()

[Link]()
tes

[Link]

Enter the Choice:


a

1
nk

Enter the ele:

15
Ve

[11, 12, 13, 14, 15]

*****Choice*****

[Link](E)

[Link]()

[Link]()
[Link]()

[Link]

Enter the Choice:

Enter the ele to searched:

11

ii
Ele found at position :5

ath
*****Choice*****

[Link](E)

aip
[Link]()

[Link]()
hM
[Link]()

[Link]

Enter the Choice:


tes

Enter the ele to searched:


a

15
nk

Ele found at position :1

*****Choice*****
Ve

[Link](E)

[Link]()

[Link]()

[Link]()

[Link]
Enter the Choice:

Stack operation Stopped...

====================================================================

Note:

=>search() method on Stack<E> searches the element from top-of-stack to

ii
Bottom-of-Stack and display the position of an element.

ath
================================================================

aip
hM
a tes
nk
Ve

You might also like