diff --git a/c/01.Singly_Linked_List.c b/c/01.Singly_Linked_List.c new file mode 100644 index 0000000..8137d00 --- /dev/null +++ b/c/01.Singly_Linked_List.c @@ -0,0 +1,428 @@ +/* + * C Program to Implement Singly Linked List using Dynamic Memory Allocation + */ +#include +#include +#define ISEMPTY printf("\nEMPTY LIST:"); +/* + * Node Declaration + */ +typedef struct node +{ + int value; + struct node *next; +} snode; + +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 *); + +snode *newnode, *ptr, *prev, *temp; +snode *first = NULL, *last = NULL; + +/* + * Main :contains menu + */ + +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; +} + +/* + * Creating Node + */ +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; + } +} + +/* + * Inserting Node at First + */ +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----"); +} + +/* + * Inserting Node at Last + */ +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----"); +} + +/* + * Inserting Node at position + */ +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"); + } +} + +/* + * Sorted Linked List + */ +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); + } + } +} + +/* + * Delete Node from specified position in a non-empty list + */ +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"); + } + } +} +/* + * Updating Node value in a non-empty list + */ +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"); + } + } +} + +/* + * searching an element in a non-empty 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); + } + } +} +/* + * Displays non-empty List from Beginning to End + */ +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); + } + } +} + +/* + * Display non-empty list in Reverse Order + */ +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 diff --git a/c/02.Doubly_linked_list.c b/c/02.Doubly_linked_list.c new file mode 100644 index 0000000..3027c8c --- /dev/null +++ b/c/02.Doubly_linked_list.c @@ -0,0 +1,83 @@ + +#include +#include + +struct Node { + int data; + struct Node* next; + struct Node* prev; +}; + +struct Node* head; + + +struct Node* GetNewNode(int x) { + struct Node* newNode + = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = x; + newNode->prev = NULL; + newNode->next = NULL; + return newNode; +} + +void InsertAtHead(int x) { + struct Node* newNode = GetNewNode(x); + if(head == NULL) { + head = newNode; + return; + } + head->prev = newNode; + newNode->next = head; + head = newNode; +} + +void InsertAtTail(int x) { + struct Node* temp = head; + struct Node* newNode = GetNewNode(x); + if(head == NULL) { + head = newNode; + return; + } + while(temp->next != NULL) temp = temp->next; + temp->next = newNode; + newNode->prev = temp; +} + +void Print() { + struct Node* temp = head; + printf("Forward: "); + while(temp != NULL) { + printf("%d ",temp->data); + temp = temp->next; + } + printf("\n"); +} + +void ReversePrint() { + struct Node* temp = head; + if(temp == NULL) return; + + while(temp->next != NULL) { + temp = temp->next; + } + + printf("Reverse: "); + while(temp != NULL) { + printf("%d ",temp->data); + temp = temp->prev; + } + printf("\n"); +} + +int main() { + + + head = NULL; + + + InsertAtTail(2); Print(); ReversePrint(); + InsertAtTail(4); Print(); ReversePrint(); + InsertAtHead(6); Print(); ReversePrint(); + InsertAtTail(8); Print(); ReversePrint(); + +} diff --git a/c/03.Circular_Linked_List.c b/c/03.Circular_Linked_List.c new file mode 100644 index 0000000..9b5b99e --- /dev/null +++ b/c/03.Circular_Linked_List.c @@ -0,0 +1,361 @@ +/* + * C Program to Demonstrate Circular Single Linked List + * source - https://www.sanfoundry.com/c-program-implement-circular-single-linked-list/ + */ +#include +#include + +struct node +{ + int data; + struct node *link; +}; + +struct node *head = NULL, *x, *y, *z; + +void create(); +void ins_at_beg(); +void ins_at_pos(); +void del_at_beg(); +void del_at_pos(); +void traverse(); +void search(); +void sort(); +void update(); +void rev_traverse(struct node *p); + +void main() +{ + int ch; + + printf("\n 1.Creation \n 2.Insertion at beginning \n 3.Insertion at remaining"); + printf("\n4.Deletion at beginning \n5.Deletion at remaining \n6.traverse"); + printf("\n7.Search\n8.sort\n9.update\n10.Exit\n"); + while (1) + { + printf("\n Enter your choice:"); + scanf("%d", &ch); + switch(ch) + { + case 1: + create(); + break; + case 2: + ins_at_beg(); + break; + case 3: + ins_at_pos(); + break; + case 4: + del_at_beg(); + break; + case 5: + del_at_pos(); + break; + case 6: + traverse(); + break; + case 7: + search(); + break; + case 8: + sort(); + break; + case 9: + update(); + break; + case 10: + rev_traverse(head); + break; + default: + exit(0); + } + } +} + +/*Function to create a new circular linked list*/ +void create() +{ + int c; + + x = (struct node*)malloc(sizeof(struct node)); + printf("\n Enter the data:"); + scanf("%d", &x->data); + x->link = x; + head = x; + printf("\n If you wish to continue press 1 otherwise 0:"); + scanf("%d", &c); + while (c != 0) + { + y = (struct node*)malloc(sizeof(struct node)); + printf("\n Enter the data:"); + scanf("%d", &y->data); + x->link = y; + y->link = head; + x = y; + printf("\n If you wish to continue press 1 otherwise 0:"); + scanf("%d", &c); + } +} + +/*Function to insert an element at the begining of the list*/ + +void ins_at_beg() +{ + x = head; + y = (struct node*)malloc(sizeof(struct node)); + printf("\n Enter the data:"); + scanf("%d", &y->data); + while (x->link != head) + { + x = x->link; + } + x->link = y; + y->link = head; + head = y; +} + +/*Function to insert an element at any position the list*/ + +void ins_at_pos() +{ + struct node *ptr; + int c = 1, pos, count = 1; + + y = (struct node*)malloc(sizeof(struct node)); + if (head == NULL) + { + printf("cannot enter an element at this place"); + } + printf("\n Enter the data:"); + scanf("%d", &y->data); + printf("\n Enter the position to be inserted:"); + scanf("%d", &pos); + x = head; + ptr = head; + while (ptr->link != head) + { + count++; + ptr = ptr->link; + } + count++; + if (pos > count) + { + printf("OUT OF BOUND"); + return; + } + while (c < pos) + { + z = x; + x = x->link; + c++; + } + y->link = x; + z->link = y; +} + +/*Function to delete an element at any begining of the list*/ + +void del_at_beg() +{ + if (head == NULL) + printf("\n List is empty"); + else + { + x = head; + y = head; + while (x->link != head) + { + x = x->link; + } + head = y->link; + x->link = head; + free(y); + } +} + +/*Function to delete an element at any position the list*/ + +void del_at_pos() +{ + if (head == NULL) + printf("\n List is empty"); + else + { + int c = 1, pos; + printf("\n Enter the position to be deleted:"); + scanf("%d", &pos); + x = head; + while (c < pos) + { + y = x; + x = x->link; + c++; + } + y->link = x->link; + free(x); + } +} + +/*Function to display the elements in the list*/ + +void traverse() +{ + if (head == NULL) + printf("\n List is empty"); + else + { + x = head; + while (x->link != head) + { + printf("%d->", x->data); + x = x->link; + } + printf("%d", x->data); + } +} + +/*Function to search an element in the list*/ + +void search() +{ + int search_val, count = 0, flag = 0; + printf("\nenter the element to search\n"); + scanf("%d", &search_val); + if (head == NULL) + printf("\nList is empty nothing to search"); + else + { + x = head; + while (x->link != head) + { + if (x->data == search_val) + { + printf("\nthe element is found at %d", count); + flag = 1; + break; + } + count++; + x = x->link; + } + if (x->data == search_val) + { + printf("element found at postion %d", count); + } + if (flag == 0) + { + printf("\nelement not found"); + } + + } +} + +/*Function to sort the list in ascending order*/ + +void sort() +{ + struct node *ptr, *nxt; + int temp; + + if (head == NULL) + { + printf("empty linkedlist"); + } + else + { + ptr = head; + while (ptr->link != head) + { + nxt = ptr->link; + while (nxt != head) + { + if (nxt != head) + { + if (ptr->data > nxt->data) + { + temp = ptr->data; + ptr->data = nxt->data; + nxt->data = temp; + } + } + else + { + break; + } + nxt = nxt->link; + } + ptr = ptr->link; + } + } +} + +/*Function to update an element at any position the list*/ +void update() +{ + struct node *ptr; + int search_val; + int replace_val; + int flag = 0; + + if (head == NULL) + { + printf("\n empty list"); + } + else + { + printf("enter the value to be edited\n"); + scanf("%d", &search_val); + fflush(stdin); + printf("enter the value to be replace\n"); + scanf("%d", &replace_val); + ptr = head; + while (ptr->link != head) + { + if (ptr->data == search_val) + { + ptr->data = replace_val; + flag = 1; + break; + } + ptr = ptr->link; + } + if (ptr->data == search_val) + { + ptr->data = replace_val; + flag = 1; + } + if (flag == 1) + { + printf("\nUPdate sucessful"); + } + else + { + printf("\n update not successful"); + } + } +} + +/*Function to display the elements of the list in reverse order*/ + +void rev_traverse(struct node *p) +{ + int i = 0; + + if (head == NULL) + { + printf("empty linked list"); + } + else + { + if (p->link != head) + { + i = p->data; + rev_traverse(p->link); + printf(" %d", i); + } + if (p->link == head) + { + printf(" %d", p->data); + } + } +} \ No newline at end of file