Advertisement
shabbyheart

Untitled

Apr 22nd, 2019
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Node{
  5. public:
  6. int data;
  7. Node *next;
  8. };
  9. class Linklist{
  10. public:
  11. Node *head;
  12. public:
  13. Linklist(){
  14. head = NULL;
  15. }
  16. void insert_at_first(int x){
  17. Node *newNode = new Node();
  18. newNode->data = x;
  19. newNode->next = head;
  20. head = newNode;
  21. }
  22. void insertValue(int index,int x){
  23. Node *temp = head;
  24. if( index < 0 && temp == NULL)
  25. {
  26. cout<<"Insert Can not possible";
  27. }
  28. int cont=0;
  29. while(index > cont && temp)
  30. {
  31. temp = temp->next;
  32. cont++;
  33. }
  34.  
  35. Node *newNode = new Node();
  36. newNode->data = x;
  37. newNode->next = temp->next;
  38. temp->next = newNode;
  39. }
  40. void deleteValue( int key){
  41. Node *temp = head , *prev;
  42. if( temp != NULL && temp->data == key)
  43. {
  44. head = temp->next;
  45. delete(temp);
  46. return;
  47. }
  48. while( temp != NULL && temp->data != key)
  49. {
  50. prev = temp;
  51. temp = temp->next;
  52. }
  53. if(temp == NULL)
  54. {
  55. cout<<"Value is not found"<<endl;
  56. return;
  57. }
  58. prev->next = temp->next;
  59. delete(temp);
  60. }
  61. void display(){
  62. Node *temp = head;
  63. if(temp == NULL)
  64. {
  65. cout<<"Link List is Empty"<<endl;
  66. return;
  67. }
  68. while(temp != NULL)
  69. {
  70. cout<<temp->data<<" ";
  71. temp = temp->next;
  72. }
  73. cout<<endl;
  74. }
  75. };
  76. int main()
  77. {
  78. Linklist ll;
  79. cout<<"how many value you want to insert: ";
  80. int n;
  81. cin>>n;
  82. int value;
  83. cin>>value;
  84. ll.insert_at_first(value);
  85.  
  86. for(int i=0;i<n-1;i++)
  87. {
  88. cin>>value;
  89. ll.insertValue(i,value);
  90. }
  91. ll.display();
  92. cout<<endl<<"which value you want to delete ";
  93. cin>>value;
  94. ll.deleteValue(value);
  95. ll.display();
  96.  
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement