Skip to content
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

Completed Pre Course 1 #2002

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
54 changes: 0 additions & 54 deletions Exercise_1.cpp

This file was deleted.

31 changes: 28 additions & 3 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,54 @@ class Stack {

boolean isEmpty()
{
//Write your code here
//Write your code here
if(a.length==0)
{
return true;
}
return false;
}

Stack()
{
//Initialize your constructor
//Initialize your constructor
top = -1;
}

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(isEmpty())
{
top++;
a[top] = x;
}
a[++top]=x;
return true;
}

int pop()
{
{
if(isEmpty())
{
System.out.println("Stack Underflow");
return 0;
}
return a[top--];
//If empty return 0 and print " Stack Underflow"
//Write your code here
// return 1;
}

int peek()
{
//Write your code here
if(isEmpty())
{
return -1;
}
return a[top];
}
}

Expand Down
35 changes: 0 additions & 35 deletions Exercise_1.js

This file was deleted.

24 changes: 0 additions & 24 deletions Exercise_1.py

This file was deleted.

52 changes: 0 additions & 52 deletions Exercise_2.cpp

This file was deleted.

128 changes: 76 additions & 52 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,76 @@
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

StackNode(int data)
{
//Constructor here
}
}


public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
}

public void push(int data)
{
//Write code to push data to the stack.
}

public int pop()
{
//If Stack Empty Return 0 and print "Stack Underflow"
//Write code to pop the topmost element of stack.
//Also return the popped element
}

public int peek()
{
//Write code to just return the topmost element without removing it.
}

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

StackAsLinkedList sll = new StackAsLinkedList();

sll.push(10);
sll.push(20);
sll.push(30);

System.out.println(sll.pop() + " popped from stack");

System.out.println("Top element is " + sll.peek());
}
}
class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

StackNode(int data) {
//Constructor here
this.data = data;
}
}


public boolean isEmpty() {
//Write your code here for the condition if stack is empty.
if (root == null) {
return true;
}
return false;
}

public void push(int data) {
//Write code to push data to the stack.
StackNode newNode = new StackNode(data);
// if (isEmpty()) {
// root = newNode;
// }
newNode.next = root;
root = newNode;
// root.next = newNode;

}

public int pop() {
//If Stack Empty Return 0 and print "Stack Underflow"
//Write code to pop the topmost element of stack.
//Also return the popped element
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}
int data = root.data;
root = root.next;
return data;
}

public int peek() {
//Write code to just return the topmost element without removing it.
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}
int data = root.data;

return data;
}
}
class Exercise_2 {

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

StackAsLinkedList sll = new StackAsLinkedList();

sll.push(10);
sll.push(20);
sll.push(30);
// sll.pop();
System.out.println(sll.pop() + " popped from stack");

System.out.println("Top element is " + sll.peek());
}

}
36 changes: 0 additions & 36 deletions Exercise_2.js

This file was deleted.

Loading