Program:
import java.io.*;
class Node
{
public int data;
public Node next;
public Node(int x)
{
data=x;
}
public void display()
{
System.out.print(data+" ");
}
}
class stacker
{
public Node first;
public stacker()
{
first=null;
}
public boolean isEmpty()
{
return(first==null);
}
public void push(int value)
{
Node newnode=new Node(value);
newnode.next=first;
first=newnode;
}
public int pop()
{
if(isEmpty())
{
System.out.println("\n Stack Empty...");
return 0;
}
else
{
int r1=first.data;
first=first.next;
return r1;
}
}
public void display()
{
Node current=first;
while(current!=null)
{
System.out.print(" "+current.data);
current=current.next;
}
System.out.println("");
}
}
class stack_list
{
public static void main(String arg[]) throws IOException
{
stacker a1=new stacker();
DataInputStream x=new DataInputStream(System.in);
System.out.print("\n\t\t<<STACK USING LINKEDLIST>>\n\nStack ready...\n\nEnter number of elements to push: ");
int n1=Integer.parseInt(x.readLine());
System.out.println("\nEnter elements: ");
for(int i=0;i<n1;i++)
{
a1.push(Integer.parseInt(x.readLine()));
}
System.out.println("\nDo you want to see the stack items?\n\t1.Yes 2.No");
switch(Integer.parseInt(x.readLine()))
{
case 1:
System.out.println("\nThe current stack items:");
a1.display();
break;
case 2:
default:
break;
}
System.out.println("\nEnter the number of elements to pop: ");
int n2=Integer.parseInt(x.readLine());
for(int i=0;i<n2;i++)
{
System.out.println(a1.pop()+" is poped from stack... ");
}
System.out.println("\nDo you want to see the stack items?\n\t1.Yes 2.No");
switch(Integer.parseInt(x.readLine()))
{
case 1:
System.out.println("\nThe current stack items:");
a1.display();
break;
case 2:
default:
break;
}
}
}
Output:
nn@linuxmint ~ $ javac stack_list.java
Note: stack_list.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java stack_list
<<STACK USING LINKEDLIST>>
Stack ready...
Enter number of elements to push: 3
Enter elements:
1
2
3
Do you want to see the stack items?
1.Yes 2.No
1
The current stack items:
3 2 1
Enter the number of elements to pop:
1
3 is poped from stack...
Do you want to see the stack items?
1.Yes 2.No
1
The current stack content:
2 1
nn@linuxmint ~ $
import java.io.*;
class Node
{
public int data;
public Node next;
public Node(int x)
{
data=x;
}
public void display()
{
System.out.print(data+" ");
}
}
class stacker
{
public Node first;
public stacker()
{
first=null;
}
public boolean isEmpty()
{
return(first==null);
}
public void push(int value)
{
Node newnode=new Node(value);
newnode.next=first;
first=newnode;
}
public int pop()
{
if(isEmpty())
{
System.out.println("\n Stack Empty...");
return 0;
}
else
{
int r1=first.data;
first=first.next;
return r1;
}
}
public void display()
{
Node current=first;
while(current!=null)
{
System.out.print(" "+current.data);
current=current.next;
}
System.out.println("");
}
}
class stack_list
{
public static void main(String arg[]) throws IOException
{
stacker a1=new stacker();
DataInputStream x=new DataInputStream(System.in);
System.out.print("\n\t\t<<STACK USING LINKEDLIST>>\n\nStack ready...\n\nEnter number of elements to push: ");
int n1=Integer.parseInt(x.readLine());
System.out.println("\nEnter elements: ");
for(int i=0;i<n1;i++)
{
a1.push(Integer.parseInt(x.readLine()));
}
System.out.println("\nDo you want to see the stack items?\n\t1.Yes 2.No");
switch(Integer.parseInt(x.readLine()))
{
case 1:
System.out.println("\nThe current stack items:");
a1.display();
break;
case 2:
default:
break;
}
System.out.println("\nEnter the number of elements to pop: ");
int n2=Integer.parseInt(x.readLine());
for(int i=0;i<n2;i++)
{
System.out.println(a1.pop()+" is poped from stack... ");
}
System.out.println("\nDo you want to see the stack items?\n\t1.Yes 2.No");
switch(Integer.parseInt(x.readLine()))
{
case 1:
System.out.println("\nThe current stack items:");
a1.display();
break;
case 2:
default:
break;
}
}
}
Output:
nn@linuxmint ~ $ javac stack_list.java
Note: stack_list.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java stack_list
<<STACK USING LINKEDLIST>>
Stack ready...
Enter number of elements to push: 3
Enter elements:
1
2
3
Do you want to see the stack items?
1.Yes 2.No
1
The current stack items:
3 2 1
Enter the number of elements to pop:
1
3 is poped from stack...
Do you want to see the stack items?
1.Yes 2.No
1
The current stack content:
2 1
nn@linuxmint ~ $
0 comments:
Post a Comment