0% found this document useful (0 votes)
12 views1 page

7

The document contains a C program that implements a singly linked list. It defines a node structure and provides functions to insert a new node at the beginning of the list and to print the list. The main function demonstrates the insertion of three nodes and prints the resulting linked list.

Uploaded by

Tech- 420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

7

The document contains a C program that implements a singly linked list. It defines a node structure and provides functions to insert a new node at the beginning of the list and to print the list. The main function demonstrates the insertion of three nodes and prints the resulting linked list.

Uploaded by

Tech- 420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#include <stdlib.h>

// Define the node structure


struct Node {
int data;
struct Node* next;
};

// Function to insert a new node at the beginning


void insertAtBeginning(struct Node** head_ref, int new_data) {
// Allocate memory for new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

// Put in the data


new_node->data = new_data;

// Make next of new node as head


new_node->next = (*head_ref);

// Move the head to point to the new node


(*head_ref) = new_node;
}

// Function to print the linked list


void printList(struct Node* node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;

insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);

printf("Linked list: ");


printList(head);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy