diff --git a/LinkListStackDemo.java b/LinkListStackDemo.java new file mode 100644 index 0000000..7a4ad68 --- /dev/null +++ b/LinkListStackDemo.java @@ -0,0 +1,79 @@ +class Node { + +public int data; +public Node next; + +public Node(int data) { + this.data = data; +} + +public void displayNode() { + System.out.print(data); + System.out.print(" "); + + } +} + +class LinkList { + +private Node first = null; + +public void insertFirst(int data) { + Node n = new Node(data); + n.next = first; + first = n; +} + +public Node deleteFirst() { + Node temp = first; + first = first.next; + return temp; +} + +public void displayList() { + Node current = first; + while (current != null) { + current.displayNode(); + current = current.next; + } +} + +public boolean isEmpty() { + return (first == null); + } +} + +class LinkListStack { + +LinkList li = new LinkList(); + +public void push(int data) { + li.insertFirst(data); +} + +public void pop() { + while(!li.isEmpty()){ + li.deleteFirst(); + } +} + +public void displayStack() { + System.out.println(" "); + li.displayList(); + } +} + +public class LinkListStackDemo { + +public static void main(String[] args) { + LinkListStack st = new LinkListStack(); + + st.push(50); + st.push(70); + st.push(190); + st.displayStack(); + st.pop(); + st.displayStack(); + + } +} diff --git a/LinkedList.java b/LinkedList.java new file mode 100644 index 0000000..ed9e116 --- /dev/null +++ b/LinkedList.java @@ -0,0 +1,40 @@ +// A simple Java program for traversal of a linked list +class LinkedList +{ + Node head; // head of list + + /* Linked list Node. This inner class is made static so that + main() can access it */ + static class Node { + int data; + Node next; + Node(int d) { data = d; next=null; } // Constructor + } + + /* This function prints contents of linked list starting from head */ + public void printList() + { + Node n = head; + while (n != null) + { + System.out.print(n.data+" "); + n = n.next; + } + } + + /* method to create a simple linked list with 3 nodes*/ + public static void main(String[] args) + { + /* Start with the empty list. */ + LinkedList llist = new LinkedList(); + + llist.head = new Node(1); + Node second = new Node(2); + Node third = new Node(3); + + llist.head.next = second; // Link first node with the second node + second.next = third; // Link first node with the second node + + llist.printList(); + } +} diff --git a/QueueArrTest.java b/QueueArrTest.java new file mode 100644 index 0000000..b8cb6c7 --- /dev/null +++ b/QueueArrTest.java @@ -0,0 +1,94 @@ +// Java program for array implementation of queue + +// A class to represent a queue +class Queue +{ + int front, rear, size; + int capacity; + int array[]; + + public Queue(int capacity) { + this.capacity = capacity; + front = this.size = 0; + rear = capacity - 1; + array = new int[this.capacity]; + + } + + // Queue is full when size becomes equal to + // the capacity + boolean isFull(Queue queue) + { return (queue.size == queue.capacity); + } + + // Queue is empty when size is 0 + boolean isEmpty(Queue queue) + { return (queue.size == 0); } + + // Method to add an item to the queue. + // It changes rear and size + void enqueue( int item) + { + if (isFull(this)) + return; + this.rear = (this.rear + 1)%this.capacity; + this.array[this.rear] = item; + this.size = this.size + 1; + System.out.println(item+ " enqueued to queue"); + } + + // Method to remove an item from queue. + // It changes front and size + int dequeue() + { + if (isEmpty(this)) + return Integer.MIN_VALUE; + + int item = this.array[this.front]; + this.front = (this.front + 1)%this.capacity; + this.size = this.size - 1; + return item; + } + + // Method to get front of queue + int front() + { + if (isEmpty(this)) + return Integer.MIN_VALUE; + + return this.array[this.front]; + } + + // Method to get rear of queue + int rear() + { + if (isEmpty(this)) + return Integer.MIN_VALUE; + + return this.array[this.rear]; + } +} + + +// Driver class +public class QueueArrTest +{ + public static void main(String[] args) + { + Queue queue = new Queue(1000); + + queue.enqueue(10); + queue.enqueue(20); + queue.enqueue(30); + queue.enqueue(40); + + System.out.println(queue.dequeue() + + " dequeued from queue\n"); + + System.out.println("Front item is " + + queue.front()); + + System.out.println("Rear item is " + + queue.rear()); + } +}