Practice questions for Linked List and Recursion |  ; A linked list node ; Prints a linked list in reverse manner ; prints alternate nodes of a Linked List , first from head to end , and then from end to head . ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver code ; Start with the empty list ; Using push ( ) to construct below list 1 -> 2 -> 3 -> 4 -> 5
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * next ; } ; void fun1 ( Node * head ) { if ( head == NULL ) return ; fun1 ( head -> next ) ; cout << head -> data << " ▁ " ; } void fun2 ( Node * start ) { if ( start == NULL ) return ; cout << start -> data << " ▁ " ; if ( start -> next != NULL ) fun2 ( start -> next -> next ) ; cout << start -> data << " ▁ " ; } void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ( ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int main ( ) { Node * head = NULL ; push ( & head , 5 ) ; push ( & head , 4 ) ; push ( & head , 3 ) ; push ( & head , 2 ) ; push ( & head , 1 ) ; cout << " Output ▁ of ▁ fun1 ( ) ▁ for ▁ list ▁ 1 - > 2 - > 3 - > 4 - > 5 ▁ STRNEWLINE " ; fun1 ( head ) ; cout << " Output of fun2 ( ) for list 1 -> 2 -> 3 -> 4 -> 5 " ; fun2 ( head ) ; return 0 ; }