Esempio n. 1
0
// Look for a node having the specified value
ListNode * LinkedList::find(const int val)
{
  ListNode *node = head.getNext();

  while (node != 0 && node->getVal() != val) {
    node = node->getNext();
  }
  if (node->getVal() == val) {
    return node;
  }
  return 0;
}
Esempio n. 2
0
// Remove the first node having the specified value (if present)
int LinkedList::remove(const int val)
{
  ListNode *node = head.getNext();
  ListNode *prev = &head;

  while (node != 0 && node->getVal() != val) {
    prev = node;
    node = node->getNext();
  }
  if (node->getVal() == val) {
    prev->setNext(node->getNext());
    delete node;
    return val;
  }
  return -1;
}
Esempio n. 3
0
// Show the contents of the list
void LinkedList::print(void)
{
  ListNode *node = head.getNext();

  while (node != 0) {
    cout << setw(6) << node->getVal();
    node = node->getNext();
  }
  cout << endl;
}
Esempio n. 4
0
// Insert in sorted order (ascending)
ListNode * LinkedList::insert(const int val)
{
  ListNode *node = head.getNext();
  ListNode *prev = &head;

  while (node != 0 && node->getVal() < val) {
    prev = node;
    node = node->getNext();
  }
  return prev->setNext(new ListNode(val, prev->getNext()));
}