Stack Using Linked List
Stack Using Linked List
Stack Using Linked List
Stack using
linked list
import java.io.*;
class Stack1
{
Stack1 top,next,prev;
int data;
Stack1()
{
data=0;
next=prev=null;
}
Stack1(int d)
{
data=d;
next=prev=null;
}
void push(int n)
{
Stack1 nn;
nn=new Stack1(n);
if(top==null)
top=nn;
else
{
nn.next=top;
top.prev=nn;
top=nn;
}
}
int pop()
{
int k=top.data;
if(top.next==null)
{
top=null;
return k;
}
else
{
top=top.next;
top.prev=null;
return k;
}
}
boolean isEmpty()
{
if(top==null)
return true;
else
return false;
}
void display()
{
Stack1 ptr;
for(ptr=top;ptr!=null;ptr=ptr.next)
System.out.print(ptr.data+" ");
}
public static void main(String args[ ])throws Exception
{
int x; int ch;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
Stack1 a=new Stack1();
do{
System.out.println("enter 1 for pushing");
System.out.println("enter 2 for poping");
System.out.println("enter 3 for isEmpty");
System.out.println("enter 4 for display");
System.out.println("Enter 0 for exit");
System.out.println("enter ur choice ");
ch=Integer.parseInt(b.readLine()); switch(ch)
{
case 1:System.out.println("enter element to insert");
int e=Integer.parseInt(b.readLine());
a.push(e);
break;
case 2:if(!a.isEmpty())
{
int p=a.pop();
System.out.println("deleted element is "+p);
}
else
{
System.out.println("stack is empty");
}
break;
case 3:System.out.println(a.isEmpty());
break;
case 4:if(!a.isEmpty())
{
a.display();
}
else
{
System.out.println("list is empty");
}
while(ch!=0);
}
}
Output:
2. //Queue using Linked list
import java.io.*;
class Qlnk
{
Qlnk front,rear,next;
int data;
Qlnk()
{
data=0;
next=null;
}
Qlnk(int d)
{
data=d;
next=null;
}
Qlnk getFront()
{
return front;
}
Qlnk getRear()
{
return rear;
}
void insertelm(int item)
{
Qlnk nn;
nn=new Qlnk(item);
if(isEmpty())
{
front=rear=nn;
}
else
{
rear.next=nn;
rear=nn;
}
}
int delelm()
{
if(isEmpty())
{
System.out.println("deletion failed");
return -1;
}
else
{
int k=front.data;
if(front!=rear)
front=front.next;
else rear=front=null;
return k;
}
}
boolean isEmpty()
{
if(rear==null)
return
true; else
return false;
}
int size()
{
Qlnk ptr;
int cnt=0;
for(ptr=front;ptr!=null;ptr=ptr.next)
cnt++;
return cnt;
}
void display()
{
Qlnk ptr; if(!
isEmpty())
{
for(ptr=front;ptr!=null;ptr=ptr.next)
System.out.print(ptr.data+" ");
}
else
System.out.println("q is empty");
}
public static void main(String arr[])throws Exception
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Qlnk m=new Qlnk();
int ch;
do
{
System.out.println("enter 1 for insert");
System.out.println("enter 2 for
deletion");
System.out.println("enter 3 for
getFront");
System.out.println("enter 4 for getRear");
System.out.println("enter 5 for size");
System.out.println("enter 6 for display");
System.out.println("enter 0 for exit");
System.out.println("enter ur choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("enter ele to insert");
int item=Integer.parseInt(br.readLine());
m.insertelm(item);
break;
case 2:
int k=m.delelm();
System.out.println("deleted ele is "+k);
break;
case 3:
System.out.println("front index is"+(m.getFront()).data);
break;
case 4:
System.out.println("rear index is"+(m.getRear()).data);
break;
case 5:
System.out.println("size is"+m.size());
break;
case 6:
m.display();break;
}
}while(ch!=0);
}
}
Output:
3. implementation of De-queue
(using circular array)
// A structure to represent a Deque
class Deque {
static final int MAX = 100;
int arr[];
int front;
int rear;
int size;
public Deque(int size)
{
arr = new int[MAX];
front = -1;
rear = 0;
this.size = size;
}
/*// Operations on Deque:
void insertfront(int key);
void insertrear(int key);
void deletefront();
void deleterear();
bool isFull();
bool isEmpty();
int getFront();
int getRear();*/
// Checks whether Deque is full or not.
boolean isFull()
{
return ((front == 0 && rear == size - 1)
|| front == rear + 1);
}
// Checks whether Deque is empty or not.
boolean isEmpty() { return (front == -1); }
// Inserts an element at front
void insertfront(int key)
{
// check whether Deque if full or not
if (isFull()) {
System.out.println("Overflow");
return;
}
// If queue is initially empty
if (front == -1) {
front = 0;
rear = 0;
}
// front is at first position of queue
else if (front == 0)
front = size - 1;
else // decrement front end by '1'
front = front - 1;
// insert current element into Deque
arr[front] = key;
}
// function to inset element at rear end
// of Deque.
void insertrear(int key)
{
if (isFull()) {
System.out.println(" Overflow ");
return;
}
// If queue is initially empty
if (front == -1) {
front = 0;
rear = 0;
}
// rear is at last position of queue
else if (rear == size - 1)
rear = 0;
// increment rear end by '1'
else
rear = rear + 1;
// insert current element into Deque
arr[rear] = key;
}
// Deletes element at front end of Deque
void deletefront()
{
// check whether Deque is empty or not
if (isEmpty()) {
System.out.println("Queue Underflow\n");
return;
}
// Deque has only one element
if (front == rear) {
front = -1;
rear = -1;
}
else
// back to initial position
if (front == size - 1)
front = 0;
else // increment front by '1' to remove current
// front value from Deque
front = front + 1;
}
// Delete element at rear end of Deque
void deleterear()
{
if (isEmpty()) {
System.out.println(" Underflow");
return;
}
// Deque has only one element
if (front == rear) {
front = -1;
rear = -1;
}
else if (rear == 0)
rear = size - 1;
else
rear = rear - 1;
}
// Returns front element of Deque
int getFront()
{
// check whether Deque is empty or not
if (isEmpty()) {
System.out.println(" Underflow");
return -1;
}
return arr[front];
}
// function return rear element of Deque
int getRear()
{
// check whether Deque is empty or not
if (isEmpty() || rear < 0) {
System.out.println(" Underflow\n");
return -1;
}
return arr[rear];
}
// Driver code
public static void main(String[] args)
{
Deque dq = new Deque(5);
// Function calls
System.out.println(
"Insert element at rear end : 5 ");
dq.insertrear(5);
System.out.println(
"insert element at rear end : 10 ");
dq.insertrear(10);
System.out.println("get rear element : "
+ dq.getRear());
dq.deleterear();
System.out.println(
"After delete rear element new rear become : "
+ dq.getRear());
System.out.println(
"inserting element at front end");
dq.insertfront(15);
System.out.println("get front element: "
+ dq.getFront());
dq.deletefront();
System.out.println(
"After delete front element new front become : "
+ +dq.getFront());
}
}
Output
Insert element at rear end : 5
insert element at rear end : 10
get rear element 10
After delete rear element new rear become 5
inserting element at front end
get front element 15
After delete front element new front become 5
import java.util.*;
// Structure for the elements in the
// priority queue
class item {
public int value;
public int priority;
};
class GFG {
// Store the element of a priority queue
static item[] pr = new item[100000];
// Pointer to the last index
static int size = -1;
// Function to insert a new element
// into priority queue
static void enqueue(int value, int priority)
{
// Increase the size
size++;
// Insert the element
pr[size] = new item();
pr[size].value = value;
pr[size].priority = priority;
}
// Function to check the top element
static int peek()
{
int highestPriority = Integer.MIN_VALUE;
int ind = -1;
// Check for the element with
// highest priority
for (int i = 0; i <= size; i++) {
// If priority is same choose
// the element with the
// highest value
if (highestPriority == pr[i].priority
&& ind > -1
&& pr[ind].value < pr[i].value) {
highestPriority = pr[i].priority;
ind = i;
}
else if (highestPriority < pr[i].priority) {
highestPriority = pr[i].priority;
ind = i;
}
}
// Return position of the element
return ind;
}
// Function to remove the element with
// the highest priority
static void dequeue()
{
// Find the position of the element
// with highest priority
int ind = peek();
// Shift the element one index before
// from the position of the element
// with highest priority is found
for (int i = ind; i < size; i++) {
pr[i] = pr[i + 1];
}
// Decrease the size of the
// priority queue by one
size--;
}
public static void main(String[] args)
{
// Function Call to insert elements
// as per the priority
enqueue(10, 2);
enqueue(14, 4);
enqueue(16, 4);
enqueue(12, 3);
// Stores the top element
// at the moment
int ind = peek();
System.out.println(pr[ind].value);
// Dequeue the top element
dequeue();
// Check the top element
ind = peek();
System.out.println(pr[ind].value);
// Dequeue the top element
dequeue();
// Check the top element
ind = peek();
System.out.println(pr[ind].value);
}
}
Output
16
14
12