Our Feeds

Tuesday, 6 October 2020

AJITH KP

Singly Linked List (SLL) Reverse Order Traversal

Hi There, 


Please find the C (ANSI C11) code for reverse order traversal of Singly Linked List (SLL).

SOURCE CODE
 
#include 
#include 
/*
 * C11 Version of ANSI C
 * AJITH K P (C) _TERMINAL_CODERS_ http://terminalcoders.blogspot.com
 */
struct sll {
  int data;
  struct sll *link;
};
void print_reverse(struct sll *node){
  if(node == NULL){
    return;
  }
  else{
    print_reverse(node->link);
  }
  printf_s("%d ", node->data);
}
int main() {
  struct sll *root = malloc(sizeof(struct sll));
  int n;
  printf_s("Enter number of nodes:");
  scanf_s("%d", &n);
  printf_s("Enter values of %d nodes:", n);
  scanf_s("%d", &root->data);
  root->link = NULL;
  struct sll *tmp = root;
  for(int i=0;idata);
    node->link = NULL;
    tmp->link = node;
    tmp = node;
  }
  printf_s("SLL Traversal in Reverse Order: ");
  tmp = root;
  print_reverse(tmp);
  return 0;
}