Skip to content

Commit a81972d

Browse files
authored
Create Stack-Stack Using LL
1 parent cc3bf63 commit a81972d

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

Stack-Stack Using LL

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
Implement a Stack Data Structure specifically to store integer data using a Singly Linked List.
2+
The data members should be private.
3+
You need to implement the following public functions :
4+
1. Constructor:
5+
It initialises the data members as required.
6+
2. push(data) :
7+
This function should take one argument of type integer. It pushes the element into the stack and returns nothing.
8+
3. pop() :
9+
It pops the element from the top of the stack and in turn, returns the element being popped or deleted. In case the stack is empty, it returns -1.
10+
4. top :
11+
It returns the element being kept at the top of the stack. In case the stack is empty, it returns -1.
12+
5. size() :
13+
It returns the size of the stack at any given instance of time.
14+
6. isEmpty() :
15+
It returns a boolean value indicating whether the stack is empty or not.
16+
Operations Performed on the Stack:
17+
Query-1(Denoted by an integer 1): Pushes an integer data to the stack.
18+
19+
Query-2(Denoted by an integer 2): Pops the data kept at the top of the stack and returns it to the caller.
20+
21+
Query-3(Denoted by an integer 3): Fetches and returns the data being kept at the top of the stack but doesn't remove it, unlike the pop function.
22+
23+
Query-4(Denoted by an integer 4): Returns the current size of the stack.
24+
25+
Query-5(Denoted by an integer 5): Returns a boolean value denoting whether the stack is empty or not.
26+
Input Format:
27+
The first line contains an integer 'q' which denotes the number of queries to be run against each operation in the stack.
28+
Then the test cases follow.
29+
30+
Every 'q' lines represent an operation that needs to be performed.
31+
32+
For the push operation, the input line will contain two integers separated by a single space, representing the type of the operation in integer and the integer data being pushed into the stack.
33+
34+
For the rest of the operations on the stack, the input line will contain only one integer value, representing the query being performed on the stack.
35+
Output Format:
36+
For Query-1, you do not need to return anything.
37+
For Query-2, prints the data being popped from the stack.
38+
For Query-3, prints the data kept on the top of the stack.
39+
For Query-4, prints the current size of the stack.
40+
For Query-5, prints 'true' or 'false'(without quotes).
41+
42+
Output for every query will be printed in a separate line.
43+
Note:
44+
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
45+
Constraints:
46+
1 <= q <= 10^5
47+
1 <= x <= 5
48+
-2^31 <= data <= 2^31 - 1 and data != -1
49+
50+
Where 'q' is the total number of queries being performed on the stack, 'x' is the range for every query and data represents the integer pushed into the stack.
51+
52+
Time Limit: 1 second
53+
Sample Input 1:
54+
6
55+
1 13
56+
1 47
57+
4
58+
5
59+
2
60+
3
61+
Sample Output 1:
62+
2
63+
false
64+
47
65+
13
66+
Sample Input 2:
67+
4
68+
5
69+
2
70+
1 10
71+
5
72+
Sample Output 2:
73+
true
74+
-1
75+
false
76+
Explanation of Sample Input 2:
77+
There are 4 queries in total.
78+
The first one is Query-5: It tells whether the stack is empty or not. Since the stack is empty at this point, the output is 'true'.
79+
80+
The second one is Query-2: It pops the data from the stack. Since at this point in time, no data exist in the stack hence, it prints -1.
81+
82+
The third one is Query-1: It pushes the specified data 10 into the stack and since the function doesn't return anything, nothing is printed.
83+
84+
The fourth one is Query-5: It tells whether the stack is empty at this point or not. Since the stack has one element and hence it is not empty, false is printed.
85+
86+
***********************************************************Code********************************************************
87+
88+
public class Stack {
89+
90+
//Define the data members
91+
private Node head;
92+
private int size;
93+
94+
95+
public Stack() {
96+
//Implement the Constructor
97+
head=null;
98+
size=0;
99+
}
100+
101+
102+
103+
/*----------------- Public Functions of Stack -----------------*/
104+
105+
106+
public int getSize() {
107+
return size;
108+
//Implement the getSize() function
109+
}
110+
111+
public boolean isEmpty() {
112+
//Implement the isEmpty() function
113+
return size==0;
114+
}
115+
116+
public void push(int element) {
117+
//Implement the push(element) function
118+
Node newNode=new Node(element);
119+
if (head==null)
120+
{
121+
head=newNode;
122+
}
123+
else
124+
{
125+
newNode.next=head;
126+
head=newNode;
127+
}
128+
size++;
129+
}
130+
131+
public int pop() {
132+
//Implement the pop() function
133+
if(head==null)
134+
return -1;
135+
int temp=head.data;
136+
head=head.next;
137+
size--;
138+
return temp;
139+
}
140+
141+
public int top() {
142+
//Implement the top() function
143+
if(head==null)
144+
return -1;
145+
return head.data;
146+
}
147+
}

0 commit comments

Comments
 (0)