0% found this document useful (0 votes)
26 views12 pages

Stack Using Arrays

Uploaded by

Backroom Hero
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)
26 views12 pages

Stack Using Arrays

Uploaded by

Backroom Hero
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

 Stack Using Arrays:

public class ArrayStack {


private int maxSize;
private int[] stackArray;
private int top;

// Constructor
public ArrayStack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}

// Push method to add an element to the stack


public void push(int value) {
if (isFull()) {
[Link]("Stack is full. Cannot push " + value);
} else {
top++;
stackArray[top] = value;
[Link](value + " pushed to stack");
}
}

// Pop method to remove and return the top element from the stack
public int pop() {
if (isEmpty()) {
[Link]("Stack is empty. Cannot pop.");
return -1;
} else {
int poppedValue = stackArray[top];
top--;
return poppedValue;
}
}

// Peek method to return the top element without removing it


public int peek() {
if (isEmpty()) {
[Link]("Stack is empty. Cannot peek.");
return -1;
} else {
return stackArray[top];
}
}
// Check if the stack is empty
public boolean isEmpty() {
return (top == -1);
}

// Check if the stack is full


public boolean isFull() {
return (top == maxSize - 1);
}

// Main method to test the ArrayStack


public static void main(String[] args) {
ArrayStack stack = new ArrayStack(5);

[Link](10);
[Link](20);
[Link](30);

[Link]("Top element: " + [Link]());

[Link]([Link]() + " popped from stack");


[Link]([Link]() + " popped from stack");

[Link](40);
[Link]("Top element: " + [Link]());
}
}
 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++)
[Link](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) {
[Link](q[i] + "--");
i = (i + 1) % maxsize;
c++;
}
}

}
 Priority Queue:
import [Link];

public class Priority_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;
for(int i=front;i<rear;i++) {
for (int j = front; j < rear; j++) {
if(q[j]>q[j+1])
{
int temp=q[j];
q[j]=q[j+1];
q[j+1]=temp;
}

}
}
}

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++)
[Link](q[i]+"--");
}

public class Queue_Main


{
public static void main(String args[])
{
int ch;
Scanner in=new Scanner([Link]);
Linear_queue obj=new Linear_queue ();
[Link]("Enter size of queue:");
int size=[Link]();
obj.create_queue(size);//user given size :stack
do
{
[Link]("[Link]\[Link]\[Link]\[Link]\n:");
ch=[Link]();
switch(ch)
{
case 1:
if(!obj.is_full())//if not full then take data
{
[Link]("Enter data to insert:");
int e=[Link]();
[Link](e);
[Link]("Data to enqueued");
}
else {
[Link]("Queue Full");
}
break;
case 2:
if(!obj.is_empty())//if not Empty then dequeue
{
int e=[Link]();
[Link]("Data dequeued:"+e);
}
else
{
[Link]("Queue Empty");
}
break;
case 3:
if(!obj.is_empty())//if not Empty then print
{
[Link]("Data in queue");
obj.print_queue();
}
else
{
[Link]("Queue Empty");
}
break;
case 0:
[Link]("Exiting.....");
break;
default:
[Link]("Wrong option selected");
break;
}
}while(ch!=0);
}

}
 LinkedList
public class Node
{
int data;
Node next;//self ref structure
Node(int data)
{
[Link]=data;
[Link]=null;//next=null;
}
}

public class Linear_linked_list


{
Node root;

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
{
[Link]=root;//1
root=n;//2
}
[Link](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([Link]!=null)//2
t=[Link];
[Link]=n;//3
}
[Link](e+" inserted");
}

void delete_left()
{
if(root==null)//not there then n is root
[Link]("Empty list");
else
{
Node t=root;//1
root=[Link];//2
[Link]([Link]+" deleted");
}
}
void delete_right()
{
if(root==null)//not there then n is root
[Link]("Empty list");
else
{
Node t=root;//1
Node t2=t;//1
while([Link]!=null)//2
{
t2=t;
t=[Link];
}
if (t==root)//one node only
root=null;
else
[Link] = null;//3

[Link]([Link]+" deleted");
}
}
void print_list()
{ if(root==null)
[Link]("Empty list");
else
{
Node t=root;//1
while(t!=null)
{
[Link]("|"+[Link]+"|->");
t=[Link];
}
}
}
void search_list(int key)
{ if(root==null)
[Link]("Empty list");
else
{
Node t=root;//1
while(t!=null)
{
if([Link]==key)
break;
t=[Link];
}
if(t==null)
[Link](key+" Not Found");
else
[Link](key+" Found");
}
}
}
public class Linked_List_Main
{
public static void main(String args[])
{
int ch,e;
Scanner in = new Scanner([Link]);
Linear_linked_list obj = new Linear_linked_list();
obj.create_list();//user given size :list
do {
[Link]("\[Link] Left\[Link] Right\[Link]
Left\[Link] Right\[Link] List\[Link]\[Link]\n:");
ch = [Link]();
switch (ch) {
case 1:
[Link]("Enter data:");
e = [Link]();
obj.insert_left(e);
break;
case 2:
[Link]("Enter data:");
e = [Link]();
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:
[Link]("Enter data:");
e = [Link]();
obj.search_list(e);
break;
case 0:
[Link]("Exiting.....");
break;
default:
[Link]("Wrong option selected");
break;
}
} while (ch != 0);
}
}

You might also like