Skip to content

Commit 877cdc3

Browse files
committed
JAva program update
1 parent ed0ef1c commit 877cdc3

3 files changed

Lines changed: 261 additions & 0 deletions

File tree

dataStructure/MYStackByList.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package dataStructure;
2+
3+
//Java program to Implement a stack
4+
//using singly linked list
5+
//import package
6+
import static java.lang.System.exit;
7+
8+
//Create Stack Using Linked list
9+
class StackUsingLinkedlist {
10+
11+
// A linked list node
12+
private class Node {
13+
14+
int data; // integer data
15+
Node link; // reference variable Node type
16+
}
17+
// create global top reference variable global
18+
Node top;
19+
// Constructor
20+
StackUsingLinkedlist()
21+
{
22+
this.top = null;
23+
}
24+
25+
// Utility function to add an element x in the stack
26+
public void push(int x) // insert at the beginning
27+
{
28+
// create new node temp and allocate memory
29+
Node temp = new Node();
30+
31+
// initialize data into data field
32+
temp.data = x;
33+
34+
// put top reference into link
35+
temp.link = top;
36+
37+
// update top reference
38+
top = temp;
39+
}
40+
41+
// Utility function to check if the stack is empty or not
42+
public boolean isEmpty()
43+
{
44+
return top == null;
45+
}
46+
47+
// Utility function to return top element in a stack
48+
public int peek()
49+
{
50+
// check for empty stack
51+
if (!isEmpty()) {
52+
return top.data;
53+
}
54+
else {
55+
System.out.println("Stack is empty");
56+
return -1;
57+
}
58+
}
59+
60+
// Utility function to pop top element from the stack
61+
public void pop() // remove at the beginning
62+
{
63+
// check for stack underflow
64+
if (top == null) {
65+
System.out.print("\nStack Underflow");
66+
return;
67+
}
68+
69+
// update the top pointer to point to the next node
70+
top = (top).link;
71+
}
72+
73+
public void display()
74+
{
75+
// check for stack underflow
76+
if (top == null) {
77+
System.out.printf("\nStack Underflow");
78+
exit(1);
79+
}
80+
else {
81+
Node temp = top;
82+
while (temp != null) {
83+
84+
// print node data
85+
System.out.printf("%d->", temp.data);
86+
87+
// assign temp link to temp
88+
temp = temp.link;
89+
}
90+
}
91+
}
92+
}
93+
//main class
94+
public class MYStackByList {
95+
public static void main(String[] args)
96+
{
97+
// create Object of Implementing class
98+
StackUsingLinkedlist obj = new StackUsingLinkedlist();
99+
// insert Stack value
100+
obj.push(11);
101+
obj.push(22);
102+
obj.push(33);
103+
obj.push(44);
104+
105+
// print Stack elements
106+
obj.display();
107+
108+
// print Top element of Stack
109+
System.out.printf("\nTop element is %d\n", obj.peek());
110+
111+
// Delete top element of Stack
112+
obj.pop();
113+
obj.pop();
114+
115+
// print Stack elements
116+
obj.display();
117+
118+
// print Top element of Stack
119+
System.out.printf("\nTop element is %d\n", obj.peek());
120+
}
121+
}

dataStructure/MyQueue.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//Java program for array implementation of queue
2+
package dataStructure;
3+
//A class to represent a queue
4+
class Queue{
5+
int front, rear, size;
6+
int capacity;
7+
int array[];
8+
9+
public Queue(int capacity) {
10+
this.capacity = capacity;
11+
front = this.size = 0;
12+
rear = capacity - 1;
13+
array = new int[this.capacity];
14+
}
15+
16+
//Queue is full when size becomes equal to capacity
17+
boolean isFull(Queue queue) {
18+
return(queue.size == queue.capacity);
19+
}
20+
21+
//Queue is Empty when size is 0
22+
boolean isEmpty(Queue queue){
23+
return(queue.size == 0);
24+
}
25+
26+
//Method to add an item to the queue
27+
//It changes rear and size
28+
void enqueue(int item) {
29+
if(isFull(this))
30+
return;
31+
this.rear = (this.rear+1)%this.capacity;
32+
this.array[this.rear] = item;
33+
this.size = this.size+1;
34+
System.out.println(item + " enqueud to queue");
35+
}
36+
37+
//Method to remove an item from queue
38+
//It changes front and size
39+
int dequeue() {
40+
if(isEmpty(this))
41+
return Integer.MIN_VALUE;
42+
43+
int item = this.array[this.front];
44+
this.front = (this.front + 1) % this.capacity;
45+
this.size = this.size-1;
46+
return item;
47+
48+
}
49+
50+
//Method to get front of queue
51+
int front() {
52+
if(isEmpty(this))
53+
return Integer.MIN_VALUE;
54+
55+
return this.array[this.front];
56+
}
57+
58+
//Method to get rear of queue
59+
int rear() {
60+
if(isEmpty(this))
61+
return Integer.MIN_VALUE;
62+
63+
return this.array[this.rear];
64+
}
65+
}
66+
67+
//Driver Class
68+
public class MyQueue{
69+
public static void main(String[] args){
70+
71+
Queue queue = new Queue(1000);
72+
73+
queue.enqueue(10);
74+
queue.enqueue(20);
75+
queue.enqueue(30);
76+
queue.enqueue(40);
77+
78+
System.out.println(queue.dequeue()+(" dequeued from queue"));
79+
System.out.println("Front item is " + queue.front());
80+
System.out.println("Rear item is "+ queue.rear());
81+
}
82+
}

dataStructure/MyStack.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dataStructure;
2+
3+
class Stack {
4+
static final int MAX = 1000;
5+
int top;
6+
int a[] = new int[MAX];
7+
8+
boolean isEmpty() {
9+
return (top<0);
10+
}
11+
12+
Stack(){
13+
top = -1;
14+
}
15+
16+
boolean push(int x) {
17+
if(top>= (MAX-1)) {
18+
System.out.println(x+"pushed into stack");
19+
return false;
20+
}
21+
else {
22+
a[++top] = x;
23+
System.out.println(x+"pushed into stack");
24+
return true;
25+
}
26+
}
27+
28+
int pop() {
29+
if(top<0) {
30+
System.out.println("Stack underflow");
31+
return 0;
32+
}
33+
else {
34+
int x = a[top--];
35+
return x;
36+
}
37+
}
38+
39+
int peek() {
40+
if(top<0) {
41+
System.out.println("Stack underflow");
42+
return 0;
43+
}
44+
else {
45+
int x = a[top]; return x;
46+
}
47+
}
48+
49+
class MyStack{
50+
public void main (String[] args) {
51+
Stack s = new Stack();
52+
s.push(10);
53+
s.push(20);
54+
s.push(30);
55+
System.out.println(s.pop() + " Popped from stack");
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)