Saturday, July 15, 2023

Circular linked Stack, QUEUE -Data structure

 

 Circular linked Stack, QUEUE -Data structure

Circular linked

 

circular linked list is a linked list in which the last node points back to the first node, forming a circle. This means that there is no "end" to the list, and you can start traversing the list from any node and eventually end up back at the node you started from.

Circular linked lists are often used in applications where it is necessary to keep track of the current position in the list, such as in a game where players take turns. They can also be used to implement queues and other data structures.

Here are some of the advantages of using circular linked lists:

  • They are easy to implement.
  • They can be used to implement queues and other data structures.
  • They are efficient for traversing the list from any node.

Here are some of the disadvantages of using circular linked lists:

  • They can be more difficult to debug than other types of linked lists.
  • They can be less efficient for inserting and deleting nodes in the middle of the list.

stack

stack is a linear data structure that follows the last-in, first-out (LIFO) principle. This means that the last element added to the stack is the first element that is removed. Stacks are often used in applications where it is necessary to keep track of the order in which things were added, such as in a call stack or a undo/redo stack.

In Java, stacks are implemented using the Stack class. The Stack class provides methods for pushing and popping elements from the stack, as well as for checking if the stack is empty or full.

import java.util.Stack;

 

public class StackExample {

 

  public static void main(String[] args) {

    Stack<Integer> stack = new Stack<>();

 

    stack.push(1);

    stack.push(2);

    stack.push(3);

 

    System.out.println(stack.pop()); // 3

    System.out.println(stack.pop()); // 2

    System.out.println(stack.pop()); // 1

  }

}

This code creates a stack and pushes three elements onto the stack. The code then pops two elements from the stack and prints them.

Here is an explanation of how the code works:

  • The Stack class is imported from the java.util package.
  • A new Stack object is created.
  • Three elements are pushed onto the stack.
  • Two elements are popped from the stack and printed.

 

QUEUE

 

queue is a linear data structure that follows the first-in, first-out (FIFO) principle

In Java, queues are implemented using the Queue interface. The Queue interface provides methods for adding and removing elements from the queue, as well as for checking if the queue is empty or full.

import java.util.Queue;

import java.util.LinkedList;

 

public class QueueExample {

 

  public static void main(String[] args) {

    Queue<Integer> queue = new LinkedList<>();

 

    queue.add(1);

    queue.add(2);

    queue.add(3);

 

    System.out.println(queue.poll()); // 1

    System.out.println(queue.poll()); // 2

    System.out.println(queue.poll()); // 3

  }

}

 

No comments:

eLearning and Blackboard

  IT professional, U bring a unique set of skills and expertise that can greatly contribute to the successful development and implementati...