0% found this document useful (0 votes)
14 views

Linked1 C

This C program creates and traverses a linked list. It defines a node struct with data and next pointer fields, and global head pointer. It contains functions to create a linked list by taking user input for n nodes, and to traverse the list by printing the data of each node. The main function gets the number of nodes from user, calls the createList function to build the linked list, and then calls traverseList to print out the data of all nodes.

Uploaded by

Mohd Tausif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Linked1 C

This C program creates and traverses a linked list. It defines a node struct with data and next pointer fields, and global head pointer. It contains functions to create a linked list by taking user input for n nodes, and to traverse the list by printing the data of each node. The main function gets the number of nodes from user, calls the createList function to build the linked list, and then calls traverseList to print out the data of all nodes.

Uploaded by

Mohd Tausif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 /**

2 * C program to create and traverse a Linked List


3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 /* Structure of a node */
9 struct node {
10 int data; // Data
11 struct node *next; // Address
12 }*head;
13
14
15 /*
16 * Functions to create and display list
17 */
18 void createList(int n);
19 void traverseList();
20
21
22 void main()
23 {
24 int n;
25
26 printf("Enter the total number of nodes: ");
27 scanf("%d", &n);
28
29 createList(n);
30
31 printf("\nData in the list \n");
32 traverseList();
33
34 getch();
35 }
36
37 /*
38 * Create a list of n nodes
39 */
40 void createList(int n)
41 {
42 struct node *newNode, *temp;
43 int data, i;
44
45 head = (struct node *)malloc(sizeof(struct node));
46
47 // Terminate if memory not allocated
48 if(head == NULL)
49 {
50 printf("Unable to allocate memory.");
51 exit(0);
52 }
53
54
55 // Input data of node from the user
56 printf("Enter the data of node 1: ");
57 scanf("%d", &data);
58
59 head->data = data; // Link data field with data
60 head->next = NULL; // Link address field to NULL
61
62
63 // Create n - 1 nodes and add to list
64 temp = head;
65 for(i=2; i<=n; i++)
66 {
67 newNode = (struct node *)malloc(sizeof(struct node));
68
69 /* If memory is not allocated for newNode */
70 if(newNode == NULL)
71 {
72 printf("Unable to allocate memory.");
73 break;
74 }
75
76 printf("Enter the data of node %d: ", i);
77 scanf("%d", &data);
78
79 newNode->data = data; // Link data field of newNode
80 newNode->next = NULL; // Make sure new node points to NULL
81
82 temp->next = newNode; // Link previous node with newNode
83 temp = temp->next; // Make current node as previous node
84 }
85 }
86
87
88 /*
89 * Display entire list
90 */
91 void traverseList()
92 {
93 struct node *temp;
94
95 // Return if list is empty
96 if(head == NULL)
97 {
98 printf("List is empty.");
99 return;
100 }
101
102 temp = head;
103 while(temp != NULL)
104 {
105 printf("Data = %d\n", temp->data); // Print data of current node
106 temp = temp->next; // Move to next node
107 }
108 }

You might also like