fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int val;
  5. Node*next;
  6. };
  7. Node*InsertAtBegin(Node*root,int x)
  8. {
  9. Node*newnode=new Node();
  10. newnode->val=x;
  11. newnode->next=NULL;
  12. if(root==NULL)
  13. {
  14. root=newnode;
  15. return root;
  16. }
  17. else
  18. {
  19. newnode->next=root;
  20. root=newnode;
  21. return root;
  22. }
  23. }
  24. Node*DeleteFromBegin(Node*root,int x)
  25. {
  26. if(root==NULL)
  27. return root;
  28. Node*temp=root;
  29. root=root->next;
  30. delete temp;
  31. return root;
  32. }
  33. void Print(Node*root)
  34. {
  35. Node*currnode;
  36. currnode=root;
  37. while(currnode!=NULL)
  38. {
  39. cout<<"val"<<currnode->val<<"->"<<endl;
  40. currnode=currnode->next;
  41. }
  42. cout<<endl;
  43. }
  44. int main()
  45. {
  46. Node*root=NULL;
  47. int n;
  48. cin>>n;
  49. if(n<=0)
  50. {
  51. cout<<endl;
  52. return 0;
  53. }
  54. int a[n];
  55. for(int i=0;i<n;i++)
  56. {
  57. cin>>a[i];
  58. }
  59. Print(root);
  60. for(int i=0;i<n;i++)
  61. {
  62. root=InsertAtBegin(root,a[i]);
  63. }
  64. Print(root);
  65. for(int i=0;i<n;i++)
  66. {
  67. root=DeleteFromBegin(root,a[i]);
  68. }
  69. Print(root);
  70. }
Success #stdin #stdout 0s 5288KB
stdin
3
6 3 9
stdout
val9->
val3->
val6->