fork download
  1. /*
  2.   Get Nth element from the end in a linked list of integers
  3.   Number of elements in the list will always be greater than N.
  4.   Node is defined as
  5.   struct Node
  6.   {
  7.   int data;
  8.   struct Node *next;
  9.   }
  10. */
  11. int GetNode(Node *head, int position)
  12. {
  13. Node *p = head;
  14. Node *q = head;
  15. for(int i = 0; i < position; i++){
  16. if(p == NULL) return 0;
  17. p = p->next;
  18. }
  19. if(p == NULL) return 0;
  20. while(p->next != NULL){
  21. p = p->next;
  22. q = q->next;
  23. }
  24. return q->data;
  25. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:11:13: error: 'Node' was not declared in this scope
 int GetNode(Node *head, int position)
             ^
prog.cpp:11:19: error: 'head' was not declared in this scope
 int GetNode(Node *head, int position)
                   ^
prog.cpp:11:25: error: expected primary-expression before 'int'
 int GetNode(Node *head, int position)
                         ^
prog.cpp:11:37: error: expression list treated as compound expression in initializer [-fpermissive]
 int GetNode(Node *head, int position)
                                     ^
prog.cpp:12:1: error: expected ',' or ';' before '{' token
 {
 ^
stdout
Standard output is empty