Skip to content

Stack and Queue added to new folder Data Structures #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 19, 2017
Merged
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
Binary file not shown.
132 changes: 132 additions & 0 deletions Competitive Coding/Data Structures/Queue/queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package Data_Structures;
import java.util.*;
class Queue
{
private int q[], front, rear, size;


/***** Constructor to initialize queue *****/
queue(int n)
{ //Begin constructor
size = n;
front = -1;
rear = -1;
q = new int[size];

for(int i = 0; i<size; i++)
q[i] = 0;
} //end constructor



/***** Fucntion to insert an item to queue *****/
private void insert(int item)
{ //Begin insert()
if(rear == (size-1)) //Checks if rear pointer has reached the last limit (Queue is completely full or not)
{
System.out.println("Queue Overflow");
}

elseif(front == -1 && rear == -1) //Checks if queue is empty to insert the very first element
{
front = 0; rear = 0;
q[rear] = item;
}

else
{
rear+=1;
q[rear] = item;
}

} //End insert()




/***** Fucntion to delete an item from queue *****/
private void delete()
{ //Begin delete()


if(front == -1 && rear == -1) //Checks if queue is empty
{
System.out.println("Queue Underflow");
}




elseif(front == rear) //Checks if only one element is present
{
System.out.println("Deleted element:\t"+q[front]);
front = -1; rear = -1;
}

else
{
System.out.println("Deleted element:\t"+q[front]);
front += 1;
}

} //End delete()



/***** Function to display the elements of the queue *****/
private void display()
{ //Begin display()
if(front == - 1 && rear == -1)
{
System.out.println("Queue is EMPTY");
}

else
{
System.out.print("Elements of the queue:\nfront ---> ");

for(int i = front; i <= rear; i++)
System.out.print(q[i]+"\t");

System.out.println("<--- rear");

}
} //End display()




public static void main(String args[])
{ //Begin main()

Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the queue");
int n = sc.nextInt();

queue q1 = new queue(n);

int choice;
do
{ //Begin do
System.out.println("\nEnter:\n1. Insert\n2. Delete\n3. Display");
choice = sc.nextInt();
switch(choice)
{ //Begin switch
case 1: System.out.println("\nEnter an element to insert");
int element = sc.nextInt();
q1.insert(element);
break;

case 2: q1.delete();
break;

case 3: q1.display();
break;

default: System.out.println("Wrong choice. Exitting.");
break;
} //End switch
} //End do
while(choice >= 1 && choice <= 3);
} //End main()
} //End class
Binary file not shown.
130 changes: 130 additions & 0 deletions Competitive Coding/Data Structures/Stack/stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package Data_Structures
import java.util.*;
class stack
{ //Begin class

int top = -1, size, s[];


/*****Constructor to initialise the stack *****/
stack(int n)
{ //Begin constructor
size = n;

s = new int[size];
for(int i = 0; i < size; i++)
{ //Begin for
s[i] = 0;
} //End for
} //End constructor



/***** Fucntion to insert/push item to stack *****/
private void push(int item)
{ //Begin push()

if(top == (size-1)) //Checks if stack is full
{ //Begin if
System.out.println("Stack overflow");
} //End if

else //Element is entered to the stack when there is place
{ //Begin else
top++;
s[top] = item;
} //End else

} //End push()



/***** Function to delete/pop an item (topmost element) from stack *****/
private void pop()
{ //Begin pop()

if(top == -1) //Checks if stack is empty
{ //Begin if
System.out.println("Stack underflow");
} //End if

else
{ //Begin else
System.out.println("Item deleted is:\t"+s[top]);
top--;
} //End else
} //End pop()


/***** Function to peep (show the topmost element in stack) *****/
private void peep()
{ //Begin peep()
if(top == -1)
System.out.println("No element to peep");

else
System.out.println("Peep: top ---> "+s[top]);
} //End peep()



/***** Function to display the elements of stack *****/
private void display()
{ //Begin display()

if(top == -1)
{ //Begin if
System.out.println("No elements in stack");
} //End if

else
{ //Begin else

System.out.print("Elements of the stack: top---> ");
for(int i=top; i>=0; i--)
{
System.out.print(s[i]+"\t");
}
System.out.println();
} //End else
} //End display()


/***** main fucntion *****/
public static void main(String args[])
{ //Begin main()

Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the stack");
int n = sc.nextInt();

stack st = new stack(n);

int choice;
do
{ //Begin do
System.out.println("\nEnter:\n1. Push\n2. Pop\n3. Peep\n4. Display");
choice = sc.nextInt();
switch(choice)
{ //Begin switch
case 1: System.out.println("\nEnter an element to push");
int element = sc.nextInt();
st.push(element);
break;

case 2: st.pop();
break;

case 3: st.peep();
break;

case 4: st.display();
break;

default: System.out.println("Wrong choice. Exitting!");
break;
} //End switch
} //End do
while(choice >= 1 && choice <= 4);
} //End main()
} //End class