eolymp
bolt
Try our new interface for solving problems
Problems

Class LinkedList

Class LinkedList

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

  • Empty - Checks if list is empty. Return true or false.
  • addFirst - Add element to the start of a list.
  • addLast - Add element to the end of a list.
  • removeFirst - Remove element from the start of a list.
  • removeLast - Remove element from the end of a list.
  • size - Return the number of elements in a list.
  • Print - Print in one line the elements of a list.

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 is removed successfully
  public boolean removeLast(); // delete last element of the list, return true is 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 is removed successfully
  bool removeLast(); // delete last element of the list, return true is removed successfully
  int size(); // return the size of the list
  void Print(); // print values of the list in one line
};

prb10045.gif

Time limit 1 second
Memory limit 128 MiB
Author Mykhailo Medvediev