Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5. class node
  6. {
  7. public:
  8. class node *next;
  9. int data;
  10. };
  11.  
  12. class stack : public node
  13. {
  14. node *head;
  15. int tos;
  16. public:
  17. stack()
  18. {
  19. tos=-1;
  20. }
  21. void push(int x)
  22. {
  23. if (tos < 0 )
  24. {
  25. head =new node;
  26. head->next=NULL;
  27. head->data=x;
  28. tos ++;
  29. }
  30. else
  31. {
  32. node *temp,*temp1;
  33. temp=head;
  34. if(tos >= 100)
  35. {
  36. cout <<"stack over flow";
  37. return;
  38. }
  39. tos++;
  40. while(temp->next != NULL)
  41. temp=temp->next;
  42. temp1=new node;
  43. temp->next=temp1;
  44. temp1->next=NULL;
  45. temp1->data=x;
  46. }
  47. }
  48. int count()
  49. { node *temp;
  50. temp=head;
  51. int c=0;
  52. if (tos < 0)
  53. {
  54. cout <<" stack under flow";
  55. return 0;
  56. }
  57. while(temp != NULL)
  58. {
  59. c++;
  60. temp=temp->next;
  61. }
  62. return c;
  63. }
  64.  
  65. void display()
  66. {
  67. node *temp;
  68. temp=head;
  69. if (tos < 0)
  70. {
  71. cout <<" stack under flow";
  72. return;
  73. }
  74. while(temp != NULL)
  75. {
  76. cout <<temp->data<< " ";
  77. temp=temp->next;
  78. }
  79. }
  80.  
  81. void pop()
  82. {
  83. node *temp;
  84. temp=head;
  85. if( tos < 0 )
  86. {
  87. cout <<"stack under flow";
  88. return;
  89. }
  90. tos--;
  91. while(temp->next->next!=NULL)
  92. {
  93. temp=temp->next;
  94. }
  95. temp->next=NULL;
  96. }
  97.  
  98.  
  99. void SplitStack()
  100. { stack s2;stack s3; // temp stacks
  101. while(head!=NULL)
  102. {
  103. int temp = head->data;
  104. if(temp%2==0)
  105. s2.push(temp);
  106. else
  107. s3.push(temp);
  108. head=head->next;
  109. }
  110.  
  111. cout<<"Stack S2 (Even) : "<<endl;
  112. s2.display();
  113. cout<<endl<<"Stack S3 (Odd) : "<<endl;
  114. s3.display();
  115.  
  116.  
  117. }
  118.  
  119.  
  120.  
  121. };
  122. main()
  123. {
  124. stack s1;
  125. s1.push(70); s1.push(25); s1.push(30); s1.push(15); s1.push(0); s1.push(11);
  126. s1.display();
  127. cout<<endl<<"The Number of Data in Stack : " << s1.count() <<endl;
  128. s1.pop();s1.pop();
  129. s1.push(66); s1.push(77); s1.push(3);
  130. s1.SplitStack();
  131. system("pause");
  132. return (0);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement