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 pr1 #2067

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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.debug.settings.onBuildFailureProceed": true
}
29 changes: 27 additions & 2 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,54 @@ class Stack {

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

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

}

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(top>= MAX-1){
System.out.println("stack is 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 is empty");
return 0;
}
else{
return a[top--];
}
}

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

}
}
}

Expand Down
52 changes: 0 additions & 52 deletions Exercise_2.java

This file was deleted.

44 changes: 29 additions & 15 deletions Exercise_3.java → LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,50 @@ static class Node {
Node next;

// Constructor
Node(int d)
{
//Write your code here
Node(int d) {
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 newNode = new Node(data);

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

// then make the new node as head
if (list.head == null) {
list.head = newNode;
} else {
// Else traverse till the last node
// and insert the new_node there

// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}

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

last.next = newNode;
}

// Return the list by head
return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
Node currentNode = list.head;

// Traverse through the LinkedList

// Print the data at current node

// Go to next node
while (currentNode != null) {
// Print the data at current node
System.out.print(currentNode.data + " ");

// Go to the next node
currentNode = currentNode.next;
}
}

// Driver code
Expand Down
Binary file added Main.class
Binary file not shown.
Binary file added Stack.class
Binary file not shown.
68 changes: 68 additions & 0 deletions StackAsLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

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 newNode = new StackNode(data);
newNode.next = root; // Link the new node to the current root
root = newNode;
}

public int pop()
{
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}

// Pop the topmost element
int poppedData = root.data; // Store data of the top node
root = root.next; // Move root to the next node
return poppedData;
}

public int peek()
{
if (isEmpty()) {
System.out.println("Stack is empty");
return 0;
}

// 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());
}
}