Please open the menu to show more

Monday, July 31, 2017

How to implement stack in java by using array, a code by vasu

Stack is very important concept in computer science.
It is a liner data structure, that do supports a LIFO behavior (Last In First Out). This is widely used in IT industry. 


This very good code of stack is implemented by 
Vasu Gupta / IT / Maharaja Agrasen Institute of Technology

In this code the stack is implemented using array. I will also update stack implementation using linked list.
# save this class inside Stack.java file #

import java.util.Scanner;

// Create a class Stack consisting of main method in it
public class Stack {

// Declare some static variables
        // so they can be accessed from anywhere
static int max = 5 , top = -1 ;
static int[] stack = new int[max];
static int data = 0 ;
public static void main(String[] args) {
// Create Scanner class object
Scanner sc = new Scanner(System.in);
// Get user input data again and again
do {
System.out.print("Enter the data : ");
data = sc.nextInt();
push(data); // Push the data into the stack
System.out.print("Do you want to add more (y/n) : ");
}while(sc.next().equals("y"));
// Show the stack data : 
System.out.print("Stack : ");
showData();
// Pop the top element out of the stack
System.out.print("Poping one element out : ");
pop();
showData();
// Show the top element of the stack
peek();
}
//  method push() so that it can be accessed in the static main method
public static void push(int data) {
// Check if stack is full
if(top == max-1) {
System.out.println("Stack is Full");
}else { // Else push the data
top++ ;
stack[top] = data ;
}
}
// method pop()  to delete the top element in the stack
public static void pop() {
// Check if stack is empty
if(top == 0) {
System.out.println("Stack Empty");
}else { // Else pop the data
stack[top] = 0  ;
top-- ; 
}
}
// method peek() to fetch the top element from the stack
public static void peek() {
// Check if stack is empty
if(top == 0) {
System.out.println("Stack Empty");
}else { // Else print the top element of the stack
System.out.println("Top Element : " + stack[top]);
}
}
// method showData() to show the data of the stack
public static void showData() {
for (int i = 0; i < top; i++) { // Traverse until top
System.out.print(stack[i] + "-->");
}
System.out.println(stack[top]);
}
}

Saturday, July 29, 2017

Who says java does not supports pointers (part-2), see, how to implement a Doubly Linked List in Java

This very use full cide is implemented by
Vasu Gupta / IT / Maharaja Agrasen Institute of technology

# in this code a doubly linked list has been implemented

# save this class inside Node.java file #


import java.util.Scanner;

//Create a node class for creating new nodes in linked list
class Node{
private int data ; // data of that node
private Node next ; // node data of next linked node
private Node prev ; // node data of previous linked node
 
public Node() {  // initialize new node
data = 0 ;
next = null ;
prev = null ;
}
// Input data in new node
public Node(int d) {
data = d ;
}
// set link to the next node
public void setNextLink(Node link) {
next = link ;
}
// set link to the previous node
public void setPrevLink(Node link) {
prev = link ;
}
// Get link of the next node
public Node getNextLink() {
return next ;
}
// Get link of the previous node
public Node getPrevLink() {
return prev ;
}
// Get data of the current node
public int getData() {
return data ;
}
}

# save this class inside LinkList.java file #

