Stack Using Arrays
Stack Using Arrays
// Constructor
public ArrayStack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
// Pop method to remove and return the top element from the stack
public int pop() {
if (isEmpty()) {
System.out.println("Stack is empty. Cannot pop.");
return -1;
} else {
int poppedValue = stackArray[top];
top--;
return poppedValue;
}
}
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
System.out.println("Top element: " + stack.peek());
}
}
Queue
public class Linear_queue
{
int q[],maxsize,front,rear;
void create_queue(int size)
{
maxsize=size;
front=0;
rear=-1;
q=new int[maxsize];
}
void enqueue(int e)
{
//q[++rear]=e;
rear++;
q[rear]=e;
}
boolean is_full()
{
if(rear==maxsize-1)
return true;
else
return false;
}
int dequeue()
{
int temp=q[front];
front++;
return temp;
}
boolean is_empty()
{
if(front>rear)
return true;
else
return false;
}
void print_queue()
{
for(int i=front;i<=rear;i++)
System.out.print(q[i]+"--");
}
}
Circular Queue:
public class Circular_queue
{
int q[],maxsize,front,rear,count;
void create_queue(int size)
{
maxsize=size;
front=0;
rear=-1;
count=0;
q=new int[maxsize];
}
void enqueue(int e)
{
//q[++rear]=e;
rear=(rear+1)%maxsize;
count++;
q[rear]=e;
}
boolean is_full()
{
if(count==maxsize)
return true;
else
return false;
}
int dequeue()
{
int temp=q[front];
front=(front+1)%maxsize;
count--;
return temp;
}
boolean is_empty()
{
if(count==0)
return true;
else
return false;
}
void print_queue() {
int c = 0;
int i = front;
while (c < count) {
System.out.print(q[i] + "--");
i = (i + 1) % maxsize;
c++;
}
}
}
Priority Queue:
import java.util.Scanner;
}
}
}
boolean is_full()
{
if(rear==maxsize-1)
return true;
else
return false;
}
int dequeue()
{
int temp=q[front];
front++;
return temp;
}
boolean is_empty()
{
if(front>rear)
return true;
else
return false;
}
void print_queue()
{
for(int i=front;i<=rear;i++)
System.out.print(q[i]+"--");
}
}
LinkedList
public class Node
{
int data;
Node next;//self ref structure
Node(int data)
{
this.data=data;
this.next=null;//next=null;
}
}
void create_list()
{
root=null;//root is not there
}
void insert_left(int e)
{
Node n=new Node(e);
if(root==null)//not there then n is root
root=n;
else
{
n.next=root;//1
root=n;//2
}
System.out.println(e+" inserted");
}
void insert_right(int e)
{
Node n=new Node(e);//created
if(root==null)//not there then n is root
root=n;
else
{
Node t=root;//1
while(t.next!=null)//2
t=t.next;
t.next=n;//3
}
System.out.println(e+" inserted");
}
void delete_left()
{
if(root==null)//not there then n is root
System.out.println("Empty list");
else
{
Node t=root;//1
root=root.next;//2
System.out.println(t.data+" deleted");
}
}
void delete_right()
{
if(root==null)//not there then n is root
System.out.println("Empty list");
else
{
Node t=root;//1
Node t2=t;//1
while(t.next!=null)//2
{
t2=t;
t=t.next;
}
if (t==root)//one node only
root=null;
else
t2.next = null;//3
System.out.println(t.data+" deleted");
}
}
void print_list()
{ if(root==null)
System.out.println("Empty list");
else
{
Node t=root;//1
while(t!=null)
{
System.out.print("|"+t.data+"|->");
t=t.next;
}
}
}
void search_list(int key)
{ if(root==null)
System.out.println("Empty list");
else
{
Node t=root;//1
while(t!=null)
{
if(t.data==key)
break;
t=t.next;
}
if(t==null)
System.out.println(key+" Not Found");
else
System.out.println(key+" Found");
}
}
}
public class Linked_List_Main
{
public static void main(String args[])
{
int ch,e;
Scanner in = new Scanner(System.in);
Linear_linked_list obj = new Linear_linked_list();
obj.create_list();//user given size :list
do {
System.out.println("\n1.Insert Left\n2.Insert Right\n3.Delete
Left\n4.Delete Right\n5.Print List\n6.Search\n0.Exit\n:");
ch = in.nextInt();
switch (ch) {
case 1:
System.out.println("Enter data:");
e = in.nextInt();
obj.insert_left(e);
break;
case 2:
System.out.println("Enter data:");
e = in.nextInt();
obj.insert_right(e);
break;
case 3:
obj.delete_left();
break;
case 4:
obj.delete_right();
break;
case 5:
obj.print_list();
break;
case 6:
System.out.println("Enter data:");
e = in.nextInt();
obj.search_list(e);
break;
case 0:
System.out.println("Exiting.....");
break;
default:
System.out.println("Wrong option selected");
break;
}
} while (ch != 0);
}
}