Advertisement
Adrita

task 2( ds lab 6 ) 3rd sem

Mar 17th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8. typedef struct node Node;
  9. Node* create_node(int item, Node *next)
  10. {
  11. Node* new_node=new Node();
  12. if(new_node==NULL)
  13. cout<<"Error"<<endl;
  14. else
  15. {
  16. new_node->data=item;
  17. new_node->next=next;
  18. }
  19. return new_node;
  20.  
  21. }
  22. Node* insertn(int item,Node* head,int key)
  23. {
  24. Node* new_node=create_node(item,NULL);
  25. if(head==NULL)
  26. cout<<"Error"<<endl;
  27. else
  28. {
  29. Node *current=new Node();
  30. current=head;
  31. while(current!=NULL&&current->data!=key)
  32. current=current->next;
  33. if(current!=NULL)
  34. {
  35. new_node->next=current->next;
  36. current->next=new_node;
  37. }
  38. else
  39. cout<<"Value not found"<<endl;
  40. }
  41. return head;
  42. }
  43. void display(Node *head)
  44. {
  45. cout<<"Linked list:"<<endl;
  46. while(head!=NULL)
  47. {
  48. cout<<head->data<<" ";
  49. head=head->next;
  50. }
  51. cout<<endl;
  52. }
  53. Node* remove_head(Node* head)
  54. {
  55. head=head->next;
  56. return head;
  57. }
  58. Node* remove_tail(Node* head)
  59. {
  60. Node *current=new Node();
  61. Node *prev=new Node();
  62. current=head;
  63. while(current->next!=NULL)
  64. {
  65. prev=current;
  66. current=current->next;
  67. }
  68. prev->next=NULL;
  69. return head;
  70. }
  71. Node* remove_value(Node* head,int key)
  72. {
  73. Node* temp=new Node();
  74. temp=head;
  75. Node* prev=new Node();
  76. if(temp!=NULL&&temp->data==key)
  77. {
  78. head=temp->next;
  79. return head;
  80. }
  81. while(temp!=NULL && temp->data!=key)
  82. {
  83. prev=temp;
  84. temp=temp->next;
  85. }
  86. if(temp==NULL)
  87. {
  88. cout<<"Value not present"<<endl;
  89. return head;
  90. }
  91. prev->next=temp->next;
  92. return head;
  93. }
  94. Node* prepend(int item,Node* head)
  95. {
  96. Node* new_node=create_node(item,head);
  97. return new_node;
  98. }
  99. int main()
  100. {
  101. Node* head=NULL;
  102. head=prepend(10,head);
  103. head=insertn(20,head,10);
  104. head=insertn(30,head,20);
  105. head=insertn(40,head,30);
  106. head=insertn(50,head,40);
  107. head=insertn(60,head,50);
  108. head=insertn(70,head,60);
  109. display(head);
  110. int t;
  111. cin>>t;
  112. for(int i=0; i<t; i++)
  113. {
  114. cout<<"Enter 1 to delete head,2 to delete tail, 3 to delete a value"<<endl;
  115. int n;
  116. cin>>n;
  117. if(n==1)
  118. {
  119. head=remove_head(head);
  120. display(head);
  121. }
  122. else if(n==2)
  123. {
  124. head=remove_tail(head);
  125. display(head);
  126. }
  127. else if(n==3)
  128. {
  129. cout<<"Enter the value"<<endl;
  130. int key;
  131. cin>>key;
  132. head=remove_value(head,key);
  133. display(head);
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement