📜  将第一个元素添加到链表 - 无论代码示例

📅  最后修改于: 2022-03-11 15:00:53.536000             🧑  作者: Mango

代码示例1
void push_front(int newElement) {
  
  //1. allocate node
  Node newNode = new Node();
  
  //2. assign data element
  newNode.data = newElement;
  
  //3. assign null to the next and prev
  //   of the new node
  newNode.next = null; 
  newNode.prev = null;

  //4. Check the list is empty or not,
  //   if empty make the new node as head 
  if(head == null) {
    head = newNode;
  } else {
    
    //5. Adjust the links and make the new
    //   node as head
    head.prev = newNode;
    newNode.next = head;
    head = newNode;
  }    
}