Exemplo n.º 1
0
int main (int argc, char* argv[])
{
  int dataInt[] = {1, 2, 3};

  CSinglyListNode* head = NULL;

  // ------------- InsertInFront ------------
  for (int i = 0; i < 3; i++)
  {
    if (!InsertInFront(&head, dataInt[i]))
      std::cout << "failed to insert : " << dataInt[i] << std::endl;
  }

  std::cout << "Length(head):" << Length(head) << std::endl;
  PrintList(head);
  // ----------------------------------------

  // --InsertInFrontUseCppReferenceToPointer--
  for (int i = 0; i < 3; i++)
  {
    if (!InsertInFrontUseCppReferenceToPointer(head, dataInt[i]))
      std::cout << "failed to insert : " << dataInt[i] << std::endl;
  }

  std::cout << "Length(head):" << Length(head) << std::endl;
  PrintList(head);
  // ----------------------------------------

  std::cout << "bye" << std::endl;

  return 0;
}
Exemplo n.º 2
0
/* Function: to create a new linked list
 * Input:
 *    values[]: array of integers containing values for data fields
 *    len: length of values[] array
 * Return: New linked list where each node data contains
 *         a value from values[] array
 * */
struct node* CreateList(int values[], int len) {
	// check for empty list
	if (len == 0) return NULL;
	struct node* head = NULL;
	int i;
	for(i=len; i>0; i--) {
		head = InsertInFront(head, values[i-1]);
	}
	return head;
}