Example-
Linked list 2->3->1->9->6
Answer is 9
program-
#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct node{
struct node *link;
int data;
};
struct node *first=NULL;
void get(struct node *ptr)
{
struct node *cpt;
while(ptr->link!=NULL)
{
cpt=ptr;
ptr=ptr->link;
}
printf("Second last element is %d",cpt->data);
}
void main()
{
struct node *cpt,*ptr;
int n,pos;
char ch;
clrscr();
//input
first=(struct node*)malloc(sizeof(struct node));
printf("Enter first element in Linklist\n");
scanf("%d",&first->data);
ptr=first;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("Enter another data\n");
scanf("%d",&cpt->data);
ptr->link=cpt;
ptr=ptr->link;
printf("Continue(y/n) ?");
ch=getch();
}while(ch=='y');
ptr->link=NULL;
get(first);
getch();
}
Enter first element in Linked list
2
Enter another data
4
Continue(y/n)?
Enter another data
1
Continue(y/n) ?
Enter another data
8
Continue(y/n) ?
Enter another data
0
Continue(y/n) ?
Second last element is 8