diff --git a/C/Circular_Doubly_Linked_List/.gitignore b/C/Circular_Doubly_Linked_List/.gitignore new file mode 100644 index 00000000..f8305e74 --- /dev/null +++ b/C/Circular_Doubly_Linked_List/.gitignore @@ -0,0 +1 @@ +a.out \ No newline at end of file diff --git a/C/Circular_Doubly_Linked_List/Deleting.c b/C/Circular_Doubly_Linked_List/Deleting.c new file mode 100644 index 00000000..16804979 --- /dev/null +++ b/C/Circular_Doubly_Linked_List/Deleting.c @@ -0,0 +1,208 @@ +#include +#include +typedef struct node{ + int data; + struct node *leftAddress; + struct node *rightAddress; +}Node; + +Node *start=NULL, *end=NULL; + +int sizeofList() +{ + int count=0; + Node *ptr; + ptr = start; + while(ptr->rightAddress != start) + { + count = count + 1; + ptr = ptr->rightAddress; + } + count = count + 1; + return count; +} +void add(int no) +{ + Node *newNode, *ptr, *flag; + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + if(start == NULL) + { + start = newNode; + newNode->rightAddress = start; + newNode->leftAddress = start; + end = newNode; + } + else + { + ptr = start; + flag = start; + while(ptr->rightAddress != start) + { + ptr = ptr->rightAddress; + } + ptr->rightAddress = newNode; + newNode->leftAddress = ptr; + newNode->rightAddress = start; + flag->leftAddress = newNode; + end = newNode; + } +} +void deleteBeg() +{ + Node *ptr, *flag; + ptr = start; + flag = ptr->rightAddress; + while(ptr->rightAddress != start) + { + ptr = ptr->rightAddress; + } + ptr->rightAddress = flag; + flag->leftAddress = ptr; + start = flag; +} +void deleteEnd() +{ + Node *ptr, *flag, *temp; + ptr = start; + temp = start; + while(ptr->rightAddress != start) + { + flag = ptr; + ptr = ptr->rightAddress; + } + flag->rightAddress = start; + end = flag; + temp->leftAddress = end; + free(ptr); +} +void deletePos(int pos) +{ + Node *ptr, *flag; + ptr = start; + int count = 1; + while(ptr->rightAddress != start) + { + if(count==pos) + { + flag->rightAddress = ptr->rightAddress; + ptr->rightAddress->leftAddress = flag; + free(ptr); + break; + } + flag = ptr; + ptr = ptr->rightAddress; + count = count + 1; + } +} +void delete() +{ + Node *ptr; + ptr = start; + free(ptr); +} +void display() +{ + Node *ptr; + ptr = start; + printf("<->START<->"); + while(ptr->rightAddress != start) + { + printf("%d<->",ptr->data); + ptr = ptr->rightAddress; + } + if(ptr->data == 0) + { + printf("END<->"); + return; + } + printf("%d<->",ptr->data); + printf("END<->"); +} +void revDisplay() +{ + Node *ptr; + ptr = end; + printf("<->END<->"); + while(ptr->leftAddress != end) + { + printf("%d<->",ptr->data); + ptr = ptr->leftAddress; + } + if(ptr->data == 0) + { + printf("END<->"); + return; + } + printf("%d<->",ptr->data); + printf("START<->"); +} +int main() +{ + printf("Deleting & Traversing\n"); + printf("*********************\n\n"); + printf("1: Add\n2: Delete\n3: Display\n4: Reverse Display\n0: Exit\n\n"); + int choice,no,pos; + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + int size = sizeofList(); + while(1) + { + printf("Enter Position : "); + scanf("%d",&pos); + if(pos==1) + { + if(size==1) + { + delete(); + break; + } + deleteBeg(); + break; + } + else if(pos<1 || pos>size) + { + continue; + } + else if(pos==size) + { + deleteEnd(); + break; + } + else + { + deletePos(pos); + break; + } + } + printf("\n"); + } + else if(choice==3) + { + display(); + printf("\n\n"); + } + else if(choice==4) + { + revDisplay(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Circular_Doubly_Linked_List/Inserting.c b/C/Circular_Doubly_Linked_List/Inserting.c new file mode 100644 index 00000000..016d0602 --- /dev/null +++ b/C/Circular_Doubly_Linked_List/Inserting.c @@ -0,0 +1,199 @@ +#include +#include + +typedef struct node{ + int data; + struct node *leftAddress; + struct node *rightAddress; +}Node; + +Node *start=NULL, *end=NULL; + +int sizeofList() +{ + int count=0; + Node *ptr; + ptr = start; + while(ptr->rightAddress != start) + { + count = count + 1; + ptr = ptr->rightAddress; + } + count = count + 1; + return count; +} +void add(int no) +{ + Node *newNode, *ptr, *flag; + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + if(start==NULL) + { + start = newNode; + newNode->leftAddress = start; + newNode->rightAddress = start; + end = newNode; + } + else + { + ptr = start; + flag = start; + while(ptr->rightAddress != start) + { + ptr = ptr->rightAddress; + } + ptr->rightAddress = newNode; + newNode->leftAddress = ptr; + + newNode->rightAddress = flag; + flag->leftAddress = newNode; + + end = newNode; + } +} +void insertBeg(int no) +{ + Node *newNode, *ptr, *flag; + ptr = start; + flag = start; + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + + while(ptr->rightAddress != start) + { + ptr = ptr->rightAddress; + } + start = newNode; + newNode->rightAddress = flag; + flag->leftAddress = newNode; + newNode->leftAddress = ptr; + ptr->rightAddress = newNode; + +} +void insertPos(int no, int pos) +{ + Node *ptr, *flag, *newNode; + ptr = start; + int count=1; + + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + + while(ptr->rightAddress != start) + { + if(count==pos) + { + flag->rightAddress = newNode; + newNode->leftAddress = flag; + newNode->rightAddress = ptr; + ptr->leftAddress = newNode; + break; + } + flag = ptr; + ptr = ptr->rightAddress; + count = count + 1; + } + if(ptr->rightAddress==start || count==pos) + { + flag->rightAddress = newNode; + newNode->leftAddress = flag; + newNode->rightAddress = ptr; + ptr->leftAddress = newNode; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("<->START<->"); + while(ptr->rightAddress != start) + { + printf("%d<->",ptr->data); + ptr = ptr->rightAddress; + } + printf("%d<->",ptr->data); + printf("END<->"); +} +void revDisplay() +{ + Node *ptr; + ptr = end; + printf("<->START<->"); + while(ptr->leftAddress != end) + { + printf("%d<->",ptr->data); + ptr = ptr->leftAddress; + } + printf("%d<->",ptr->data); + printf("START<->"); +} +int main() +{ + printf("Inserting & Traversing\n"); + printf("**********************\n\n"); + int choice,no,pos; + printf("1: Add\n2: Insert\n3: Display\n4: Reverse Display\n0: Exit\n\n"); + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + int size = sizeofList(); + printf("Enter Number : "); + scanf("%d",&no); + while(1) + { + printf("Enter Position : "); + scanf("%d",&pos); + if(pos==1) + { + insertBeg(no); + break; + } + else if(pos<1 || pos>size+1) + { + continue; + } + else if(pos==size+1) + { + add(no); + break; + } + else + { + insertPos(no,pos); + break; + } + } + printf("\n"); + } + else if(choice==3) + { + display(); + printf("\n\n"); + } + else if(choice==4) + { + revDisplay(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Circular_Doubly_Linked_List/README.md b/C/Circular_Doubly_Linked_List/README.md new file mode 100644 index 00000000..f10e66cf --- /dev/null +++ b/C/Circular_Doubly_Linked_List/README.md @@ -0,0 +1,5 @@ +# Circular Doubly Linked List + +1. Adding & Traversing. +2. Adding, Inserting & Traversing. +3. Adding, Deleting & Traversing. diff --git a/C/Circular_Doubly_Linked_List/Traversing.c b/C/Circular_Doubly_Linked_List/Traversing.c new file mode 100644 index 00000000..195c23af --- /dev/null +++ b/C/Circular_Doubly_Linked_List/Traversing.c @@ -0,0 +1,101 @@ +#include +#include + +typedef struct node{ + int data; + struct node *leftAddress; + struct node *rightAddress; +}Node; + +Node *start=NULL, *end=NULL; + +void add(int no) +{ + Node *newNode, *ptr, *flag; + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + if(start==NULL) + { + start = newNode; + newNode->rightAddress = start; + newNode->leftAddress = start; + end = newNode; + } + else + { + ptr = start; + flag = start; + while(ptr->rightAddress != start) + { + ptr = ptr->rightAddress; + } + ptr->rightAddress = newNode; + newNode->leftAddress = ptr; + + flag->leftAddress = newNode; + newNode->rightAddress = flag; + + end = newNode; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("<->START<->"); + while(ptr->rightAddress != start) + { + printf("%d<->",ptr->data); + ptr = ptr->rightAddress; + } + printf("%d<->",ptr->data); + printf("END<->"); +} +void revDisplay() +{ + Node *ptr; + ptr = end; + printf("<->END<->"); + while(ptr->leftAddress != end) + { + printf("%d<->",ptr->data); + ptr = ptr->leftAddress; + } + printf("%d<->",ptr->data); + printf("START<->"); +} +int main() +{ + printf("Adding & Traversing\n"); + printf("*******************\n\n"); + printf("1: Add\n2: Display\n3: Reverse Display\n0: Exit\n\n"); + int choice,no; + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + display(); + printf("\n\n"); + } + else if(choice==3) + { + revDisplay(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Circular_Linked_List/.gitignore b/C/Circular_Linked_List/.gitignore new file mode 100644 index 00000000..f8305e74 --- /dev/null +++ b/C/Circular_Linked_List/.gitignore @@ -0,0 +1 @@ +a.out \ No newline at end of file diff --git a/C/Circular_Linked_List/Deleting.c b/C/Circular_Linked_List/Deleting.c new file mode 100644 index 00000000..3b6f256c --- /dev/null +++ b/C/Circular_Linked_List/Deleting.c @@ -0,0 +1,158 @@ +#include +#include + +typedef struct node{ + int data; + struct node *nextAddress; +}Node; + +Node *start = NULL; + +int sizeofList() +{ + int count=0; + Node *ptr; + ptr = start; + while(ptr->nextAddress != start) + { + count = count + 1; + ptr = ptr->nextAddress; + } + count = count + 1; + return count; +} +void add(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = NULL; + if(start==NULL) + { + start = newNode; + newNode->nextAddress = start; + } + else + { + ptr = start; + while(ptr->nextAddress != start) + { + ptr = ptr->nextAddress; + } + ptr->nextAddress = newNode; + newNode->nextAddress = start; + } +} +void deleteBeg() +{ + Node *ptr, *flag; + ptr = start; + flag = start; + while(ptr->nextAddress != start) + { + ptr = ptr->nextAddress; + } + start = flag->nextAddress; + ptr->nextAddress = start; + free(flag); +} +void deletePos(int pos) +{ + Node *ptr, *flag; + ptr = start; + int count=1; + while(ptr->nextAddress != start) + { + if(count==pos) + { + flag->nextAddress = ptr->nextAddress; + free(ptr); + break; + } + flag = ptr; + ptr = ptr->nextAddress; + count = count + 1; + } +} +void deleteEnd() +{ + Node *ptr, *flag; + ptr = start; + while(ptr->nextAddress != start) + { + flag = ptr; + ptr = ptr->nextAddress; + } + flag->nextAddress = start; + free(ptr); +} +void display() +{ + Node *ptr; + ptr = start; + printf("START->"); + while(ptr->nextAddress != start) + { + printf("%d->",ptr->data); + ptr = ptr->nextAddress; + } + printf("%d->",ptr->data); + printf("START"); +} +int main() +{ + printf("Deleting & Traversing\n"); + printf("*********************\n\n"); + int choice,pos,no; + printf("1: Add\n2: Delete\n3: Display\n0: Exit\n\n"); + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + int size = sizeofList(); + while(1) + { + printf("Enter Position : "); + scanf("%d",&pos); + if(pos==1) + { + deleteBeg(); + break; + } + else if(pos==size) + { + deleteEnd(); + break; + } + else if(pos<1 || pos>size) + { + continue; + } + else + { + deletePos(pos); + break; + } + } + printf("\n"); + } + else if(choice==3) + { + display(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Circular_Linked_List/Inserting.c b/C/Circular_Linked_List/Inserting.c new file mode 100644 index 00000000..75faff04 --- /dev/null +++ b/C/Circular_Linked_List/Inserting.c @@ -0,0 +1,163 @@ +#include +#include + +typedef struct node{ + int data; + struct node *nextAddress; +}Node; + +Node *start = NULL; + +int sizeofList() +{ + int count=0; + Node *ptr; + ptr = start; + while(ptr->nextAddress != start) + { + count = count + 1; + ptr = ptr->nextAddress; + } + count = count + 1; + return count; +} +void add(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = NULL; + if(start == NULL) + { + start = newNode; + newNode->nextAddress = start; + } + else + { + ptr = start; + while(ptr->nextAddress != start) + { + ptr = ptr->nextAddress; + } + ptr->nextAddress = newNode; + newNode->nextAddress = start; + } +} +void insertBeg(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + + ptr = start; + while(ptr->nextAddress != start) + { + ptr = ptr->nextAddress; + } + ptr->nextAddress = newNode; + newNode->nextAddress = start; + start = newNode; +} +void insertPos(int no, int pos) +{ + Node *newNode, *ptr, *flag; + ptr = start; + int count = 1; + while(ptr->nextAddress != start) + { + if(count==pos) + { + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = NULL; + + newNode->nextAddress = flag->nextAddress; + flag->nextAddress = newNode; + break; + } + count = count + 1; + flag = ptr; + ptr = ptr->nextAddress; + } + if(count==pos && ptr->nextAddress == start) + { + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = NULL; + + newNode->nextAddress = flag->nextAddress; + flag->nextAddress = newNode; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("START->"); + while(ptr->nextAddress != start) + { + printf("%d->",ptr->data); + ptr = ptr->nextAddress; + } + printf("%d->",ptr->data); + printf("START"); +} +int main() +{ + printf("Inserting & Traversing\n"); + printf("**********************\n\n"); + printf("1: Add\n2: Insert\n3: Display\n0: Exit\n\n"); + int choice,no,pos; + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + int size = sizeofList(); + printf("Enter Number : "); + scanf("%d",&no); + while(1) + { + printf("Enter Position : "); + scanf("%d",&pos); + if(pos==1) + { + insertBeg(no); + break; + } + else if(pos == size+1) + { + add(no); + break; + } + else if(pos<1 || pos>size+1) + { + continue; + } + else + { + insertPos(no,pos); + break; + } + } + printf("\n"); + } + else if(choice==3) + { + display(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Circular_Linked_List/README.md b/C/Circular_Linked_List/README.md new file mode 100644 index 00000000..f55e7ed9 --- /dev/null +++ b/C/Circular_Linked_List/README.md @@ -0,0 +1,5 @@ +# Circular Linked List + +1. Adding & Traversing. +2. Adding, Inserting & Traversing. +3. Adding, Deleting & Traversing. \ No newline at end of file diff --git a/C/Circular_Linked_List/Traversing.c b/C/Circular_Linked_List/Traversing.c new file mode 100644 index 00000000..0b3164b5 --- /dev/null +++ b/C/Circular_Linked_List/Traversing.c @@ -0,0 +1,73 @@ +#include +#include + +typedef struct node{ + int data; + struct node *nextAddress; +}Node; + +Node *start = NULL; + +void add(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = NULL; + if(start == NULL) + { + start = newNode; + newNode->nextAddress = start; + } + else + { + ptr = start; + while(ptr->nextAddress != start) + { + ptr = ptr->nextAddress; + } + ptr->nextAddress = newNode; + newNode->nextAddress = start; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("START->"); + while(ptr->nextAddress != start) + { + printf("%d->",ptr->data); + ptr = ptr->nextAddress; + } + printf("%d->",ptr->data); + printf("END"); +} +int main() +{ + printf("Adding & Traversing\n"); + printf("*******************\n\n"); + printf("1: Add\n2: Display\n0: Exit\n\n"); + int choice,no; + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + display(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Doubly_Linked_List/.gitignore b/C/Doubly_Linked_List/.gitignore new file mode 100644 index 00000000..f8305e74 --- /dev/null +++ b/C/Doubly_Linked_List/.gitignore @@ -0,0 +1 @@ +a.out \ No newline at end of file diff --git a/C/Doubly_Linked_List/Deleting.c b/C/Doubly_Linked_List/Deleting.c new file mode 100644 index 00000000..10b982f3 --- /dev/null +++ b/C/Doubly_Linked_List/Deleting.c @@ -0,0 +1,152 @@ +#include +#include + +typedef struct node{ + int data; + struct node *leftAddress; + struct node *rightAddress; +}Node; + +Node *start=NULL, *end=NULL; + +int sizeofList() +{ + int count=0; + Node *ptr; + ptr = start; + while(ptr != NULL) + { + count = count + 1; + ptr = ptr->rightAddress; + } + return count; +} +void add(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + if(start==NULL) + { + start = newNode; + } + else + { + ptr = start; + while(ptr->rightAddress!=NULL) + { + ptr = ptr->rightAddress; + } + ptr->rightAddress = newNode; + newNode->leftAddress = ptr; + end = newNode; + } +} +void deleteBeg() +{ + Node *ptr; + ptr = start; + start = ptr->rightAddress; + ptr->rightAddress->leftAddress = NULL; + free(ptr); +} +void deletePos(int pos) +{ + Node *ptr,*flag; + ptr = start; + int count=1; + while(ptr!=NULL) + { + if(count==pos) + { + flag->rightAddress = ptr->rightAddress; + ptr->rightAddress->leftAddress = flag; + } + flag = ptr; + ptr = ptr->rightAddress; + count = count + 1; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("START<->"); + while(ptr!=NULL) + { + printf("%d<->",ptr->data); + ptr = ptr->rightAddress; + } + printf("END"); +} +void revDisplay() +{ + Node *ptr; + ptr = end; + printf("END<->"); + while(ptr!=NULL) + { + printf("%d<->",ptr->data); + ptr = ptr->leftAddress; + } + printf("START"); +} +int main() +{ + printf("Deleting & Traversing\n"); + printf("*********************\n\n"); + printf("1: Add\n2: Delete\n3: Display\n4: Reverse Display\n\n"); + int choice,no,pos; + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + int size = sizeofList(); + while(1) + { + printf("Enter Position : "); + scanf("%d",&pos); + if(pos==1) + { + deleteBeg(); + break; + } + else if(pos<1 || pos>size) + { + continue; + } + else + { + deletePos(pos); + break; + } + } + printf("\n"); + } + else if(choice==3) + { + display(); + printf("\n\n"); + } + else if(choice==4) + { + revDisplay(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Doubly_Linked_List/Inserting.c b/C/Doubly_Linked_List/Inserting.c new file mode 100644 index 00000000..217f54d2 --- /dev/null +++ b/C/Doubly_Linked_List/Inserting.c @@ -0,0 +1,173 @@ +#include +#include + +typedef struct node{ + int data; + struct node *leftAddress; + struct node *rightAddress; +}Node; + +Node *start=NULL, *end=NULL; + +int sizeofList() +{ + int count=0; + Node *ptr; + ptr = start; + while(ptr != NULL) + { + count = count + 1; + ptr = ptr->rightAddress; + } + return count; +} +void add(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + if(start == NULL) + { + start = newNode; + } + else + { + ptr = start; + while(ptr->rightAddress != NULL) + { + ptr = ptr->rightAddress; + } + ptr->rightAddress = newNode; + newNode->leftAddress = ptr; + end = newNode; + } +} +void insertBeg(int no) +{ + Node *newNode, *ptr; + ptr = start; + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + + newNode->rightAddress = ptr; + ptr->leftAddress = newNode; + start = newNode; +} +void insertPos(int no, int pos) +{ + Node *ptr, *newNode, *flag; + ptr=start; + int count=1; + while(ptr!=NULL) + { + if(count==pos) + { + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + + newNode->rightAddress = ptr; + ptr->leftAddress = newNode; + flag->rightAddress = newNode; + newNode->leftAddress = flag; + break; + + } + flag = ptr; + ptr = ptr->rightAddress; + count = count + 1; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("START<->"); + while(ptr != NULL) + { + printf("%d<->",ptr->data); + ptr = ptr->rightAddress; + } + printf("END"); +} +void revDisplay() +{ + Node *ptr; + ptr = end; + printf("END<->"); + while(ptr != NULL) + { + printf("%d<->",ptr->data); + ptr = ptr->leftAddress; + } + printf("START"); +} +int main() +{ + printf("Inserting & Traversing\n"); + printf("**********************\n\n"); + int choice,no,pos; + printf("1: Add\n2: Insert\n3: Display\n4: Reverse Display\n0: Edit\n\n"); + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + int size = sizeofList(); + printf("Enter Number : "); + scanf("%d",&no); + while(1) + { + printf("Enter Position : "); + scanf("%d",&pos); + if(pos==1) + { + insertBeg(no); + break; + } + else if(pos==size+1) + { + add(no); + break; + } + else if(pos<1 || pos>size+1) + { + continue; + } + else + { + insertPos(no,pos); + break; + } + } + printf("\n"); + } + else if(choice==3) + { + display(); + printf("\n\n"); + } + else if(choice==4) + { + revDisplay(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Doubly_Linked_List/README.md b/C/Doubly_Linked_List/README.md new file mode 100644 index 00000000..4e494f8f --- /dev/null +++ b/C/Doubly_Linked_List/README.md @@ -0,0 +1,5 @@ +# Doubly Linked List + +1. Adding & Traversing. +2. Adding, Inserting & Traversing. +3. Adding, Deleting & Traversing. \ No newline at end of file diff --git a/C/Doubly_Linked_List/Traversing.c b/C/Doubly_Linked_List/Traversing.c new file mode 100644 index 00000000..af5a85f2 --- /dev/null +++ b/C/Doubly_Linked_List/Traversing.c @@ -0,0 +1,90 @@ +#include +#include +typedef struct node{ + int data; + struct node *leftAddress; + struct node *rightAddress; +}Node; + +Node *start = NULL, *end = NULL; + +void add(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->leftAddress = NULL; + newNode->data = no; + newNode->rightAddress = NULL; + if(start == NULL) + { + start = newNode; + } + else + { + ptr = start; + while(ptr->rightAddress != NULL) + { + ptr = ptr->rightAddress; + } + ptr->rightAddress = newNode; + newNode->leftAddress = ptr; + end = newNode; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("START<->"); + while(ptr!=NULL) + { + printf("%d<->",ptr->data); + ptr = ptr->rightAddress; + } + printf("END"); +} +void revDisplay() +{ + Node *ptr; + ptr = end; + printf("END<->"); + while(ptr!=NULL) + { + printf("%d<->",ptr->data); + ptr = ptr->leftAddress; + } + printf("START"); +} +int main() +{ + printf("Adding & Traversing\n"); + printf("*******************\n\n"); + printf("1: Add\n2: Display\n3: Reverse Display\n0: Exit\n\n"); + int choice,no; + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + display(); + printf("\n\n"); + } + else if(choice==3) + { + revDisplay(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Singly_Linked_List/.gitignore b/C/Singly_Linked_List/.gitignore new file mode 100644 index 00000000..f8305e74 --- /dev/null +++ b/C/Singly_Linked_List/.gitignore @@ -0,0 +1 @@ +a.out \ No newline at end of file diff --git a/C/Singly_Linked_List/Deleting.c b/C/Singly_Linked_List/Deleting.c new file mode 100644 index 00000000..9b0d48a0 --- /dev/null +++ b/C/Singly_Linked_List/Deleting.c @@ -0,0 +1,131 @@ +#include +#include + +typedef struct node{ + int data; + struct node *nextAddress; +}Node; + +Node *start = NULL; + +int sizeofList() +{ + Node *ptr; + ptr = start; + int count=0; + while(ptr != NULL) + { + count = count + 1; + ptr = ptr->nextAddress; + } + return count; +} +void add(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = NULL; + if(start == NULL) + { + start = newNode; + } + else + { + ptr = start; + while(ptr->nextAddress!=NULL) + { + ptr = ptr->nextAddress; + } + ptr->nextAddress = newNode; + } +} +void deleteBeg() +{ + Node *ptr; + ptr = start; + start = ptr->nextAddress; + free(ptr); +} +void deletePos(int pos) +{ + Node *ptr, *flag; + ptr = start; + int count = 1; + while(ptr != NULL) + { + if(count==pos) + { + flag->nextAddress = ptr->nextAddress; + free(ptr); + break; + } + flag = ptr; + ptr = ptr->nextAddress; + count = count + 1; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("START->"); + while(ptr != NULL) + { + printf("%d->",ptr->data); + ptr = ptr->nextAddress; + } + printf("END"); +} +int main() +{ + printf("Deleting & Traversing\n"); + printf("*********************\n\n"); + int choice,pos,no; + printf("1: Add\n2: Delete\n3: Display\n0: Exit\n\n"); + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + while(1) + { + printf("Enter Position : "); + scanf("%d",&pos); + int size = sizeofList(); + if(pos==1) + { + deleteBeg(); + break; + } + else if(pos>size || pos<1) + { + continue; + } + else + { + deletePos(pos); + break; + } + } + printf("\n"); + } + else if(choice==3) + { + display(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Singly_Linked_List/Inserting.c b/C/Singly_Linked_List/Inserting.c new file mode 100644 index 00000000..969a6bc8 --- /dev/null +++ b/C/Singly_Linked_List/Inserting.c @@ -0,0 +1,142 @@ +#include +#include + +typedef struct node{ + int data; + struct node *nextAddress; +}Node; + +Node *start=NULL; + +int sizeOfList() +{ + Node *ptr; + ptr = start; + int count=0; + while(ptr != NULL) + { + count = count + 1; + ptr = ptr->nextAddress; + } + return count; +} +void add(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = NULL; + if(start == NULL) + { + start = newNode; + } + else + { + ptr = start; + while(ptr->nextAddress!=NULL) + { + ptr = ptr->nextAddress; + } + ptr->nextAddress = newNode; + } +} +void insertBeg(int no) +{ + Node *newNode; + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = start; + start = newNode; +} +void insertPos(int no, int pos) +{ + Node *ptr; + ptr = start; + int count=1; + while(ptr != NULL) + { + count = count + 1; + if(count==pos) + { + Node *newNode; + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = ptr->nextAddress; + ptr->nextAddress = newNode; + } + ptr = ptr->nextAddress; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("START->"); + while(ptr != NULL) + { + printf("%d->",ptr->data); + ptr = ptr->nextAddress; + } + printf("END"); +} +int main() +{ + printf("Inserting & Traversing\n"); + printf("**********************\n\n"); + int no,pos; + int choice; + printf("1: Add\n2: Insert\n3: Display\n0: No\n\n"); + while(1) + { + printf("Enter Choice : "); + scanf("%d",&choice); + if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + add(no); + } + else if(choice==2) + { + printf("Enter Number : "); + scanf("%d",&no); + while(1) + { + printf("Enter Position : "); + scanf("%d",&pos); + + int size = sizeOfList(); + if(pos==1) + { + insertBeg(no); + break; + } + else if(pos==size+1) + { + add(no); + break; + } + else if(pos>size || pos<1) + { + continue; + } + else + { + insertPos(no,pos); + break; + } + } + printf("\n"); + } + else if(choice==3) + { + display(); + printf("\n\n"); + } + else if(choice==0) + { + break; + } + } +} \ No newline at end of file diff --git a/C/Singly_Linked_List/README.md b/C/Singly_Linked_List/README.md new file mode 100644 index 00000000..5852d84f --- /dev/null +++ b/C/Singly_Linked_List/README.md @@ -0,0 +1,5 @@ +# Singly Linked List + +1. Adding & Traversing. +2. Adding, Inserting & Traversing. +3. Adding, Deleting & Traversing. \ No newline at end of file diff --git a/C/Singly_Linked_List/Traversing.c b/C/Singly_Linked_List/Traversing.c new file mode 100644 index 00000000..a89ad9f1 --- /dev/null +++ b/C/Singly_Linked_List/Traversing.c @@ -0,0 +1,68 @@ +#include +#include +typedef struct node{ + int data; + struct node *nextAddress; +}Node; +Node *start = NULL; +void insert(int no) +{ + Node *newNode, *ptr; + newNode = (Node *)malloc(sizeof(Node)); + newNode->data = no; + newNode->nextAddress = NULL; + if(start == NULL) + { + start = newNode; + } + else + { + ptr = start; + while(ptr->nextAddress!=NULL) + { + ptr = ptr->nextAddress; + } + ptr->nextAddress = newNode; + } +} +void display() +{ + Node *ptr; + ptr = start; + printf("START->"); + while(ptr != NULL) + { + printf("%d->",ptr->data); + ptr = ptr->nextAddress; + } + printf("END"); +} +int main() +{ + printf("Traversing a Singly Linked List\n"); + printf("*******************************\n\n"); + printf("1: Insert\n2: Display\n0: No\n\n"); + int choice, no; + while(1) + { + printf("Enter your Choice : "); + scanf("%d",&choice); + if(choice==0) + { + printf("\n"); + break; + } + else if(choice==1) + { + printf("Enter Number : "); + scanf("%d",&no); + printf("\n"); + insert(no); + } + else if(choice==2) + { + display(); + printf("\n\n"); + } + } +} \ No newline at end of file