0% found this document useful (0 votes)
3 views15 pages

Data Structures

The document contains Java implementations for data structures including Stack, Queue, Singly Linked List, and Singly Circular Linked List. Each data structure has methods for insertion, deletion, and display operations. The code includes user interaction through a console menu for executing these operations.

Uploaded by

Stephy Bless
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)
3 views15 pages

Data Structures

The document contains Java implementations for data structures including Stack, Queue, Singly Linked List, and Singly Circular Linked List. Each data structure has methods for insertion, deletion, and display operations. The code includes user interaction through a console menu for executing these operations.

Uploaded by

Stephy Bless
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
You are on page 1/ 15

STACK USING ARRAYS

import java.util.*;
class StackUsingArray{
int top = -1;
final int n = 5;
int stack[] = new int[n];
Scanner sc = new Scanner(System.in);
void push(){
System.out.println("Enter the data");
int data = sc.nextInt();
if(top==n-1){
System.out.println("Overflow");
}else{
top++;
stack[top]=data;
}
}
void pop(){
if(top==-1){
System.out.println("Underflow");

}else{
top--;
}
}
void peek(){
if(top==-1){
System.out.println("Underflow");
}else{
System.out.println(stack[top]);
}
}
void display(){
for(int i=top; i>=0; i--){
System.out.print(stack[i]+" ");
}
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
StackUsingArray obj = new StackUsingArray();
do{
System.out.println("Enter the choice : ");
choice = sc.nextInt();
switch(choice){
case 1:
obj.push();
break;
case 2:
obj.pop();
break;
case 3:
obj.peek();
break;
case 4:
obj.display();
break;
default:
System.out.println("Enter the valid choice");
break;
}

}while(choice!=0);

}
}

QUEUE USING ARRAYS

import java.util.*;
class Queue{
int front = -1;
int rear = -1;
final int n= 5;
int queue[] = new int[n];
void enqueue(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the data: ");
int data = sc.nextInt();
if(front==-1 && rear==-1){
front = rear = 0;
queue[rear] = data;
}else if(rear == n-1){
System.out.println("Overflow");
}else{
rear++;
queue[rear]=data;
}
}
void dequeue(){
if(front==-1 || front>rear){
System.out.println("underflow");
}else{
front++;
}
}
void display(){
for(int i=front; i<=rear; i++){
System.out.print(queue[i]+" ");
}
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
Queue obj = new Queue();
do{
System.out.println("Enter the choice : ");
choice = sc.nextInt();
switch(choice){
case 1:
obj.enqueue();
break;
case 2:
obj.dequeue();
break;

case 4:
obj.display();
break;
default:
System.out.println("Enter the valid choice");
break;
}

}while(choice!=0);
}
}

SINGLY LINKED LIST


INSERTION
BEGIN
POS
END
DELETION
BEGIN
POS
END

import java.util.*;
class Node{
int data;
Node next;
Node(int data){
this.data = data;
this.next = null;
}
}
class LinkedList{
Node head = null;
Node tail = null;
Scanner sc = new Scanner(System.in);
void create(){
System.out.println("Enter your value: ");
int data = sc.nextInt();
Node newnode = new Node(data);
if(head==null){
head = tail = newnode;
}else{
tail.next=newnode;
tail = newnode;
}
}
void Insertatbegin(){
System.out.println("Enter your value: ");
int data = sc.nextInt();
Node newnode = new Node(data);

newnode.next = head;
head = newnode;

}
void insertatpos(){
System.out.println("Enter your value: ");
int data = sc.nextInt();
System.out.println("Enter your pos: ");
int pos = sc.nextInt();
Node newnode = new Node(data);
int count=1;
Node temp = head;
while(count<pos-1){

temp = temp.next;
count++;

newnode.next = temp.next;
temp.next = newnode;
}
void deleteatbegin(){

head = head.next;
}
void deleteatend(){
Node temp = head;
while(temp.next.next!=null){
temp = temp.next;
}
tail = temp;
tail.next = null;
}
void deleteatpos(){
Node temp = head;
int count = 1;
System.out.println("Enter your pos: ");
int pos = sc.nextInt();
while(count<pos-1){
temp = temp.next;
count++;
}
temp.next = temp.next.next;
}
void display(){
Node temp = head;
while(temp!=null){
System.out.print(temp.data+" ");
temp = temp.next;
}
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList obj = new LinkedList();
int choice;
do{
System.out.println("Enter the choice : ");
choice = sc.nextInt();
switch(choice){
case 1:
obj.create();
break;
case 2:
obj.Insertatbegin();
break;
case 3:
obj.insertatpos();
break;
case 4:
obj.deleteatbegin();
break;
case 5:
obj.deleteatend();
break;
case 6:
obj.deleteatpos();
break;
case 7:
obj.display();
break;
default:
System.out.println("Enter the valid choice");
break;
}
}
while(choice!=0);

}
}

SINGLY CIRCULAR LINKED LIST


import java.util.*;
class Node {
int data;
Node next;

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

class CircularLinkedlist {
Node tail = null;
Scanner sc = new Scanner(System.in);
void create() {
System.out.println("Enter the data:");
int data = sc.nextInt();
Node newnode = new Node(data);
if (tail == null) {
tail = newnode;
tail.next=tail;
} else {
newnode.next=tail.next;
tail.next = newnode;
tail = newnode;
}
}
void insertAtBegin(){
System.out.println("Enter the data:");
int data=sc.nextInt();
Node newnode=new Node(data);
newnode.next=tail.next;
tail.next=newnode;
}
void insertAtEnd(){
create();
}
void deleteAtBegin(){
tail.next=tail.next.next;
}
void deleteAtEnd(){
Node temp=tail.next;
while(temp.next.next!=tail.next){
temp=temp.next;
}
temp.next=tail.next;
tail=temp;
}
void display(){
Node temp=tail.next;
while(temp.next!=tail.next){
System.out.print(temp.data+"->");
temp=temp.next;
}
System.out.print(temp.data);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CircularLinkedlist obj = new CircularLinkedlist();
int choice;

do {
System.out.println("Enter your choice:");
choice = sc.nextInt();

switch (choice) {
case 1:
obj.create();
break;
case 2:
obj.display();
break;
case 3:
obj.insertAtBegin();
break;
case 4:
obj.insertAtEnd();
case 5:
obj.deleteAtBegin();
break;
case 6:
obj.deleteAtEnd();
break;
default:
System.out.println("Enter a valid choice");
break;
}
} while (choice != 0);
}
}
import java.util.*;
class Node{
int data;
Node next;
Node(int data){
this.data = data;
this.next = null;
}
}
class LinkedList{
Node front = null;
Node rear = null;
Scanner sc = new Scanner(System.in);
void enqueue(){
System.out.println("Enter your value: ");
int data = sc.nextInt();
Node newnode = new Node(data);
if(front==null && rear == null){
front = rear = newnode;
}else{
rear.next = newnode;
rear = newnode;
}

}
void dequeue(){
if(front==null){
System.out.println("underflow");
}else{
front = front.next;
}
}
void display(){
Node temp = front;
while(temp!=null){
System.out.print(temp.data+" ");
temp = temp.next;
}
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList obj = new LinkedList();
int choice;
do{
System.out.println("Enter the choice : ");
choice = sc.nextInt();
switch(choice){
case 1:
obj.enqueue();
break;
case 2:
obj.dequeue();
break;
case 3:
obj.display();
break;

default:
System.out.println("Enter the valid choice");
break;
}

}
while(choice!=0);

}
}

You might also like