Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- int age;
- struct node *next;
- } Node;
- Node *head;
- void insert_at_nth(int ag,int pos)
- {
- Node *temp = head;
- Node *new_node = (Node*) malloc(sizeof(Node));
- new_node->age = ag;
- new_node->next = NULL;
- if(pos == 1)
- {
- new_node->next = head;
- head = new_node;
- return;
- }
- pos-=2;
- while(pos--)
- {
- if(temp == NULL || head == NULL)
- {
- printf("Position is on Mars Baby!! ('!^)\n");
- return;
- }
- temp = temp->next;
- }
- if(temp == NULL || head == NULL)
- {
- printf("Position is on Mars Baby!! ('!^)\n");
- return;
- }
- new_node->next = temp->next;
- temp->next = new_node;
- }
- void print()
- {
- Node *current = NULL;
- current = head;
- while(current!=NULL)
- {
- printf("%d ",current->age);
- current = current->next;
- }
- }
- int main()
- {
- int n,age,position;
- printf("Enter how many nodes you want to create: ");
- scanf("%d",&n);
- for(int i=0;i<n;i++)
- {
- printf("Enter age : ");
- scanf("%d",&age);
- printf("Enter Position : ");
- scanf("%d",&position);
- insert_at_nth(age,position);
- }
- print();
- return 0;
- }
Add Comment
Please, Sign In to add comment