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

Linked3 C

This C program defines functions to create, delete the middle node of, and display a singly linked list. It takes user input for the number of nodes to create and the position of the middle node to delete. It allocates memory for each new node, links them together, deletes the requested node by adjusting the next pointers, and displays the final list.

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)
13 views

Linked3 C

This C program defines functions to create, delete the middle node of, and display a singly linked list. It takes user input for the number of nodes to create and the position of the middle node to delete. It allocates memory for each new node, links them together, deletes the requested node by adjusting the next pointers, and displays the final list.

Uploaded by

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

1 /**

2 * C program to delete middle node of Singly Linked List


3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8
9 /* Structure of a node */
10 struct node {
11 int data; // Data
12 struct node *next; // Address
13 } *head;
14
15
16 /* Functions used in program */
17 void createList(int n);
18 void deleteMiddleNode(int position);
19 void displayList();
20
21
22 void main()
23 {
24 int n, position;
25
26 /*
27 * Create a singly linked list of n nodes
28 */
29 printf("Enter the total number of nodes: ");
30 scanf("%d", &n);
31 createList(n);
32
33 printf("\nData in the list \n");
34 displayList();
35
36 printf("\nEnter the node position you want to delete: ");
37 scanf("%d", &position);
38
39 /* Delete middle node from list */
40 deleteMiddleNode(position);
41
42 printf("\nData in the list \n");
43 displayList();
44
45 getch();
46 }
47
48
49 /*
50 * Create a list of n nodes
51 */
52 void createList(int n)
53 {
54 struct node *newNode, *temp;
55 int data, i;
56
57 head = (struct node *)malloc(sizeof(struct node));
58
59 /*
60 * If unable to allocate memory for head node
61 */
62 if(head == NULL)
63 {
64 printf("Unable to allocate memory.");
65 }
66 else
67 {
68 /*
69 * Read data of node from the user
70 */
71 printf("Enter the data of node 1: ");
72 scanf("%d", &data);
73
74 head->data = data; // Link the data field with data
75 head->next = NULL; // Link the address field to NULL
76
77 temp = head;
78
79 /*
80 * Create n nodes and adds to linked list
81 */
82 for(i=2; i<=n; i++)
83 {
84 newNode = (struct node *)malloc(sizeof(struct node));
85
86 /* If memory is not allocated for newNode */
87 if(newNode == NULL)
88 {
89 printf("Unable to allocate memory.");
90 break;
91 }
92 else
93 {
94 printf("Enter the data of node %d: ", i);
95 scanf("%d", &data);
96
97 newNode->data = data; // Link the data field of newNode with data
98 newNode->next = NULL; // Link the address field of newNode with NULL
99
100 temp->next = newNode; // Link previous node i.e. temp to the newNode
101 temp = temp->next;
102 }
103 }
104
105 printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
106 }
107 }
108
109
110
111 /*
112 * Delete middle node of the linked list
113 */
114 void deleteMiddleNode(int position)
115 {
116 int i;
117 struct node *toDelete, *prevNode;
118
119 if(head == NULL)
120 {
121 printf("List is already empty.");
122 }
123 else
124 {
125 toDelete = head;
126 prevNode = head;
127
128 for(i=2; i<=position; i++)
129 {
130 prevNode = toDelete;
131 toDelete = toDelete->next;
132
133 if(toDelete == NULL)
134 break;
135 }
136
137 if(toDelete != NULL)
138 {
139 if(toDelete == head)
140 head = head->next;
141
142 prevNode->next = toDelete->next;
143 toDelete->next = NULL;
144
145 /* Delete nth node */
146 free(toDelete);
147
148 printf("SUCCESSFULLY DELETED NODE FROM MIDDLE OF LIST\n");
149 }
150 else
151 {
152 printf("Invalid position unable to delete.");
153 }
154 }
155 }
156
157
158 /*
159 * Display entire list
160 */
161 void displayList()
162 {
163 struct node *temp;
164
165 /*
166 * If the list is empty i.e. head = NULL
167 */
168 if(head == NULL)
169 {
170 printf("List is empty.");
171 }
172 else
173 {
174 temp = head;
175 while(temp != NULL)
176 {
177 printf("Data = %d\n", temp->data); // Print the data of current node
178 temp = temp->next; // Move to next node
179 }
180 }
181 }
182
183

You might also like