void insertn(node **head,int n) { if((*head)==NULL) { printf("List is empty"); return; } else if(count(*head)<n) { printf("invalid value"); return; } else if(n==1) insertbeg(&(*head)); else { node* temp=(*head); node* prev=temp; while(temp->next!=NULL && --n) { prev=temp; temp=temp->next; } prev->next=createnode(insertdata()); (prev->next)->next=temp; } }
void main() { int info,a,c; struct node *np; char ch; start=NULL; clrscr(); do { printf("ENTER YOUR CHOICE:\n"); printf("Enter 1 to insert at begining\n"); printf("Enter 2 to insert after a given element\n"); printf("Enter 3 to insert at end\n"); printf("Enter 4 to delete a particular element\n"); printf("Enter 5 to reverse the current linked list\n"); scanf("%d",&a); if(a==1) { printf("Enter the element: \n"); scanf("%d",&info); np=create_new_node(info); insertbeg(np); } else if(a==2) { printf("Enter the element\n"); scanf("%d",&info); np=create_new_node(info); insertmid(np); } else if(a==3) { printf("Enter the element\n"); scanf("%d",&info); np=create_new_node(info); insertend(np); } else if(a==4) { printf("Enter the element\n"); scanf("%d",&info); np=create_new_node(info); del(info); } else if(a==5) { reverse(c); } display(start); c=count(start); printf("The number of nodes are %d\n",c); printf("\nDo you want to proceed? y/n\n"); scanf(" %c",&ch); } while(ch=='y'); getch(); }
void main() { start=NULL; int value,k=0,choice,pos; char ans,ch; printf("MENU DRIVEN PROGRAM:\n"); printf("1.Create linked list\n"); printf("2.Insert the value at the end\n"); printf("3.Insert the value at the begining\n"); printf("4.Insert the value at nth position\n"); do { printf("What do you want to do:"); scanf("%d",&choice); switch(choice) { case 1:if(k==1) printf("\nYou have already created linked list!!!!! "); else { do { printf("\nEnter the value:"); scanf("%d",&value); create(value); fflush(stdin); printf("Do you want to add more element in the list?"); scanf("%c",&ch); }while(ch=='y'||ch=='Y'); k++; }break; case 2:if(k==1) {printf("\nEnter the value to insert at the end:"); scanf("%d",&value); insertend(value); } else printf("\nFirstly create a linked list!!!!"); break; case 3:if(k==1) {printf("\nEnter the value to insert at the begining:"); scanf("%d",&value); insertbeg(value); } else printf("\nFirstly create a linked list!!!!"); break; case 4:if(k==1) {printf("\nEnter the value and position:"); scanf("%d%d",&value,&pos); insertn(value,pos); } else printf("\nFirstly create a linked list!!!!"); break; } fflush(stdin); printf("\nDo you want to perform another operation?"); scanf("%c",&ans); }while(ans=='y'||ans=='Y'); printf("\nThe linked list is:"); temp=start; printf("%d\t",temp->data); while(temp->next!=NULL) { temp=temp->next; printf("%d\t",temp->data); } }