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 precourse1 #2074

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
32 changes: 24 additions & 8 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,45 @@ class Stack {

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

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

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(top == MAX -1) {
throw new RuntimeException("Stack Overflow has occurred");
}
else{
a[++top]=x;
return true;
}
}

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
if(top ==-1)
{
throw new RuntimeException("Stack Underflow has occurred");
}
else {
return a[top--];
}
}

int peek()
{
//Write your code here
{
if(top ==-1)
{
throw new RuntimeException("Stack Underflow has occurred");
}
else {
return a[top];
}
}
}

Expand Down
39 changes: 27 additions & 12 deletions 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 @@ -8,31 +8,46 @@ static class StackNode {

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


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

public void push(int data)
{
//Write code to push data to the stack.
StackNode node = new StackNode(data);
node.next = root;
root = 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(root == null)
{
throw new RuntimeException("Stack Underflow has occurred");
}
else {
int data = root.data;
root = root.next;
return data;
}
}

public int peek()
{
//Write code to just return the topmost element without removing it.
{
if(root == null)
{
throw new RuntimeException("Stack Underflow has occurred");
}
else{
return root.data;
}
}

//Driver code
Expand Down
35 changes: 20 additions & 15 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 @@ -17,34 +17,39 @@ static class Node {
// Constructor
Node(int d)
{
//Write your code here
data =d;
}
}

// 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
if(list.head == null){
list.head = newNode;
}
else {
Node lastNode = list.head;
while(lastNode.next !=null){
lastNode = lastNode.next;
}
lastNode.next = newNode;
}

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

}

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

// Print the data at current node

// Go to next node
Node currentNode = list.head;

while (currentNode != null){
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
}

// Driver code
Expand Down