Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit b2a8e66

Browse files
committed
add stack and queue
1 parent ef8d6ba commit b2a8e66

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/StackQueue.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module StackQueue where
2+
3+
newtype Stack a = Stack [a] deriving Show
4+
5+
emptyS = Stack []
6+
7+
pop :: Stack a -> Maybe (Stack a, a)
8+
pop (Stack []) = Nothing
9+
pop (Stack (x:xs)) = Just (Stack xs, x)
10+
11+
push :: a -> Stack a -> Stack a
12+
push x (Stack l) = Stack (x:l)
13+
14+
newtype Queue a = Queue [a] deriving Show
15+
16+
emptyQ = Queue []
17+
18+
deQue :: Queue a -> Maybe (Queue a, a)
19+
deQue (Queue []) = Nothing
20+
deQue (Queue xs) = Just (Queue (init xs), last xs)
21+
22+
enQue :: a -> Queue a -> Queue a
23+
enQue x (Queue l) = Queue (l++[x])
24+
25+
-- alternative the list can be use as Stack or Queue

0 commit comments

Comments
 (0)