diff --git a/05. Arrays/Reverse an array.cpp b/05. Arrays/Reverse an array.cpp index e4aba831..bc48ce7f 100644 --- a/05. Arrays/Reverse an array.cpp +++ b/05. Arrays/Reverse an array.cpp @@ -1,5 +1,27 @@ #include using namespace std; + +//fun reverse array using while loop -start here +void reverse_array(int arr[],int n) +{ + int start=0; + int end=n-1; + while(start>A[i]; } + reverse_array(A,n); for(int begin=0;begin<(n/2);begin++) { diff --git a/11. Linked List/doubly_linked_list.cpp b/11. Linked List/doubly_linked_list.cpp index e6c2a89c..b0360275 100644 --- a/11. Linked List/doubly_linked_list.cpp +++ b/11. Linked List/doubly_linked_list.cpp @@ -1,89 +1,258 @@ -#include +#include using namespace std; - -class Node -{ - public: - Node *back; - int data; - Node *next; + +// A doubly linked list node +struct Node { + int data; + struct Node* next; + struct Node* prev; }; - -Node *first=NULL; - -void create_dll(int A[],int n) + +//inserts node at the front of the list +void insert_front(struct Node** head, int new_data) { - first=new Node; - first->back=NULL; - first->data=A[0]; - first->next=NULL; - - Node *current_ptr=first; - - for(int i=1;idata=A[i]; - - temp->back=current_ptr; - temp->next=NULL; - current_ptr->next=temp; - current_ptr=temp; - } + //allocate memory for New node + struct Node* newNode = new Node; + + //assign data to new node + newNode->data = new_data; + + //new node is head and previous is null, since we are adding at the front + newNode->next = (*head); + newNode->prev = NULL; + + //previous of head is new node + if ((*head) != NULL) + (*head)->prev = newNode; + + //head points to new node + (*head) = newNode; } - - -void Display() +/* Given a node as prev_node, insert a new node after the given node */ +void insert_After(struct Node* prev_node, int new_data) +{ + //check if prev node is null + if (prev_node == NULL) { + cout<<"Previous node is required , it cannot be NULL"; + return; +} + //allocate memory for new node + struct Node* newNode = new Node; + + //assign data to new node + newNode->data = new_data; + + //set next of newnode to next of prev node + newNode->next = prev_node->next; + + //set next of prev node to newnode + prev_node->next = newNode; + + //now set prev of newnode to prev node + newNode->prev = prev_node; + + //set prev of new node's next to newnode + if (newNode->next != NULL) + newNode->next->prev = newNode; +} + +//insert a new node at the end of the list +void insert_end(struct Node** head, int new_data) { - Node *ptr=first; - while(ptr) - { - cout<<"<-"<data<<"->"; - ptr=ptr->next; + //allocate memory for node + struct Node* newNode = new Node; + + struct Node* last = *head; //set last node value to head + + //set data for new node + newNode->data = new_data; + + //new node is the last node , so set next of new node to null + newNode->next = NULL; + + //check if list is empty, if yes make new node the head of list + if (*head == NULL) { + newNode->prev = NULL; + *head = newNode; + return; +} + +//otherwise traverse the list to go to last node +while (last->next != NULL) +last = last->next; + +//set next of last to new node +last->next = newNode; + +//set last to prev of new node +newNode->prev = last; +return; +} + +// This function prints contents of linked list starting from the given node +void displayList(struct Node* node) { + struct Node* last; + + while (node != NULL) { + cout<data<<"<==>"; + last = node; + node = node->next; + } + if(node == NULL) + cout<<"NULL"; } - cout<next, 30); + + cout<<"Doubly linked list is as follows: "<next; - x=ptr->data; - delete(ptr); - first->back=NULL; - } - - else - { - Node *q=first; - for(int i=0;inext; - } - if(q!=NULL) - { - q->back->next=q->next; - q->next->back=q->back; +10<==>20<==>30<==>40<==>50<==>NULL - x=q->data; - delete(q); - } - } - return x; -} +The above program constructs a doubly linked list by inserting the nodes using three insertion methods i.e. inserting the node at the front, inserting the node at the end and inserting the node after the given node. +Next, we demonstrate the same operation as a Java implementation. -int main() +// Java Class for Doubly Linked List +class Doubly_linkedList { + Node head; // list head + + /* Doubly Linked list Node*/ + class Node { + int data; + Node prev; + Node next; + + //create a new node using constructor + Node(int d) { data = d; } + } + +// insert a node at the front of the list +public void insert_front(int new_data) { - int A[]={1,2,3,4,5}; - int n ; - n= sizeof(A)/sizeof(A[0]); - create_dll(A,n); - Display(); - cout<"); + last = node; + node = node.next; + } + if(node == null) + System.out.print("null"); + System.out.println(); + + } +} +class Main{ + public static void main(String[] args) + { + /* Start with the empty list */ + Doubly_linkedList dll = new Doubly_linkedList(); + + // Insert 40. + dll.insert_end(40); + + // Insert 20 at the beginning. + dll.insert_front(20); + + // Insert 10 at the beginning. + dll.insert_front(10); + + // Insert 50 at the end. + dll.insert_end(50); + + // Insert 30, after 20. + dll.Insert_After(dll.head.next, 30); + + System.out.println("Doubly linked list created is as follows: "); + dll.displaylist(dll.head); + } +} diff --git a/stock_buy_and_sell_variations/stock_first_type b/stock_buy_and_sell_variations/stock_first_type new file mode 100644 index 00000000..e69de29b