fork download
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. struct node{
  4. int data;
  5. struct node* next;
  6. };
  7. struct node* createnode(int val)
  8. {
  9. struct node* p=(struct node*)malloc(sizeof(struct node));
  10. p->data=val;
  11. p->next=NULL;
  12. return p;
  13. }
  14.  
  15. struct node* insertlinked(struct node* head,int pos,int num)
  16. {
  17. struct node* temp=head;
  18. if(pos==0)
  19. {
  20. struct node* temp1=createnode(num);
  21. temp1->next=head;
  22. head=temp1;
  23. return head;
  24. }
  25. while(pos>1 && temp!=NULL)
  26. {
  27. pos--;
  28. temp=temp->next;
  29. }
  30. if(temp==NULL)
  31. {
  32. printf("Node not found\n");
  33. return NULL;
  34. }
  35. struct node* temp2=createnode(num);
  36. temp2->next=temp->next;
  37. temp->next=temp2;
  38. return head;
  39. }
  40. struct node* dellinked(struct node* head,int pos)
  41. {
  42. struct node* temp=head;
  43. if(pos==0)
  44. {
  45. head=head->next;
  46. free(temp);
  47. return head;
  48. }
  49. while(pos>1 && temp->next!=NULL)
  50. {
  51. pos--;
  52. temp=temp->next;
  53. }
  54. if(temp->next==NULL)
  55. {
  56. printf("Node not foumd\n");
  57. return NULL;
  58. }
  59. struct node* temp1=temp->next;
  60. temp->next=temp->next->next;
  61. free(temp1);
  62. return head;
  63. }
  64. void printlinked(struct node* head)
  65. {
  66. if(head==NULL)
  67. return;
  68. while(head!=NULL)
  69. {
  70. printf("%d>>",head->data);
  71. head=head->next;
  72. }
  73. }
  74. int main()
  75. {
  76. int pos,val,pos1;
  77. struct node* head=createnode(10);
  78. head->next=createnode(20);
  79. head->next->next=createnode(30);
  80. head->next->next->next=createnode(40);
  81. printf("enter the pos and value to be inserted\n");
  82. scanf("%d %d",&pos,&val);
  83. printf("enter the value to be deleted\n");
  84. scanf("%d",&pos1);
  85. struct node* temp=insertlinked(head,pos,val);
  86. printlinked(temp);
  87. printf("\n");
  88. struct node* temp1=dellinked(temp,pos1);
  89. printlinked(temp1);
  90. return 0;
  91. }
  92.  
Success #stdin #stdout 0.01s 5280KB
stdin
1 67
0
stdout
enter the pos and value to be inserted
enter the value to be deleted
10>>67>>20>>30>>40>>
67>>20>>30>>40>>