Practical No.
20: Write a 'C' program to
create a Singly Linked List and traverse in
reverse order using recursion.
Name :- GANESH BALU PATANGE
Branch :- CO3K
#include <stdio.h>
struct Node* newNode =
#include <stdlib.h>
createNode(value);
if (*headRef == NULL) {
struct Node {
*headRef = newNode;
int data;
return;
struct Node* next;
}
};
struct Node* temp = *headRef;
while (temp->next != NULL)
temp = temp->next;
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)
temp->next = newNode;
malloc(sizeof(struct Node));
}
newNode->data = value;
void printReverse(struct Node* head) {
newNode->next = NULL;
if (head == NULL)
return newNode;
return;
}
printReverse(head->next);
void append(struct Node** headRef, int
value) { printf("%d ", head->data);
} displayList(head);
void displayList(struct Node* head) { printf("\nLinked List in reverse order
(using recursion):\n");
struct Node* temp = head;
printReverse(head);
while (temp != NULL) {
printf("\n");
printf("%d -> ", temp->data);
temp = temp->next;
return 0;
}
}
printf("NULL\n");
int main() {
struct Node* head = NULL;
int n, value;
printf("Enter number of nodes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter data for node %d: ", i + 1);
scanf("%d", &value);
append(&head,value);
printf("\nLinked List in normal order:\n");