-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_2.c
57 lines (53 loc) · 861 Bytes
/
2_2.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
#include<stdio.h>
struct node
{
int data;
struct node *next;
}*head=NULL,*temp;
void main()
{
int n,key;
scanf("%d",&n);
create_nodes(n);
scanf("%d",&key);
n_last_element(key);
}
void create_nodes(int n)
{
int i;
for(i=0;i<n;i++)
{
struct node *new=(struct node*)malloc(sizeof(struct node));
new->next=NULL;
scanf("%d",&new->data);
if(head==NULL)
{
head=new;
temp=head;
}
else
{
temp->next=new;
temp=temp->next;
}
}
}
n_last_element(int key)
{
int i;
struct node *p1=head,*p2=head;
for(i=0;i<key-1;i++)
{
if(!p2)
{
return 0;
}
p2=p2->next;
}
while(p2->next)
{
p2=p2->next;
p1=p1->next;
}
printf("%d",p1->data);
}