diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000..a1b154b5 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +custom: https://www.buymeacoffee.com/swaazshetty diff --git a/C/README.md b/C/README.md index 8a0b269a..e484fb77 100644 --- a/C/README.md +++ b/C/README.md @@ -72,3 +72,4 @@ | Program-68 | Program to accept 0s and 1s as input and print if it consists 3 consecutive 0s | | Program-69 | Program to find strong number | | Program-70 | Program to make a simple calculator | +| Program-71 | Program to implement stack using array & linked list | diff --git a/C/program-71/program.c b/C/program-71/program.c new file mode 100644 index 00000000..daee7a94 --- /dev/null +++ b/C/program-71/program.c @@ -0,0 +1,231 @@ +// Implementing stack using Array in C language + +// ------------------------------------------------------- + +#include +#include + +struct Stack{ + int size; + int top; + int *arr; +}; + +// ---- operations to perform : +// isEmpty- +// isFull- +// push- +// pop- +// peek- +// stackTop- +// stackBottom- + +// ------------------------------------- + +int isEmpty(struct Stack * ptr){ + if(ptr->top == -1) + return 1; + else + return 0; +} +// ------------------------------------- + +int isFull(struct Stack * ptr){ + if(ptr->top == ptr->size-1) + //if(ptr->top == ptr->arr[ptr->top]) + return 1; + else + return 0; +} +// ------------------------------------- + +void push(struct Stack *ptr, int data){ + if(isFull(ptr)){ + printf("Stack Overflow for data %d \n", data); + } + else{ + ptr->top+=1; + ptr->arr[ptr->top] = data; + } +} +// ------------------------------------- + +int pop(struct Stack *ptr){ + if(isEmpty(ptr)) + printf("Stack Underflow"); + else{ + int p = ptr->arr[ptr->top]; + ptr->top-=1; + return p; + } +} +// ------------------------------------- + +int peek(struct Stack *ptr, int i){ + int arrInd = ptr->top-i+1; + if(arrInd <= -1) + printf("Not valid position."); + else + return ptr->arr[arrInd]; + return -1; +} +// ------------------------------------- + +int stackTop(struct Stack *ptr){ + return ptr->arr[ptr->top]; +} +// ------------------------------------- + +int stackBottom(struct Stack *ptr){ + return ptr->arr[0]; +} +// ------------------------------------- + +int main(){ + struct Stack *s = (struct Stack *)malloc(sizeof(struct Stack)); + s->size=10; + s->top=-1; + s->arr=(int *)malloc(s->size*sizeof(int)); + + printf("Before pushing: Full %d\n",isFull(s)); + printf("Before pushing: Empty %d\n",isEmpty(s)); + + push(s, 1); + push(s, 2); + push(s, 3); + push(s, 4); + push(s, 5); + push(s, 6); + push(s, 7); + push(s, 8); + push(s, 9); + push(s, 10); + push(s, 11); + push(s, 12); + push(s, 13); + + printf("After pushing: Full %d\n",isFull(s)); + printf("After pushing: Empty %d\n",isEmpty(s)); + + printf("\n----------------------\n"); + printf("Peeking through stack\n"); + for(int j=1;jtop+2;j++){ + printf("The value at position %d is %d\n", j, peek(s, j)); + } + + printf("\nPopped %d from stack", pop(s)); + printf("\nPopped %d from stack", pop(s)); + printf("\nPopped %d from stack", pop(s)); + printf("\nPopped %d from stack\n\n", pop(s)); + + printf("After popping: Full %d\n",isFull(s)); + printf("After popping: Empty %d\n",isEmpty(s)); + + printf("\n----------------------\n"); + printf("Peeking through stack\n"); + for(int j=1;jtop+2;j++){ + printf("The value at position %d is %d\n", j, peek(s, j)); + } + + printf("\nStack at top now is %d", stackTop(s)); + printf("\nStack at bottom now is %d", stackBottom(s)); + + return 0; +} + +// ------------------------------------------------------------------------------------------------------ + +// Implementing Stack using Linked List in C language + +#include +#include + +struct Node{ + int data; + struct Node *next; +}; + +// isEmpty +// isFull +// push +// pop +struct Node * top = NULL; + +void traverseLinkedList(struct Node *ptr){ + while(ptr!=NULL){ + printf("Element is %d\n", ptr->data); + ptr=ptr->next; + } +} + +int isEmpty(struct Node *top){ + if(top == NULL) + return 1; + else + return 0; +} + +int isFull(struct Node *top){ + struct Node *n = (struct Node*)malloc(sizeof(struct Node)); + if (n==NULL) + return 1; + else + return 0; +} + +//int pop(struct Node **top){ +// if(isEmpty(top)) +// printf("Stack Underflow"); +// else{ +// struct Node *p = *top; +// *top = (*top) -> next; +// int m=p->data; +// return m; +// } +//} + +int pop(struct Node *tp){ + if(isEmpty(top)) + printf("Stack Underflow"); + else{ + struct Node *p = tp; + top = (tp) -> next; + int m=p->data; + free(p); + return m; + } +} + +struct Node *push(struct Node *top, int x){ + if (isFull(top)) + return 1; + else{ + struct Node *pr = (struct Node*)malloc(sizeof(struct Node)); + pr->data = x; + pr->next = top; + top = pr; + return top; + } +} + +int main(){ + //struct Node * top = NULL; + top = push(top, 3); + top = push(top, 6); + top = push(top, 11); + top = push(top, 23); + top = push(top, 47); + + traverseLinkedList(top); + + // int element=pop(&top); + int element=pop(top); + printf("Popped element is %d \n", element); + + traverseLinkedList(top); + + return 0; + +} + + diff --git a/Python/README.md b/Python/README.md index b17427a9..ea61a89e 100644 --- a/Python/README.md +++ b/Python/README.md @@ -33,3 +33,4 @@ | Program-29 | Program which will display the fibonacci series | | Program-30 | Program to make simple calculator | | Program-31 | Program to print Amstrong numbers | +| Program-32 | Program to implement menu-driven list implementation like traversal, inserting single/multiple element, deletion with index/value | diff --git a/Python/program-32/program.py b/Python/program-32/program.py index 697d0a8f..5c4e0160 100644 --- a/Python/program-32/program.py +++ b/Python/program-32/program.py @@ -1,22 +1,91 @@ -def make_readable(s): - t = ['00','00','00'] - def c(a): - if len(a) == 2 : return str(a) - else: return '0'+str(a) - - if s < 60: - t[2] = c(str(s)) - - elif s >= 60 and s < 3600: - t[1] = c(str(int(s/60))) - t[2] = c(str(s%60)) - - else: - t[0] = c(str(int(s/3600))) - t[1] = c(str(int((s%3600)/60))) - t[2] = c(str(((s%3600)%60))) - - return f'{t[0]}:{t[1]}:{t[2]}' - -# print(make_readable(359999)) output: 99:59:59 -# print(make_readable(9678)) output: 2:41:18 \ No newline at end of file +# Made by Nitin Kumar +# using purely python language +# and list 'data structure' type +# comments are added for clear understanding of every code used +# open source for learning + + +# -----------------------------DATA STRUCTURE IN PYTHON USING LIST---------------------------------------- + + +# declaring & initializing list named 'tree' +tree=[] + +# option by default 'y' +ans = 'Y' + +# loop to constantly ask what to do +while ans == 'Y' or ans == 'y': + + # FUNCTION USED IN THIS DATA STRUCTURE + + # function to insert multiple items in lists + def insert_items(): + nol = int(input("Enter the number of items to fill in the tree:\t")) + for i in range(0,nol): + i = int(input("Enter the items:\t")) + tree.append(i) + + # function to display items in the list + def display_items(): + if tree == []: + print("You haven't added any item !!") + else: + print("The tree made is",tree) + + # funtion to add one item to the list + def add_item(): + index = int(input("Enter the index of element after which you want to add item:\t")) + item = int(input("Enter the value:\t")) + tree.insert(index,item) + + # function to delete item in list + + # function to delete item when value of item to be deleted is known + def delete_item_value(): + value = int(input("Enter the value of element which you want to delete:\t")) + tree.remove(value) + + # function to delete item when index of item to be deleted is known + def delete_item_index(): + index = int(input("Enter the index of element which you want to delete:\t")) + tree.pop(index) + + # MAIN THING TO BE DISPLAYED EVERYTIME YOU TYPE 'Y' OR 'y' TO DO TASKS INSIDE THE TREE + + # 1. ADD MULTIPLE ITEMS IN TREE + # 2. DISPLAY ITEMS + # 3. ADD ONE ITEM ONLY + # 4. DELETE ITEM IF + # 41. VALUE OF ITEM TO BE DELETED IS KNOWN + # 42. INDEX OF ITEM TO BE DELETED IS KNOWN + # 5. EXIT + + try: + print("\n\n========================================") + option = int(input("What do you want to do ? \n1. Add multiple items\n2. Display items\n3. Add one item\n4. Delete item when\n\t 41. You know the value of item\n\t 42. You know the index of item\n5. Exit\n\nYOUR ANSWER HERE\t")) + print("========================================\n\n") + + # CONDITIONS TO BE FOLLOWED FOR SUCCESSFUL EXECUTION + + if option == 1: + insert_items() + elif option == 2: + display_items() + elif option == 3: + add_item() + elif option == 41: + delete_item_value() + elif option == 42: + delete_item_index() + elif option == 5: + exit() + else: + print("Invalid option selected , Try again !!") + + except: + print("Please choose number from 1 to 5") + + + #Question asked for next iteration + ans = input("Do you want to continue ? ( type 'Y' or 'y' to continue )\t")