0% found this document useful (0 votes)
2 views2 pages

SingleLinkedList

The document contains a C program that implements a singly linked list with functions to insert nodes at the end and to traverse and print the list. It defines a Node structure and uses a global head pointer to manage the list. The main function demonstrates inserting several integers and printing the resulting list.

Uploaded by

patelsaab2323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

SingleLinkedList

The document contains a C program that implements a singly linked list with functions to insert nodes at the end and to traverse and print the list. It defines a Node structure and uses a global head pointer to manage the list. The main function demonstrates inserting several integers and printing the resulting list.

Uploaded by

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

#include <stdio.

h>

#include <stdlib.h>

// Singly Linked List structure

struct Node {

int data;

struct Node* next;

};

// Global head pointer

struct Node* head = NULL;

// Function to insert a node at the end of the list

void insert(int value) {

// Create a new node

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

// Check if the list is empty

if (head == NULL) {

head = newNode;

} else {

// Traverse to the end of the list

struct Node* current = head;

while (current->next != NULL) {

current = current->next;

// Link the new node to the last node


current->next = newNode;

// Function to traverse and print the list

void traverse() {

struct Node* current = head;

while (current != NULL) {

printf("%d ", current->data);

current = current->next;

int main() {

// Insert elements into the list

insert(10);

insert(20);

insert(30);

insert(40);

// Traverse and print the list

printf("List: ");

traverse();

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