-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLLmid.cpp
More file actions
55 lines (49 loc) · 799 Bytes
/
LLmid.cpp
File metadata and controls
55 lines (49 loc) · 799 Bytes
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
#include<bits/stdc++.h>
using namespace std;
struct Lst
{
int data;
struct Lst* next;
};
void printMiddle(struct Lst *head)
{
struct Lst *slowp,*fastp;
slowp= head;
fastp= head;
if(head!=NULL)
{
while(fastp!=NULL && fastp->next!=NULL)
{
fastp=fastp->next->next;
slowp=slowp->next;
}
cout << slowp->data << endl;
}
}
void printList(struct Lst *ptr)
{
while (ptr != NULL)
{
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}
void push(struct Lst **head_ref,int ndata)
{
struct Lst *node=(struct Lst*) malloc(sizeof(struct Lst));
node->data=ndata;
node->next= (*head_ref);
(*head_ref)=node;
}
int main()
{
struct Lst *head=NULL;
for (int i=5; i>0; i--)
{
push(&head, i);
printList(head);
printMiddle(head);
}
return 0;
}