Data structure lab program
Data structure lab program
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push(int x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
int pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}
int peek()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
}
}
void print(){
for(int i = top;i>-1;i--){
System.out.print(" "+ a[i]);
}
}
}
class StackArray {
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(" STACK USING ARRAYS\n\n");
System.out.println(s.pop() + " Popped from stack");
System.out.println("Top element is :" + s.peek());
System.out.print("Elements present in stack :");
s.print();
}
}
OUTPUT
STACK USING ARRAYS
class Queue {
static private int front, rear, capacity;
static private int queue[];
Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
class StaticQueueinjava {
public static void main(String[] args)
{
Queue q = new Queue(4);
System.out.printf("\n QUEUE USING ARRAYS \ n\n");
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf("\n\nafter two node deletion\n\n");
q.queueDisplay();
q.queueFront();
}
}
OUTPUT
Queue is Empty
20 <-- 30 <-- 40 <-- 50 <--
Queue is full
20 <-- 30 <-- 40 <-- 50 <--
20 deleted
30 deleted
after two node deletion
40 <-- 50 <--
Front Element is: 40
3. STACK USING SINGLY LINKED LIST
import static java.lang.System.exit;
class StackLink {
public static void main(String[] args)
{
StackUsingLinkedlist obj= new StackUsingLinkedlist();
obj.push(11);
obj.push(22);
obj.push(33);
obj.push(44);
System.out.printf("STACK USING LINKED LIST");
obj.display();
obj.pop();
obj.pop();
obj.display();
Node top;
if (temp == null) {
System.out.print("\nHeap Overflow");
return;
}
temp.data = x;
temp.link = top;
top = temp;
}
class Node {
int data;
Node next;
class Queue {
Node front, rear;
int length;
public Queue() {
this.front = this.rear = null;
this.length=0;
}
if (this.rear == null) {
this.front = this.rear = temp;
return;
}
this.rear.next = temp;
this.rear = temp;
}
void deque() {
if (this.front == null)
return;
if (this.front == null)
this.rear = null;
temp.next = null;
}
int peek() {
if (this.front != null)
return this.front.data;
return Integer.MIN_VALUE;
}
int size(){
return this.length;
}
void printQueue(){
Node temp=this.front;
System.out.print("Element of Queue : ");
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.println();
}
}
q.printQueue();
System.out.println("Size :" + q.size());
q.deque();
q.deque();
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.printQueue();
System.out.println("Size :" + q.size());
System.out.println("Front item is: " + q.peek());
}
}
OUTPUT
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
result.append(c);
}
else if (c == '(') {
stack.push(c);
}
else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
result.append(stack.pop());
}
stack.pop(); // Pop '('
}
else {
while (!stack.isEmpty() && (prec(s.charAt(i)) < prec(stack.peek()) ||
prec(s.charAt(i)) == prec(stack.peek()) &&
associativity(s.charAt(i)) == 'L')) {
result.append(stack.pop());
}
stack.push(c);
}
}
while (!stack.isEmpty()) {
result.append(stack.pop());
}
System.out.println(result);
}
abcd^e-fgh*+^*+i-
6. BINARY TREE TRAVERSAL(INORDER,PREORDER,POSTORDER)
import java.io.*;
class Node {
int data;
Node left;
Node right;
Node(int v)
{
this.data = v;
this.left = this.right = null;
}
}
class BTT{
// Inorder Traversal
public static void printInorder(Node node)
{
if (node == null)
return;
// Visit node
System.out.print(node.data + " ");
// Visit node
System.out.print(node.data + " ");
// Visit node
System.out.print(node.data + " ");
}
// Function call
System.out.print("Inorder Traversal: ");
printInorder(root);
System.out.print("\nPreorder Traversal: ");
printPreorder(root);
System.out.print("\nPostorder Traversal: ");
printPostorder(root);
}
}
OUTPUT
OUTPUT
LINEAR SEARCH
50 is found at index: 3
7. BINARY SEARCH
class BinarySearch {
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int mid = (l + r) / 2;
if (arr[mid] == x)
{
return mid;
} else if (arr[mid] > x)
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
return -1;
}
OUTPUT
BINARY SEARCH
import java.io.*;
import java.lang.*;
import java.util.*;
class ShortestPath {
static final int V = 9;
int minDistance(int dist[], Boolean sptSet[])
{
return min_index;
}
dist[src] = 0;
sptSet[u] = true;
for (int v = 0; v < V; v++)
// Function call
t.dijkstra(graph, 0);
}
}
OUTPUT