Advertisement
Guest User

C++ linked list user input out of bound validation causes pr

a guest
May 9th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. using namespace std;
  4. int size;
  5. struct node
  6. {
  7. int orderId;
  8. string flavour;
  9. node *next;
  10. };
  11. class list
  12. {
  13. private:
  14. node *head, *tail;
  15. public:
  16. list()
  17. {
  18. head=NULL;
  19. tail=NULL;
  20. }
  21.  
  22. void display() //print out list
  23. {
  24. node *temp=new node;
  25. temp=head;
  26.  
  27. if(head==NULL)
  28. {
  29. cout<<"You haven't ordered any cake(s)"<<endl;
  30. }
  31.  
  32. while(temp!=NULL)
  33. {
  34. cout<<temp->orderId<<"\t"<<temp->flavour<<endl;
  35. temp=temp->next;
  36. }
  37.  
  38. }
  39.  
  40. void createnode(int orderId_value, string flavour_value) //create node at last position
  41. {
  42. node *temp=new node;
  43. temp->orderId=orderId_value;
  44. temp->flavour=flavour_value;
  45. temp->next=NULL;
  46. if(head==NULL)
  47. {
  48. head=temp;
  49. tail=temp;
  50. temp=NULL;
  51. }
  52. else
  53. {
  54. tail->next=temp;
  55. tail=temp;
  56. }
  57. size++;
  58. }
  59.  
  60. bool update_data(int key) //update node data with given position
  61. {
  62. node *current=new node;
  63. current=head;
  64. string updated_flavour;
  65.  
  66. if(head==NULL)
  67. {
  68. cout<<"No list"<<endl;
  69. }
  70.  
  71. cout<<"Enter new flavour"<<endl;
  72. cin>>updated_flavour;
  73.  
  74. for(int i=1;i<key;i++){
  75. current = current->next;
  76. }
  77. current->flavour=updated_flavour;
  78.  
  79. }
  80.  
  81. void delete_position(int pos) //delete node with given position
  82. {
  83. node *current=new node;
  84. node *previous=new node;
  85. current=head;
  86.  
  87. if(head==NULL)
  88. {
  89. cout<<"You haven't ordered any cake(s)"<<endl;
  90. }
  91.  
  92. for(int i=1;i<pos;i++)
  93. {
  94. previous=current;
  95. current=current->next;
  96. }
  97. previous->next=current->next;
  98.  
  99. if(pos == 1){
  100. node *temp=new node;
  101. temp=head;
  102. head=head->next;
  103. delete temp;
  104. }
  105. size--;
  106. }
  107. };
  108. int main()
  109. {
  110. int orderId_input=0, destroy_input, position_input,checker;
  111. string flavour_input;
  112.  
  113. list list; //created a list named list.
  114. list.display();
  115.  
  116. cout<<"====Take 1===="<<endl;
  117. cout<<"Enter flavour for cake!"<<endl;
  118. cin>>flavour_input;
  119.  
  120. orderId_input++;
  121. list.createnode(orderId_input,flavour_input);
  122. list.display();
  123.  
  124. cout<<"====Take 2===="<<endl;
  125. cout<<"Enter flavour for cake!"<<endl;
  126. cin>>flavour_input;
  127.  
  128. orderId_input++;
  129. list.createnode(orderId_input,flavour_input);
  130. list.display();
  131.  
  132. cout<<"====Take 3===="<<endl;
  133. cout<<"Enter flavour for cake!"<<endl;
  134.  
  135. cin>>flavour_input;
  136.  
  137. orderId_input++;
  138. list.createnode(orderId_input,flavour_input);
  139. list.display();
  140.  
  141. cout<<"====destroy node===="<<endl;
  142. cout<<"Which cake would you like to destroy?"<<endl;
  143.  
  144. do{
  145.  
  146. cin>>destroy_input;
  147.  
  148. }while(destroy_input > size);
  149.  
  150. list.delete_position(destroy_input);
  151.  
  152. cout<<"====Result after destroy===="<<endl;
  153. list.display();
  154. cout<<"There are currently "<<size<<" cake(s)"<<endl;
  155.  
  156.  
  157. system("pause");
  158.  
  159. return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement