From 98c7521ed2db39f02f73f368355987813b528af8 Mon Sep 17 00:00:00 2001 From: sujalgera01 Date: Sun, 7 Mar 2021 00:16:03 +0530 Subject: [PATCH 1/2] start adding Linked List in DSA --- .../Linked List/addAtBeginList.c | 99 +++++++++++++++++++ .../Linked List/addAtMiddleList.c | 36 +++++++ .../Linked List/listSetup.c | 70 +++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 CompetitiveProgramming/Linked List/addAtBeginList.c create mode 100644 CompetitiveProgramming/Linked List/addAtMiddleList.c create mode 100644 CompetitiveProgramming/Linked List/listSetup.c diff --git a/CompetitiveProgramming/Linked List/addAtBeginList.c b/CompetitiveProgramming/Linked List/addAtBeginList.c new file mode 100644 index 0000000..4fa61ff --- /dev/null +++ b/CompetitiveProgramming/Linked List/addAtBeginList.c @@ -0,0 +1,99 @@ +/* +Lets make the new node and try to add in the beginning of the first node. + +Make the node by creating temp variable then try to make the links and connections to +the data part and link part of first node. + +------IMPORTANT--------- +Always try to make the connection from right side i.e. link part bcoz if we make the data part connection first +then we will loose the root element address. So always make link connection first with other nodes.. + +*/ +#include +#include +struct node +{ + int data; + struct node* link; +}; + +struct node* root = NULL; +void main() +{ + int ch; + while(1) + { + printf("1.addAtBegin"); + printf("2.addAtEnd"); + printf("3.Display"); + printf("4.Quit"); + printf("Enter your choice: "); + scanf("%d",&ch); + switch(ch) + { + case 1: addAtBegin() + break; + case 2: addAtEnd() + break; + case 3: display() + break; + case 4: exit(-1) + default: printf("Invalid\n"); + } + } +} + + +void addAtBegin(){ + + struct node* temp; + temp = (struct node*)malloc(sizeof(struct node)); + printf("Enter node data"); + scanf("%d",&temp->data); + temp->link = NULL; + + if(root == NULL){ + root = temp; + } + else{ + temp->link = root; //Left side Connection with first node. + root = temp; //Right side Connection with root node. + } + +} + +void append(){ + struct node* temp; //Initializing the node... + temp = (struct node*)malloc(sizeof(struct node)); + printf("Enter node data"); // Enter data value and initially link to null value. + scanf("%d",&temp -> data); + temp -> link = NULL; + + if(root == NULL){ + root = temp; + } + else{ + struct node* p; //Traversing till the end node and then adding the newly created node. + p = root; + while(p -> link != NULL){ + p = p->link; + } + p->link = temp; + } +} + +void display(){ + struct node* temp; + temp = root; + + if(root == NULL){ + printf("Empty List"); + } + else{ + while(temp != NULL){ + printf("%d ->",temp->data); + temp = temp->link; + } + printf("%d", temp->data) //Printing last element where temp is! + } +} \ No newline at end of file diff --git a/CompetitiveProgramming/Linked List/addAtMiddleList.c b/CompetitiveProgramming/Linked List/addAtMiddleList.c new file mode 100644 index 0000000..531fe87 --- /dev/null +++ b/CompetitiveProgramming/Linked List/addAtMiddleList.c @@ -0,0 +1,36 @@ +/* +Lets make the new node and try to add in the middle of the first node. + +Now we have to add the node in middle of 2 nodes. So we will first make connection of link part with right node and then +the connection of left node to the new node. + +To do this we need 2 pointers because we will enter the value from user where we have to insert the new node that +is called location then after the location we will add newly created node..... + +*/ + +void addAtMiddle(){ + struct node* temp; + struct node* p; + int i, len, loc; + + printf("Enter the location"); + scanf("%d",&loc); + + len = length(); //length function defined in next commit. + if(loc > len){ + printf("wrong Location"); + } + else{ + p = root; + while(ilink; + i++; + } + temp = (struct node*)malloc(sizeof(struct node)); + printf("Emter node data"); + scanf("%d",&temp->data); + temp->link = p->link; //Building Connections. + p->link = temp; + } +} \ No newline at end of file diff --git a/CompetitiveProgramming/Linked List/listSetup.c b/CompetitiveProgramming/Linked List/listSetup.c new file mode 100644 index 0000000..b1d891c --- /dev/null +++ b/CompetitiveProgramming/Linked List/listSetup.c @@ -0,0 +1,70 @@ +/* +Now let us start with Linked Lists. + +It is a type of dynamic collection. Elements are stored in the form of nodes. +Nodes contains two parts: +1) data part. (Stores data inside) +2) link part. (connect one node to other)(Stores the address of the previous node.) + +There are 3 types of linked list i.e. +.Single linked Lists +.Double linked Lists +.Circular linked Lists + +First we will discuss "single linked list" and various operations on it. + +So, let's make a setup int his and we will use this a s a common for all the operations. +(Also called Implementation) +*/ + +#include +#include + +struct node{ //Node Creation + int data; + struct node* link; +}; +struct node* root = NULL; //First node always connect to root node that contains only address + +void main(){ + int choice; + while(1){ + printf("1. Add at End\n"); + printf("2. Add at Begin\n"); + printf("3. Add at Middle\n"); + printf("4. Delete the node\n"); + printf("5. Display\n"); + printf("6. Length\n"); + printf("7. Node Swap\n"); + printf("8. Reverse the list\n"); + printf("9. Sort\n"); + printf("10. Quit\n"); + + printf("enter the choice"); + scanf("%d",&choice); + + switch(choice){ + case 1: append(); //Fn's that we are going to perform on Single LInked list. + break; + case 2: addAtBegin(); + break; + case 3: addAtMiddle(); + break; + case 4: delete(); + break; + case 5: display(); + break; + case 6: length(); + break; + case 7: swap(); + break; + case 8: sort(); + break; + case 9: exit(0); + break; + default : printf("Wrong Choice"); + } + } +} + +// Will be adding regularly about the List.... Stay Tuned! \ No newline at end of file From 4d5fe22067b05b042d565d4365625dc2583f245b Mon Sep 17 00:00:00 2001 From: sujalgera01 Date: Fri, 19 Mar 2021 14:41:09 +0530 Subject: [PATCH 2/2] more on linked list --- .../Linked List/appendList.c | 28 ++++++++++++++ .../Linked List/deleteList.c | 38 +++++++++++++++++++ .../Linked List/displayList.c | 23 +++++++++++ 3 files changed, 89 insertions(+) create mode 100644 CompetitiveProgramming/Linked List/appendList.c create mode 100644 CompetitiveProgramming/Linked List/deleteList.c create mode 100644 CompetitiveProgramming/Linked List/displayList.c diff --git a/CompetitiveProgramming/Linked List/appendList.c b/CompetitiveProgramming/Linked List/appendList.c new file mode 100644 index 0000000..8e34957 --- /dev/null +++ b/CompetitiveProgramming/Linked List/appendList.c @@ -0,0 +1,28 @@ +/* +Let's start writing the code to append the list. We have to check for if list contains elements +before the insertion or not. + +Then start adding the nodes to the list by creating the temporary node that will +vanish off after the addition. + +*/ + +void append(){ + struct node* temp; //Initializing the node... + temp = (struct node*)malloc(sizeof(struct node)); + printf("Enter node data"); // Enter data value and initially link to null value. + scanf("%d",&temp -> data); + temp -> link = NULL; + + if(root == NULL){ + root = temp; + } + else{ + struct node* p; //Traversing till the end node and then adding the newly created node. + p = root; + while(p -> link != NULL){ + p = p->link; + } + p->link = temp; + } +} \ No newline at end of file diff --git a/CompetitiveProgramming/Linked List/deleteList.c b/CompetitiveProgramming/Linked List/deleteList.c new file mode 100644 index 0000000..d87e1ff --- /dev/null +++ b/CompetitiveProgramming/Linked List/deleteList.c @@ -0,0 +1,38 @@ +/* +Now let's start deleting the node from the beginning or any location you want. +It means you can remove any node from the list just by giving the location. + +*/ + +void delete(){ + struct node* temp; + int loc,len; + printf("Enter the location: "); // Ask the location from the user + scanf("%d",&loc); + +len = length() + if(loc > len){ + printf("Invalid Location"); // Check if loc is valid or not. + } + + else if(loc == 1){ //Deleting the node at beginning... + temp = root; + root = temp->link; + temp->link = NULL; + free(temp); + } + + else{ // Deleting the node from somewhere in the middle or at the end. + struct node* p; + struct node* q; + p = root; + while(i < loc-1){ + p = p->link; + i++; + } + q = p->link; + p->link = q->link; + q->link = NULl; + free(q); + } +} \ No newline at end of file diff --git a/CompetitiveProgramming/Linked List/displayList.c b/CompetitiveProgramming/Linked List/displayList.c new file mode 100644 index 0000000..28891d4 --- /dev/null +++ b/CompetitiveProgramming/Linked List/displayList.c @@ -0,0 +1,23 @@ +/* +So, now its time to display the list after we have append it. +We have added the nodes in the beggining, middle and at the end so lets look at the elements +we have entered. + + +*/ + +void display(){ + struct node* temp; + temp = root; + + if(root == NULL){ + printf("Empty List"); + } + else{ + while(temp != NULL){ + printf("%d ->",temp->data); + temp = temp->link; + } + printf("%d", temp->data) //Printing last element where temp is! + } +} \ No newline at end of file