JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Implement stack using Linked List in java data structure and algorithm in data structure and algorithm

1:35 AM
Implement stack using Linked List in java data structure and algorithm in data structure and algorithm The stack is an abstract data type that demonstrates Last in first out (LIFO) behavior. We will implement the same behavior using Linked List. There are two most important operations of Stack: Push: We will push elements to the beginning of the linked list to demonstrate the push behavior of the stack. Pop: We will remove the first element of linked list to demonstrate the pop behavior of Stack. Java Program: Let's create a java program to create a stack using Linked List. package org.arpit.java2blog; public class LinkedListStack { private Node head; // the first node // nest class to define linkedlist node private class Node { int value; Node next; } public LinkedListStack() { head = null; } // Remove value from the beginning of the list for demonstrating behaviour of stack public int pop() throws LinkedListEmptyException { if (head

Stack implementation in java data structure and algorithm

1:16 AM
Stack is abstract data type which demonstrates Last in first out (LIFO) behavior. We will implement same behavior using Array. Although java provides implementation for all abstract data types such as Stack,Queue and LinkedList but it is always good idea to understand basic data structures and implement them yourself. Java Program to implement Stack using Array: package org.arpit.java2blog; /**  * @author java Gian  */ public class StackCustom { int size; int arr[]; int top; StackCustom(int size) { this.size = size; this.arr = new int[size]; this.top = -1; } public void push(int pushedElement) { if (!isFull()) { top++; arr[top] = pushedElement; System.out.println("Pushed element:" + pushedElement); } else { System.out.println("Stack is full !"); } } public int pop() { if (!isEmpty()) { int returnedTop = top; top--; System.out.println("Popped element :" + arr[returnedTop]);

.