Advertisement
Maruf_Hasan

Double linked list

Apr 15th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6. int info;
  7. node *prev;
  8. node *next;
  9. };
  10. node *root=NULL,*tail=NULL;
  11.  
  12. void create(int data)
  13. {
  14. node *ptr;
  15. ptr=new node();
  16. ptr->prev=NULL;
  17. ptr->next=NULL;
  18. ptr->info=data;
  19. if(root==NULL)
  20. {
  21. root=ptr;
  22. tail=ptr;
  23. }
  24. else
  25. {
  26. tail->next=ptr;
  27. ptr->prev=tail;
  28. tail=ptr;
  29. }
  30. }
  31. void traverse()
  32. {
  33. node *srt;
  34. for(srt=root; ;)
  35. {
  36. cout<<srt->info<<endl;
  37. srt=srt->next;
  38. if(srt->next==NULL)
  39. break;
  40. }
  41. cout<<srt->info<<endl;
  42. }
  43. insert_first()
  44. {
  45. int data;
  46. cin>>data;
  47. node *ptr;
  48. ptr=new node();
  49. ptr->prev=NULL;
  50. ptr->next=root;
  51. ptr->info=data;
  52. root->prev=ptr;
  53. root=ptr;
  54. }
  55. int main()
  56. {
  57. int n;
  58. cin>>n;
  59. int a[n+5];
  60. for(int i=0;i<n;i++)
  61. {
  62. cin>>a[i];
  63. create(a[i]);
  64. }
  65. insert_first();
  66. traverse();
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement