{}
run-icon
main.c
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { int data; struct node* next; }; struct node* head = NULL; void insertAtEnd(int x) { struct node *newnode, *temp; newnode = (struct node*)malloc(sizeof(struct node)); if (newnode == NULL) { printf("Memory allocation failed\n"); return; } newnode->data = x; newnode->next = NULL; if (head == NULL) { head = newnode; } else { temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = newnode; } } void display() { struct node* temp = head; if (temp == NULL) { printf("List is empty\n"); return; } printf("Linked List: "); while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { insertAtEnd(10); insertAtEnd(20); insertAtEnd(30); display(); return 0; }
Output