Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int a;
- struct node *next;
- }node;
- node *head=NULL, *list=NULL;
- void insert_at_nth()
- {
- int n;
- printf("ENTER THE POSITION:\n");
- scanf("%d", &n);
- node *N=(node*)malloc(sizeof(node));
- printf("ENTER THE VALUE:\n");
- scanf(" %d", &N->a);
- N->next=NULL;
- if(n==1)
- {
- N->next=head;
- head=N;
- return;
- }
- else
- {
- list=head;
- n=n-2;
- while(n!=0 && list->next!=NULL)//while(p--)
- {
- list=list->next;
- n--;
- }
- N->next=list->next;
- list->next=N;
- }
- }
- void display()
- {
- list=head;
- while(list!=NULL)
- {
- printf("Data = %d\n", list->a);
- list=list->next;
- }
- }
- int main()
- {
- int x;
- printf("ENTER THE CASE:\n");
- scanf("%d", &x);
- if(x==0)
- {
- printf("Empty\n");
- return 0;
- }
- node *N=(node*)malloc(sizeof(node));
- printf("ENTER THE VALUE:\n");
- scanf("%d", &N->a);
- head=N;
- N->next=NULL;
- x=x-1;
- while(x--)
- {
- insert_at_nth();
- }
- display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement