diff --git a/bin/com/sort/bubble sort c++ b/bin/com/sort/bubble sort c++ new file mode 100644 index 0000000..f6b792a --- /dev/null +++ b/bin/com/sort/bubble sort c++ @@ -0,0 +1,38 @@ +#include +using namespace std; + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void bubbleSort(int arr[], int n) +{ + int i, j; + for (i = 0; i < n-1; i++) + + + for (j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + swap(&arr[j], &arr[j+1]); +} + +void printArray(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + +int main() +{ + int arr[] = {64, 34, 25, 12, 22, 11, 90}; + int n = sizeof(arr)/sizeof(arr[0]); + bubbleSort(arr, n); + cout<<"Sorted array: \n"; + printArray(arr, n); + return 0; +} diff --git a/src/com/ds/Stack data Structure b/src/com/ds/Stack data Structure new file mode 100644 index 0000000..acb9314 --- /dev/null +++ b/src/com/ds/Stack data Structure @@ -0,0 +1,78 @@ +// Java Code for Linked List Implementation + +public class StackAsLinkedList { + + StackNode root; + + static class StackNode { + int data; + StackNode next; + + StackNode(int data) + { + this.data = data; + } + } + + public boolean isEmpty() + { + if (root == null) { + return true; + } + else + return false; + } + + public void push(int data) + { + StackNode newNode = new StackNode(data); + + if (root == null) { + root = newNode; + } + else { + StackNode temp = root; + root = newNode; + newNode.next = temp; + } + System.out.println(data + " pushed to stack"); + } + + public int pop() + { + int popped = Integer.MIN_VALUE; + if (root == null) { + System.out.println("Stack is Empty"); + } + else { + popped = root.data; + root = root.next; + } + return popped; + } + + public int peek() + { + if (root == null) { + System.out.println("Stack is empty"); + return Integer.MIN_VALUE; + } + else { + return root.data; + } + } + + 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()); + } +}