-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.go
More file actions
61 lines (56 loc) · 1.19 KB
/
Stack.go
File metadata and controls
61 lines (56 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import "fmt"
var st [100]int
var top int
func main(){
top=-1
ch := 0
temp:=0
for true{
fmt.Println("Enter you choice:")
fmt.Println("1. PUSH\n2. POP\n3. PRINT\n4. EXIT")
fmt.Scanf("%d\n",&ch)
switch ch {
case 1:
fmt.Println("Enter the value...")
fmt.Scanf("%d\n",&temp)
push(temp);
case 2:
temp=pop()
if temp!=-1{
fmt.Println("The popped value is ",temp)
}
case 3:
print()
case 4:
return
default:
fmt.Println("Please enter a valid choice")
}
}
}
func print(){
i:=0
if top==-1 {
fmt.Println("First insert elements into the stack")
}else{
fmt.Printf("The values are as follows")
for i<=top{
fmt.Println(st[i])
i++
}
}
}
func pop() int{
if top==-1{
fmt.Println("Please push values before popping")
return -1;
}
temp:=st[top]
top--
return temp
}
func push(n int){
top++
st[top]=n
}