Skip to content

Commit 31ef349

Browse files
Merge pull request #16 from vedant1003k/patch-2
Create middleNode.c
2 parents d7260d1 + 14e83b5 commit 31ef349

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Diff for: Linked List/middleNode.c

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
struct node
4+
{
5+
int data;
6+
struct node *next;
7+
} * temp, *head;
8+
9+
void createDCLL(int); // creating a list
10+
void display(); // displaying the lsit
11+
struct node *middleNode(struct node *); // finding middle part and resturning middle node of linked list
12+
int main()
13+
{
14+
int n;
15+
printf("enter the no of node");
16+
scanf("%d", &n);
17+
createDCLL(n);
18+
head = middleNode(head); // updating head of the linked list ( pointing it to middle node of linked list )
19+
display();
20+
}
21+
void createDCLL(int n)
22+
{
23+
int i, num;
24+
struct node *newnode;
25+
if (n >= 1)
26+
{
27+
head = (struct node *)malloc(sizeof(struct node));
28+
if (head != NULL)
29+
{
30+
printf("enter the data");
31+
scanf("%d", &num);
32+
head->data = num;
33+
head->next = NULL;
34+
temp = head;
35+
}
36+
for (i = 2; i <= n; i++)
37+
{
38+
newnode = (struct node *)malloc(sizeof(struct node));
39+
if (newnode != NULL)
40+
{
41+
printf("enter the data");
42+
scanf("%d", &num);
43+
newnode->data = num;
44+
temp->next = newnode;
45+
newnode->next = NULL;
46+
temp = newnode;
47+
}
48+
}
49+
}
50+
}
51+
void display()
52+
{
53+
temp = head;
54+
while (temp != 0)
55+
{
56+
printf("%d\t", temp->data);
57+
temp = temp->next;
58+
}
59+
}
60+
struct node *middleNode(struct node *head)
61+
{
62+
int c = 0, i = 0;
63+
struct node *current;
64+
current = head;
65+
while (current != NULL)
66+
{
67+
c++; // c = length of linked list
68+
current = current->next;
69+
}
70+
c = c / 2;
71+
current = head;
72+
while (i < c)
73+
{
74+
current = current->next;
75+
i++;
76+
}
77+
return current;
78+
}

0 commit comments

Comments
 (0)