Advertisement
galib71

Linked_List_stack_implementation

Mar 24th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class node;
  5. node* head=NULL;
  6. //node* ptr=head;
  7. class node
  8. {
  9. private:
  10. int data;
  11. public:
  12. void setvalue(int x)
  13. {
  14. data=x;
  15. }
  16. int getvalue()
  17. {
  18. return data;
  19. }
  20. node* ptrnext;
  21.  
  22. void display()
  23. {
  24. node* ptr=head;
  25. while(ptr!=NULL)
  26. {
  27. cout<<ptr->data<<" ";
  28. ptr=ptr->ptrnext;
  29. }
  30. cout<<endl<<endl;
  31. }
  32. ////////////////////////////////////////////////////////////
  33. void search_item(int item)
  34. {
  35. node* ptr=head;
  36. while(ptr->data != item && ptr-> ptrnext !=NULL)
  37. ptr = ptr->ptrnext;
  38. if(ptr->data==item)
  39. cout<<item<<" "<<"Found"<<endl;
  40. else
  41. cout<<item<<" "<<"Not Found"<<endl;
  42.  
  43. }
  44. ///////////////////////////////////////////////////////////
  45. void ensert_end_stack(int k,node* newn)
  46. {
  47. newn->data=k;
  48. newn->ptrnext=NULL;
  49. }
  50. /////////////////////////////////////////////////////////
  51. void delete_end_stack()
  52. {
  53. node* ptr=head;
  54. node* ptr2;
  55. while(ptr->ptrnext->ptrnext!=NULL)
  56. ptr=ptr->ptrnext;
  57. ptr2=ptr->ptrnext;
  58. ptr->ptrnext = NULL;
  59. }
  60. };
  61. int main()
  62. {
  63. node n1,n2,n3,n4;
  64. n1.setvalue(10);
  65. n2.setvalue(20);
  66. n3.setvalue(30);
  67. head=&n1;
  68. n1.ptrnext=&n2;
  69. n2.ptrnext=&n3;
  70. n3.ptrnext=NULL;
  71. cout<<"Existing Linked List : ";
  72. n1.display();
  73.  
  74. int search_value;
  75. cout<<"Insert data to search : ";
  76. cin>>search_value;
  77. cout<<endl;
  78. n1.search_item(search_value);
  79. cout<<endl;
  80.  
  81. int add_value;
  82. node* newnode;
  83. newnode=&n4;
  84. cout<<"Insert data to add : ";
  85. cin>>add_value;
  86. cout<<endl;
  87. n3.ptrnext=&n4;
  88. n4.ensert_end_stack(add_value,newnode);
  89. cout<<"After insertion : ";
  90. n4.display();
  91. cout<<endl;
  92.  
  93. n4.delete_end_stack();
  94. cout<<"After Deletion : ";
  95. n1.display();
  96.  
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement