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 Precourse 1 #2055

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
62 changes: 42 additions & 20 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,68 @@
class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
//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
boolean isEmpty() {
// Write your code here
return (top == -1);
}

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

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

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

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

void print() {
for (int i = top; i > -1; i--) {
System.out.print(" " + a[i]);
}
System.out.println(); // Add a newline for better output readability
}
}

// Driver code
class Main {
public static void main(String args[])
{
public static void main(String args[]) {
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
s.print();
System.out.println(s.pop() + " Popped from stack");
s.print();
}
}
29 changes: 28 additions & 1 deletion Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class StackAsLinkedList {
class StackAsLinkedList {

StackNode root;

Expand All @@ -9,30 +9,57 @@ static class StackNode {
StackNode(int data)
{
//Constructor here
this.data=data;
this.next=null;
}
}


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

}

public void push(int data)
{
StackNode new_node = new StackNode(data);
//Write code to push data to the stack.
if(root==null){
root=new_node;
}
else{
new_node.next=root;//new element is inserted in the top of the stack
root=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

if(isEmpty()){
System.out.println("Stack Underflow");
return 0;
}
else{
int popped = root.data; // getting the popped value
root=root.next;
return popped;
}
}

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

//Driver code
Expand Down
23 changes: 21 additions & 2 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 {
class LinkedList {

Node head; // head of list

Expand All @@ -18,13 +18,16 @@ static class Node {
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
Node new_node = new Node(data);

// If the Linked List is empty,
// then make the new node as head
Expand All @@ -34,7 +37,18 @@ public static LinkedList insert(LinkedList list, int data)

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


if(list.head==null){
list.head=new_node;
}
else{
Node temp=list.head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=new_node; // node inserted at the end of the lisr
}
return list;
}

// Method to print the LinkedList.
Expand All @@ -45,6 +59,11 @@ public static void printList(LinkedList list)
// Print the data at current node

// Go to next node
Node temp=list.head;
while(temp!=null){
System.out.println(temp.data); // prints each value in the lisr
temp=temp.next;
}
}

// Driver code
Expand Down