SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Linked List Implementation(print nth node from the last of list)
Linked List Implementation(print nth node from the last of list)
#include
#include
struct node { int data; struct node *next; }; struct node* newnode(int data) { struct node *new=(struct node*)malloc(sizeof(struct node)); new->data=data; new->next=NULL; return new; } void insertastail(struct node **head,int data) { struct node *temp; struct node *current=*head; if(current==NULL) { *head=newnode(data); } else { temp=newnode(data); while(current->next!=NULL) current=current->next; current->next=temp; } } void printlist(struct node *head) { struct node *current=head; while(current!=NULL) { printf("%d ",current->data); current=current->next; } } int findsize(struct node *head) { if(head==NULL) return 0; return 1 + findsize(head->next); } int nthnodefromend(struct node *head,int n) { int size=findsize(head); int i=1; while(i
next; i++; } return head->next->data; } int main(void) { struct node *head=NULL; int data; int i,n; scanf("%d",&data); for(i=5;i<=data;i++) insertastail(&head,i); printlist(head); scanf("%d",&n); printf("\n%d from last is %d\n",n,nthnodefromend(head,n)); return 0; }
PREVIOUS
NEXT
HOME