Advertisement
Farhana_Zaman

fdfgdfgf

Jun 3rd, 2022
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. typedef struct node Node;
  3. using namespace std;
  4. struct node {
  5. int data;
  6. Node *next;
  7.  
  8. };
  9.  
  10. Node *func1(Node *head,int item)
  11. {
  12. Node *loc;
  13. Node *locp;
  14. Node *current= head;
  15. int c=0;
  16. if(head==NULL)
  17. {
  18. return head;
  19. }
  20.  
  21. while(current!=NULL)
  22. {
  23. if(current->data ==item)
  24. {
  25. loc=current->next;
  26. c=1;
  27. break;
  28. }
  29. locp=current;
  30. loc=current->next;
  31.  
  32. current= current->next;
  33. }
  34. if(c==1)
  35. {
  36. locp->next=loc;
  37. return head;
  38. }
  39.  
  40. }
  41.  
  42. Node *func2(Node *head, int n)
  43. {
  44. int c=1;
  45. Node *loc;
  46. Node *locp;
  47. Node *current= head;
  48.  
  49. while(current!=NULL)
  50. {
  51. if(c==n)
  52. {
  53. loc=current->next;
  54. break;
  55.  
  56. }
  57. locp=current;
  58. current=current->next;
  59. c++;
  60. }
  61. locp->next= loc;
  62. return head;
  63. }
  64. Node *func3(Node *head,Node *locp, Node *loc)
  65. {
  66.  
  67. locp->next=loc->next;
  68. return head;
  69. }
  70. Node show( Node *head)
  71. {
  72. Node *current= head;
  73.  
  74. while(current!=NULL)
  75. {
  76. cout<<current->data<<" ";
  77. current=current->next;
  78. }
  79. cout<<endl;
  80. }
  81. int main()
  82. {
  83. int n=4;
  84.  
  85. Node *ar[n+1];
  86.  
  87. for(int i=0;i<n;i++)
  88. {
  89. ar[i]= (Node*)malloc(sizeof(Node));
  90. ar[i]->data=i+1;
  91. }
  92.  
  93. Node *head =ar[0];
  94.  
  95. for(int i=0;i<n-1;i++)
  96. {
  97. ar[i]->next= ar[i+1];
  98. }
  99.  
  100. ar[n-1]->next= NULL;
  101. cout<<"[Suppose linked list have these values : 1,2,3,4 ]"<<endl;
  102. cout<<"Enter any option from below: "<<endl;
  103. cout<<"Press 1: Delete a node which contains a certain value"<<endl;
  104. cout<<"Press 2: Delete the nth node you want to delete"<<endl;
  105. cout<<"Press 3: Delete a node by entering two location i.e. previous node & the location of the node"<<endl;
  106.  
  107. int opt;
  108. cin>>opt;
  109.  
  110. if(opt==1)
  111. {
  112. int v;
  113. cout<<"Enter the value you want to delete"<<endl;
  114. cin>>v;
  115. func1(head,v);
  116. show(head);
  117. }
  118. else if (opt == 2)
  119. {
  120. int v;
  121. cout<<"Enter the nth node number"<<endl;
  122. cin>>v;
  123.  
  124. func2(head, v);
  125. show(head);
  126. }
  127. else if (opt ==3)
  128. {
  129. Node *loc= (Node*)malloc(sizeof(Node));
  130. Node *locp=(Node*)malloc(sizeof(Node));
  131. cout<<"Enter LOC: ";
  132. }
  133. else
  134. {
  135. cout<<"Option out of Range"<<endl;
  136. }
  137. return 0;
  138. }
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement