-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingLinkedList.java
More file actions
95 lines (95 loc) · 2.25 KB
/
Copy pathStackUsingLinkedList.java
File metadata and controls
95 lines (95 loc) · 2.25 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chapter05;
import java.util.Scanner;
/**
*
* @author harsh
*/
class Node{
int data;
Node next;
Node(){
data=0;
next = null;
}
}
class Stack{
Scanner scan = new Scanner(System.in);
public int x;
public Node front;
Stack(){
front = null;
}
Node newNode(int a){
Node temp = new Node();
temp.data=a;
temp.next=null;
return temp;
}
void push(){
Node temp;
System.out.println("Enter data to push");
x= scan.nextInt();
temp = newNode(x);
if(front == null){
System.out.println("First element pushed");
front = temp;
}else{
temp.next=front;
front=temp;
System.out.println("Element pushed");
}
}
void pop(){
if(front==null){
System.out.println("Empty Stack");
}else{
front=front.next;
System.out.println("Element Poped");
}
}
void display(){
Node temp=front;
if(front == null){
System.out.println("Empty Stack");
}else{
while(temp!=null){
System.out.println(temp.data+"\t");
temp=temp.next;
}
System.out.println("List Displayed");
}
}
}
public class StackUsingLinkedList {
public static void main(String[] args){
Stack abc = new Stack();
int choice;
do
{
System.out.println(" 1.Push element \t 2.Pop element \t 3.Display \t 4.EXIT\nEnter choice:");
choice = Integer.parseInt(new Scanner(System.in).nextLine());
switch(choice)
{
case 1:
abc.push();
break;
case 2:
abc.pop();
break;
case 3:
abc.display();
break;
default:
System.out.println("INVALID COMMAND");
break;
case 4:
break;
}
}while(choice!=4);
}
}