fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5. int data;
  6. struct node* next,*pre;
  7. }s;
  8. s *head = NULL,*tail=NULL,*p,*q;
  9. void create(void)
  10. {
  11. p=(s*)malloc (sizeof(s));
  12. printf("enter the data");
  13. scanf("%d",&p->data);
  14. if(head==NULL)
  15. {
  16. head=p;
  17. tail=p;
  18. head->pre=NULL;
  19. }
  20. else
  21. {
  22. p->pre=tail;
  23. tail->next=p;
  24. tail=p;
  25. }
  26. tail->next=NULL;
  27. }
  28. void insert_at_beg(void)
  29. {
  30. p=(s*)malloc (sizeof(s));
  31. printf("enter the data");
  32. scanf("%d",&p->data);
  33. if(head==NULL)
  34. {
  35. printf("no linked list created");
  36. }
  37. else
  38. {
  39. head->pre=p;
  40. p->next=head;
  41. head=p;
  42. }
  43. }
  44. void ins_pos(void)
  45. {
  46. int i=0,pos;
  47. printf("enter position in which node will be inserted");
  48. scanf("%d",&pos);
  49. for(p=head;p!=NULL;p=p->next)
  50. {
  51. i++;
  52. }
  53. if(pos>i)
  54. {
  55. printf("there is no position in that linked list");
  56. }
  57. else
  58. {
  59. q=head;
  60. for(i=1;i<pos;i++)
  61. {
  62. q=q->next;
  63. }
  64. p=(s*)malloc (sizeof(s));
  65. printf("enter the data");
  66. scanf("%d",&p->data);
  67. p->next=q->next;
  68. p->pre=q->pre;
  69. q->next=p;
  70. p=p->next;
  71. p->pre=q->next;
  72. }
  73. }
  74. void del_pos(void)
  75. {
  76. int i=0,pos;
  77. printf("enter position in which node will be inserted");
  78. scanf("%d",&pos);
  79. for(p=head;p->next!=NULL;p=p->next)
  80. {
  81. i++;
  82. }
  83. if(pos>i)
  84. {
  85. printf("there is no position in that linked list");
  86. }
  87. else
  88. {
  89. q=head;
  90. for(i=1;i<pos;i++)
  91. {
  92. q=q->next;
  93. }
  94. p=q->next;
  95. q->next=p->next;
  96. q=p->next;
  97. q->pre=p->pre;
  98. free(p);
  99. }
  100. }
  101. void search(void)
  102. {
  103. int x;
  104. printf("enter the element to be found");
  105. scanf("%d",&x);
  106. for(p=head;p!=tail;p=p->next)
  107. {
  108. if(x==p->data)
  109. {
  110. printf("element is found");
  111. return;
  112. }
  113. }
  114. printf("element found ");
  115. }
  116. void display(void)
  117. {
  118. for(p=head;p!=NULL;p=p->next)
  119. {
  120. printf("%d",p->data);
  121. }
  122. }
  123. void main()
  124. {
  125. int i;
  126.  
  127. {
  128. printf("1.create\n2.insert_at_beg\n3.ns_pos\n4.del_pos\n5.search\n6.display\n");
  129. printf("choose the choice");
  130. scanf("%d",&i);
  131. switch(i)
  132. {
  133. case 1:
  134. create();
  135. break;
  136. case 2:
  137. insert_at_beg();
  138. break;
  139. case 3:
  140. ins_pos();
  141. break;
  142. case 4:
  143. del_pos();
  144. break;
  145. case 5:
  146. search();
  147. break;
  148. case 6:
  149. display();
  150. break;
  151. default:
  152. break;
  153.  
  154. }
  155.  
  156. }
  157. }
Success #stdin #stdout 0s 5464KB
stdin
1
2
stdout
1.create
2.insert_at_beg
3.ns_pos
4.del_pos
5.search
6.display
choose the choiceenter the data