123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Node {
- int data;
- struct Node *next;
- } Node;
- void insert(Node **head, int data) {
- Node *newNode = (Node *)malloc(sizeof(Node));
- newNode->data = data;
- newNode->next = NULL;
- if (*head == NULL) {
- *head = newNode;
- } else {
- Node *temp = *head;
- while (temp->next != NULL) {
- temp = temp->next;
- }
- temp->next = newNode;
- }
- }
- void display(Node *head) {
- Node *temp = head;
- while (temp != NULL) {
- printf("%d ", temp->data);
- temp = temp->next;
- }
- printf("\n");
- }
- int main() {
- Node *head = NULL;
- insert(&head, 1);
- insert(&head, 2);
- insert(&head, 3);
- display(head);
- return 0;
- }
|