Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Queue (Linked List Implementation) and Implement A Stack Using Singly Linked List

Queue(Linked List Implementation) and Implement a stack using singly linked list
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Queue (Linked List Implementation) and Implement A Stack Using Singly Linked List

Queue(Linked List Implementation) and Implement a stack using singly linked list
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 11 practice tasks:

Task 1: Queue – Linked List Implementation


 In the lectures, we introduced Queue and discussed array implementation. In this week, linked
list implementation of Queue is discussed. The following two main operations must be
implemented efficiently.

 In a Queue data structure, we maintain two pointers, front and rear. The front points the first
item of queue and rear points to last item.

 enQueue() This operation adds a new node after rear and moves rear to the next node.

 deQueue() This operation removes the front node and moves front to the next node.

Source Code
class QNode {
int key;
QNode next;
// constructor to create a new linked list node
public QNode(int key) {
this.key = key;
this.next = null;
}
}
// A class to represent a queue
// The queue, front stores the front node of LL and rear stores the
// last node of LL
class Queue {
QNode front, rear;
public Queue() {
this.front = this.rear = null;
}

// Method to add an key to the queue.


void enqueue(int key) {
// Create a new LL node
QNode temp = new QNode(key);
// If queue is empty, then new node is front and rear both
if (this.rear == null) {
this.front = this.rear = temp;
return;
}
// Add the new node at the end of queue and change rear
this.rear.next = temp;
this.rear = temp;
}
// Method to remove an key from queue.
QNode dequeue() {
// If queue is empty, return NULL.
if (this.front == null)
return null;
// Store previous front and move front one node ahead
QNode temp = this.front;
this.front = this.front.next;
// If front becomes NULL, then change rear also as NULL
if (this.front == null)
this.rear = null;
return temp;
}
}

public class QueueUsingLL {


public static void main(String[] args) {
Queue q = new Queue();
q.enqueue(11);
q.enqueue(22);
q.dequeue();
q.dequeue();
q.enqueue(33);
q.enqueue(44);
q.enqueue(55);
System.out.println("Dequeued item is " + q.dequeue().key);
}
}

Output:
Dequeued item is 30
Task 2: Implement a stack using singly linked list
 All the single linked list operations perform based on Stack operations LIFO and with the help of
that knowledge we are going to implement a stack using single linked list.

 using single linked lists so how to implement here it is linked list means what we are storing the
information in the form of nodes and we need to follow the stack rules and we need to
implement using single linked list nodes

 Rules of Stack: Last in first out - and all the operations we should perform so with the help of a
top variable only with the help of top variables are how to insert the elements

 There are two most important operations of Stack:

 Push : We will push element to beginning of linked list to demonstrate push behavior
of stack.

 Pop : We will remove first element of linked list to demonstrate pop behavior of Stack .
Source Code
public class StackUsingLL {
private Node head; // the first node

// nest class to define linkedlist node


private class Node {
int value;
Node next;
}

public StackUsingLL() {
head = null;
}

//Remove value from beginning of the list for demonstrating behaviour of stack
public int pop() {
if (head == null) {
System.out.println("Linked List is Empty!! Exception occured!!");
return -1;
}
int value = head.value;
head = head.next;
return value;
}

// Add value to the beginning of the list for demonstrating behaviour of stack
public void push(int value) {
Node oldHead = head;
head = new Node();
head.value = value;
head.next = oldHead;
}

public static void printList(Node head) {


Node temp = head;
while (temp != null) {
System.out.format("%d ", temp.value);
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StackUsingLL lls=new StackUsingLL();
lls.push(20);
lls.push(50);
lls.push(80);
lls.push(40);
lls.push(60);
lls.push(75);
System.out.println("Element removed from LinkedList: "+lls.pop());
System.out.println("Element removed from LinkedList: "+lls.pop());
lls.push(10);
System.out.println("Element removed from LinkedList: "+lls.pop());
printList(lls.head);
}
}

Output:
Element removed from LinkedList: 75
Element removed from LinkedList: 60
Element removed from LinkedList: 10
40 80 50 20

You might also like