Skip to content

Commit fdfd046

Browse files
authored
Add files via upload
This is the code for stack implementation on java without the use of collection class.
0 parents  commit fdfd046

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

stack.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
public class stack
2+
{
3+
node head;
4+
public static class node
5+
{
6+
int data;
7+
node next;
8+
node(int data)
9+
{
10+
this.data=data;
11+
next=null;
12+
}
13+
}
14+
public static stack push(stack st,int data)
15+
{
16+
node new_node=new node(data);
17+
if(st.head==null)
18+
{
19+
st.head=new_node;
20+
return st;
21+
}
22+
else
23+
{
24+
node temp=st.head;
25+
while(temp.next!=null)
26+
temp=temp.next;
27+
temp.next=new_node;
28+
return st;
29+
}
30+
}
31+
public static stack pop(stack st)
32+
{
33+
stack str= new stack();
34+
node temp=st.head;
35+
while(temp.next!=null)
36+
{
37+
if(temp.next!=null)
38+
str=push(str,temp.data);
39+
temp=temp.next;
40+
}
41+
return str;
42+
}
43+
public static void display(stack st)
44+
{
45+
node temp=st.head;
46+
while(temp!=null)
47+
{
48+
49+
System.out.print(temp.data+"\t");
50+
temp=temp.next;
51+
}
52+
}
53+
public static void main(String args[])
54+
{
55+
stack st=new stack();
56+
st=push(st,1);
57+
st=push(st,2);
58+
st=push(st,3);
59+
st=push(st,4);
60+
st=pop(st);
61+
st=pop(st);
62+
st=pop(st);
63+
st=push(st,5);
64+
display(st);
65+
}
66+
}
67+
68+

0 commit comments

Comments
 (0)