Skip to content
Open

done #2387

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
26 changes: 21 additions & 5 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
if(top<0) return true;
else return false;//Write your code here
}

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

boolean push(int x)
{
//Check for stack Overflow
if(top>= MAX-1) return false;
else{
top++;
a[top]=x;
return true;
} //Check for stack Overflow
//Write your code here
}

int pop()
{
//If empty return 0 and print " Stack Underflow"
if(top<0)
{
System.out.println("Stack Underflow");
return 0;}

else{
int res=a[top];
top--;
return res;
} //If empty return 0 and print " Stack Underflow"
//Write your code here
}

int peek()
{
//Write your code here
if(top<0) return 0;
return a[top];//Write your code here
}
}

Expand Down
160 changes: 91 additions & 69 deletions Exercise_3.java → LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,92 @@
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
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given 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

}

// 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
}

// 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);
}
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)
{
this.data=d;
this.next=null;//Write your code here
}
}

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

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

else{
Node temp=list.head;
while(temp.next!=null)
{
temp=temp.next;
}

temp.next=t;
}
return list;
// Else traverse till the last node
// and insert the new_node there

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

}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
Node cur=list.head;
while(cur!=null)
{ System.out.println(cur.data);
cur=cur.next;
}

// Traverse through the LinkedList

// Print the data at current node

// Go to next node
}

// 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);
}
}
126 changes: 74 additions & 52 deletions Exercise_2.java → StackAsLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,74 @@
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

StackNode(int data)
{
//Constructor here
}
}


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

public void push(int data)
{
//Write code to push data to the stack.
}

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
}

public int peek()
{
//Write code to just return the topmost element without removing it.
}

//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());
}
}
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

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


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

public void push(int data)
{
StackNode t=new StackNode(data);
if(root==null)
{
root=t;}

else{
t.next=root;
root=t;//Write code to push data to the stack.
} }

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

int t=root.data;
root=root.next;
return t;
//If Stack Empty Return 0 and print "Stack Underflow"
//Write code to pop the topmost element of stack.
//Also return the popped element
}

public int peek()
{
if(isEmpty())
{
return 0;
}
else return root.data;//Write code to just return the topmost element without removing it.
}

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