fork download
  1. ///Here the create list and print list function has been already implemented. Your task is only to implement
  2. ///(i)the find function and then(ii) insert a value(val) after the value(x) use the find function
  3. #include<iostream>
  4. using namespace std;
  5. class Node{
  6. public:
  7. int val;
  8. Node *prev;
  9. Node *next;
  10. };
  11. class doublylinkedlist{
  12. private:
  13. Node *head,*tail;
  14. public:
  15. doublylinkedlist()
  16. {
  17. head=NULL;
  18. tail=NULL;
  19. }
  20. void createFirstElement(int x){
  21. Node *newnode=new Node;
  22. newnode->val=x;
  23. newnode->prev=NULL;
  24. newnode->next=NULL;
  25. head=newnode;
  26. tail=head;
  27. }
  28.  
  29. void createlist(int x)
  30. {
  31. Node *newnode=new Node;
  32. newnode->val=x;
  33. newnode->next=NULL;
  34. newnode->prev=NULL;
  35.  
  36. if(head==NULL)
  37. {
  38. createFirstElement(x);
  39. return;
  40. }
  41. else{
  42. tail->next=newnode;
  43. newnode->prev=tail;
  44. tail=newnode;
  45. }
  46. }
  47.  
  48. ///implement a find function which will search for the value(x)
  49.  
  50.  
  51. void insertdata(int val,int x){} ///Use the find function and then insert the value(data) after x
  52.  
  53. void printListfromstart(){
  54. Node *temp=head;
  55. while(temp!=NULL)
  56. {
  57. cout<<temp->val<<" ";
  58. temp=temp->next;
  59. }
  60. }
  61. };
  62. int main()
  63. {
  64. int x,val;
  65. cin>>val>>x;
  66. doublylinkedlist DLL;
  67. DLL.createlist(1);
  68. DLL.createlist(2);
  69. DLL.createlist(3);
  70. DLL.createlist(4);
  71. DLL.createlist(5);
  72. DLL.insertdata(val,x);
  73. DLL.printListfromstart();
  74. }
  75.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
1 2 3 4 5