From 4d3da88635d91e837095eca6769d0b8dce8b02f9 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 22 Oct 2024 08:48:45 +0530 Subject: [PATCH] Upload files to "DS/C/Lab/Shortcodes" --- DS/C/Lab/Shortcodes/BST.c | 92 +++++++++++++ DS/C/Lab/Shortcodes/DoublyLLShort.c | 195 ++++++++++++++++++++++++++++ DS/C/Lab/Shortcodes/dcll.c | 54 ++++++++ DS/C/Lab/Shortcodes/dllconcs.c | 55 ++++++++ DS/C/Lab/Shortcodes/tree.c | 69 ++++++++++ 5 files changed, 465 insertions(+) create mode 100644 DS/C/Lab/Shortcodes/BST.c create mode 100644 DS/C/Lab/Shortcodes/DoublyLLShort.c create mode 100644 DS/C/Lab/Shortcodes/dcll.c create mode 100644 DS/C/Lab/Shortcodes/dllconcs.c create mode 100644 DS/C/Lab/Shortcodes/tree.c diff --git a/DS/C/Lab/Shortcodes/BST.c b/DS/C/Lab/Shortcodes/BST.c new file mode 100644 index 0000000..e6b7afe --- /dev/null +++ b/DS/C/Lab/Shortcodes/BST.c @@ -0,0 +1,92 @@ +#include +#include + +typedef struct TreeNode { + int d; + struct TreeNode *l, *r; +} T; + +T *c(int d) { + T *n = malloc(sizeof(T)); + n->d = d; + n->l = n->r = NULL; + return n; +} + +T *i(T *r, int d) { + if (!r) return c(d); + if (d < r->d) r->l = i(r->l, d); + else if (d > r->d) r->r = i(r->r, d); + return r; +} + +T *f(T *r) { + while (r->l) r = r->l; + return r; +} + +T *d(T *r, int k) { + if (!r) return NULL; + if (k < r->d) r->l = d(r->l, k); + else if (k > r->d) r->r = d(r->r, k); + else { + if (!r->l) { + T *t = r->r; + free(r); + return t; + } else if (!r->r) { + T *t = r->l; + free(r); + return t; + } + T *t = f(r->r); + r->d = t->d; + r->r = d(r->r, t->d); + } + return r; +} + +void t(T *r) { + if (r) { + t(r->l); + printf("%d ", r->d); + t(r->r); + } +} + +int s(T *r, int k, int *p) { + if (!r) return 0; + int l = s(r->l, k, p); + if (l) return l; + if (++*p == k) return *p; + return s(r->r, k, p); +} + +int main() { + T *r = NULL; + int c, v; + while (1) { + printf("\n1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\n"); + scanf("%d", &c); + if (c == 1) { + scanf("%d", &v); + r = i(r, v); + } else if (c == 2) { + scanf("%d", &v); + r = d(r, v); + } else if (c == 3) { + scanf("%d", &v); + int p = 0, f = s(r, v, &p); + printf(f ? "Found at %d\n" : "Not found\n", f); + } else if (c == 4) { + t(r); + printf("\n"); + } else if (c == 5) { + break; + } else { + printf("Invalid\n"); + } + } + free(r); + return 0; +} diff --git a/DS/C/Lab/Shortcodes/DoublyLLShort.c b/DS/C/Lab/Shortcodes/DoublyLLShort.c new file mode 100644 index 0000000..9dbfa6c --- /dev/null +++ b/DS/C/Lab/Shortcodes/DoublyLLShort.c @@ -0,0 +1,195 @@ +#include +#include +typedef struct N { + int d; + struct N *p, *n; +} N; + +N *h; + +void i(int d) { + N *n = malloc(sizeof(N)); + n->d = d; + n->n = 0; + if (!h) { + n->p = 0; + h = n; + return; + } + N *t = h; + while (t->n) t = t->n; + t->n = n; + n->p = t; +} + +void delete_rear() { + if (!h) { + puts("Empty"); + return; + } + if (!h->n) { + free(h); + h = 0; + return; + } + N *t = h; + while (t->n) t = t->n; + t->p->n = 0; + free(t); +} + +void ip(int d, int p) { + N *n = malloc(sizeof(N)); + n->d = d; + if (p == 1) { + n->p = 0; + n->n = h; + if (h) h->p = n; + h = n; + return; + } + N *t = h; + for (int i = 1; i < p - 1 && t; i++) t = t->n; + if (!t) { + puts("Out of range"); + return; + } + n->n = t->n; + n->p = t; + if (t->n) t->n->p = n; + t->n = n; +} + +void dp(int p) { + if (!h) { + puts("Empty"); + return; + } + N *t = h; + if (p == 1) { + h = h->n; + if (h) h->p = 0; + free(t); + return; + } + for (int i = 1; i < p && t; i++) t = t->n; + if (!t) { + puts("Out of range"); + return; + } + t->p->n = t->n; + if (t->n) t->n->p = t->p; + free(t); +} + +void ia(int d, int k) { + N *n = malloc(sizeof(N)); + n->d = d; + N *t = h; + while (t && t->d != k) t = t->n; + if (!t) { + puts("Not found"); + return; + } + n->n = t->n; + n->p = t; + if (t->n) t->n->p = n; + t->n = n; +} + +void ib(int d, int k) { + N *n = malloc(sizeof(N)); + n->d = d; + if (!h) { + puts("Empty"); + return; + } + if (h->d == k) { + n->n = h; + n->p = 0; + h->p = n; + h = n; + return; + } + N *t = h; + while (t->n && t->n->d != k) t = t->n; + if (!t->n) { + puts("Not found"); + return; + } + n->n = t->n; + n->p = t; + t->n->p = n; + t->n = n; +} + +void t() { + for (N *t = h; t; t = t->n) printf("%d ", t->d); + puts(""); +} + +void r() { + N *t = 0, *c = h; + while (c) { + t = c->p; + c->p = c->n; + c->n = t; + c = c->p; + } + if (t) h = t->p; +} + +int main() { + int c, d, p, k; + while (1) { + puts("\n1.Insert rear\n2.Delete rear\n3.Insert position\n4.Delete position\n5.Insert after\n6.Insert before\n7.Traverse\n8.Reverse\n9.Exit"); + printf("Choice: "); + scanf("%d", &c); + switch (c) { + case 1: + printf("Data: "); + scanf("%d", &d); + i(d); + break; + case 2: + delete_rear(); + break; + case 3: + printf("Data: "); + scanf("%d", &d); + printf("Position: "); + scanf("%d", &p); + ip(d, p); + break; + case 4: + printf("Position: "); + scanf("%d", &p); + dp(p); + break; + case 5: + printf("Data: "); + scanf("%d", &d); + printf("After: "); + scanf("%d", &k); + ia(d, k); + break; + case 6: + printf("Data: "); + scanf("%d", &d); + printf("Before: "); + scanf("%d", &k); + ib(d, k); + break; + case 7: + t(); + break; + case 8: + r(); + break; + case 9: + exit(0); + default: + puts("Invalid"); + } + } +} diff --git a/DS/C/Lab/Shortcodes/dcll.c b/DS/C/Lab/Shortcodes/dcll.c new file mode 100644 index 0000000..7333e22 --- /dev/null +++ b/DS/C/Lab/Shortcodes/dcll.c @@ -0,0 +1,54 @@ +#include +#include + +typedef struct c { int d; struct c *n, *p; } *C; +C l = NULL; + +C new_c(int v) { + C t = (C)malloc(sizeof(struct c)); + t->d = v; t->n = t->p = NULL; return t; +} + +void insert_beg(int v) { + C t = new_c(v); + if (!l) { l = t; l->n = l->p = l; } + else { + t->n = l->n; + l->n->p = t; + t->p = l; + l->n = t; + } +} + +void del_end() { + if (!l) printf("List is empty"); + else { + C p = l->p; + p->n = l->n; + l->n->p = p; + free(l); + l = p; + } +} + +void traverse() { + if (!l) return; + C c = l->n; + do { printf("%d ", c->d); c = c->n; } while (c != l->n); + printf("\n"); +} + +int main() { + char cont = 'y'; int ch; + while (cont == 'y') { + printf("1. Insert at beginning\n2. Delete at end\n3. Traverse\n"); + scanf("%d", &ch); + if (ch == 1) { int v; scanf("%d", &v); insert_beg(v); } + else if (ch == 2) { del_end(); printf("Element deleted\n"); } + else if (ch == 3) { traverse(); } + printf("Continue? (y/n)\n"); + fflush(stdin); + scanf(" %c", &cont); + } + return 0; +} diff --git a/DS/C/Lab/Shortcodes/dllconcs.c b/DS/C/Lab/Shortcodes/dllconcs.c new file mode 100644 index 0000000..d282df5 --- /dev/null +++ b/DS/C/Lab/Shortcodes/dllconcs.c @@ -0,0 +1,55 @@ +#include +#include + +typedef struct node { + int d; + struct node *l, *r; +} *N; + +void concat(N *X1, N *X2) { + if (*X1 == NULL) *X1 = *X2; + else if (*X2) { + N t = *X1; + while (t->r) t = t->r; + t->r = *X2; + (*X2)->l = t; + } +} + +N new_node(int d) { + N n = (N)malloc(sizeof(struct node)); + n->d = d; + n->l = n->r = NULL; + return n; +} + +void insert(N *h, int d) { + N n = new_node(d); + if (!*h) *h = n; + else { + N t = *h; + while (t->r) t = t->r; + t->r = n; + n->l = t; + } +} + +void print(N h) { + while (h) { + printf("%d ", h->d); + h = h->r; + } + printf("\n"); +} + +int main() { + N X1 = NULL, X2 = NULL; + insert(&X1, 1); + insert(&X1, 2); + insert(&X1, 3); + insert(&X2, 4); + insert(&X2, 5); + concat(&X1, &X2); + print(X1); + return 0; +} diff --git a/DS/C/Lab/Shortcodes/tree.c b/DS/C/Lab/Shortcodes/tree.c new file mode 100644 index 0000000..0f6cde2 --- /dev/null +++ b/DS/C/Lab/Shortcodes/tree.c @@ -0,0 +1,69 @@ +#include +#include +#define M 100 + +typedef struct N *P; +typedef struct N{int d;P l,r;}N;P root; + +int t=-1;P stk[M]; +P cn(int v){P n=malloc(sizeof(N));n->d=v;n->l=n->r=NULL;return n;} + +void ct(int n){ + int v,i;char dir[50];P p=NULL,c; + for(int k=0;kl:c->r; + } + if(c||dir[i]){free(tmp);continue;} + (dir[i-1]=='L'||dir[i-1]=='l')?(p->l=tmp):(p->r=tmp); + } + } +} + +int e(){return t==-1;}void p(P n){if(tl;}if(!(n=o()))break;printf("%d ",n->d);n=n->r;}} +void po(P n){while(1){while(n){p(n);printf("%d ",n->d);n=n->l;}if(!(n=o()))break;n=n->r;}} +void pso(P n){while(1){if(n){p(n);n=n->l;}else{if(e())break;P tmp=stk[t]->r;if(!tmp){tmp=o();printf("%d ",tmp->d);while(!e()&&tmp==stk[t]->r){tmp=o();printf("%d ",tmp->d);}}else n=tmp;}}} + +P pn(P n,int v){if(!n)return NULL;if((n->l&&n->l->d==v)||(n->r&&n->r->d==v))return n;P l=pn(n->l,v);return l?l:pn(n->r,v);} + +int md(P n){if(!n)return 0;int l=md(n->l),r=md(n->r);return(l>r?l:r)+1;} +int pa(P n,int v){if(!n)return 0;if(n->d==v)return 1;if(pa(n->l,v)||pa(n->r,v)){printf("%d ",n->d);return 1;}return 0;} +void cl(P n,int *c){if(n&&!n->l&&!n->r)(*c)++;if(n){cl(n->l,c);cl(n->r,c);}} + +int main(){ + int n,ch,v,lc; + printf("\nBinary Tree Menu:\n"); + printf("1. Create Tree\n"); + printf("2. Inorder Traversal\n"); + printf("3. Preorder Traversal\n"); + printf("4. Postorder Traversal\n"); + printf("5. Find Parent of Node\n"); + printf("6. Find Max Depth\n"); + printf("7. Print Ancestors of Node\n"); + printf("8. Count Leaf Nodes\n"); + printf("9. Exit\n"); + do{ + printf("Enter choice: "); + scanf("%d",&ch); + switch(ch){ + case 1:printf("Enter number of nodes: ");scanf("%d",&n);ct(n);break; + case 2:printf("Inorder: ");io(root);puts("");break; + case 3:printf("Preorder: ");po(root);puts("");break; + case 4:printf("Postorder: ");pso(root);puts("");break; + case 5:printf("Enter value to find parent: ");scanf("%d",&v);P p=pn(root,v);p?printf("Parent: %d\n",p->d):puts("Node not found");break; + case 6:printf("Max Depth: %d\n",md(root));break; + case 7:printf("Enter value to find ancestors: ");scanf("%d",&v);pa(root,v)?puts(""):puts("No ancestors found");break; + case 8:lc=0;cl(root,&lc);printf("Leaf count: %d\n",lc);break; + case 9:break; + default:puts("Invalid choice"); + } + }while(ch!=9); + return 0; +}