fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3. class Node{
  4. public:
  5. Node* next;
  6. int data;
  7.  
  8. };
  9. void push(Node** head_ref, int new_data){
  10. Node* new_node= new Node();
  11. new_node-> data = new_data;
  12. new_node->next = (*head_ref);
  13. (*head_ref) = new_node;
  14. }
  15.  
  16. void deleteNode(Node** head_ref, int position){
  17. Node* temp = *head_ref;
  18. if(temp == NULL){
  19. return;
  20. }
  21. if(position == 0){
  22. *head_ref = temp->next;
  23. free(temp);
  24. return;
  25. }
  26. for(int i=0; temp != NULL && i<position-1;i++){
  27. temp = temp->next;
  28. }
  29. if(temp== NULL || temp->next == NULL){
  30. return;
  31. }
  32. Node* next = temp->next->next;
  33.  
  34. free(temp->next);
  35. temp ->next = next;
  36. }
  37.  
  38. void printList(Node* node){
  39. while(node != NULL){
  40. cout<< node-> data<<" ";
  41. node = node->next;
  42. }
  43. }
  44.  
  45. int main() {
  46. // your code goes here
  47. Node* head =NULL;
  48.  
  49. push(&head , 19);
  50. push(&head , 190);
  51. push(&head , 191);
  52. push(&head , 1900);
  53. push(&head , 19000);
  54.  
  55. cout<<"original list: "<<endl;
  56. printList(head);
  57. cout<<endl;
  58.  
  59. cout<<"Patterned List:"<<endl;
  60. deleteNode(&head , 2);
  61. printList(head);
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0s 4272KB
stdin
Standard input is empty
stdout
original list: 
19000 1900 191 190 19 
Patterned List:
19000 1900 190 19