Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions LinkListStackDemo.java
Original file line number Diff line number Diff line change
@@ -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();

}
}
40 changes: 40 additions & 0 deletions LinkedList.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
94 changes: 94 additions & 0 deletions QueueArrTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}