Data Structures Practical 20
Question 1
Write a C program to traverse linked list using recursion
traverse.c
C
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *start = NULL;
void traverseRecursive(struct Node *ptr);
void insert(int data);
void main()
{
int choice, data, targetData, val;
char ch;
do
{
printf("\n--- SINGLY LINKED LIST OPERATIONS ---\n");
printf("1. Insert\n");
printf("2. Display List (Recursive Traversal)\n");
printf("3. Exit\n");
printf("-------------------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insert(data);
break;
case 2:
displayList();
break;
case 3:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
printf("\nDo you want to continue(y/n)?");
ch = getch();
} while (ch == 'y' || ch == 'Y');
}
void displayList()
{
if (start == NULL)
{
printf("The list is empty.\n");
return;
}
printf("Linked List (via Recursion): ");
traverseRecursive(start); /* Start recursive call chain passing head */
printf("NULL\n");
}
void traverseRecursive(struct Node *ptr)
{
if (ptr == NULL)
{
return;
}
printf("%d -> ", ptr->data);
traverseRecursive(ptr->next);
}
void insert(int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL)
{
printf("Memory allocation failed!\n");
return;
}
newNode->data = data;
newNode->next = start;
start = newNode;
printf("%d inserted at the beginning.\n", data);
}
Output
Question 2
Write a program in C to calculate length of linked list using recursion.
listlength.c
C
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *start = NULL;
void displayList();
int getLengthRecursive(struct Node *ptr);
void insert(int data);
void main()
{
int choice, length;
char ch;
do
{
printf("\n--- LINKED LIST LENGTH VIA RECURSION ---\n");
printf("1. Insert\n");
printf("2. Display List\n");
printf("3. Calculate Length (Recursive)\n");
printf("4. Exit\n");
printf("----------------------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter node data: ");
scanf("%d", &num);
insert(num);
break;
case 2:
displayList();
break;
case 3:
length = getLengthRecursive(start);
printf("\nThe total number of nodes (Length) is: %d\n", length);
break;
case 4:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
printf("\nDo you want to continue(y/n)?");
ch = getch();
} while (ch == 'y' || ch == 'Y');
}
int getLengthRecursive(struct Node *ptr)
{
if (ptr == NULL)
{
return 0;
}
return (1 + getLengthRecursive(ptr->next));
}
void displayList()
{
struct Node *ptr = start;
if (start == NULL)
{
printf("The list is empty.\n");
return;
}
printf("Linked List: ");
while (ptr != NULL)
{
printf("%d -> ", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}
void insert(int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *ptr = start;
if (newNode == NULL)
{
printf("Memory allocation failed!\n");
return;
}
newNode->data = data;
newNode->next = NULL;
if (start == NULL)
{
start = newNode;
return;
}
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newNode;
}
Output
