Stack Queue
Stack Queue
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
class List{
Node head;
int count=0;
List(){
head= null;
boolean isEmpty(){
if(head!=null)
return false;
else
return true;
Node currNode=head;
currNode=currNode.next;
currIndex++;
if(index==1){
newNode.next=head;
head=newNode;
count++;
else{
newNode.next=currNode.next;
currNode.next=newNode;
count++;
return newNode;
Node prevNode=null;
Node currNode=head;
int currIndex=1;
currNode=currNode.next;
currIndex++;
if(currNode!=null){
if(prevNode==null){
head=currNode.next;
count--;
else{
prevNode.next=currNode.next;
currNode=null;
count--;
return true;
return false;
void listTraversal(){
Node ptr=head.next;
System.out.println(head.data+" ");
System.out.println(ptr.data+" ");
ptr=ptr.next;
}
class Task1{
Node current,prev,next;
current=head;
prev=null;
while(current!=null){
next=current.next;
current.next=prev;
prev=current;
current=next;
head=prev;
return head;
//I have two approaches whether i implement it by using stack pop method or by recursively.
if(currNode==null)
return;
printReverse(currNode.next);
System.out.print(currNode.data+"-> ");
Node curr=head;
while(curr.next!=null){
curr=curr.next;
curr.next=head;
return head;
//This method returns true if list contains a loop or circular otherwise return false.
Node slow,fast;
slow=fast=head;
do{
slow=slow.next;
fast=fast.next.next;
return true;
else
return false;
l1.insertNode(1, 4);
l1.insertNode(2, 8);
l1.insertNode(3, 12);
l1.insertNode(2, 6);
System.out.println("Before reversal of list");
l1.listTraversal();
// l1.head=list1.makeReverse(l1.head);
// l1.listTraversal();
// list1.printReverse(l1.head);
l1.head=list1.makeCircular(l1.head);
l1.listTraversal();
if(list1.detectCycle(l1.head)==true){
else{
Task 2
class Stack{
arr=new char[size];
capacity=size;
top=-1;
// Utilize function to add an element x in the stack and check for stack overflow
if(!isFull()){
arr[++top]=x;
// System.out.println("Inserting "+x);
else{
System.out.println("Stack is overflow");
//Utility function to pop top elemnt from the stack and check for stack underflow
if(!isEmpty()){
// System.out.println("Removing "+arr[top]);
return arr[top--];
else{
System.out.println("Stack is underflow");
return 0;
}
//Utilize function to return top element in a stack
return arr[top];
return top+1;
if(top==-1){
return true;
return false;
if(top+1==capacity){
return true;
return false;
class Task4 {
static boolean balanceBrackets(Stack s,char[] exp){
char popped;
s.push(exp[i]);
if(s.isEmpty()){
return false;
popped=s.pop();
if(!isMatch(popped,exp[i])){
return false;
if(s.isEmpty()){
return true;
else{
return false;
return true;
return true;
else
return false;
String exp="((8){(9-8)})";
for(int i=0;i<exp.length();i++){
ch[i]=exp.charAt(i);
if(!balanceBrackets(s1, ch)){
else{
Task 3
class Task5 {
int count=0;
int j=i+1;
while(j<n){
if(text[i]==text[j]){
count++;
j++;
j=i-1;
while(j>=0){
if(text[i]==text[j]){
count++;
j--;
if(count==0){
return text[i];
System.out.println("Outside loop");
return 0;
String text="ACCESS";
char[] word=text.toCharArray();
Task 4
import java.util.*;
int x;
rows = 7;
cols = 7;
array[i][j] = 1 + r.nextInt(50);
System.out.print(array[i][j]+ "\t");
System.out.println("\n");
sum = array[0][0];
System.out.print("Print the path followed to reach the target \n" + sum + " ");
while(i<rows-1 || j<cols-1) {
max = 0;
if(i-1 > 0 && j-1 > 0 && i-1 != prevR && j-1 != prevC && array[i-1][j-1] > max){
maxR = i-1;
maxC = j-1;
max = array[maxR][maxC];
maxR = i-1;
maxC = j;
max = array[maxR][maxC];
if(i-1 > 0 && j+1 < cols && i-1 != prevR && j+1 != prevC && array[i-1][j+1] > max)
{
maxR = i-1;
maxC = j+1;
max = array[maxR][maxC];
maxR = i;
maxC = j-1;
max = array[maxR][maxC];
if(j+1 < cols && j+1 != prevC && array[i][j+1] > max){
maxR = i;
maxC = j+1;
max = array[maxR][maxC];
if(i+1 < rows && i+1 != prevR && array[i+1][j] > max){
maxR = i+1;
maxC = j;
max = array[maxR][maxC];
if(i+1 < rows && i+1 != prevR && j-1 > -1 && j-1 != prevC && array[i+1][j] > max){
maxR = i+1;
maxC = j-1;
max = array[maxR][maxC];
if(i+1 < rows && j+1 < cols && i+1 != prevR && j+1 != prevC && array[i+1][j+1] >
max){
maxR = i+1;
maxC = j+1;
max = array[maxR][maxC];
i = prevR = maxR;
j = prevC = maxC;
sum += max;