Created
November 16, 2022 08:18
-
-
Save akshanshgusain/3a4e8b8b91d90f0f75e07ee3da1f56ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Queue | |
import ( | |
"container/list" | |
"fmt" | |
) | |
type Queue struct { | |
Queue *list.List | |
} | |
func (s *Queue) Size() int { | |
return s.Queue.Len() | |
} | |
func (s *Queue) Empty() bool { | |
return s.Queue.Len() == 0 | |
} | |
//Push element at the front of the list: O(1) | |
func (s *Queue) Push(value string) { | |
s.Queue.PushBack(value) | |
} | |
//Pop element from the front if the list: O(1) | |
func (s *Queue) Pop() error { | |
if s.Queue.Len() > 0 { | |
e := s.Queue.Front() | |
s.Queue.Remove(e) | |
return nil | |
} | |
return fmt.Errorf("queue is empty") | |
} | |
// Front Get the top element: O(1) | |
func (s *Queue) Front() (string, error) { | |
if s.Queue.Len() > 0 { | |
if val, ok := s.Queue.Front().Value.(string); ok { | |
return val, nil | |
} | |
return "", fmt.Errorf("queue datatype is incorrect") | |
} | |
return "", fmt.Errorf("queue is empty") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment