0% found this document useful (0 votes)
11 views

Stack Queue

The document contains code for various linked list operations like reversing a linked list, printing it in reverse order, making it circular, and detecting cycles. It also contains code for a stack implementation using an array and checking balanced brackets. Random number matrix generation and finding the maximum path sum is also shown.

Uploaded by

Aaqib Rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Stack Queue

The document contains code for various linked list operations like reversing a linked list, printing it in reverse order, making it circular, and detecting cycles. It also contains code for a stack implementation using an array and checking balanced brackets. Random number matrix generation and finding the maximum path sum is also shown.

Uploaded by

Aaqib Rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Task 1

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 insertNode(int index,int x){

if(index<0) return null;


int currIndex=1;

Node currNode=head;

while(currNode!=null && currIndex<index-1){

currNode=currNode.next;

currIndex++;

if(index>1 && currNode==null) return null;

Node newNode =new Node(x);

if(index==1){

newNode.next=head;

head=newNode;

count++;

else{

newNode.next=currNode.next;

currNode.next=newNode;

count++;

return newNode;

boolean deleteNode(int x){

Node prevNode=null;

Node currNode=head;

int currIndex=1;

while(currNode!=null && currNode.data!=x){


prevNode=currNode;

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+" ");

while(ptr!=null && ptr!=head){

System.out.println(ptr.data+" ");

ptr=ptr.next;

}
class Task1{

//This method will reverse the list permanently

public Node makeReverse(Node head){

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;

//This method only print list in reverse order

//I have two approaches whether i implement it by using stack pop method or by recursively.

//I selected print in recursively order because it is reduced my code.

public void printReverse(Node currNode){

if(currNode==null)

return;

printReverse(currNode.next);

System.out.print(currNode.data+"-> ");

//This method makes a list circular


public Node makeCircular(Node head){

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.

boolean detectCycle(Node head){

Node slow,fast;

slow=fast=head;

do{

slow=slow.next;

fast=fast.next.next;

}while(fast!=slow && fast!=null);

if(fast==slow && fast!=null)

return true;

else

return false;

public static void main(String[] args) {

List l1=new List();

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();

Task1 list1=new Task1();

// l1.head=list1.makeReverse(l1.head);

// System.out.println("After reversal of list");

// l1.listTraversal();

// System.out.println("Print list in reverse order: ");

// list1.printReverse(l1.head);

l1.head=list1.makeCircular(l1.head);

l1.listTraversal();

if(list1.detectCycle(l1.head)==true){

System.out.println("The list contains a cycle");

else{

System.out.println("The list does not contain a cycle");

Task 2

class Stack{

private char arr[];

private int capacity;

private int top;

//Constructor to initialize stack


Stack(int size){

arr=new char[size];

capacity=size;

top=-1;

// Utilize function to add an element x in the stack and check for stack overflow

public void push(char x){

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

public char pop(){

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

public int peek(){

return arr[top];

//Utilize function to return the size of the stack

public int size(){

return top+1;

//Utilize function to check if the stack is empty or not

public boolean isEmpty(){

if(top==-1){

return true;

return false;

//Utilize function to check if the stack is full or not

public boolean isFull(){

if(top+1==capacity){

return true;

return false;

class Task4 {
static boolean balanceBrackets(Stack s,char[] exp){

char popped;

for(int i=0; i<exp.length;i++){

if(exp[i]=='{' || exp[i]=='(' || exp[i]=='['){

s.push(exp[i]);

else if(exp[i]=='}' || exp[i]==')' || 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;

static boolean isMatch(char x,char y){

if(x=='(' && y==')')

return true;

else if(x=='[' && y==']')


return true;

else if(x=='{' && y=='}')

return true;

else

return false;

public static void main(String[] args) {

Stack s1=new Stack(20);

String exp="((8){(9-8)})";

char[] ch=new char[exp.length()];

for(int i=0;i<exp.length();i++){

ch[i]=exp.charAt(i);

if(!balanceBrackets(s1, ch)){

System.out.println("Brackets are not balanced");

else{

System.out.println("Brackets are balanced");

Task 3

class Task5 {

static char firstSingleLetter(final char[] text,final int n){


for(int i=0;i<n;i++){

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;

public static void main(String[] args) {

String text="ACCESS";

char[] word=text.toCharArray();

System.out.println("First letter that occurs only once is: "+firstSingleLetter(word, word.length));


}

Task 4

import java.util.*;

public class Task6 {

public static void main(String args[]) {

Random r = new Random();

int x;

int rows, cols;

rows = 7;

cols = 7;

int array [][] = new int[rows][cols];

for(int i=0; i<rows; i++)

for(int j=0; j<cols; j++)

array[i][j] = 1 + r.nextInt(50);

for(int i=0; i<rows; i++){

for(int j=0; j<cols; j++){

System.out.print(array[i][j]+ "\t");

System.out.println("\n");

int i, j, maxR, maxC, max, sum, prevR, prevC;


i = j = maxC = maxR = prevR = prevC = 0;

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];

if(i-1 > 0 && i-1 != prevR && array[i-1][j] > max){

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];

if(j-1 > 0 && j-1 != prevC && array[i][j-1] > max){

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;

System.out.print(max + " ");

System.out.println("Sum of number we crossed is "+ sum);

You might also like