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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

boolean isEmpty()
{
//Write your code here
return top < 0;
}

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

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if (top >= MAX-1)
{
System.out.println("Stack Overflow");
return false;
}
else
{
a[++top] = x;
return true;
}
}

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

int peek()
{
//Write your code here
{
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
} else {
return a[top];
}
}
}

Expand All @@ -40,7 +60,8 @@ public static void main(String args[])
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
s.push(30);
System.out.println(s.pop() + " Popped from stack");
System.out.println("Top element is " + s.peek());
}
}
52 changes: 0 additions & 52 deletions Exercise_2.java

This file was deleted.

44 changes: 27 additions & 17 deletions Exercise_3.java → LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import java.io.*;

// Java program to implement
// Java program to implement
// a Singly Linked List
public class LinkedList {

Expand All @@ -17,25 +15,31 @@ static class Node {
// Constructor
Node(int d)
{
//Write your code here
this .data = d;
this.next = null;
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data

// If the Linked List is empty,
// then make the new node as head

// Else traverse till the last node
// and insert the new_node there
{

// Insert the new_node at last node
// Return the list by head

}
Node newNode = new Node(data);

if(list.head == null)
list.head = newNode;
else
{
Node temp = list.head;
while (temp.next != null)
{
temp = temp.next;
}
temp.next = newNode;
}

return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
Expand All @@ -44,7 +48,13 @@ public static void printList(LinkedList list)

// Print the data at current node

// Go to next node
// Go to next node
Node temp = list.head;
while(temp != null)
{
System.out.print(temp.data + " ");
temp = temp.next;
}
}

// Driver code
Expand Down
11 changes: 11 additions & 0 deletions PreCourse-1.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
75 changes: 75 additions & 0 deletions StackAsLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
public class StackAsLinkedList {
// Time: push/pop/peek/isEmpty are all O(1).
// Space: O(n) for n nodes.

StackNode root;

static class StackNode {
int data;
StackNode next;

StackNode(int data)
{
this.data = data;
this.next = null;
}
}


public boolean isEmpty()
{
return root == null;
}

public void push(int data)
{
// Create a new node
StackNode newNode = new StackNode(data);
// New node points to current top
newNode.next = root;
// New node becomes the new top
root = newNode;
}

public int pop()
{

if (isEmpty())
{
System.out.println("Stack Underflow");
return 0;
}
else
{
int popData = root.data;
root = root.next;
return popData;
}
}

public int peek()
{
if (isEmpty())
{
System.out.println("Stack Underflow");
return 0;
}
else
return root.data;
}

//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());
}
}
8 changes: 8 additions & 0 deletions out/production/PreCourse-1/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions out/production/PreCourse-1/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions out/production/PreCourse-1/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions out/production/PreCourse-1/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading