Advertisement
swaks

stak ique

Oct 31st, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. class SLLNode<E>
  2. {
  3. E element;
  4. SLLNode <E>succ;
  5. public SLLNode(E el,SLLNode<E>s)
  6. {
  7. element=el;
  8. succ=s;
  9.  
  10.  
  11. }
  12.  
  13. }
  14.  
  15. class Queue<E>
  16. {
  17. SLLNode<E>head;
  18. SLLNode<E>rear;
  19. int length;
  20. public Queue()
  21. {
  22. head=null;
  23. rear=null;
  24. length=0;
  25.  
  26. }
  27. public void enque(E el)
  28. {
  29. if(head==null)
  30. rear=head=new SLLNode<E>(el,null);
  31. else
  32. {
  33. SLLNode<E> pom=new SLLNode<E>(el,null);
  34. rear.succ=pom;
  35. rear=pom;}
  36. length++;
  37.  
  38.  
  39. }
  40. public E peek()
  41. {
  42. return head.element;
  43. }
  44. public E deque()
  45. {
  46. E element=null;
  47. if(head!=null)
  48. {
  49. element=head.element;
  50. head=head.succ;
  51. length--;
  52. }
  53.  
  54. return element;
  55.  
  56. }
  57. public int size()
  58. {return length; }
  59. public String toString()
  60. {
  61. SLLNode<E> tmp=head;
  62. String s="";
  63. while(tmp!=null)
  64. {s+=tmp.element.toString()+" ";
  65. tmp=tmp.succ;}
  66. return s;
  67. }
  68.  
  69.  
  70.  
  71. }
  72.  
  73. class Stack<E>
  74. {
  75. SLLNode<E>top;
  76. int length;
  77. public Stack()
  78. {
  79. top=null;
  80. length=0;
  81.  
  82. }
  83. public void push(E el)
  84. {
  85. if(top==null)
  86. top=new SLLNode<E>(el,null);
  87. else
  88. {
  89. SLLNode<E> pom=new SLLNode<E>(el,top);
  90. top=pom;}
  91. length++;
  92.  
  93.  
  94. }
  95. public E peek()
  96. {
  97. return top.element;
  98. }
  99. public E pop()
  100. {
  101. E element=null;
  102. if(top!=null)
  103. {
  104. element=top.element;
  105. top=top.succ;
  106. length--;
  107. }
  108.  
  109. return element;
  110.  
  111. }
  112. public int size()
  113. {return length; }
  114. public String toString()
  115. {
  116. SLLNode<E> tmp=top;
  117. String s="";
  118. while(tmp!=null)
  119. {s+=tmp.element.toString()+" ";
  120. tmp=tmp.succ;}
  121. return s;
  122. }
  123.  
  124.  
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement