

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
C program to create and display Singly Linked List -hrithikpc
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!


#include <stdio.h> #include <stdlib.h>
/* Structure of a node */
struct node { int data; //Data part struct node next; //Address part }head;
void createList(int n); void traverseList();
int main() {
int n;
printf("Enter the total number of nodes: "); scanf("%d", &n);
createList(n);
printf("\nData in the list \n"); traverseList();
return 0; }
head = (struct node *)malloc(sizeof(struct node));
head->data = data; //Links the data field with data head->next = NULL; //Links the address field to NULL
temp = head;
/* If memory is not allocated for newNode */ if(newNode == NULL) { printf("Unable to allocate memory."); break; } else { printf("Enter the data of node %d: ", i); scanf("%d", &data);
newNode->data = data; //Links the data field of newNode with data newNode->next = NULL; //Links the address field of newNode with NULL
temp->next = newNode; //Links previous node i.e. temp to the newNode temp = temp->next; } } } }
struct node *temp;