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

Precourse-1 #2008

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
92 changes: 54 additions & 38 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,62 @@
class Stack {
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
}
//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

Stack()
{
//Initialize your constructor
}

boolean push(int x)
{
boolean isEmpty()
{
//Write your code here
return top == -1;
}

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

boolean push(int x)
{
//Check for stack Overflow
if(top == MAX-1) return false;
//Write your code here
}

int pop()
{
a[top + 1] = x;
return true;
}

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

int peek()
{
int x = a[top];
top = top - 1;
return x;
}

int peek()
{
//Write your code here
}
}

// Driver code
class Main {
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");
}
if(top == -1) {
return 0;
}
return a[top];
}
}

// Driver code
class Main {
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");
}
}
52 changes: 0 additions & 52 deletions Exercise_2.java

This file was deleted.

70 changes: 0 additions & 70 deletions Exercise_3.java

This file was deleted.

87 changes: 87 additions & 0 deletions LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import java.io.*;

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

Node head; // head of list

// Linked list Node.
// This inner class is made static
// so that main() can access it
static class Node {

int data;
Node next;

// Constructor
Node(int d)
{
//Write your code here
data = d;
next = null;
}
}

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

// If the Linked List is empty,
// then make the new node as head
if(list.head == null) {
list.head = newNode;
}

// Else traverse till the last node
// and insert the new_node there
else {
Node temp = list.head;
while(temp.next != null) {

temp = temp.next;
}
// Insert the new_node at last node
temp.next = newNode;
}

// Return the list by head
return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
Node temp = list.head;
// Traverse through the LinkedList
while(temp != null) {
// Print the data at current node
System.out.println(temp.data);
// Go to next node
temp = temp.next;
}
}

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

//
// ******INSERTION******
//

// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);

// Print the LinkedList
printList(list);
}
}
73 changes: 73 additions & 0 deletions StackAsLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.*;

public 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.
return root == null;
}

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

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(root == null) {
System.out.print("Stack Underflow");
return 0;
} else {
int x = root.data;
root = root.next;
return x;
}
}

public int peek()
{
//Write code to just return the topmost element without removing it.
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());
}
}