From 6dcaf68b4bd76555e3611c9bf8a8f49dc6e8a003 Mon Sep 17 00:00:00 2001 From: Akash Date: Sat, 27 Oct 2018 14:28:56 +0530 Subject: [PATCH 1/4] added circular queue code for python --- circularQueue | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/circularQueue b/circularQueue index 8b13789..aa60efc 100644 --- a/circularQueue +++ b/circularQueue @@ -1 +1,31 @@ +class Node(object): + def __init__(self, item = None): + self.item = [None] * 4 + self.next = None + self.previous = None +class CircularQueue(object): + def __init__(self): + self.length = 0 + self.head = None + self.tail = None + def enqueue(self, x): + newNode = Node(x) + newNode.next = None + if self.head == None: + self.head = newNode + self.tail = newNode + elif self.length < 4: + self.tail.next = newNode + newNode.previous = self.tail + self.tail = newNode + else: + self.tail = (self.tail + 1) % 4 + self.length += 1 + def dequeue(self): + if self.count == 0: + print ("The Queue is empty!") + self.count -= 1 + return self.item.pop() + def size(self): + return self.length From 24082993c8e8a7a638be1aa70eb010056dc6d41b Mon Sep 17 00:00:00 2001 From: Akash Date: Sat, 27 Oct 2018 15:00:07 +0530 Subject: [PATCH 2/4] added palindrome code for python --- palindrome | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/palindrome b/palindrome index 31741f9..364d91a 100644 --- a/palindrome +++ b/palindrome @@ -1,9 +1,20 @@ - -A palindrome is a sequence of characters which reads the same backward as forward. -"noon","malayalam",12321, 4554 are few examples of palindrome. -We can check if a number is a palindrome or not by taking the reverse of the number and comparing it with the original number. -To find the reverse of a number the individual digits are taken one by one from the right.Initially rev=0.At each stage rev is multiplied by 10 and added to digit, -rev found at the end the process of taking individual digits gives the reverse of the original number. -We can check if a string is a palindrome or not by taking the reverse of the string and comparing it with the original string. -To do this,the first character is compared with the last character.Then the second character is compared with last but one character and so on. -If at any stage in this process, the characters are not matched then the given string is not a palindrome. \ No newline at end of file +def reverse(s): + return s[::-1] + +def isPalindrome(s): + + rev = reverse(s) + + # Checking if both string are equal or not + if (s == rev): + return True + return False + + +s = "malayalam" +ans = isPalindrome(s) + +if ans == 1: + print("Yes") +else: + print("No") \ No newline at end of file From 9b62fdfa941195a4762cf1d90fdce8e1d7f45cae Mon Sep 17 00:00:00 2001 From: Akash Date: Sat, 27 Oct 2018 15:39:47 +0530 Subject: [PATCH 3/4] added code --- Algorithm | 1 + linkedList | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 160000 Algorithm diff --git a/Algorithm b/Algorithm new file mode 160000 index 0000000..658ae37 --- /dev/null +++ b/Algorithm @@ -0,0 +1 @@ +Subproject commit 658ae371e7392ff6b4923153d62609772db3ed95 diff --git a/linkedList b/linkedList index 2b93b0c..c70cf58 100644 --- a/linkedList +++ b/linkedList @@ -1,4 +1,4 @@ -Linked lists are a liner data structure. In this Every element is a seperate object. + hey guys ,Linked lists are a liner data structure. In this Every element is a seperate object. Here the elements are not stored in contiguous locations.Elements are linked with each other with help of pointers. As the size of array is fixed we can use linked lists instead as they provide the liberty to have a dynamic or non-fixed length. Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted. From 93ec6db237bf7c25f57e03e3d68a04ad4e65faef Mon Sep 17 00:00:00 2001 From: Akash Date: Sat, 27 Oct 2018 23:09:08 +0530 Subject: [PATCH 4/4] singly linked list code --- linkedList | 418 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 400 insertions(+), 18 deletions(-) diff --git a/linkedList b/linkedList index c70cf58..f32816a 100644 --- a/linkedList +++ b/linkedList @@ -1,18 +1,400 @@ - hey guys ,Linked lists are a liner data structure. In this Every element is a seperate object. -Here the elements are not stored in contiguous locations.Elements are linked with each other with help of pointers. -As the size of array is fixed we can use linked lists instead as they provide the liberty to have a dynamic or non-fixed length. -Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted. -But in linked lists new elements can be inserted or deleted with ease. -A linked list is represented by a pointer to the first node of the linked list. The first node is called head. If the linked list is empty, then value of head is NULL. -Every element of a list is called as node. Node has 2 parts ie, info part and link part. -An example for list in C -struct Node -{ - int data; - struct Node *link; -}; -We might think that linked lists are full of advantages but it's not true there are following dissadvantages: -1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists efficiently with its default implementation. Read about it here. -2) Extra memory space for a pointer is required with each element of the list. -3) Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not there in case of linked lists. -Hence we can say that no data structure is best, it all depends on the way we use a specific data structure. +#include +#include +#define ISEMPTY printf("\nEMPTY LIST:"); + +struct node +{ + int value; + struct node *next; +}; + +snode* create_node(int); +void insert_node_first(); +void insert_node_last(); +void insert_node_pos(); +void sorted_ascend(); +void delete_pos(); +void search(); +void update_val(); +void display(); +void rev_display(snode *); + +typedef struct node snode; +snode *newnode, *ptr, *prev, *temp; +snode *first = NULL, *last = NULL; + + + int main() + { + int ch; + char ans = 'Y'; + + while (ans == 'Y'||ans == 'y') + { + printf("\n---------------------------------\n"); + printf("\nOperations on singly linked list\n"); + printf("\n---------------------------------\n"); + printf("\n1.Insert node at first"); + printf("\n2.Insert node at last"); + printf("\n3.Insert node at position"); + printf("\n4.Sorted Linked List in Ascending Order"); + printf("\n5.Delete Node from any Position"); + printf("\n6.Update Node Value"); + printf("\n7.Search Element in the linked list"); + printf("\n8.Display List from Beginning to end"); + printf("\n9.Display List from end using Recursion"); + printf("\n10.Exit\n"); + printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); + printf("\nEnter your choice"); + scanf("%d", &ch); + + switch (ch) + { + case 1: + printf("\n...Inserting node at first...\n"); + insert_node_first(); + break; + case 2: + printf("\n...Inserting node at last...\n"); + insert_node_last(); + break; + case 3: + printf("\n...Inserting node at position...\n"); + insert_node_pos(); + break; + case 4: + printf("\n...Sorted Linked List in Ascending Order...\n"); + sorted_ascend(); + break; + case 5: + printf("\n...Deleting Node from any Position...\n"); + delete_pos(); + break; + case 6: + printf("\n...Updating Node Value...\n"); + update_val(); + break; + case 7: + printf("\n...Searching Element in the List...\n"); + search(); + break; + case 8: + printf("\n...Displaying List From Beginning to End...\n"); + display(); + break; + case 9: + printf("\n...Displaying List From End using Recursion...\n"); + rev_display(first); + break; + case 10: + printf("\n...Exiting...\n"); + return 0; + break; + default: + printf("\n...Invalid Choice...\n"); + break; + } + printf("\nYOU WANT TO CONTINUE (Y/N)"); + scanf(" %c", &ans); + } + return 0; + } + +snode* create_node(int val) +{ + newnode = (snode *)malloc(sizeof(snode)); + if (newnode == NULL) + { + printf("\nMemory was not allocated"); + return 0; + } + else + { + newnode->value = val; + newnode->next = NULL; + return newnode; + } +} + + +void insert_node_first() +{ + int val; + + printf("\nEnter the value for the node:"); + scanf("%d", &val); + newnode = create_node(val); + if (first == last && first == NULL) + { + first = last = newnode; + first->next = NULL; + last->next = NULL; + } + else + { + temp = first; + first = newnode; + first->next = temp; + } + printf("\n----INSERTED----"); +} + + +void insert_node_last() +{ + int val; + + printf("\nEnter the value for the Node:"); + scanf("%d", &val); + newnode = create_node(val); + if (first == last && last == NULL) + { + first = last = newnode; + first->next = NULL; + last->next = NULL; + } + else + { + last->next = newnode; + last = newnode; + last->next = NULL; + } + printf("\n----INSERTED----"); +} + + +void insert_node_pos() +{ + int pos, val, cnt = 0, i; + + printf("\nEnter the value for the Node:"); + scanf("%d", &val); + newnode = create_node(val); + printf("\nEnter the position "); + scanf("%d", &pos); + ptr = first; + while (ptr != NULL) + { + ptr = ptr->next; + cnt++; + } + if (pos == 1) + { + if (first == last && first == NULL) + { + first = last = newnode; + first->next = NULL; + last->next = NULL; + } + else + { + temp = first; + first = newnode; + first->next = temp; + } + printf("\nInserted"); + } + else if (pos>1 && pos<=cnt) + { + ptr = first; + for (i = 1;i < pos;i++) + { + prev = ptr; + ptr = ptr->next; + } + prev->next = newnode; + newnode->next = ptr; + printf("\n----INSERTED----"); + } + else + { + printf("Position is out of range"); + } +} + + +void sorted_ascend() +{ + snode *nxt; + int t; + + if (first == NULL) + { + ISEMPTY; + printf(":No elements to sort\n"); + } + else + { + for (ptr = first;ptr != NULL;ptr = ptr->next) + { + for (nxt = ptr->next;nxt != NULL;nxt = nxt->next) + { + if (ptr->value > nxt->value) + { + t = ptr->value; + ptr->value = nxt->value; + nxt->value = t; + } + } + } + printf("\n---Sorted List---"); + for (ptr = first;ptr != NULL;ptr = ptr->next) + { + printf("%d\t", ptr->value); + } + } +} + + +void delete_pos() +{ + int pos, cnt = 0, i; + + if (first == NULL) + { + ISEMPTY; + printf(":No node to delete\n"); + } + else + { + printf("\nEnter the position of value to be deleted:"); + scanf(" %d", &pos); + ptr = first; + if (pos == 1) + { + first = ptr->next; + printf("\nElement deleted"); + } + else + { + while (ptr != NULL) + { + ptr = ptr->next; + cnt = cnt + 1; + } + if (pos > 0 && pos <= cnt) + { + ptr = first; + for (i = 1;i < pos;i++) + { + prev = ptr; + ptr = ptr->next; + } + prev->next = ptr->next; + } + else + { + printf("Position is out of range"); + } + free(ptr); + printf("\nElement deleted"); + } + } +} + +void update_val() +{ + int oldval, newval, flag = 0; + + if (first == NULL) + { + ISEMPTY; + printf(":No nodes in the list to update\n"); + } + else + { + printf("\nEnter the value to be updated:"); + scanf("%d", &oldval); + printf("\nEnter the newvalue:"); + scanf("%d", &newval); + for (ptr = first;ptr != NULL;ptr = ptr->next) + { + if (ptr->value == oldval) + { + ptr->value = newval; + flag = 1; + break; + } + } + if (flag == 1) + { + printf("\nUpdated Successfully"); + } + else + { + printf("\nValue not found in List"); + } + } +} + + +void search() +{ + int flag = 0, key, pos = 0; + + if (first == NULL) + { + ISEMPTY; + printf(":No nodes in the list\n"); + } + else + { + printf("\nEnter the value to search"); + scanf("%d", &key); + for (ptr = first;ptr != NULL;ptr = ptr->next) + { + pos = pos + 1; + if (ptr->value == key) + { + flag = 1; + break; + } + } + if (flag == 1) + { + printf("\nElement %d found at %d position\n", key, pos); + } + else + { + printf("\nElement %d not found in list\n", key); + } + } +} + +void display() +{ + if (first == NULL) + { + ISEMPTY; + printf(":No nodes in the list to display\n"); + } + else + { + for (ptr = first;ptr != NULL;ptr = ptr->next) + { + printf("%d\t", ptr->value); + } + } +} + + +void rev_display(snode *ptr) +{ + int val; + + if (ptr == NULL) + { + ISEMPTY; + printf(":No nodes to display\n"); + } + else + { + if (ptr != NULL) + { + val = ptr->value; + rev_display(ptr->next); + printf("%d\t", val); + } + + } +} \ No newline at end of file