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 #2073

Open
wants to merge 1 commit 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
36 changes: 29 additions & 7 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,58 @@
class Stack {
import java.util.Arrays;

class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

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

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

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

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
if(top == 0) {
return 0;
}
int new_array[] = new int[MAX];
int value = a[top-1];
for(int i=0; i<top-1; i++) {
new_array[i] = a[i];
}
a=new_array;
top = top -1;
return value;
}

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

}

// Driver code
Expand All @@ -40,7 +62,7 @@ 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");
}
}
47 changes: 40 additions & 7 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class StackAsLinkedList {
public class StackAsLinkedList {

StackNode root;

Expand All @@ -8,38 +8,71 @@ static class StackNode {

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


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

public void push(int data)
{
//Write code to push data to the stack.
{
//Write code to push data to the stack.
StackNode new_node = new StackNode(data);
if(null == root){
root = new_node;
return;
}
StackNode temp = root;
while(temp.next != null) {
temp = temp.next;
}
temp.next = new_node;
}

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
//Also return the popped element
if(isEmpty()){
System.out.println("Stack Underflow");
return 0;
}
StackNode temp = root;
while(temp.next != null && temp.next.next != null) {
temp = temp.next;
}
StackNode pop_node = temp.next;
temp.next = null;
return pop_node.data;
}

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

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

StackAsLinkedList sll = new StackAsLinkedList();
StackAsLinkedList sll = new StackAsLinkedList();

sll.push(10);
sll.push(20);
Expand Down
39 changes: 28 additions & 11 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Node head; // head of list

Expand All @@ -17,41 +17,58 @@ static class Node {
// Constructor
Node(int d)
{
//Write your code here
//Write your code here
this.data = d;
}
}

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

// If the Linked List is empty,
// then make the new node as head
// 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

// Return the list by head
if (null == list.head){
list.head = new_node;
return list;
}

Node temp = list.head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = new_node;
return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
public static void printList(LinkedList list)
{
// Traverse through the LinkedList

// Print the data at current node

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

// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();
LinkedList list = new LinkedList();

//
// ******INSERTION******
Expand All @@ -67,4 +84,4 @@ public static void main(String[] args)
// Print the LinkedList
printList(list);
}
}
}