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

Done precourse-1 #2065

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
117 changes: 78 additions & 39 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,85 @@
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
}

Stack()
{
//Initialize your constructor
}

boolean push(int x)
{
//Kindly include Time and Space complexity at top of each file
// Time Complexity :O(1)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :No


// Your code here along with comments explaining your approach


static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

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

}

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

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
}

int pop()
{
if(top == MAX){
System.out.println("Stack Overflow");
return false;
} else {
a[top] = x;
top++;
System.out.println("Pushed into stack " +x);
return true;
}
}

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
}

int peek()
{
if(isEmpty()){
System.out.println("Stack Underflow");
return 0;
} else {
top--;
int x = a[top];
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 (isEmpty()) {
System.out.println("Stack Empty");
return 0;
} else {
int index = a[top];
// System.out.println("Top element is " +index);
return index;
}

}
}

// 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");
System.out.println(s.peek() + "Top element 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.

84 changes: 84 additions & 0 deletions LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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
// Else traverse till the last node
// and insert the new_node there

// Insert the new_node at last node
// Return the list by head
if(list.head == null){
list.head = newNode;
} else {
Node last = list.head;
while(last.next != null){
last = last.next;
}
last.next = newNode;
}
return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
Node currentNode = list.head;
// Traverse through the LinkedList
while(currentNode!=null) {
// Print the data at current node
System.out.println("Current Node " + currentNode.data + " ");
// Go to next node
currentNode = currentNode.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);
}
}
Loading