-
Notifications
You must be signed in to change notification settings - Fork 256
/
28 Middle of Linkedlist.c
86 lines (78 loc) · 1.8 KB
/
28 Middle of Linkedlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
#include<stdlib.h>
struct Node
{
struct Node* prev;
int data;
struct Node* next;
}*first=NULL;
void create(int A[],int n)
{
struct Node* last, * t;
int i;
first = (struct Node*)malloc(sizeof(struct Node));
first->data = A[0]; // Take data from 1st loc of array passed ;
first->prev = first->next = NULL; // set both pointer as NULL;
last=first; // move last to first ;
for (i = 1; i < n; i++)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = A[i];
// As i have to Always insert a new node after the last node so ;
//t - next = last->next is same as t->next = head;
t->next = last->next;// this means the newly created node is attached at the end of the list(i.e appended to the last node); // last node will always contain NULL so it is same thing.
t->prev = last;
last->next = t;
last = t;
}
}
void Display(struct Node* p)
{
while (p) //means repeat the loop until p is not equals to null. I.e until we reach the last node, we want to traverse the list
{
printf("->%d ", p->data);
p = p->next;
}
printf("\n");
}
int Length(struct Node* p)
{
int len = 0;
while (p)
{
len++;
p = p->next;
}
return len;
}
struct Node *Middleelement(struct Node* p)
{
struct Node* q;
q = p;
while (q)
{
q = q->next;
if (q)
q= q->next;
if (q)
p = p->next;
}
return p;
}
int main()
{
int no, i, A[10],pos;
struct Node* t;
printf("enter the size of Array\n ");
scanf_s("%d", &no);
printf("enter the elements of array\n");
for (i = 0; i <no; i++)
scanf_s("%d", &A[i]);
create(A, no);
printf("Given DLL is\n ");
Display(first);
t=Middleelement(first);
printf("\n middle elements is %d\n",t->data);
printf("length of linked list is %d", Length(first));
return 0;
}