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_nth()
- {
- int p;
- printf("Enter position:");
- scanf("%d",&p);
- node *n=(node*)malloc(sizeof(node));
- printf("enter value:");
- scanf("%d",&n->a);
- n->next=NULL;
- if(p == 1)
- {
- n->next=head;
- head=n;
- return;
- }
- list=head;
- p=p-2;
- while(p!=0 && list->next!=NULL)
- {
- list=list->next;
- p--;
- }
- 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,i,b,j;
- printf("Number of nodes:");
- scanf("%d",&i);
- if(i == 0)
- {
- printf("no value available");
- }
- node *n=(node*)malloc(sizeof(node));
- printf("enter value to create a list:");
- scanf("%d",&n->a);
- n->next=NULL;
- head=n;
- for(j=0 ; j<i ; j++)
- {
- insert_nth();
- }
- display( );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement