Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions CompetitiveProgramming/Linked List/addAtBeginList.c
Original file line number Diff line number Diff line change
@@ -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<stdio.h>
#include<stdlib.h>
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!
}
}
36 changes: 36 additions & 0 deletions CompetitiveProgramming/Linked List/addAtMiddleList.c
Original file line number Diff line number Diff line change
@@ -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(i<loc){
p = p->link;
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;
}
}
28 changes: 28 additions & 0 deletions CompetitiveProgramming/Linked List/appendList.c
Original file line number Diff line number Diff line change
@@ -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;
}
}
38 changes: 38 additions & 0 deletions CompetitiveProgramming/Linked List/deleteList.c
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 23 additions & 0 deletions CompetitiveProgramming/Linked List/displayList.c
Original file line number Diff line number Diff line change
@@ -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!
}
}
70 changes: 70 additions & 0 deletions CompetitiveProgramming/Linked List/listSetup.c
Original file line number Diff line number Diff line change
@@ -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<stdio.h>
#include<stdlib.h>

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!