Class LinkedList#10045

Class LinkedList

1 second
128 megabytes
Very easy

Implement a Single Linked List data structure that supports next operations:

Write the code according to the next interface:

// Java
class ListNode
{
  int data; // node value
  ListNode next; // reference to the next node
  public ListNode(int data); // constructor
}

class LinkedList
{
  ListNode head, tail; // reference to the head and to the tail of the list
  public LinkedList(); // constructor
  public boolean Empty(); // check if list is empty
  public void addFirst(int val); // add val to the head of the list
  public void addLast(int val); // add val to the tail of the list
  public boolean removeFirst(); // delete first element of the list, return true if removed successfully
  public boolean removeLast(); // delete last element of the list, return true if removed successfully
  public int size(); // return the size of the list
  public void Print(); // print values of the list in one line
}
// C++
class ListNode
{
public:
  int data; // node value
  ListNode *next; // pointer to the next node 
  ListNode(int data); // constructor
};

class LinkedList
{
public:
  ListNode *head, *tail;  // pointers to the head and to the tail of the list
  LinkedList(); // constructor
  bool Empty(); // check if list is empty
  void addFirst(int val); // add val to the head of the list
  void addLast(int val); // add val to the tail of the list
  bool removeFirst(); // delete first element of the list, return true if removed successfully
  bool removeLast(); // delete last element of the list, return true if removed successfully
  int size(); // return the size of the list
  void Print(); // print values of the list in one line
};
prb10045.gif