0% found this document useful (0 votes)
13 views8 pages

Add On

Uploaded by

daxelectro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Add On

Uploaded by

daxelectro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

import java.util.

Scanner;
class Node {
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}

public class QueueLinkedList {


Node front, rear;
QueueLinkedList() {
front = rear = null;
}
boolean isEmpty() {
return front == null;
}
void enqueue(int value) {
Node newNode = new Node(value);
if (rear == null) {
front = rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
System.out.println("Enqueued: " + value);
}

void dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty.");
} else {
System.out.println("Dequeued: " + front.data);
front = front.next;
if (front == null) {
rear = null;
}
}
}
void displayQueue() {
if (isEmpty()) {
System.out.println("Queue is empty.");
} else {
Node temp = front;
System.out.print("Queue: ");
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
}

public static void main(String[] args) {


QueueLinkedList queue = new QueueLinkedList();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.displayQueue();
queue.dequeue();
queue.displayQueue();
}
}

Output:
Algorithm :
1. Start.
2. Define a class Node with instance variables data and next.
3. Define a class QueueLinkedList with instance variables front and rear.
4. Implement the isEmpty() method to check if the queue is empty.
5. Define the enqueue() method to add an element to the queue.
6. Define the dequeue() method to remove an element from the queue.
7. Implement the displayQueue() method to display the elements of the queue.
8. In main(), create a queue.
9. Perform enqueue and dequeue operations.
10. Display the queue after operations.
import java.util.Scanner;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}

public class StackLinkedList {


Node top;
StackLinkedList() {
top = null;
}
boolean isEmpty() {
return top == null;
}

void push(int value) {


Node newNode = new Node(value);
newNode.next = top;
top = newNode;
System.out.println("Pushed: " + value);
}

void pop() {
if (isEmpty()) {
System.out.println("Stack is empty.");
} else {
System.out.println("Popped: " + top.data);
top = top.next;
}
}

void displayStack() {
if (isEmpty()) {
System.out.println("Stack is empty.");
} else {
Node temp = top;
System.out.print("Stack: ");
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
}

public static void main(String[] args) {


StackLinkedList stack = new StackLinkedList();
stack.push(10);
stack.push(20);
stack.push(30);
stack.displayStack();
stack.pop();
stack.displayStack();
}
}

Output:

Algorithm :
1. Start.
2. Define a class Node with instance variables data and next.
3. Define a class StackLinkedList with an instance variable top.
4. Implement the isEmpty() method to check if the stack is empty.
5. Define the push() method to add an element to the stack.
6. Define the pop() method to remove an element from the stack.
7. Implement the displayStack() method to display the elements of the stack.
8. In main(), create a stack.
9. Perform push and pop operations.
10. Display the stack after operations.

You might also like