In: Computer Science
public
static
void
main(String[]
args)
{
/* Start with the empty list. */
LinkedList
list =
newLinkedList();
//
// ******INSERTION******
//
// Insert the values
deleteFront(list);
deleteBack(list);
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);
// Basic Operations on the
LinkedList
printList(list);
insertFront(list,0);
printList(list);
insertBack(list,999);
printList(list);
deleteFront(list);
printList(list);
deleteBack(list);
printList(list);
}
// end of main
import java.util.*;
public class Main
{
//method to delete front element
public static void deleteFront(LinkedList list)
{
list.removeFirst();
}
//method to insert front element
public static void insertFront(LinkedList list, int num)
{
list.addFirst(num);
}
//method to delete back element
public static void deleteBack(LinkedList list)
{
list.removeLast();
}
//method to insert back element
public static void insertBack(LinkedList list, int num)
{
list.addLast(num);
}
//method to insert element
public static LinkedList insert(LinkedList list, int n)
{
list.add(n);
return list;
}
//method to print the linked list
public static void printList(LinkedList list)
{
Iterator<Integer> iterator=list.iterator();
System.out.println("\n");
while(iterator.hasNext())
{
System.out.print(iterator.next() + " ");
}
}
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();
//
// ******INSERTION******
//
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
// Insert the values
deleteFront(list);
deleteBack(list);
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);
// Basic Operations on the LinkedList
printList(list);
insertFront(list,0);
printList(list);
insertBack(list,999);
printList(list);
deleteFront(list);
printList(list);
deleteBack(list);
printList(list);
} // end of main
}
OUTPUT: