-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.java
68 lines (65 loc) · 1.03 KB
/
stack.java
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
62
63
64
65
66
67
68
public class stack
{
node head;
public static class node
{
int data;
node next;
node(int data)
{
this.data=data;
next=null;
}
}
public static stack push(stack st,int data)
{
node new_node=new node(data);
if(st.head==null)
{
st.head=new_node;
return st;
}
else
{
node temp=st.head;
while(temp.next!=null)
temp=temp.next;
temp.next=new_node;
return st;
}
}
public static stack pop(stack st)
{
stack str= new stack();
node temp=st.head;
while(temp.next!=null)
{
if(temp.next!=null)
str=push(str,temp.data);
temp=temp.next;
}
return str;
}
public static void display(stack st)
{
node temp=st.head;
while(temp!=null)
{
System.out.print(temp.data+"\t");
temp=temp.next;
}
}
public static void main(String args[])
{
stack st=new stack();
st=push(st,1);
st=push(st,2);
st=push(st,3);
st=push(st,4);
st=pop(st);
st=pop(st);
st=pop(st);
st=push(st,5);
display(st);
}
}