//class for creating linked list
public class LinkList{
private Node start ;
private Node end ;
public int size ;
// Initialize all the variables
public LinkList() {
start = null ;
end = null ;
size = 0 ;
}
// Adding nodes at starting of the linked list
public void addAtStart(int data) {
Node node = new Node(data); // Create object of Node class
size++ ; // Increment size of linked list
if(start == null) {
start = node ; // make start the current node
start.setNextLink(null);
start.setPrevLink(null);
end = start ; // make node the ending node
}else {
start.setPrevLink(node); // Attach new node before start
node.setNextLink(start); // Set start to the next of the node
node.setPrevLink(null);
start = node ; // make the new node as start
}
}
public void addAtEnd(int data) {
Node node = new Node(data);
size++ ;
if(start == null) {
start = node ;
start.setNextLink(null);
start.setPrevLink(null);
end = start ;
}else {
end.setNextLink(node); // Attach new node after end
node.setPrevLink(null);
node.setPrevLink(end); // Set end to the previous of the node
end = node ; // make the new node as end
}
}
// Adding node at the middle of the linked list
public void addAtMiddle(int data , int pos) {
Node node = new Node(data);
size++ ;
Node preptr = start ;
Node ptr = start ;
ptr = ptr.getNextLink();
int count = 1 ;
while(count != pos) {
preptr = preptr.getNextLink();
ptr = ptr.getNextLink();
count++ ;
}
preptr.setNextLink(node);
node.setPrevLink(preptr);
node.setNextLink(ptr);
ptr.setPrevLink(node);
}
// Adding node after a certain data of the linked list
public void addAfterData(int data , int refData) {
Node node = new Node(data);
size++ ;
Node preptr = start ;
Node ptr = start ;
ptr = ptr.getNextLink();
while(preptr.getData() != refData) {
preptr = preptr.getNextLink();
ptr = ptr.getNextLink();
}
preptr.setNextLink(node);
node.setPrevLink(preptr);
node.setNextLink(ptr);
ptr.setPrevLink(node);
}
// Deleting node in linked list
public void deleteNode(int refData) {
Node preptr = start ;
Node ptr = start ;
ptr = ptr.getNextLink();
while(ptr.getData() != refData) {
preptr = preptr.getNextLink();
ptr = ptr.getNextLink();
}
preptr.setNextLink(ptr.getNextLink());
ptr.getNextLink().setPrevLink(preptr);
ptr = null ;
size-- ; // Reduce size of linked list
}
// Traversing data from left to right 
public void showDataFromLeft() {
Node ptr = start ;
while(ptr.getNextLink()!=null) {
System.out.print(ptr.getData() + "-->");
ptr = ptr.getNextLink();
}
System.out.print(ptr.getData());
}
// Traversing data from right to lest 
public void showDataFromRight() {
Node ptr = end ;
while(ptr.getPrevLink()!=null) {
System.out.print(ptr.getData() + "-->");
ptr = ptr.getPrevLink();
}
System.out.print(ptr.getData());
}
}

# save this class inside DoublyLinkedList .java file #

public class DoublyLinkedList {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int data = 0 ;
LinkList ll = new LinkList();
System.out.println("Choose : \n1. Insert at Start \n2. Insert At End \n3. Use already linklist");
switch (sc.nextInt()) {
case 1:
// Get data from user again and again and add them to linked list
do {
System.out.print("Enter the data : ");
data = sc.nextInt();
ll.addAtStart(data);
System.out.print("Do you want to add more (y/n) : ");
}while(sc.next().equals("y"));
System.out.println("From left to right : ");
ll.showDataFromLeft(); // show data from left to right of linked list
System.out.println("\nFrom right to left : ");
ll.showDataFromRight(); // show data from right to left of linked list
break;
case 2:
do {
System.out.print("Enter the data : ");
data = sc.nextInt();
ll.addAtEnd(data);
System.out.print("Do you want to add more (y/n) : ");
}while(sc.next().equals("y"));
System.out.println("From left to right : ");
ll.showDataFromLeft();
System.out.println("\nFrom right to left : ");
ll.showDataFromRight();
break;
case 3 : 
ll.addAtEnd(5);
ll.addAtEnd(10);
ll.addAtEnd(15);
ll.addAtEnd(20);
ll.addAtEnd(25);
System.out.println("From left to right : ");
ll.showDataFromLeft();
System.out.println("\nFrom right to left : ");
ll.showDataFromRight();
break ;

default:
break;
}
System.out.println("\nChoose : \n1. Insert At Middle \n2. Insert After A Data \n3. Delete A Node");
switch (sc.nextInt()) {
case 1:
System.out.print("Enter the data : ");
data = sc.nextInt();
int pos = 0 ;
if(ll.size%2 == 0) {
pos = ll.size/2 ;
}else {
pos = (ll.size - 1)/2 ;
}
ll.addAtMiddle(data , pos);
// Calculating middle position
System.out.println("From left to right : ");
ll.showDataFromLeft();
System.out.println("\nFrom right to left : ");
ll.showDataFromRight();
break;
case 2:
System.out.print("Enter the data : ");
data = sc.nextInt();
System.out.print("Enter the data after which you want to add new data : ");
ll.addAfterData(data , sc.nextInt());
System.out.println("From left to right : ");
ll.showDataFromLeft();
System.out.println("\nFrom right to left : ");
ll.showDataFromRight();
break;
case 3:
System.out.print("Enter the data to be deleted : ");
data = sc.nextInt();
ll.deleteNode(data); // Delete node
System.out.println("From left to right : ");
ll.showDataFromLeft();
System.out.println("\nFrom right to left : ");
ll.showDataFromRight();
break;

default:
break;
}

}

}