├── .gitignore ├── README.md ├── Unit1 ├── 1_C_Revision │ ├── 1_ptr.c │ ├── 2_ptr_const.c │ ├── 3_ptr_arr_incr.c │ ├── arryPtr.c │ ├── factorial.c │ ├── fibonacci.c │ ├── file1.c │ ├── file2.c │ ├── fnCat.c │ ├── pragmaDir.c │ ├── registr.c │ ├── staticStorage.c │ ├── struct1.c │ ├── structRet.c │ ├── swapVar.c │ └── toh.c ├── 2_SLL │ ├── 1_SLL_withPreconditions │ │ ├── llist.h │ │ ├── llistClient.c │ │ └── llistImpl.c │ ├── 2_SLL_ErrorCondnsHandled │ │ ├── llist.h │ │ ├── llistClient.c │ │ └── llistImpl.c │ ├── 3_SLL_Variations │ │ ├── llist.c │ │ ├── llist.h │ │ └── llistImpl.c │ ├── 4_ordList_Precondn │ │ ├── llist.h │ │ ├── ord.c │ │ └── ordImpl.c │ ├── 5_concat │ │ ├── concat.c │ │ ├── concatImpl.c │ │ └── llist.h │ ├── 6_Poly │ │ ├── llist.h │ │ ├── polyImpl.c │ │ ├── polyc.c │ │ └── tailPtrImpl │ │ │ ├── llist.h │ │ │ ├── llistImpl.c │ │ │ └── llistc.c │ └── 7_DelEvenInfoNodes │ │ ├── llist.h │ │ ├── llistClient.c │ │ └── llistImpl.c ├── 3_DLL │ ├── llist.c │ ├── llist.h │ └── llistImpl.c ├── 4_CircularSLL │ ├── cll.h │ ├── llistClient.c │ └── llistImpl.c ├── 5_CircularDLL │ ├── cdll.c │ ├── cdll.h │ └── cdllImpl.c ├── 6_MultiList_SparseMatrix │ ├── multi.c │ ├── multi.h │ └── multiImpl.c └── SLL_withReturnValues_ErrorHandling_NotDoneinUrClass │ ├── 1_SLL_withRetValues │ ├── llist.h │ ├── llistImpl.c │ └── llistc.c │ └── 2_SLL_Variation │ ├── DblPtr │ ├── llist.h │ ├── llistImpl.c │ └── llistcl.c │ └── SnglPtr_NodeStruct │ ├── llist.h │ ├── llistc.c │ └── llistimpl.c ├── Unit2 ├── 1_Stack_ArrayImplmntn_Static │ ├── stack.c │ ├── stack.h │ └── stackImpl.c ├── 2_Stack_ArrayImplmntn_Dynamic │ ├── stack.c │ ├── stack.h │ └── stackImpl.c ├── 3_ChangedPrototype_StackArrayImplmntn_Static │ ├── stack.c │ ├── stack.h │ └── stackImpl.c ├── 4_Parentheses │ ├── parentheses.c │ ├── parentheses.h │ └── parenthesesImpl.c ├── 5_PostfixEval │ ├── postfix.c │ ├── postfix.h │ └── postfixImpl.c ├── 6_InfixToPostfix │ ├── inToPost.c │ ├── inToPost.h │ └── inToPostImpl.c └── Queue │ ├── 1_SimpleQ │ ├── ArrayImplementation │ │ ├── queue.c │ │ ├── queueImpl.c │ │ └── queue_Array.h │ └── LinkedListImplementation │ │ └── qListImpl.c │ ├── 2_CircularQ │ ├── ArrayImplementation │ │ ├── cirq.c │ │ ├── cirq.h │ │ └── cirqImpl.c │ └── LinkedListImplementation │ │ └── cqListImpl.c │ ├── 3_PriorityQ │ ├── ArrayImplementation_DescendingPriorityQueue │ │ ├── priQArray.c │ │ ├── priQArray.h │ │ └── priQArrayImpl.c │ └── LinkedListImplementation_AscendingPriorityQueue │ │ ├── priQll.h │ │ ├── priQllImpl.c │ │ └── priqll.c │ ├── 4_CircularQ_DEQUE_Array │ ├── 1_cirQ.c │ ├── 2_DEQUE_CirArray_Mod.c │ └── 3_DEQUEUE_CirArray.c │ ├── 5_DEQUE_LinkedList │ ├── deq.c │ ├── deq.h │ └── dequeImpl.c │ └── 6_Josephus_usingCircularLL │ ├── josephus.c │ ├── josephus.h │ └── josephusImpl.c ├── Unit3 ├── 1_BST_Creation_Traversal │ ├── bst.c │ ├── bst.h │ └── bstImpl.c ├── 2_BST_Creatn_findMin_findMax_Search_Deletion │ ├── bst.c │ ├── bst.h │ └── bstImpl.c ├── 3_BST_ArrayImpl_Creation_Traversal │ ├── bstArray.c │ ├── bstArray.h │ └── bstArrayImpl.c ├── 4_BinaryTree_dynamicAllocnImpl_ArrayImpl │ ├── binaryTree_ArrayImpl.c │ └── binaryTree_dynamicAllocn.c ├── 5_ExpressionTree │ ├── exprTree.c │ ├── exprTree.h │ └── exprTreeImpl.c ├── 6_IterativeTraversal │ └── bst_IterTraversal.c ├── 7_Heap │ ├── botmUpMaxHeap.c │ └── topDown_MaxHeap.c ├── 8_ThreadedBinaryTree │ └── rightInThreadedBST.c ├── 9_PriorityQueueUsingHeap │ └── descPq.c ├── bstDictionary.c └── bstMenudriven.c ├── Unit4 ├── 10_chkForPathUsingDFS.c ├── 1_graphRepresentation.c ├── 2_bfsTraversal.c ├── 3_UndirectedGraphConnectivityUsingBFS.c ├── 4_DigraphConnectivityUsingBFS.c ├── 5_chkForPathUsingBFS.c ├── 6_dfsTraversal_Iterative.c ├── 7_dfsTraversalRecursive.c ├── 8_UndirectedGraphConnectivityUsingDFS.c └── 9_DigraphConnectivityUsingDFS.c └── Unit5 ├── Hashing ├── hashChaining.c └── linearProbUpdated.c ├── dictionaryusingTrie.c └── trie.c /.gitignore: -------------------------------------------------------------------------------- 1 | test.c 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DSA-Programs 2 | ## Programs done in class 3 | -------------------------------------------------------------------------------- /Unit1/1_C_Revision/1_ptr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a=10; 6 | int *p=&a; 7 | 8 | int *q; 9 | q=&a; 10 | 11 | int **pp=&p; 12 | printf("%u %u\n",&a,p); 13 | printf("%d %d\n",*p,**pp); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/2_ptr_const.c: -------------------------------------------------------------------------------- 1 | //const and ptr 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | /* int const p=10; 8 | // const int q=20; 9 | // p=10; 10 | int const q=20; 11 | int const *const ptr=&p; 12 | // p++; //Error 13 | ptr=&q; 14 | */ 15 | /* const int r=11; 16 | int *z=&r; 17 | // r++; //Error 18 | int s=++*z; 19 | printf("%d %d\n",*z,r); 20 | printf("%d %d\n",++*z,r); */ 21 | 22 | int a=10; 23 | const int const *p=&a; 24 | printf("%d",*p); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/3_ptr_arr_incr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[]={10,20,30}; 6 | int *ptr=a; 7 | // a++; 8 | int q=*ptr++; //int q; q=ptr++; *ptr; 9 | // int r=*++ptr; 10 | // int s=++*ptr; 11 | 12 | printf("%d %d\n",q,*ptr); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/arryPtr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int* f1(int *); 4 | int* f2(int a[]); 5 | void f3(int *); 6 | 7 | int main() 8 | { 9 | int a[]={1,2,3}; 10 | /* int *ptr=a; 11 | 12 | printf("%d\n",f1(ptr)==f1(a)); 13 | printf("sizeof(a)=%lu sizeof(ptr)=%lu\n",sizeof(a),sizeof(ptr)); 14 | printf("%d\n",sizeof(a)==sizeof(ptr)); 15 | */ 16 | /* a++;*/ 17 | 18 | printf("%d %d %d\n",a[0],a[1],a[2]); 19 | int *ptr=f2(a); 20 | printf("%d %d %d\n",a[0],a[1],a[2]); 21 | f3(ptr); 22 | printf("%d %d %d\n",a[0],a[1],a[2]); 23 | 24 | return 0; 25 | } 26 | 27 | /* int* f1(int *a) 28 | { 29 | return a; 30 | } 31 | */ 32 | int* f2(int a[]) 33 | { 34 | a++; 35 | *a=100; 36 | return a; 37 | } 38 | 39 | void f3(int *a) 40 | { 41 | a++; 42 | *a=200; 43 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long int fact(int); 4 | 5 | int main() 6 | { 7 | printf("%ld\n",fact(5)); 8 | return 0; 9 | } 10 | 11 | long int fact(int n) 12 | { 13 | if(n==0) 14 | return 1; 15 | return n*fact(n-1); 16 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fib(int); 4 | 5 | int main() 6 | { 7 | printf("%d\n",fib(5)); 8 | return 0; 9 | } 10 | 11 | int fib(int n) 12 | { 13 | if(n==1) 14 | return 0; 15 | if(n==2) 16 | return 1; 17 | return fib(n-1)+fib(n-2); 18 | } 19 | -------------------------------------------------------------------------------- /Unit1/1_C_Revision/file1.c: -------------------------------------------------------------------------------- 1 | #ifndef file1 2 | #define file1 3 | int a=20; 4 | #endif -------------------------------------------------------------------------------- /Unit1/1_C_Revision/file2.c: -------------------------------------------------------------------------------- 1 | #include 2 | //#include"file1.c" 3 | extern int a; 4 | int main() 5 | { 6 | printf("%d\n",a); 7 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/fnCat.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* void add1(); 3 | int add2(); 4 | void add3(int,int); 5 | int add4(int,int); */ 6 | int main() 7 | { 8 | int a=2,b=3; 9 | int sum=a+b; 10 | printf("sum=%d\n",sum); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/pragmaDir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #pragma pack(8) //n= 2^0, 2^1, 2^2, 2^3 3 | struct test 4 | { 5 | char b; //1 byte 6 | //3 bytes padding 7 | int a; //4 bytes 8 | }; 9 | 10 | int main() 11 | { 12 | printf("%lu\n",sizeof(struct test)); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/registr.c: -------------------------------------------------------------------------------- 1 | //register 2 | #include 3 | //register int a=100; 4 | int main() 5 | { 6 | register int a=100; 7 | // int *p=&a; //Error 8 | printf("%d\n",a); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/staticStorage.c: -------------------------------------------------------------------------------- 1 | //static 2 | 3 | #include 4 | void fn(); 5 | 6 | int main() 7 | { 8 | int a=10; 9 | printf("In main a=%d\n",a); 10 | fn(); 11 | printf("In main a=%d\n",a); 12 | fn(); 13 | } 14 | 15 | void fn() 16 | { 17 | static int a=10; 18 | int b=20; b++; 19 | a++; 20 | printf("In fn a=%d b=%d\n",a,b); 21 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/struct1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct student 4 | { 5 | int rollNo; 6 | char name[20]; 7 | int marks; 8 | }; 9 | typedef struct student student_t; 10 | 11 | void display(const struct student *s); //struct student *s=&s1; 12 | int main() 13 | { 14 | struct student s1={1,"abc",20},s2; 15 | struct student s3={.name="def",.rollNo=2,.marks=20}; 16 | s2=s1; 17 | display(&s1); 18 | display(&s3); 19 | return 0; 20 | } 21 | 22 | void display(const struct student *s) 23 | { 24 | printf("%d %s %d\n",(*s).rollNo,s->name,s->marks); 25 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/structRet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct demo 4 | { 5 | int a; 6 | double b; 7 | }; 8 | typedef struct demo DEMO; 9 | DEMO* f1(DEMO *d); 10 | DEMO f2(DEMO d); 11 | int main() 12 | { 13 | DEMO d={1,2.5}; 14 | /* DEMO *d2=f1(&d); 15 | printf("%d %lf\n",d.a,d.b); 16 | printf("%d %lf\n",d2->a,d2->b); */ 17 | DEMO d3=f2(d); 18 | printf("%d %lf\n",d.a,d.b); 19 | printf("%d %lf\n",d3.a,d3.b); 20 | return 0; 21 | } 22 | DEMO* f1(DEMO *d) 23 | { 24 | d->a=10; 25 | return d; 26 | } 27 | DEMO f2(DEMO d) 28 | { 29 | d.a=100; 30 | return d; 31 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/swapVar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap1(int,int); 4 | void swap2(int*,int*); 5 | 6 | int main() 7 | { 8 | int a=2,b=3; 9 | 10 | printf("Before swapping a=%d b=%d\n",a,b); 11 | swap1(a,b); 12 | printf("After swapping a=%d b=%d\n",a,b); 13 | 14 | printf("Before swapping a=%d b=%d\n",a,b); 15 | swap2(&a,&b); 16 | printf("After swapping a=%d b=%d\n",a,b); 17 | return 0; 18 | } 19 | 20 | void swap1(int m,int n) //int m=a, int n=b 21 | { 22 | int temp; 23 | temp=m; 24 | m=n; 25 | n=temp; 26 | } 27 | 28 | void swap2(int *x,int *y) 29 | { 30 | int temp; 31 | temp=*x; 32 | *x=*y; 33 | *y=temp; 34 | } -------------------------------------------------------------------------------- /Unit1/1_C_Revision/toh.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ToH(int n,char source,char temp,char dest); 4 | 5 | int main() 6 | { 7 | int n; 8 | printf("Enter the no. of discs\n"); 9 | scanf("%d",&n); 10 | ToH(n,'A','B','C'); 11 | return 0; 12 | } 13 | 14 | 15 | void ToH(int n,char source,char temp,char dest) 16 | { 17 | if(n==1) 18 | { 19 | printf("Move disc %d from %c to %c\n",n,source,dest); 20 | return; 21 | } 22 | ToH(n-1,source,dest,temp); 23 | printf("Move disc %d from %c to %c\n",n,source,dest); 24 | ToH(n-1,temp,source,dest); 25 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/1_SLL_withPreconditions/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node*next; 5 | }NODE; 6 | 7 | typedef struct llist 8 | { 9 | NODE* head; 10 | }LLIST; 11 | 12 | void initList(LLIST*); 13 | void insertFront(LLIST*,int); 14 | void insertLast(LLIST*,int); 15 | void insertAtPos(LLIST*,int,int); 16 | void display(LLIST*); 17 | void destroyList(LLIST*); 18 | void deleteFront(LLIST *,int*); 19 | void deleteLast(LLIST *,int *); 20 | void deleteAtPos(LLIST *,int *,int); 21 | void reverseList(LLIST *); 22 | int searchKey(LLIST *,int); 23 | -------------------------------------------------------------------------------- /Unit1/2_SLL/1_SLL_withPreconditions/llistClient.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | LLIST lobj; 7 | initList(&lobj); 8 | int choice,ele,pos; 9 | 10 | do{ 11 | printf("1.Insert Front 2.Insert Last 3.Insert At Pos 4.Display\n"); 12 | printf("5.Delete Front 6.Delete Last 7.Delete At Pos 8.ReverseList 9.Search Key\n"); 13 | printf("Enter your choice\n"); 14 | scanf("%d",&choice); 15 | 16 | switch(choice) 17 | { 18 | case 1:printf("Enter an integer\n"); 19 | scanf("%d",&ele); 20 | insertFront(&lobj,ele); 21 | break; 22 | case 2:printf("Enter an integer\n"); 23 | scanf("%d",&ele); 24 | insertLast(&lobj,ele); 25 | break; 26 | case 3:printf("Enter an integer\n"); 27 | scanf("%d",&ele); 28 | printf("Enter the pos\n"); 29 | scanf("%d",&pos); 30 | insertAtPos(&lobj,ele,pos); 31 | break; 32 | case 4: display(&lobj); 33 | break; 34 | case 5:deleteFront(&lobj,&ele); 35 | printf("Deleted ele is %d\n",ele); 36 | break; 37 | case 6:deleteLast(&lobj,&ele); 38 | printf("Deleted ele is %d\n",ele); 39 | break; 40 | case 7: printf("Enter the pos\n"); 41 | scanf("%d",&pos); 42 | deleteAtPos(&lobj,&ele,pos); 43 | printf("Deleted ele is %d\n",ele); 44 | break; 45 | case 8:printf("Reversed List is\n"); 46 | reverseList(&lobj); 47 | display(&lobj); 48 | break; 49 | case 9:printf("Enter search element\n"); 50 | scanf("%d",&ele); 51 | pos=searchKey(&lobj,ele); 52 | if(pos>-1)printf("Found at %d\n",pos); 53 | else 54 | printf("Not found\n"); 55 | break; 56 | } 57 | }while(choice<10); 58 | destroyList(&lobj); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/1_SLL_withPreconditions/llistImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | #include 4 | 5 | void initList(LLIST* pl) 6 | { 7 | pl->head=NULL; 8 | } 9 | NODE* getNode(int ele) 10 | { 11 | NODE* temp=malloc(sizeof(NODE)); 12 | if(temp!=NULL) 13 | { 14 | temp->info=ele; 15 | temp->next=NULL;} 16 | return temp; 17 | } 18 | void insertFront(LLIST* pl,int ele) 19 | { 20 | NODE *temp=getNode(ele); 21 | temp->next=pl->head; 22 | pl->head=temp; 23 | } 24 | void insertLast(LLIST* pl,int ele) 25 | { 26 | NODE *temp=getNode(ele); 27 | //Empty List 28 | if(pl->head==NULL) 29 | { 30 | pl->head=temp; 31 | return; 32 | } 33 | NODE *p=pl->head; 34 | while(p->next!=NULL) 35 | p=p->next; 36 | p->next=temp; 37 | } 38 | /* 39 | int countNodes(LLIST *pl) 40 | { 41 | NODE *p=pl->head; 42 | int count=0; 43 | 44 | if(pl->head==NULL) 45 | return count; 46 | while(p!=NULL) 47 | { 48 | count++; 49 | p=p->next; 50 | } 51 | return count; 52 | } 53 | */ 54 | //Precondition: pos is a valid position, 0<=pos<=n 55 | //where n is the no. of nodes in the list 56 | void insertAtPos(LLIST* pl,int ele,int pos) 57 | { 58 | // int n=countNodes(pl); 59 | NODE *temp=getNode(ele); 60 | // if(pos<0 || pos>n) return; 61 | if(pos==0) 62 | { 63 | temp->next=pl->head; 64 | pl->head=temp; 65 | return; 66 | } 67 | NODE *p=pl->head,*q=NULL; 68 | for(int i=0;inext; 72 | } 73 | q->next=temp; 74 | temp->next=p; 75 | } 76 | void display(LLIST* pl) 77 | { 78 | NODE *p=pl->head; 79 | 80 | if(pl->head==NULL) 81 | { 82 | printf("Empty List\n"); 83 | return; 84 | } 85 | while(p!=NULL) 86 | { 87 | printf("%d ",p->info); 88 | p=p->next; 89 | } 90 | printf("\n"); 91 | } 92 | 93 | void destroyList(LLIST* pl) 94 | { 95 | NODE *p=pl->head,*q=NULL; 96 | while(p!=NULL) 97 | { 98 | q=p; //printf("%d ",p->info); 99 | p=p->next; 100 | free(q); 101 | } 102 | pl->head=NULL; 103 | } 104 | //Precondition: List exists and is non empty 105 | void deleteFront(LLIST *pl,int *pe) 106 | { 107 | // if(pl->head==NULL) return; 108 | NODE *p=pl->head; 109 | pl->head=p->next; 110 | *pe=p->info; 111 | free(p); 112 | } 113 | //Precondition: List exists and is non empty 114 | void deleteLast(LLIST *pl,int *pe) 115 | { 116 | // if(pl->head==NULL) return; 117 | NODE *p=pl->head; 118 | //Single Node 119 | if(p->next==NULL) 120 | { 121 | *pe=p->info; 122 | pl->head=NULL; 123 | free(p); 124 | return; 125 | } 126 | NODE *q=NULL; 127 | while(p->next!=NULL) 128 | { 129 | q=p; 130 | p=p->next; 131 | } 132 | *pe=p->info; 133 | q->next=NULL; 134 | free(p); 135 | } 136 | //Precondition: List exists and is non empty 137 | //pos is a valid position, 0<=poshead; 142 | if(pos==0) 143 | { 144 | pl->head=p->next; 145 | *pe=p->info; 146 | free(p); 147 | return; 148 | } 149 | NODE *q=NULL; 150 | for(int i=0;inext; 154 | } 155 | q->next=p->next; 156 | *pe=p->info; 157 | free(p); 158 | } 159 | void reverseList(LLIST *pl) 160 | { 161 | NODE *prev=NULL,*cur=pl->head,*next=NULL; 162 | while(cur!=NULL) 163 | { 164 | next=cur->next; 165 | cur->next=prev; 166 | prev=cur; 167 | cur=next; 168 | } 169 | pl->head=prev; 170 | } 171 | int searchKey(LLIST *pl,int ele) 172 | { 173 | NODE *p=pl->head; 174 | int pos=0; 175 | while(p!=NULL) 176 | { 177 | if(p->info==ele) 178 | { 179 | return pos; 180 | } 181 | pos++; 182 | p=p->next; 183 | } 184 | return -1; 185 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/2_SLL_ErrorCondnsHandled/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node*next; 5 | }NODE; 6 | 7 | typedef struct llist 8 | { 9 | NODE* head; 10 | }LLIST; 11 | 12 | void initList(LLIST*); 13 | void insertFront(LLIST*,int); 14 | void insertLast(LLIST*,int); 15 | void insertAtPos(LLIST*,int,int); 16 | void display(LLIST*); 17 | void destroyList(LLIST*); 18 | int deleteFront(LLIST *,int*); 19 | int deleteLast(LLIST *,int *); 20 | int deleteAtPos(LLIST *,int *,int); 21 | void reverseList(LLIST *); 22 | int searchKey(LLIST *,int); 23 | -------------------------------------------------------------------------------- /Unit1/2_SLL/2_SLL_ErrorCondnsHandled/llistClient.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | LLIST lobj; 7 | initList(&lobj); 8 | int choice,ele,pos; 9 | 10 | do{ 11 | printf("1.Insert Front 2.Insert Last 3.Insert At Pos 4.Display\n"); 12 | printf("5.Delete Front 6.Delete Last 7.Delete At Pos 8.ReverseList 9.Search Key\n"); 13 | printf("Enter your choice\n"); 14 | scanf("%d",&choice); 15 | 16 | switch(choice) 17 | { 18 | case 1:printf("Enter an integer\n"); 19 | scanf("%d",&ele); 20 | insertFront(&lobj,ele); 21 | break; 22 | case 2:printf("Enter an integer\n"); 23 | scanf("%d",&ele); 24 | insertLast(&lobj,ele); 25 | break; 26 | case 3:printf("Enter an integer\n"); 27 | scanf("%d",&ele); 28 | printf("Enter the pos\n"); 29 | scanf("%d",&pos); 30 | insertAtPos(&lobj,ele,pos); 31 | break; 32 | case 4: display(&lobj); 33 | break; 34 | case 5:if(deleteFront(&lobj,&ele)) 35 | printf("Deleted ele is %d\n",ele); 36 | else 37 | printf("Deletion Failed\n"); 38 | break; 39 | case 6:if(deleteLast(&lobj,&ele)) 40 | printf("Deleted ele is %d\n",ele); 41 | else 42 | printf("Deletion Failed\n"); 43 | break; 44 | case 7: printf("Enter the pos\n"); 45 | scanf("%d",&pos); 46 | if(deleteAtPos(&lobj,&ele,pos)) 47 | printf("Deleted ele is %d\n",ele); 48 | else 49 | printf("Deletion Failed\n"); 50 | break; 51 | case 8:printf("Reversed List is\n"); 52 | reverseList(&lobj); 53 | display(&lobj); 54 | break; 55 | case 9:printf("Enter search element\n"); 56 | scanf("%d",&ele); 57 | pos=searchKey(&lobj,ele); 58 | if(pos>-1)printf("Found at %d\n",pos); 59 | else 60 | printf("Not found\n"); 61 | break; 62 | } 63 | }while(choice<10); 64 | destroyList(&lobj); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/2_SLL_ErrorCondnsHandled/llistImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | #include 4 | 5 | void initList(LLIST* pl) 6 | { 7 | pl->head=NULL; 8 | } 9 | NODE* getNode(int ele) 10 | { 11 | NODE* temp=malloc(sizeof(NODE)); 12 | if(temp!=NULL) 13 | { 14 | temp->info=ele; 15 | temp->next=NULL;} 16 | return temp; 17 | } 18 | void insertFront(LLIST* pl,int ele) 19 | { 20 | NODE *temp=getNode(ele); 21 | temp->next=pl->head; 22 | pl->head=temp; 23 | } 24 | void insertLast(LLIST* pl,int ele) 25 | { 26 | NODE *temp=getNode(ele); 27 | //Empty List 28 | if(pl->head==NULL) 29 | { 30 | pl->head=temp; 31 | return; 32 | } 33 | NODE *p=pl->head; 34 | while(p->next!=NULL) 35 | p=p->next; 36 | p->next=temp; 37 | } 38 | 39 | int countNodes(LLIST *pl) 40 | { 41 | NODE *p=pl->head; 42 | int count=0; 43 | 44 | if(pl->head==NULL) 45 | return count; 46 | while(p!=NULL) 47 | { 48 | count++; 49 | p=p->next; 50 | } 51 | return count; 52 | } 53 | 54 | void insertAtPos(LLIST* pl,int ele,int pos) 55 | { 56 | int n=countNodes(pl); 57 | NODE *temp=getNode(ele); 58 | if(pos<0 || pos>n) return; 59 | if(pos==0) 60 | { 61 | temp->next=pl->head; 62 | pl->head=temp; 63 | return; 64 | } 65 | NODE *p=pl->head,*q=NULL; 66 | for(int i=0;inext; 70 | } 71 | q->next=temp; 72 | temp->next=p; 73 | } 74 | void display(LLIST* pl) 75 | { 76 | NODE *p=pl->head; 77 | 78 | if(pl->head==NULL) 79 | { 80 | printf("Empty List\n"); 81 | return; 82 | } 83 | while(p!=NULL) 84 | { 85 | printf("%d ",p->info); 86 | p=p->next; 87 | } 88 | printf("\n"); 89 | } 90 | 91 | void destroyList(LLIST* pl) 92 | { 93 | NODE *p=pl->head,*q=NULL; 94 | while(p!=NULL) 95 | { 96 | q=p; //printf("%d ",p->info); 97 | p=p->next; 98 | free(q); 99 | } 100 | pl->head=NULL; 101 | } 102 | 103 | int deleteFront(LLIST *pl,int *pe) 104 | { 105 | if(pl->head==NULL) 106 | return 0; 107 | NODE *p=pl->head; 108 | pl->head=p->next; 109 | *pe=p->info; 110 | free(p); 111 | return 1; 112 | } 113 | 114 | int deleteLast(LLIST *pl,int *pe) 115 | { 116 | if(pl->head==NULL) 117 | return 0; 118 | NODE *p=pl->head; 119 | //Single Node 120 | if(p->next==NULL) 121 | { 122 | *pe=p->info; 123 | pl->head=NULL; 124 | free(p); 125 | return 1; 126 | } 127 | NODE *q=NULL; 128 | while(p->next!=NULL) 129 | { 130 | q=p; 131 | p=p->next; 132 | } 133 | *pe=p->info; 134 | q->next=NULL; 135 | free(p); 136 | return 1; 137 | } 138 | 139 | int deleteAtPos(LLIST *pl,int *pe,int pos) 140 | { 141 | if(pl->head==NULL) 142 | return 0; 143 | int n=countNodes(pl); 144 | if(pos<0 || pos>=n) 145 | return 0; 146 | NODE *p=pl->head; 147 | if(pos==0) 148 | { 149 | pl->head=p->next; 150 | *pe=p->info; 151 | free(p); 152 | return 1; 153 | } 154 | NODE *q=NULL; 155 | for(int i=0;inext; 159 | } 160 | q->next=p->next; 161 | *pe=p->info; 162 | free(p); 163 | return 1; 164 | } 165 | void reverseList(LLIST *pl) 166 | { 167 | NODE *prev=NULL,*cur=pl->head,*next=NULL; 168 | while(cur!=NULL) 169 | { 170 | next=cur->next; 171 | cur->next=prev; 172 | prev=cur; 173 | cur=next; 174 | } 175 | pl->head=prev; 176 | } 177 | int searchKey(LLIST *pl,int ele) 178 | { 179 | NODE *p=pl->head; 180 | int pos=0; 181 | while(p!=NULL) 182 | { 183 | if(p->info==ele) 184 | { 185 | return pos; 186 | } 187 | pos++; 188 | p=p->next; 189 | } 190 | return -1; 191 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/3_SLL_Variations/llist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | NODE *head=NULL; 7 | 8 | int choice,ele; 9 | do{ 10 | printf("1.Insert Front 2. display 3.Insert Front\n"); 11 | scanf("%d",&choice); 12 | switch(choice) 13 | { 14 | case 1:printf("Enter an element\n"); 15 | scanf("%d",&ele); 16 | head=insertFront(head,ele); 17 | break; 18 | case 2:display(head); 19 | break; 20 | case 3:printf("Enter an element\n"); 21 | scanf("%d",&ele); 22 | insertFront1(&head,ele); 23 | break; 24 | } 25 | }while(choice<4); 26 | head=destroyList(head); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/3_SLL_Variations/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *next; 5 | }NODE; 6 | 7 | NODE* insertFront(NODE*,int); 8 | void display(NODE*); 9 | NODE* destroyList(NODE*); 10 | void insertFront1(NODE**,int); -------------------------------------------------------------------------------- /Unit1/2_SLL/3_SLL_Variations/llistImpl.c: -------------------------------------------------------------------------------- 1 | #include"llist.h" 2 | #include 3 | #include 4 | 5 | NODE* getNode(int ele) 6 | { 7 | NODE *temp=malloc(sizeof(NODE)); 8 | temp->info=ele; 9 | temp->next=NULL; 10 | return temp; 11 | } 12 | 13 | NODE* insertFront(NODE *head,int ele) 14 | { 15 | NODE* temp=getNode(ele); 16 | temp->next=head; 17 | return temp; //or // head=temp; return head; 18 | } 19 | 20 | void display(NODE* head) 21 | { 22 | if(head==NULL) 23 | { 24 | printf("Empty List\n"); 25 | return; 26 | } 27 | NODE* p=head; 28 | while(p!=NULL) 29 | { 30 | printf("%d ",p->info); 31 | p=p->next; 32 | } 33 | printf("\n"); 34 | } 35 | NODE* destroyList(NODE* headp) 36 | { 37 | NODE *p=headp,*q; 38 | while(p!=NULL) 39 | { 40 | q=p; 41 | p=p->next; 42 | free(q); 43 | } 44 | headp=NULL; 45 | // return headp; 46 | } 47 | void insertFront1(NODE** head,int ele) 48 | { 49 | NODE* temp=getNode(ele); 50 | temp->next=*head; 51 | *head=temp; 52 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/4_ordList_Precondn/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *next; 5 | }NODE; 6 | 7 | NODE* ordIns(NODE*,int); 8 | NODE* deleteFront(NODE*,int*); 9 | NODE* destroyList(NODE*); 10 | void display(NODE*); 11 | int searchKey(NODE*,int); -------------------------------------------------------------------------------- /Unit1/2_SLL/4_ordList_Precondn/ord.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | NODE* head=NULL; 7 | int choice,ele,pos; 8 | do{ 9 | printf("1.Ordered Ins 2. DelFrnt 3. Display 4:search\n"); 10 | scanf("%d",&choice); 11 | switch(choice) 12 | { 13 | case 1:printf("Enter an ele\n");; 14 | scanf("%d",&ele); 15 | head=ordIns(head,ele); 16 | break; 17 | case 2:head=deleteFront(head,&ele); 18 | printf("Deleted ele is %d\n",ele); 19 | break; 20 | case 3:display(head); 21 | break; 22 | case 4: printf("Enter an ele\n");; 23 | scanf("%d",&ele); 24 | pos=searchKey(head,ele); 25 | if(pos>-1) 26 | printf("Elemenet found at pos %d\n",pos); 27 | else 28 | printf("Ele not found\n"); 29 | break; 30 | } 31 | }while(choice<5); 32 | head=destroyList(head); 33 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/4_ordList_Precondn/ordImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | #include 4 | 5 | NODE* getNode(int ele) 6 | { 7 | NODE *temp=malloc(sizeof(NODE)); 8 | temp->info=ele; 9 | temp->next=NULL; 10 | return temp; 11 | } 12 | 13 | NODE* ordIns(NODE* phead,int ele) 14 | { 15 | NODE *p=phead,*q=NULL; 16 | NODE* temp=getNode(ele); 17 | 18 | while(p!=NULL && temp->info > p->info) 19 | { 20 | q=p; 21 | p=p->next; 22 | } 23 | 24 | if(q==NULL) //if(p==phead) 25 | { 26 | temp->next=phead; 27 | phead=temp; 28 | return phead; 29 | } 30 | 31 | q->next=temp; 32 | temp->next=p; 33 | return phead; 34 | } 35 | 36 | void display(NODE* phead) 37 | { 38 | if(phead==NULL) 39 | { 40 | printf("Empty List\n"); 41 | return; 42 | } 43 | NODE *p=phead; 44 | while(p!=NULL) 45 | { 46 | printf("%d ",p->info); 47 | p=p->next; 48 | } 49 | printf("\n"); 50 | } 51 | NODE* destroyList(NODE* phead) 52 | { 53 | NODE *p=phead,*q; 54 | while(p!=NULL) 55 | { 56 | q=p; 57 | p=p->next; 58 | free(q); 59 | } 60 | phead=NULL; 61 | return phead; 62 | } 63 | //Precondition: List is not empty 64 | NODE* deleteFront(NODE* headp,int* pe) 65 | { 66 | NODE *p=headp; 67 | headp=p->next; 68 | *pe=p->info; 69 | free(p); 70 | return headp; 71 | } 72 | 73 | int searchKey(NODE* phead,int ele) 74 | { 75 | NODE* p=phead; 76 | int pos=0; 77 | while(p!=NULL) 78 | { 79 | if(p->info==ele) 80 | { 81 | return pos; 82 | } 83 | p=p->next; 84 | pos++; 85 | } 86 | return -1; 87 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/5_concat/concat.c: -------------------------------------------------------------------------------- 1 | 2 | #include"llist.h" 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int n,ele; 9 | NODE *head1=NULL,*head2=NULL,*head3=NULL; 10 | printf("List1 nodes\n"); 11 | scanf("%d",&n); 12 | for(int i=0;i 2 | #include"llist.h" 3 | #include 4 | 5 | struct node *insertBeg(NODE* head,int ele) 6 | { 7 | NODE*temp=malloc(sizeof(NODE)); 8 | temp->info=ele; 9 | temp->next=NULL; 10 | 11 | temp->next=head; 12 | head=temp; return head; 13 | // return temp; 14 | } 15 | 16 | void display(NODE* head) 17 | { 18 | NODE* p=head; 19 | while(p!=NULL) 20 | { 21 | printf("%d ",p->info); 22 | p=p->next; 23 | } 24 | printf("\n"); 25 | } 26 | 27 | NODE* concat(NODE* head1,NODE* head2) 28 | { 29 | if(head2==NULL) 30 | return head1; 31 | if(head1==NULL) 32 | return head2; 33 | NODE *p=head1; 34 | while(p->next!=NULL) 35 | p=p->next; 36 | p->next=head2; 37 | return head1; 38 | } 39 | 40 | NODE* destroyList(NODE* phead) 41 | { 42 | NODE *p=phead,*q; 43 | while(p!=NULL) 44 | { 45 | q=p; 46 | p=p->next; 47 | free(q); 48 | } 49 | phead=NULL; 50 | return phead; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Unit1/2_SLL/5_concat/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *next; 5 | }NODE; 6 | 7 | struct node *insertBeg(NODE* head,int ele); 8 | void display(NODE* head); 9 | NODE* concat(NODE* head1,NODE* head2); 10 | NODE* destroyList(NODE* head); -------------------------------------------------------------------------------- /Unit1/2_SLL/6_Poly/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct polynomial 2 | { 3 | int coeff; 4 | int powx; 5 | struct polynomial *next; 6 | }NODE; 7 | 8 | void createPoly(NODE**); 9 | void addPoly(NODE*,NODE*,NODE**); 10 | void display(NODE*); 11 | void insertLast(NODE**,int,int); 12 | void destroyList(NODE**); 13 | int evalPoly(NODE*,int); -------------------------------------------------------------------------------- /Unit1/2_SLL/6_Poly/polyImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include"llist.h" 5 | 6 | void insertLast(NODE** phead,int cf,int px) 7 | { 8 | NODE *temp=malloc(sizeof(NODE)); 9 | temp->coeff=cf; 10 | temp->powx=px; 11 | temp->next=NULL; 12 | 13 | NODE *p=*phead; 14 | //Empty Case 15 | if(*phead==NULL) 16 | { 17 | *phead=temp; 18 | return; 19 | } 20 | while(p->next!=NULL) 21 | p=p->next; 22 | p->next=temp; 23 | } 24 | void createPoly(NODE** phead) 25 | { 26 | int choice,cf,px; 27 | while(1) 28 | { 29 | printf("Enter the coeff and pow\n"); 30 | scanf("%d %d",&cf,&px); 31 | insertLast(phead,cf,px); 32 | printf("Do you want to enter a term\n"); 33 | scanf("%d",&choice); 34 | if(choice==0) 35 | break; 36 | } 37 | } 38 | void addPoly(NODE* head1,NODE* head2,NODE** head3) 39 | { 40 | NODE *p=head1, *q=head2; 41 | int px,cf; 42 | while(p!=NULL && q!=NULL) 43 | { 44 | if(p->powx == q->powx) 45 | { 46 | cf=p->coeff+q->coeff; 47 | px=p->powx; 48 | p=p->next; 49 | q=q->next; 50 | } 51 | else if(p->powx > q->powx) 52 | { 53 | cf=p->coeff; 54 | px=p->powx; 55 | p=p->next; 56 | } 57 | else 58 | { 59 | cf=q->coeff; 60 | px=q->powx; 61 | q=q->next; 62 | } 63 | insertLast(head3,cf,px); 64 | } 65 | while(p!=NULL) 66 | { 67 | cf=p->coeff; 68 | px=p->powx; 69 | p=p->next; 70 | insertLast(head3,cf,px); 71 | } 72 | while(q!=NULL) 73 | { 74 | cf=q->coeff; 75 | px=q->powx; 76 | q=q->next; 77 | insertLast(head3,cf,px); 78 | } 79 | } 80 | void display(NODE* phead) 81 | { 82 | NODE* p=phead; 83 | while(p->next!=NULL) 84 | { 85 | printf("(%dx^%d)+",p->coeff,p->powx); 86 | p=p->next; 87 | } 88 | printf("(%dx^%d)\n",p->coeff,p->powx); 89 | } 90 | void destroyList(NODE** phead) 91 | { 92 | NODE *p=*phead,*q=NULL; 93 | while(p!=NULL) 94 | { 95 | q=p; 96 | p=p->next; 97 | free(q); 98 | } 99 | *phead=NULL; 100 | } 101 | 102 | int evalPoly(NODE* phead,int x) 103 | { 104 | int sum=0; 105 | NODE *p=phead; 106 | while(p!=NULL) 107 | { 108 | sum=sum+p->coeff*pow(x,p->powx); 109 | p=p->next; 110 | } 111 | return sum; 112 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/6_Poly/polyc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | NODE *head1=NULL,*head2=NULL,*head3=NULL; 7 | int x; 8 | printf("Polynomial 1:\n"); 9 | createPoly(&head1); 10 | display(head1); 11 | printf("Polynomial 2:\n"); 12 | createPoly(&head2); 13 | display(head2); 14 | addPoly(head1,head2,&head3); 15 | display(head3); 16 | printf("Enter value of x\n"); 17 | scanf("%d",&x); 18 | printf("Res=%d\n",evalPoly(head1,x)); 19 | destroyList(&head1); 20 | destroyList(&head2); 21 | destroyList(&head3); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/6_Poly/tailPtrImpl/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct polynomial 2 | { 3 | int coeff; 4 | int powx; 5 | struct polynomial *next; 6 | }NODE; 7 | 8 | typedef struct list 9 | { 10 | NODE *head; 11 | NODE *tail; 12 | }LLIST; 13 | 14 | void initList(LLIST*); 15 | void createPoly(LLIST*); 16 | void addPoly(LLIST*,LLIST*,LLIST*); 17 | void display(LLIST*); 18 | void insertLast(LLIST*,int,int); 19 | void destroyList(LLIST*); -------------------------------------------------------------------------------- /Unit1/2_SLL/6_Poly/tailPtrImpl/llistImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include"llist.h" 5 | 6 | void initList(LLIST* pl) 7 | { 8 | pl->head=NULL; 9 | pl->tail=NULL; 10 | } 11 | void insertLast(LLIST* pl,int cf,int px) 12 | { 13 | NODE *temp=malloc(sizeof(NODE)); 14 | temp->coeff=cf; 15 | temp->powx=px; 16 | temp->next=NULL; 17 | 18 | NODE *p=pl->head; 19 | //Empty Case 20 | if(pl->head==NULL) 21 | { 22 | pl->head=temp; 23 | pl->tail=temp; 24 | return; 25 | } 26 | pl->tail->next=temp; 27 | pl->tail=temp; 28 | } 29 | void createPoly(LLIST* pl) 30 | { 31 | int choice,cf,px; 32 | while(1) 33 | { 34 | printf("Enter the coeff and pow\n"); 35 | scanf("%d %d",&cf,&px); 36 | insertLast(pl,cf,px); 37 | printf("Do you want to enter a term\n"); 38 | scanf("%d",&choice); 39 | if(choice==0) 40 | break; 41 | } 42 | } 43 | void addPoly(LLIST* pl1,LLIST* pl2,LLIST* pl3) 44 | { 45 | NODE *p=pl1->head, *q=pl2->head; 46 | int px,cf; 47 | while(p!=NULL && q!=NULL) 48 | { 49 | if(p->powx == q->powx) 50 | { 51 | cf=p->coeff+q->coeff; 52 | px=p->powx; 53 | p=p->next; 54 | q=q->next; 55 | } 56 | else if(p->powx > q->powx) 57 | { 58 | cf=p->coeff; 59 | px=p->powx; 60 | p=p->next; 61 | } 62 | else 63 | { 64 | cf=q->coeff; 65 | px=q->powx; 66 | q=q->next; 67 | } 68 | insertLast(pl3,cf,px); 69 | } 70 | while(p!=NULL) 71 | { 72 | cf=p->coeff; 73 | px=p->powx; 74 | p=p->next; 75 | insertLast(pl3,cf,px); 76 | } 77 | while(q!=NULL) 78 | { 79 | cf=q->coeff; 80 | px=q->powx; 81 | q=q->next; 82 | insertLast(pl3,cf,px); 83 | } 84 | } 85 | void display(LLIST* pl) 86 | { 87 | NODE* p=pl->head; 88 | while(p->next!=NULL) 89 | { 90 | printf("(%dx^%d)+",p->coeff,p->powx); 91 | p=p->next; 92 | } 93 | printf("(%dx^%d)\n",p->coeff,p->powx); 94 | } 95 | void destroyList(LLIST *pl) 96 | { 97 | NODE *p=pl->head,*q=NULL; 98 | while(p!=NULL) 99 | { 100 | q=p; 101 | p=p->next; 102 | free(q); 103 | } 104 | pl->head=NULL; 105 | pl->tail=NULL; 106 | } 107 | /* 108 | int evalPoly(NODE* phead,int x) 109 | { 110 | int sum=0; 111 | NODE *p=phead; 112 | while(p!=NULL) 113 | { 114 | sum=sum+p->coeff*pow(x,p->powx); 115 | p=p->next; 116 | } 117 | return sum; 118 | } */ -------------------------------------------------------------------------------- /Unit1/2_SLL/6_Poly/tailPtrImpl/llistc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | LLIST head1,head2,head3; 7 | initList(&head1); 8 | initList(&head2); 9 | initList(&head3); 10 | int x; 11 | printf("Polynomial 1:\n"); 12 | createPoly(&head1); 13 | display(&head1); 14 | printf("Polynomial 2:\n"); 15 | createPoly(&head2); 16 | display(&head2); 17 | addPoly(&head1,&head2,&head3); 18 | display(&head3); 19 | destroyList(&head1); 20 | destroyList(&head2); 21 | destroyList(&head3); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/7_DelEvenInfoNodes/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node*next; 5 | }NODE; 6 | 7 | typedef struct llist 8 | { 9 | NODE* head; 10 | }LLIST; 11 | 12 | void initList(LLIST*); 13 | void insertFront(LLIST*,int); 14 | void insertLast(LLIST*,int); 15 | void insertAtPos(LLIST*,int,int); 16 | void display(LLIST*); 17 | void destroyList(LLIST*); 18 | int deleteFront(LLIST *,int*); 19 | int deleteLast(LLIST *,int *); 20 | int deleteAtPos(LLIST *,int *,int); 21 | void reverseList(LLIST *); 22 | int searchKey(LLIST *,int); 23 | void deleteNode(LLIST*,NODE*); 24 | void deleteEven(LLIST*); -------------------------------------------------------------------------------- /Unit1/2_SLL/7_DelEvenInfoNodes/llistClient.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | LLIST lobj; 7 | initList(&lobj); 8 | int choice,ele,pos; 9 | 10 | do{ 11 | printf("1.Insert Front 2.Insert Last 3.Insert At Pos 4.Display\n"); 12 | printf("5.Delete Front 6.Delete Last 7.Delete At Pos 8.ReverseList 9.Search Key\n"); 13 | printf("Enter your choice\n"); 14 | scanf("%d",&choice); 15 | 16 | switch(choice) 17 | { 18 | case 1:printf("Enter an integer\n"); 19 | scanf("%d",&ele); 20 | insertFront(&lobj,ele); 21 | break; 22 | case 2:printf("Enter an integer\n"); 23 | scanf("%d",&ele); 24 | insertLast(&lobj,ele); 25 | break; 26 | case 3:printf("Enter an integer\n"); 27 | scanf("%d",&ele); 28 | printf("Enter the pos\n"); 29 | scanf("%d",&pos); 30 | insertAtPos(&lobj,ele,pos); 31 | break; 32 | case 4: display(&lobj); 33 | break; 34 | case 5:if(deleteFront(&lobj,&ele)) 35 | printf("Deleted ele is %d\n",ele); 36 | else 37 | printf("Deletion Failed\n"); 38 | break; 39 | case 6:if(deleteLast(&lobj,&ele)) 40 | printf("Deleted ele is %d\n",ele); 41 | else 42 | printf("Deletion Failed\n"); 43 | break; 44 | case 7: printf("Enter the pos\n"); 45 | scanf("%d",&pos); 46 | if(deleteAtPos(&lobj,&ele,pos)) 47 | printf("Deleted ele is %d\n",ele); 48 | else 49 | printf("Deletion Failed\n"); 50 | break; 51 | case 8:printf("Reversed List is\n"); 52 | reverseList(&lobj); 53 | display(&lobj); 54 | break; 55 | case 9:printf("Enter search element\n"); 56 | scanf("%d",&ele); 57 | pos=searchKey(&lobj,ele); 58 | if(pos>-1)printf("Found at %d\n",pos); 59 | else 60 | printf("Not found\n"); 61 | break; 62 | case 10:deleteEven(&lobj); 63 | display(&lobj); 64 | break; 65 | } 66 | }while(choice<11); 67 | destroyList(&lobj); 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Unit1/2_SLL/7_DelEvenInfoNodes/llistImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | #include 4 | 5 | void initList(LLIST* pl) 6 | { 7 | pl->head=NULL; 8 | } 9 | NODE* getNode(int ele) 10 | { 11 | NODE* temp=malloc(sizeof(NODE)); 12 | if(temp!=NULL) 13 | { 14 | temp->info=ele; 15 | temp->next=NULL;} 16 | return temp; 17 | } 18 | void insertFront(LLIST* pl,int ele) 19 | { 20 | NODE *temp=getNode(ele); 21 | temp->next=pl->head; 22 | pl->head=temp; 23 | } 24 | void insertLast(LLIST* pl,int ele) 25 | { 26 | NODE *temp=getNode(ele); 27 | //Empty List 28 | if(pl->head==NULL) 29 | { 30 | pl->head=temp; 31 | return; 32 | } 33 | NODE *p=pl->head; 34 | while(p->next!=NULL) 35 | p=p->next; 36 | p->next=temp; 37 | } 38 | 39 | int countNodes(LLIST *pl) 40 | { 41 | NODE *p=pl->head; 42 | int count=0; 43 | 44 | if(pl->head==NULL) 45 | return count; 46 | while(p!=NULL) 47 | { 48 | count++; 49 | p=p->next; 50 | } 51 | return count; 52 | } 53 | 54 | void insertAtPos(LLIST* pl,int ele,int pos) 55 | { 56 | int n=countNodes(pl); 57 | NODE *temp=getNode(ele); 58 | if(pos<0 || pos>n) return; 59 | if(pos==0) 60 | { 61 | temp->next=pl->head; 62 | pl->head=temp; 63 | return; 64 | } 65 | NODE *p=pl->head,*q=NULL; 66 | for(int i=0;inext; 70 | } 71 | q->next=temp; 72 | temp->next=p; 73 | } 74 | void display(LLIST* pl) 75 | { 76 | NODE *p=pl->head; 77 | 78 | if(pl->head==NULL) 79 | { 80 | printf("Empty List\n"); 81 | return; 82 | } 83 | while(p!=NULL) 84 | { 85 | printf("%d ",p->info); 86 | p=p->next; 87 | } 88 | printf("\n"); 89 | } 90 | 91 | void destroyList(LLIST* pl) 92 | { 93 | NODE *p=pl->head,*q=NULL; 94 | while(p!=NULL) 95 | { 96 | q=p; //printf("%d ",p->info); 97 | p=p->next; 98 | free(q); 99 | } 100 | pl->head=NULL; 101 | } 102 | 103 | int deleteFront(LLIST *pl,int *pe) 104 | { 105 | if(pl->head==NULL) 106 | return 0; 107 | NODE *p=pl->head; 108 | pl->head=p->next; 109 | *pe=p->info; 110 | free(p); 111 | return 1; 112 | } 113 | 114 | int deleteLast(LLIST *pl,int *pe) 115 | { 116 | if(pl->head==NULL) 117 | return 0; 118 | NODE *p=pl->head; 119 | //Single Node 120 | if(p->next==NULL) 121 | { 122 | *pe=p->info; 123 | pl->head=NULL; 124 | free(p); 125 | return 1; 126 | } 127 | NODE *q=NULL; 128 | while(p->next!=NULL) 129 | { 130 | q=p; 131 | p=p->next; 132 | } 133 | *pe=p->info; 134 | q->next=NULL; 135 | free(p); 136 | return 1; 137 | } 138 | 139 | int deleteAtPos(LLIST *pl,int *pe,int pos) 140 | { 141 | if(pl->head==NULL) 142 | return 0; 143 | int n=countNodes(pl); 144 | if(pos<0 || pos>=n) 145 | return 0; 146 | NODE *p=pl->head; 147 | if(pos==0) 148 | { 149 | pl->head=p->next; 150 | *pe=p->info; 151 | free(p); 152 | return 1; 153 | } 154 | NODE *q=NULL; 155 | for(int i=0;inext; 159 | } 160 | q->next=p->next; 161 | *pe=p->info; 162 | free(p); 163 | return 1; 164 | } 165 | void reverseList(LLIST *pl) 166 | { 167 | NODE *prev=NULL,*cur=pl->head,*next=NULL; 168 | while(cur!=NULL) 169 | { 170 | next=cur->next; 171 | cur->next=prev; 172 | prev=cur; 173 | cur=next; 174 | } 175 | pl->head=prev; 176 | } 177 | int searchKey(LLIST *pl,int ele) 178 | { 179 | NODE *p=pl->head; 180 | int pos=0; 181 | while(p!=NULL) 182 | { 183 | if(p->info==ele) 184 | { 185 | return pos; 186 | } 187 | pos++; 188 | p=p->next; 189 | } 190 | return -1; 191 | } 192 | 193 | void deleteNode(LLIST* pl,NODE* track) 194 | { 195 | if(pl->head==track) 196 | { 197 | pl->head=pl->head->next; //pl->head=track->next; 198 | free(track); 199 | return; 200 | } 201 | NODE *p=pl->head; 202 | while(p->next!=track) 203 | p=p->next; 204 | p->next=track->next; 205 | free(track); 206 | } 207 | void deleteEven(LLIST *pl) 208 | { 209 | if(pl->head==NULL) 210 | return; 211 | NODE *p=pl->head,*toDelete=NULL; 212 | while(p!=NULL) 213 | { 214 | if(p->info%2==0) 215 | { 216 | toDelete=p; 217 | p=p->next; 218 | deleteNode(pl,toDelete); 219 | } 220 | else 221 | p=p->next; 222 | } 223 | } -------------------------------------------------------------------------------- /Unit1/3_DLL/llist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | DLIST lobj; 7 | initList(&lobj); 8 | int choice,ele,pos,givenEle,res; 9 | 10 | do{ 11 | printf("1.Insert Front 2.Insert Last 3.Display\n"); 12 | printf("4.Delete Front 5. Delete Lst 6.InsertAfterEle\n"); 13 | printf("7.DeleteGivenEle 8.InsertAtPos 9.DeleteAtPos\n"); 14 | scanf("%d",&choice); 15 | 16 | switch(choice) 17 | { 18 | case 1:printf("Enter an integer\n"); 19 | scanf("%d",&ele); 20 | insertFront(&lobj,ele); 21 | break; 22 | case 2:printf("Enter an integer\n"); 23 | scanf("%d",&ele); 24 | insertLast(&lobj,ele); 25 | break; 26 | case 4: if(deleteFront(&lobj,&ele)) 27 | printf("deleted ele is %d\n",ele); 28 | else 29 | printf("Empty List\n"); 30 | break; 31 | case 5: if(deleteLast(&lobj,&ele)) 32 | printf("deleted ele is %d\n",ele); 33 | else 34 | printf("Empty List\n"); 35 | break; 36 | case 3:display(&lobj); 37 | break; 38 | case 6:printf("Enter the ele after which you want to insert\n"); 39 | scanf("%d",&givenEle); 40 | printf("Enter the ele\n"); 41 | scanf("%d",&ele); 42 | insertAfterEle(&lobj,givenEle,ele); 43 | break; 44 | case 7:printf("Enter the ele you want to del\n"); 45 | scanf("%d",&ele); 46 | res=deleteGivenEle(&lobj,ele,&pos); 47 | if(res==0) 48 | printf("Deletion failed\n"); 49 | else 50 | printf("Element deleted at pos %d\n",pos); 51 | case 8:printf("Enter the ele\n"); 52 | scanf("%d",&ele); 53 | printf("Enter the pos\n"); 54 | scanf("%d",&pos); 55 | insertAtPos(&lobj,ele,pos); 56 | break; 57 | case 9:printf("Enter the pos\n"); 58 | scanf("%d",&pos); 59 | if(deleteAtPos(&lobj,pos,&ele)) 60 | printf("DEleted ele is %d\n",ele); 61 | else 62 | printf("DEl failed\n"); 63 | break; 64 | } 65 | }while(choice<10); 66 | destroyList(&lobj); 67 | return 0; 68 | } -------------------------------------------------------------------------------- /Unit1/3_DLL/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *prev,*next; 5 | }NODE; 6 | 7 | typedef struct dlist 8 | { 9 | struct node* head; 10 | }DLIST; 11 | 12 | void initList(DLIST*); 13 | void destroyList(DLIST*); 14 | void insertFront(DLIST*,int); 15 | void insertLast(DLIST*,int); 16 | int deleteFront(DLIST*,int*); 17 | int deleteLast(DLIST*,int*); 18 | void display(DLIST*); 19 | void insertAfterEle(DLIST*,int,int); 20 | int deleteGivenEle(DLIST*,int,int*); 21 | void insertAtPos(DLIST*,int,int); 22 | int deleteAtPos(DLIST*,int,int*); -------------------------------------------------------------------------------- /Unit1/3_DLL/llistImpl.c: -------------------------------------------------------------------------------- 1 | #include"llist.h" 2 | #include 3 | #include 4 | 5 | void initList(DLIST* pdl) 6 | { 7 | pdl->head=NULL; 8 | } 9 | void destroyList(DLIST* pdl) 10 | { 11 | NODE* p=pdl->head; 12 | if(pdl->head==NULL) 13 | return; 14 | while(p->next!=NULL) 15 | { 16 | p=p->next; 17 | free(p->prev); 18 | } 19 | free(p); 20 | } 21 | NODE* getNode(int ele) 22 | { 23 | NODE* temp=malloc(sizeof(NODE)); 24 | temp->info=ele; 25 | temp->prev=NULL; 26 | temp->next=NULL; 27 | return temp; 28 | } 29 | void insertFront(DLIST* pdl,int ele) 30 | { 31 | NODE *temp=getNode(ele); 32 | if(temp==NULL) 33 | return; 34 | temp->next=pdl->head; 35 | if(pdl->head!=NULL) 36 | pdl->head->prev=temp; 37 | pdl->head=temp; 38 | } 39 | void insertLast(DLIST* pdl,int ele) 40 | { 41 | NODE* temp=getNode(ele); 42 | if(temp==NULL) 43 | return; 44 | NODE*p =pdl->head; 45 | //Empty case 46 | if(p==NULL) 47 | { 48 | pdl->head=temp; 49 | return; 50 | } 51 | //One or more nodes 52 | while(p->next!=NULL) 53 | p=p->next; 54 | temp->prev=p; 55 | p->next=temp; 56 | } 57 | int deleteFront(DLIST* pdl,int* pe) 58 | { 59 | if(pdl->head==NULL) 60 | return 0; 61 | NODE *p=pdl->head; 62 | pdl->head=pdl->head->next; 63 | if(pdl->head!=NULL) 64 | pdl->head->prev=NULL; 65 | 66 | *pe=p->info; 67 | free(p); 68 | return 1; 69 | } 70 | int deleteLast(DLIST *pdl,int* pe) 71 | { 72 | if(pdl->head==NULL) 73 | return 0; 74 | NODE *p=pdl->head; 75 | //Single Node case 76 | if(pdl->head->next==NULL) 77 | { 78 | pdl->head=NULL; 79 | *pe=p->info; 80 | free(p); 81 | return 1; 82 | } 83 | //Multiple Node case 84 | while(p->next!=NULL) 85 | p=p->next; 86 | p->prev->next=NULL; 87 | *pe=p->info; 88 | free(p); 89 | return 1; 90 | } 91 | void display(DLIST* pdl) 92 | { 93 | NODE *p=pdl->head; 94 | if(p==NULL) 95 | { 96 | printf("Empty List\n"); 97 | return; 98 | } 99 | while(p!=NULL) 100 | { 101 | printf("%d ",p->info); 102 | p=p->next; 103 | } 104 | printf("\n"); 105 | } 106 | void insertAfterEle(DLIST* pdl,int gi,int ele) 107 | { 108 | NODE *p=pdl->head; 109 | while(p!=NULL && p->info!=gi) 110 | p=p->next; 111 | if(p==NULL) 112 | return; 113 | NODE *temp=getNode(ele); 114 | 115 | temp->prev=p; 116 | temp->next=p->next; 117 | 118 | if(p->next!=NULL) 119 | p->next->prev=temp; 120 | 121 | p->next=temp; 122 | 123 | } 124 | int deleteGivenEle(DLIST* pdl,int ele,int *pos) 125 | { 126 | int count=0; 127 | if(pdl->head==NULL) 128 | return 0; 129 | NODE* p=pdl->head; 130 | if(p->info==ele) 131 | { 132 | pdl->head=p->next; 133 | if(pdl->head!=NULL) 134 | pdl->head->prev=NULL; 135 | *pos=0; 136 | free(p); 137 | return 1; 138 | } 139 | while(p!=NULL && p->info!=ele) 140 | { 141 | count++; 142 | p=p->next; 143 | } 144 | if(p==NULL) 145 | return 0; 146 | p->prev->next=p->next; 147 | if(p->next!=NULL) 148 | p->next->prev=p->prev; 149 | *pos=count; 150 | free(p); 151 | return 1; 152 | } 153 | int countNodes(DLIST *pdl) 154 | { 155 | int count=0; 156 | if(pdl->head==NULL) 157 | return count; 158 | NODE *p=pdl->head; 159 | while(p!=NULL) 160 | { 161 | count++; 162 | p=p->next; 163 | } 164 | return count; 165 | } 166 | void insertAtPos(DLIST* pdl,int ele,int pos) 167 | { 168 | int n=countNodes(pdl); 169 | if(pos<0 || pos>n) 170 | return; 171 | if(pos==0) 172 | { 173 | insertFront(pdl,ele); 174 | return; 175 | } 176 | if(pos==n) 177 | { 178 | insertLast(pdl,ele); 179 | return; 180 | } 181 | NODE *temp=getNode(ele); 182 | NODE *p=pdl->head; 183 | for(int i=0;inext; 185 | temp->prev=p->prev; 186 | temp->next=p; 187 | p->prev->next=temp; 188 | p->prev=temp; 189 | } 190 | int deleteAtPos(DLIST* pdl,int pos,int *pe) 191 | { 192 | if(pdl->head==NULL) 193 | return 0; 194 | int n=countNodes(pdl); 195 | if(pos<0 || pos>=n) 196 | return 0; 197 | if(pos==0) 198 | { 199 | if(deleteFront(pdl,pe)) 200 | return 1; 201 | return 0; 202 | } 203 | if(pos==n-1) 204 | { 205 | int res=deleteLast(pdl,pe); 206 | return res; 207 | } 208 | NODE* p=pdl->head; 209 | for(int i=0;inext; 211 | p->prev->next=p->next; 212 | p->next->prev=p->prev; 213 | *pe=p->info; 214 | free(p); 215 | return 1; 216 | 217 | } -------------------------------------------------------------------------------- /Unit1/4_CircularSLL/cll.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *next; 5 | }NODE; 6 | 7 | typedef struct cll 8 | { 9 | NODE* last; 10 | }CLL; 11 | 12 | void initList(CLL* pl); 13 | void insertFront(CLL* pl,int ele); 14 | void insertLast(CLL* pl,int ele); 15 | void insertAtPos(CLL* pl,int ele,int pos); 16 | void destroyList(CLL* pl); 17 | void display(CLL* pl); 18 | int deleteFront(CLL* pl,int *pe); 19 | int deleteLast(CLL* pl,int *pe); 20 | int deleteAtPos(CLL* pl,int *pe,int pos); -------------------------------------------------------------------------------- /Unit1/4_CircularSLL/llistClient.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"cll.h" 3 | 4 | int main() 5 | { 6 | CLL lobj; 7 | initList(&lobj); 8 | int choice,ele,pos; 9 | 10 | do{ 11 | printf("1.Insert Front 2.Insert Last 3.Insert At Pos 4.Display\n"); 12 | printf("5.Delete Front 6.Delete Last 7.Delete At Pos\n"); 13 | printf("Enter your choice\n"); 14 | scanf("%d",&choice); 15 | 16 | switch(choice) 17 | { 18 | case 1:printf("Enter an integer\n"); 19 | scanf("%d",&ele); 20 | insertFront(&lobj,ele); 21 | break; 22 | case 2:printf("Enter an integer\n"); 23 | scanf("%d",&ele); 24 | insertLast(&lobj,ele); 25 | break; 26 | case 3:printf("Enter an integer\n"); 27 | scanf("%d",&ele); 28 | printf("Enter the pos\n"); 29 | scanf("%d",&pos); 30 | insertAtPos(&lobj,ele,pos); 31 | break; 32 | case 4: display(&lobj); 33 | break; 34 | case 5:if(deleteFront(&lobj,&ele)) 35 | printf("Deleted ele is %d\n",ele); 36 | else 37 | printf("Deletion Failed\n"); 38 | break; 39 | case 6:if(deleteLast(&lobj,&ele)) 40 | printf("Deleted ele is %d\n",ele); 41 | else 42 | printf("Deletion Failed\n"); 43 | break; 44 | case 7: printf("Enter the pos\n"); 45 | scanf("%d",&pos); 46 | if(deleteAtPos(&lobj,&ele,pos)) 47 | printf("Deleted ele is %d\n",ele); 48 | else 49 | printf("Deletion Failed\n"); 50 | break; 51 | } 52 | }while(choice<8); 53 | destroyList(&lobj); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Unit1/4_CircularSLL/llistImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"cll.h" 4 | 5 | void initList(CLL* pl) 6 | { 7 | pl->last=NULL; 8 | } 9 | 10 | void insertFront(CLL* pl,int ele) 11 | { 12 | NODE* temp=malloc(sizeof(NODE)); 13 | temp->info=ele; 14 | 15 | if(pl->last==NULL) 16 | { 17 | temp->next=temp; 18 | pl->last=temp; 19 | return; 20 | } 21 | temp->next=pl->last->next; 22 | pl->last->next=temp; 23 | } 24 | void insertLast(CLL* pl,int ele) 25 | { 26 | NODE* temp=malloc(sizeof(NODE)); 27 | temp->info=ele; 28 | 29 | if(pl->last==NULL) 30 | { 31 | temp->next=temp; 32 | pl->last=temp; 33 | return; 34 | } 35 | temp->next=pl->last->next; 36 | pl->last->next=temp; 37 | pl->last=temp; 38 | } 39 | int countNodes(CLL *pl) 40 | { 41 | int count=0; 42 | if(pl->last==NULL) 43 | return count; 44 | 45 | NODE *p=pl->last->next; 46 | do{ 47 | count++; 48 | p=p->next; 49 | }while(p!=pl->last->next); 50 | return count; 51 | 52 | /* 53 | while(p!=pl->last) 54 | { 55 | count++; 56 | p=p->next; 57 | } 58 | count++; 59 | return count; 60 | */ 61 | } 62 | void insertAtPos(CLL* pl,int ele,int pos) 63 | { 64 | int n=countNodes(pl); 65 | if(pos<0 || pos>n) 66 | return; 67 | NODE *temp=malloc(sizeof(NODE)); 68 | temp->info=ele; 69 | if(pos==0) 70 | { 71 | insertFront(pl,ele); 72 | return; 73 | } 74 | if(pos==n) 75 | { 76 | insertLast(pl,ele); 77 | return; 78 | } 79 | NODE *p=pl->last->next,*q=NULL; 80 | for(int i=0;inext; 84 | } 85 | q->next=temp; 86 | temp->next=p; 87 | } 88 | void destroyList(CLL* pl) 89 | { 90 | if(pl->last==NULL) 91 | return; 92 | NODE *p=pl->last->next; 93 | 94 | while(p!=pl->last) 95 | { 96 | pl->last->next=p->next; //printf("%d ",p->info); 97 | free(p); 98 | p=pl->last->next; 99 | } 100 | // printf("%d ",p->info); 101 | free(p); 102 | pl->last=NULL; 103 | } 104 | void display(CLL* pl) 105 | { 106 | if(pl->last==NULL) 107 | { 108 | printf("Empty List\n"); 109 | return; 110 | } 111 | 112 | NODE *p=pl->last->next; 113 | do{ 114 | printf("%d ",p->info); 115 | p=p->next; 116 | }while(p!=pl->last->next); 117 | printf("\n"); 118 | /* 119 | while(p!=pl->last) 120 | { 121 | printf("%d ",p->info); 122 | p=p->next; 123 | } 124 | printf("%d\n",p->info); 125 | */ 126 | } 127 | int deleteFront(CLL* pl,int *pe) 128 | { 129 | if(pl->last==NULL) 130 | return 0; 131 | 132 | NODE *p=pl->last->next; 133 | if(p==pl->last) 134 | { 135 | *pe=p->info; 136 | free(p); 137 | pl->last=NULL; 138 | return 1; 139 | } 140 | pl->last->next=p->next; 141 | *pe=p->info; 142 | free(p); 143 | return 1; 144 | } 145 | int deleteLast(CLL* pl,int *pe) 146 | { 147 | if(pl->last==NULL) 148 | return 0; 149 | //Single node case 150 | NODE *p=pl->last->next; 151 | if(p==pl->last) 152 | { 153 | *pe=p->info; 154 | free(p); 155 | pl->last=NULL; 156 | return 1; 157 | } 158 | //Multiple Nodes 159 | while(p->next!=pl->last) 160 | p=p->next; 161 | p->next=pl->last->next; 162 | *pe=pl->last->info; 163 | free(pl->last); 164 | pl->last=p; 165 | return 1; 166 | } 167 | int deleteAtPos(CLL* pl,int *pe,int pos) 168 | { 169 | if(pl->last==NULL) 170 | return 0; 171 | int n=countNodes(pl); 172 | if(pos<0 || pos>=n) 173 | return 0; 174 | 175 | NODE *p=pl->last->next,*q=NULL; 176 | if(pos==0) 177 | return deleteFront(pl,pe); 178 | if(pos==n-1) 179 | return deleteLast(pl,pe); 180 | 181 | for(int i=0;inext; 185 | } 186 | q->next=p->next; 187 | *pe=p->info; 188 | free(p); 189 | return 1; 190 | } -------------------------------------------------------------------------------- /Unit1/5_CircularDLL/cdll.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"cdll.h" 3 | 4 | int main() 5 | { 6 | CDLL lobj; 7 | initList(&lobj); 8 | int choice,ele,pos; 9 | 10 | do{ 11 | printf("1.Insert Front 2.Insert Last 3.Insert At Pos 4.Display\n"); 12 | printf("5.Delete Front 6.Delete Last 7.Delete At Pos\n"); 13 | printf("Enter your choice\n"); 14 | scanf("%d",&choice); 15 | 16 | switch(choice) 17 | { 18 | case 1:printf("Enter an integer\n"); 19 | scanf("%d",&ele); 20 | insertFront(&lobj,ele); 21 | break; 22 | case 2:printf("Enter an integer\n"); 23 | scanf("%d",&ele); 24 | insertLast(&lobj,ele); 25 | break; 26 | case 3:printf("Enter an integer\n"); 27 | scanf("%d",&ele); 28 | printf("Enter the pos\n"); 29 | scanf("%d",&pos); 30 | insertAtPos(&lobj,ele,pos); 31 | break; 32 | case 4: display(&lobj); 33 | break; 34 | case 5:if(deleteFront(&lobj,&ele)) 35 | printf("Deleted ele is %d\n",ele); 36 | else 37 | printf("Deletion Failed\n"); 38 | break; 39 | case 6:if(deleteLast(&lobj,&ele)) 40 | printf("Deleted ele is %d\n",ele); 41 | else 42 | printf("Deletion Failed\n"); 43 | break; 44 | case 7: printf("Enter the pos\n"); 45 | scanf("%d",&pos); 46 | if(deleteAtPos(&lobj,&ele,pos)) 47 | printf("Deleted ele is %d\n",ele); 48 | else 49 | printf("Deletion Failed\n"); 50 | break; 51 | } 52 | }while(choice<8); 53 | destroyList(&lobj); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Unit1/5_CircularDLL/cdll.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *prev,*next; 5 | }NODE; 6 | 7 | typedef struct list 8 | { 9 | NODE *head; 10 | }CDLL; 11 | 12 | void initList(CDLL* pl); 13 | void insertFront(CDLL* pl,int ele); 14 | void insertLast(CDLL* pl,int ele); 15 | void insertAtPos(CDLL* pl,int ele,int pos); 16 | void destroyList(CDLL* pl); 17 | void display(CDLL* pl); 18 | int deleteFront(CDLL* pl,int *pe); 19 | int deleteLast(CDLL* pl,int *pe); 20 | int deleteAtPos(CDLL* pl,int *pe,int pos); -------------------------------------------------------------------------------- /Unit1/5_CircularDLL/cdllImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"cdll.h" 4 | 5 | void initList(CDLL* pl) 6 | { 7 | pl->head=NULL; 8 | } 9 | void insertFront(CDLL* pl,int ele) 10 | { 11 | NODE *temp=malloc(sizeof(NODE)); 12 | temp->info=ele; 13 | //List Empty 14 | if(pl->head==NULL) 15 | { 16 | temp->prev=temp; 17 | temp->next=temp; 18 | pl->head=temp; 19 | return; 20 | } 21 | //One or more nodes 22 | NODE *last=pl->head->prev; 23 | 24 | temp->prev=last; 25 | temp->next=pl->head; 26 | last->next=temp; 27 | pl->head->prev=temp; 28 | 29 | pl->head=temp; 30 | } 31 | void insertLast(CDLL* pl,int ele) 32 | { 33 | NODE *temp=malloc(sizeof(NODE)); 34 | temp->info=ele; 35 | //List Empty 36 | if(pl->head==NULL) 37 | { 38 | temp->prev=temp; 39 | temp->next=temp; 40 | pl->head=temp; 41 | return; 42 | } 43 | //One or more nodes 44 | NODE *last=pl->head->prev; 45 | temp->prev=last; 46 | temp->next=pl->head; 47 | 48 | last->next=temp; 49 | pl->head->prev=temp; 50 | } 51 | 52 | int countNodes(CDLL *pl) 53 | { 54 | if(pl->head==NULL) 55 | return 0; 56 | NODE *p=pl->head; 57 | int count=0; 58 | do 59 | { 60 | count++; 61 | p=p->next; 62 | }while(p!=pl->head); 63 | return count; 64 | } 65 | void insertAtPos(CDLL* pl,int ele,int pos) 66 | { 67 | int n=countNodes(pl); 68 | if(pos<0 || pos>n) 69 | return; 70 | NODE *temp=malloc(sizeof(NODE)); 71 | temp->info=ele; 72 | if(pos==0) 73 | { 74 | insertFront(pl,ele); 75 | return; 76 | } 77 | if(pos==n) 78 | { 79 | insertLast(pl,ele); 80 | return; 81 | } 82 | NODE *p=pl->head; 83 | 84 | for(int i=0;inext; 86 | 87 | temp->next=p; 88 | temp->prev=p->prev; 89 | p->prev->next=temp; 90 | p->prev=temp; 91 | } 92 | void destroyList(CDLL* pl) 93 | { 94 | if(pl->head==NULL) 95 | return; 96 | 97 | NODE *last=pl->head->prev; 98 | while(pl->head!=last) 99 | { 100 | pl->head=pl->head->next; 101 | free(pl->head->prev); 102 | } 103 | free(pl->head); 104 | pl->head=NULL; 105 | } 106 | void display(CDLL* pl) 107 | { 108 | if(pl->head==NULL) 109 | { 110 | printf("Empty List\n"); 111 | return; 112 | } 113 | NODE *p=pl->head; 114 | 115 | do 116 | { 117 | printf("%d ",p->info); 118 | p=p->next; 119 | }while(p!=pl->head); 120 | 121 | printf("\n"); 122 | } 123 | int deleteFront(CDLL* pl,int *pe) 124 | { 125 | if(pl->head==NULL) 126 | return 0; 127 | NODE *last=pl->head->prev; 128 | //Single Node 129 | if(pl->head ==last) 130 | { 131 | *pe=pl->head->info; 132 | free(pl->head); 133 | pl->head=NULL; 134 | return 1; 135 | } 136 | //Multiple nodes 137 | pl->head->next->prev=last; 138 | last->next=pl->head->next; 139 | *pe=pl->head->info; 140 | free(pl->head); 141 | pl->head=last->next; 142 | return 1; 143 | } 144 | int deleteLast(CDLL* pl,int *pe) 145 | { 146 | if(pl->head==NULL) 147 | return 0; 148 | NODE *last=pl->head->prev; 149 | //Single Node 150 | if(pl->head ==last) 151 | { 152 | *pe=pl->head->info; 153 | free(pl->head); 154 | pl->head=NULL; 155 | return 1; 156 | } 157 | //Multiple nodes 158 | *pe=last->info; 159 | last->prev->next=pl->head; 160 | pl->head->prev=last->prev; 161 | free(last); 162 | return 1; 163 | } 164 | int deleteAtPos(CDLL* pl,int *pe,int pos) 165 | { 166 | int n=countNodes(pl); 167 | if(pos<0 || pos>n-1) 168 | return 0; 169 | /* if(pos==0) 170 | return deleteFront(pl,pe); 171 | if(pos==n-1) 172 | return deleteLast(pl,pe); 173 | */ 174 | 175 | NODE *p=pl->head; 176 | for(int i=0;inext; 178 | p->prev->next=p->next; 179 | p->next->prev=p->prev; 180 | if(pos==0) 181 | pl->head=p->next; 182 | *pe=p->info; 183 | free(p); 184 | return 1; 185 | } -------------------------------------------------------------------------------- /Unit1/6_MultiList_SparseMatrix/multi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"multi.h" 3 | int main() 4 | { 5 | LIST *pl; 6 | 7 | initList(&pl); 8 | createList(&pl); 9 | display(&pl); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Unit1/6_MultiList_SparseMatrix/multi.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int col; 4 | int val; 5 | struct node *next; 6 | }NODE; 7 | 8 | typedef struct list 9 | { 10 | struct list *down; 11 | NODE *head,*tail; 12 | }LIST; 13 | 14 | void initList(LIST **); 15 | void createList(LIST **); 16 | void display(LIST **); -------------------------------------------------------------------------------- /Unit1/6_MultiList_SparseMatrix/multiImpl.c: -------------------------------------------------------------------------------- 1 | #include"multi.h" 2 | #include 3 | #include 4 | void initList(LIST ** pl) 5 | { 6 | *pl=NULL; 7 | } 8 | 9 | void createList(LIST **pl) 10 | { 11 | int choice,row=0; 12 | printf("Row 0 added\n"); 13 | LIST *p=*pl; 14 | do 15 | { 16 | LIST *temp=malloc(sizeof(LIST)); 17 | temp->down=NULL; 18 | temp->head=NULL; 19 | temp->tail=NULL; 20 | 21 | if(*pl==NULL) 22 | { 23 | *pl=temp; 24 | p=temp; 25 | } 26 | else 27 | { 28 | p->down=temp; 29 | p=p->down; 30 | } 31 | row++; 32 | printf("Do you want to add row %d\n",row); 33 | scanf("%d",&choice); 34 | }while(choice); 35 | 36 | p=*pl; 37 | row=0; 38 | while(p!=NULL) 39 | { 40 | printf("Do you want to add entries to row %d\n",row); 41 | scanf("%d",&choice); 42 | if(choice) 43 | { 44 | do 45 | { 46 | NODE *temp=malloc(sizeof(NODE)); 47 | printf("Enter col no. and val\n"); 48 | scanf("%d%d",&temp->col,&temp->val); 49 | temp->next=NULL; 50 | 51 | if(p->head==NULL) 52 | { 53 | p->head=p->tail=temp; 54 | } 55 | else 56 | { 57 | p->tail->next=temp; 58 | p->tail=temp; 59 | } 60 | printf("Do you want to add any more entries to row %d\n",row); 61 | scanf("%d",&choice); 62 | }while(choice); 63 | } 64 | p=p->down; 65 | row++; 66 | } 67 | } 68 | void display(LIST **pl) 69 | { 70 | LIST *p=*pl; 71 | NODE *q; 72 | int row=0; 73 | while(p!=NULL) 74 | { 75 | q=p->head; 76 | while(q!=NULL) 77 | { 78 | printf("Row %d,Col %d,Val %d\n",row,q->col,q->val); 79 | q=q->next; 80 | } 81 | p=p->down; 82 | row++; 83 | } 84 | } -------------------------------------------------------------------------------- /Unit1/SLL_withReturnValues_ErrorHandling_NotDoneinUrClass/1_SLL_withRetValues/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *next; 5 | }NODE; 6 | 7 | typedef struct llist 8 | { 9 | NODE* head; 10 | }LLIST; 11 | 12 | void initList(LLIST*); 13 | void destroyList(LLIST*); 14 | int insertFront(LLIST*,int); 15 | int insertLast(LLIST*,int); 16 | int insertAtPos(LLIST*,int,int); 17 | void display(LLIST*); 18 | int deleteFront(LLIST*,int*); 19 | int deleteLast(LLIST*,int*); 20 | int deleteAtPos(LLIST*,int*,int); 21 | int searchKey(LLIST*,int); -------------------------------------------------------------------------------- /Unit1/SLL_withReturnValues_ErrorHandling_NotDoneinUrClass/1_SLL_withRetValues/llistImpl.c: -------------------------------------------------------------------------------- 1 | #include"llist.h" 2 | #include 3 | #include 4 | 5 | void initList(LLIST* pl) 6 | { 7 | pl->head=NULL; 8 | } 9 | void destroyList(LLIST* pl) 10 | { 11 | NODE *p=pl->head; 12 | while(pl->head!=NULL) 13 | { 14 | pl->head=p->next; 15 | free(p); 16 | p=pl->head; 17 | } 18 | } 19 | 20 | NODE* getNode(int ele) 21 | { 22 | NODE *temp=malloc(sizeof(NODE)); 23 | if(temp==NULL) 24 | return NULL; //return temp 25 | 26 | temp->info=ele; 27 | temp->next=NULL; 28 | 29 | return temp; 30 | } 31 | 32 | int insertFront(LLIST* pl,int ele) 33 | { 34 | NODE* temp=getNode(ele); 35 | if(temp==NULL) 36 | return 0; 37 | temp->next=pl->head; 38 | pl->head=temp; 39 | return 1; 40 | } 41 | int insertLast(LLIST* pl,int ele) 42 | { 43 | NODE* temp=getNode(ele); 44 | if(temp==NULL) 45 | return 0; 46 | //List Empty 47 | if(pl->head==NULL) 48 | { 49 | pl->head=temp; 50 | return 1; 51 | } 52 | NODE* p=pl->head; 53 | 54 | while(p->next!=NULL) 55 | p=p->next; 56 | p->next=temp; 57 | return 1; 58 | } 59 | int countNodes(LLIST *pl) 60 | { 61 | int count=0; 62 | if(pl->head==NULL) 63 | return count; 64 | NODE *p=pl->head; 65 | while(p!=NULL) 66 | { 67 | count++; 68 | p=p->next; 69 | } 70 | return count; 71 | } 72 | int insertAtPos(LLIST* pl,int ele,int pos) 73 | { 74 | int n=countNodes(pl); 75 | if(pos<0 || pos>n) 76 | return 0; 77 | NODE *temp=getNode(ele); 78 | if(temp==NULL) 79 | return 0; 80 | //pos = 0 81 | if(pos==0) 82 | { 83 | temp->next=pl->head; 84 | pl->head=temp; 85 | return 1; 86 | } 87 | 88 | NODE *p=pl->head,*q=NULL; 89 | 90 | for(int i=0;inext; 94 | } 95 | q->next=temp; 96 | temp->next=p; 97 | return 1; 98 | } 99 | void display(LLIST* pl) 100 | { 101 | if(pl->head==NULL) 102 | { 103 | printf("List is Empty\n"); 104 | return; 105 | } 106 | NODE *p=pl->head; 107 | while(p!=NULL) 108 | { 109 | printf("%d ",p->info); 110 | p=p->next; 111 | } 112 | printf("\n"); 113 | } 114 | int deleteFront(LLIST* pl,int* pe) 115 | { 116 | if(pl->head==NULL) 117 | return 0; 118 | NODE *p=pl->head; 119 | pl->head=p->next; 120 | *pe=p->info; 121 | free(p); 122 | return 1; 123 | } 124 | int deleteLast(LLIST* pl,int* pe) 125 | { 126 | if(pl->head==NULL) 127 | return 0; 128 | NODE *p=pl->head,*q=NULL; 129 | //Single Node Case 130 | if(p->next==NULL) 131 | { 132 | pl->head=p->next; //pl->head=NULL 133 | *pe=p->info; 134 | free(p); 135 | return 1; 136 | } 137 | //More than one node 138 | while(p->next!=NULL) 139 | { 140 | q=p; 141 | p=p->next; 142 | } 143 | *pe=p->info; 144 | free(p); 145 | q->next=NULL; 146 | return 1; 147 | } 148 | int deleteAtPos(LLIST* pl,int* pe,int pos) 149 | { 150 | int n=countNodes(pl); 151 | if(pos<0 || pos>=n) 152 | return 0; 153 | NODE *p=pl->head,*q=NULL; 154 | if(pos==0) 155 | { 156 | pl->head=p->next; 157 | *pe=p->info; 158 | free(p); 159 | return 1; 160 | } 161 | for(int i=0;inext; 165 | } 166 | q->next=p->next; 167 | *pe=p->info; 168 | free(p); 169 | return 1; 170 | } 171 | 172 | int searchKey(LLIST* pl,int key) 173 | { 174 | NODE *p=pl->head; 175 | int pos=0; 176 | if(pl->head==NULL) 177 | return -1; 178 | while(p!=NULL) 179 | { 180 | if(p->info==key) 181 | return pos; 182 | p=p->next; 183 | pos++; 184 | } 185 | return -1; 186 | } -------------------------------------------------------------------------------- /Unit1/SLL_withReturnValues_ErrorHandling_NotDoneinUrClass/1_SLL_withRetValues/llistc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | LLIST lobj; 7 | initList(&lobj); 8 | 9 | int choice,ele,pos; 10 | 11 | do{ 12 | printf("1.Insert Front 2.Insert Last 3.Display\n"); 13 | printf("4.Delete Front 5. DeleteLast 6.Delete at Pos 7.Insert At Pos 8.Search\n"); 14 | scanf("%d",&choice); 15 | switch(choice) 16 | { 17 | case 1:printf("Enter an Integer\n"); 18 | scanf("%d",&ele); 19 | if(insertFront(&lobj,ele)) 20 | printf("Inserted %d successfuuly\n",ele); 21 | else 22 | printf("Insertion failed\n"); 23 | break; 24 | case 2:printf("Enter an Integer\n"); 25 | scanf("%d",&ele); 26 | if(insertLast(&lobj,ele)) 27 | printf("Inserted %d successfuuly\n",ele); 28 | else 29 | printf("Insertion failed\n"); 30 | break; 31 | case 3:display(&lobj); 32 | break; 33 | case 4:if(deleteFront(&lobj,&ele)) 34 | printf("DEleted ele is %d\n",ele); 35 | else 36 | printf("Deletion Failed\n"); 37 | break; 38 | case 5:if(deleteLast(&lobj,&ele)) 39 | printf("DEleted ele is %d\n",ele); 40 | else 41 | printf("Deletion Failed\n"); 42 | break; 43 | case 6:printf("Enter the pos\n"); 44 | scanf("%d",&pos); 45 | if(deleteAtPos(&lobj,&ele,pos)) 46 | printf("DEleted ele is %d\n",ele); 47 | else 48 | printf("Deletion Failed\n"); 49 | break; 50 | case 7:printf("Enter the element\n"); 51 | scanf("%d",&ele); 52 | printf("Enter the pos\n"); 53 | scanf("%d",&pos); 54 | if(insertAtPos(&lobj,ele,pos)) 55 | printf("Inserted %d successfuuly\n",ele); 56 | else 57 | printf("Insertion failed\n"); 58 | break; 59 | case 8:printf("Enter the element to be searched\n"); 60 | scanf("%d",&ele); 61 | pos=searchKey(&lobj,ele); 62 | if(pos==-1) 63 | printf("Element not found\n"); 64 | else 65 | printf("Found at pos %d\n",pos); 66 | break; 67 | } 68 | }while(choice<9); 69 | destroyList(&lobj); 70 | } -------------------------------------------------------------------------------- /Unit1/SLL_withReturnValues_ErrorHandling_NotDoneinUrClass/2_SLL_Variation/DblPtr/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *next; 5 | }NODE; 6 | 7 | void initList(NODE**); 8 | void destroyList(NODE**); 9 | void insertAtPos(NODE**,int ele,int pos,int* status); 10 | void display(NODE*); 11 | void deleteAtPos(NODE**,int *pe,int pos,int* status); 12 | 13 | //10->20->30 -------------------------------------------------------------------------------- /Unit1/SLL_withReturnValues_ErrorHandling_NotDoneinUrClass/2_SLL_Variation/DblPtr/llistImpl.c: -------------------------------------------------------------------------------- 1 | #include"llist.h" 2 | #include 3 | #include 4 | 5 | void initList(NODE** phead) 6 | { 7 | *phead=NULL; 8 | } 9 | 10 | void destroyList(NODE **phead) 11 | { 12 | NODE *p=*phead; 13 | while(p!=NULL) 14 | { 15 | *phead=p->next; 16 | free(p); 17 | p=*phead; 18 | } 19 | } 20 | int countNodes(NODE *phead) 21 | { 22 | int count=0; 23 | if(phead==NULL) 24 | return count; 25 | NODE *p=phead; 26 | while(p!=NULL) 27 | { 28 | count++; 29 | p=p->next; 30 | } 31 | return count; 32 | } 33 | NODE* getNode(int ele) 34 | { 35 | NODE *temp=malloc(sizeof(NODE)); 36 | if(temp==NULL) 37 | return NULL; //return temp 38 | 39 | temp->info=ele; 40 | temp->next=NULL; 41 | 42 | return temp; 43 | } 44 | void insertAtPos(NODE** phead,int ele,int pos,int* status) 45 | { 46 | int n=countNodes(*phead); 47 | if(pos<0 || pos>n) 48 | { 49 | *status=0; 50 | return; 51 | } 52 | NODE *temp=getNode(ele); 53 | if(temp==NULL) 54 | { 55 | *status=0; 56 | return; 57 | } 58 | //pos = 0 59 | if(pos==0) 60 | { 61 | temp->next=*phead; 62 | *phead=temp; 63 | *status=1; 64 | return; 65 | } 66 | 67 | NODE *p=*phead,*q=NULL; 68 | 69 | for(int i=0;inext; 73 | } 74 | q->next=temp; 75 | temp->next=p; 76 | *status=1; 77 | } 78 | 79 | void display(NODE* phead) 80 | { 81 | if(phead==NULL) 82 | { 83 | printf("List is Empty\n"); 84 | return; 85 | } 86 | NODE *p=phead; 87 | while(p!=NULL) 88 | { 89 | printf("%d ",p->info); 90 | p=p->next; 91 | } 92 | printf("\n"); 93 | } 94 | 95 | void deleteAtPos(NODE** phead,int *pe,int pos,int* status) 96 | { 97 | int n=countNodes(*phead); 98 | if(pos<0 || pos>=n) 99 | { 100 | *status=0; 101 | return; 102 | } 103 | NODE *p=*phead,*q=NULL; 104 | if(pos==0) 105 | { 106 | *phead=p->next; 107 | *pe=p->info; 108 | free(p); 109 | *status=1; 110 | return; 111 | } 112 | for(int i=0;inext; 116 | } 117 | q->next=p->next; 118 | *pe=p->info; 119 | free(p); 120 | *status=1; 121 | } -------------------------------------------------------------------------------- /Unit1/SLL_withReturnValues_ErrorHandling_NotDoneinUrClass/2_SLL_Variation/DblPtr/llistcl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | NODE* head; 7 | initList(&head); 8 | 9 | int choice,ele,pos,status; 10 | 11 | do{ 12 | printf("1.Insert At Pos 2.DEleteAtPos 3.Display\n"); 13 | scanf("%d",&choice); 14 | switch(choice) 15 | { 16 | case 3:display(head); 17 | break; 18 | 19 | case 2:printf("Enter the pos\n"); 20 | scanf("%d",&pos); 21 | deleteAtPos(&head,&ele,pos,&status); 22 | if(status) 23 | printf("DEleted ele is %d\n",ele); 24 | else 25 | printf("Deletion Failed\n"); 26 | break; 27 | case 1:printf("Enter the element\n"); 28 | scanf("%d",&ele); 29 | printf("Enter the pos\n"); 30 | scanf("%d",&pos); 31 | insertAtPos(&head,ele,pos,&status); 32 | if(status) 33 | printf("Inserted %d successfully\n",ele); 34 | else 35 | printf("Insertion failed\n"); 36 | break; 37 | } 38 | }while(choice<4); 39 | destroyList(&head); 40 | } -------------------------------------------------------------------------------- /Unit1/SLL_withReturnValues_ErrorHandling_NotDoneinUrClass/2_SLL_Variation/SnglPtr_NodeStruct/llist.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *next; 5 | }NODE; 6 | 7 | NODE* initList(NODE*); 8 | NODE* destroyList(NODE*); 9 | NODE* insertAtPos(NODE*,int ele,int pos,int* status); 10 | void display(NODE*); 11 | NODE* deleteAtPos(NODE*,int *pe,int pos,int* status); 12 | 13 | //10->20->30 -------------------------------------------------------------------------------- /Unit1/SLL_withReturnValues_ErrorHandling_NotDoneinUrClass/2_SLL_Variation/SnglPtr_NodeStruct/llistc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"llist.h" 3 | 4 | int main() 5 | { 6 | NODE* head; 7 | head=initList(head); 8 | 9 | int choice,ele,pos,status; 10 | 11 | do{ 12 | printf("1.Insert At Pos 2.DEleteAtPos 3.Display\n"); 13 | scanf("%d",&choice); 14 | switch(choice) 15 | { 16 | case 3:display(head); 17 | break; 18 | 19 | case 2:printf("Enter the pos\n"); 20 | scanf("%d",&pos); 21 | head=deleteAtPos(head,&ele,pos,&status); 22 | if(status) 23 | printf("DEleted ele is %d\n",ele); 24 | else 25 | printf("Deletion Failed\n"); 26 | break; 27 | case 1:printf("Enter the element\n"); 28 | scanf("%d",&ele); 29 | printf("Enter the pos\n"); 30 | scanf("%d",&pos); 31 | head=insertAtPos(head,ele,pos,&status); 32 | if(status) 33 | printf("Inserted %d successfully\n",ele); 34 | else 35 | printf("Insertion failed\n"); 36 | break; 37 | } 38 | }while(choice<4); 39 | head=destroyList(head); 40 | } -------------------------------------------------------------------------------- /Unit1/SLL_withReturnValues_ErrorHandling_NotDoneinUrClass/2_SLL_Variation/SnglPtr_NodeStruct/llistimpl.c: -------------------------------------------------------------------------------- 1 | #include"llist.h" 2 | #include 3 | #include 4 | 5 | NODE* initList(NODE* phead) 6 | { 7 | phead=NULL; 8 | return phead; 9 | } 10 | 11 | NODE* destroyList(NODE *phead) 12 | { 13 | NODE *p=phead; 14 | while(p!=NULL) 15 | { 16 | phead=p->next; 17 | free(p); 18 | p=phead; 19 | } 20 | return phead; 21 | } 22 | int countNodes(NODE *phead) 23 | { 24 | int count=0; 25 | if(phead==NULL) 26 | return count; 27 | NODE *p=phead; 28 | while(p!=NULL) 29 | { 30 | count++; 31 | p=p->next; 32 | } 33 | return count; 34 | } 35 | NODE* getNode(int ele) 36 | { 37 | NODE *temp=malloc(sizeof(NODE)); 38 | if(temp==NULL) 39 | return NULL; //return temp 40 | 41 | temp->info=ele; 42 | temp->next=NULL; 43 | 44 | return temp; 45 | } 46 | NODE* insertAtPos(NODE* phead,int ele,int pos,int* status) 47 | { 48 | int n=countNodes(phead); 49 | if(pos<0 || pos>n) 50 | { 51 | *status=0; 52 | return phead; 53 | } 54 | NODE *temp=getNode(ele); 55 | if(temp==NULL) 56 | { 57 | *status=0; 58 | return phead; 59 | } 60 | //pos = 0 61 | if(pos==0) 62 | { 63 | temp->next=phead; 64 | phead=temp; 65 | *status=1; 66 | return phead; 67 | } 68 | 69 | NODE *p=phead,*q=NULL; 70 | 71 | for(int i=0;inext; 75 | } 76 | q->next=temp; 77 | temp->next=p; 78 | *status=1; 79 | return phead; 80 | } 81 | 82 | void display(NODE* phead) 83 | { 84 | if(phead==NULL) 85 | { 86 | printf("List is Empty\n"); 87 | return; 88 | } 89 | NODE *p=phead; 90 | while(p!=NULL) 91 | { 92 | printf("%d ",p->info); 93 | p=p->next; 94 | } 95 | printf("\n"); 96 | } 97 | 98 | NODE* deleteAtPos(NODE* phead,int *pe,int pos,int* status) 99 | { 100 | int n=countNodes(phead); 101 | if(pos<0 || pos>=n) 102 | { 103 | *status=0; 104 | return phead; 105 | } 106 | NODE *p=phead,*q=NULL; 107 | if(pos==0) 108 | { 109 | phead=p->next; 110 | *pe=p->info; 111 | free(p); 112 | *status=1; 113 | return phead; 114 | } 115 | for(int i=0;inext; 119 | } 120 | q->next=p->next; 121 | *pe=p->info; 122 | free(p); 123 | *status=1; 124 | return phead; 125 | } -------------------------------------------------------------------------------- /Unit2/1_Stack_ArrayImplmntn_Static/stack.c: -------------------------------------------------------------------------------- 1 | #include"stack.h" 2 | #include 3 | 4 | int main() 5 | { 6 | int choice,ele; 7 | STACK s; 8 | // s.top=-1; 9 | initStack(&s); 10 | printf("1.Push 2.Pop 3.Display 4.IsEmpty 5.IsFull 6.Peek\n"); 11 | scanf("%d",&choice); 12 | 13 | do 14 | { 15 | switch(choice) 16 | { 17 | case 1:printf("Enter an ele\n"); 18 | scanf("%d",&ele); 19 | if(push(&s,ele)) 20 | printf("%d pushed successfully\n"); 21 | else 22 | printf("Stack overflow\n"); 23 | break; 24 | case 2:if(pop(&s,&ele)) 25 | printf("Popped %d\n",ele); 26 | else 27 | printf("Stack underflow\n"); 28 | break; 29 | case 3:display(&s); 30 | break; 31 | case 4:if(isEmpty(&s)) 32 | printf("Stack is Empty\n"); 33 | else 34 | printf("Stack is not empty\n"); 35 | break; 36 | case 5:if(isFull(&s)) 37 | printf("Stack is full\n"); 38 | else 39 | printf("Stack can take elements\n"); 40 | break; 41 | case 6:if(stackTop(&s,&ele)) 42 | printf("Stack top is %d\n",ele); 43 | else 44 | printf("Stack is Empty\n"); 45 | break; 46 | } 47 | printf("1.Push 2.Pop 3.Display 4.IsEmpty 5.IsFull 6.Peek\n"); 48 | scanf("%d",&choice); 49 | }while(choice<7); 50 | } -------------------------------------------------------------------------------- /Unit2/1_Stack_ArrayImplmntn_Static/stack.h: -------------------------------------------------------------------------------- 1 | //Stack: Array Implementation, Statically created array 2 | #define MAX 5 3 | typedef struct stack 4 | { 5 | int s[MAX]; 6 | int top; 7 | }STACK; 8 | 9 | void initStack(STACK *ps); 10 | int isFull(STACK *ps); 11 | int push(STACK *ps,int ele); 12 | int isEmpty(STACK *ps); 13 | int stackTop(STACK *ps,int *pe); 14 | int pop(STACK *ps,int *pe); 15 | void display(STACK *ps); -------------------------------------------------------------------------------- /Unit2/1_Stack_ArrayImplmntn_Static/stackImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"stack.h" 3 | 4 | void initStack(STACK *ps) 5 | { 6 | ps->top=-1; 7 | } 8 | int isFull(STACK *ps) 9 | { 10 | return ps->top==MAX-1; 11 | } 12 | int push(STACK *ps,int ele) 13 | { 14 | if(isFull(ps)) 15 | return 0; 16 | //Next two lines: or ps->s[++ps->top]=ele 17 | ps->top++; 18 | ps->s[ps->top]=ele; 19 | return 1; 20 | } 21 | int isEmpty(STACK *ps) 22 | { 23 | return ps->top==-1; 24 | } 25 | 26 | int stackTop(STACK *ps,int *pe) 27 | { 28 | if(isEmpty(ps)) 29 | return 0; 30 | *pe=ps->s[ps->top]; 31 | return 1; 32 | } 33 | 34 | int pop(STACK *ps,int *pe) 35 | { 36 | if(isEmpty(ps)) 37 | return 0; 38 | *pe=ps->s[ps->top--]; 39 | 40 | /* above line or below 2 lines 41 | *pe=ps->s[ps->top]; 42 | ps->top--; 43 | */ 44 | return 1; 45 | } 46 | void display(STACK *ps) 47 | { 48 | if(isEmpty(ps)) 49 | { 50 | printf("Stack is Empty\n"); 51 | return; 52 | } 53 | for(int i=ps->top;i>=0;i--) 54 | printf("%d ",ps->s[i]); 55 | printf("\n"); 56 | } -------------------------------------------------------------------------------- /Unit2/2_Stack_ArrayImplmntn_Dynamic/stack.c: -------------------------------------------------------------------------------- 1 | #include"stack.h" 2 | #include 3 | 4 | int main() 5 | { 6 | int choice,ele; 7 | STACK s; 8 | // s.top=-1; 9 | initStack(&s,1); 10 | printf("1.Push 2.Pop 3.Display 4.IsEmpty 5.IsFull 6.Peek\n"); 11 | scanf("%d",&choice); 12 | 13 | do 14 | { 15 | switch(choice) 16 | { 17 | case 1:printf("Enter an ele\n"); 18 | scanf("%d",&ele); 19 | if(push(&s,ele)) 20 | printf("%d pushed successfully\n"); 21 | else 22 | printf("Stack overflow\n"); 23 | break; 24 | case 2:if(pop(&s,&ele)) 25 | printf("Popped %d\n",ele); 26 | else 27 | printf("Stack underflow\n"); 28 | break; 29 | case 3:display(&s); 30 | break; 31 | case 4:if(isEmpty(&s)) 32 | printf("Stack is Empty\n"); 33 | else 34 | printf("Stack is not empty\n"); 35 | break; 36 | case 5:if(isFull(&s)) 37 | printf("Stack is full\n"); 38 | else 39 | printf("Stack can take elements\n"); 40 | break; 41 | case 6:if(stackTop(&s,&ele)) 42 | printf("Stack top is %d\n",ele); 43 | else 44 | printf("Stack is Empty\n"); 45 | break; 46 | } 47 | printf("1.Push 2.Pop 3.Display 4.IsEmpty 5.IsFull 6.Peek\n"); 48 | scanf("%d",&choice); 49 | }while(choice<7); 50 | destroyStack(&s); 51 | } -------------------------------------------------------------------------------- /Unit2/2_Stack_ArrayImplmntn_Dynamic/stack.h: -------------------------------------------------------------------------------- 1 | //Stack: Array Implementation, Dynamically created array 2 | 3 | typedef struct stack 4 | { 5 | int *s; 6 | int top; 7 | int maxSize; 8 | }STACK; 9 | 10 | void initStack(STACK *ps,int n); 11 | void stackDouble(STACK *ps); 12 | int isFull(STACK *ps); 13 | int push(STACK *ps,int ele); 14 | int isEmpty(STACK *ps); 15 | int stackTop(STACK *ps,int *pe); 16 | int pop(STACK *ps,int *pe); 17 | void display(STACK *ps); 18 | void destroyStack(STACK *ps); -------------------------------------------------------------------------------- /Unit2/2_Stack_ArrayImplmntn_Dynamic/stackImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"stack.h" 4 | 5 | void initStack(STACK *ps,int n) 6 | { 7 | ps->s=malloc(sizeof(int)*n); 8 | ps->top=-1; 9 | ps->maxSize=n; 10 | } 11 | void destroyStack(STACK *ps) 12 | { 13 | printf("Going to free %d bytes\n",sizeof(int)*ps->maxSize); 14 | free(ps->s); 15 | } 16 | int isFull(STACK *ps) 17 | { 18 | return ps->top==ps->maxSize-1; 19 | } 20 | void stackDouble(STACK *ps) 21 | { 22 | if(ps->maxSize==0) 23 | ps->maxSize=1; 24 | else 25 | ps->maxSize=ps->maxSize*2; 26 | ps->s=realloc(ps->s,sizeof(int)*ps->maxSize); 27 | } 28 | int push(STACK *ps,int ele) 29 | { 30 | if(isFull(ps)) 31 | stackDouble(ps); 32 | //Next two lines: or ps->s[++ps->top]=ele 33 | ps->top++; 34 | ps->s[ps->top]=ele; 35 | return 1; 36 | } 37 | int isEmpty(STACK *ps) 38 | { 39 | return ps->top==-1; 40 | } 41 | 42 | int stackTop(STACK *ps,int *pe) 43 | { 44 | if(isEmpty(ps)) 45 | return 0; 46 | *pe=ps->s[ps->top]; 47 | return 1; 48 | } 49 | 50 | int pop(STACK *ps,int *pe) 51 | { 52 | if(isEmpty(ps)) 53 | return 0; 54 | *pe=ps->s[ps->top--]; 55 | 56 | /* above line or below 2 lines 57 | *pe=ps->s[ps->top]; 58 | ps->top--; 59 | */ 60 | return 1; 61 | } 62 | void display(STACK *ps) 63 | { 64 | if(isEmpty(ps)) 65 | { 66 | printf("Stack is Empty\n"); 67 | return; 68 | } 69 | for(int i=ps->top;i>=0;i--) 70 | printf("%d ",ps->s[i]); 71 | printf("\n"); 72 | } -------------------------------------------------------------------------------- /Unit2/3_ChangedPrototype_StackArrayImplmntn_Static/stack.c: -------------------------------------------------------------------------------- 1 | #include"stack.h" 2 | #include 3 | 4 | int main() 5 | { 6 | int choice,ele; 7 | STACK s; 8 | // s.top=-1; 9 | initStack(&s); 10 | 11 | if(isEmpty(&s)) 12 | printf("Stack is empty\n"); 13 | else 14 | printf("Stack is not empty\n"); 15 | push(&s,10); 16 | if(isFull(&s)) 17 | printf("Stack is full\n"); 18 | else 19 | printf("Stack is not full\n"); 20 | push(&s,20); 21 | if(isFull(&s)) 22 | printf("Stack is full\n"); 23 | else 24 | printf("Stack is not full\n"); 25 | printf("Popped ele is %d\n",pop(&s)); 26 | display(&s); 27 | printf("Popped ele is %d\n",pop(&s)); 28 | if(isEmpty(&s)) 29 | printf("Stack is empty\n"); 30 | else 31 | printf("Stack is not empty\n"); 32 | } -------------------------------------------------------------------------------- /Unit2/3_ChangedPrototype_StackArrayImplmntn_Static/stack.h: -------------------------------------------------------------------------------- 1 | //Stack: Array Implementation, Statically created array 2 | #define MAX 2 3 | typedef struct stack 4 | { 5 | int s[MAX]; 6 | int top; 7 | }STACK; 8 | 9 | void initStack(STACK *ps); 10 | int isFull(STACK *ps); 11 | void push(STACK *ps,int ele); 12 | int isEmpty(STACK *ps); 13 | int stackTop(STACK *ps); 14 | int pop(STACK *ps); 15 | void display(STACK *ps); -------------------------------------------------------------------------------- /Unit2/3_ChangedPrototype_StackArrayImplmntn_Static/stackImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"stack.h" 3 | 4 | void initStack(STACK *ps) 5 | { 6 | ps->top=-1; 7 | } 8 | int isFull(STACK *ps) 9 | { 10 | return ps->top==MAX-1; 11 | } 12 | void push(STACK *ps,int ele) 13 | { 14 | //Next two lines: or ps->s[++ps->top]=ele 15 | ps->top++; 16 | ps->s[ps->top]=ele; 17 | } 18 | int isEmpty(STACK *ps) 19 | { 20 | return ps->top==-1; 21 | } 22 | 23 | int stackTop(STACK *ps) 24 | { 25 | return ps->s[ps->top]; 26 | 27 | } 28 | 29 | int pop(STACK *ps) 30 | { 31 | return ps->s[ps->top--]; 32 | 33 | /* above line or below 2 lines 34 | *pe=ps->s[ps->top]; 35 | ps->top--; 36 | */ 37 | } 38 | 39 | void display(STACK *ps) 40 | { 41 | if(isEmpty(ps)) 42 | { 43 | printf("Stack is Empty\n"); 44 | return; 45 | } 46 | for(int i=ps->top;i>=0;i--) 47 | printf("%d ",ps->s[i]); 48 | printf("\n"); 49 | } -------------------------------------------------------------------------------- /Unit2/4_Parentheses/parentheses.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"parentheses.h" 3 | 4 | int main() 5 | { 6 | char input[MAX]; 7 | printf("Enter a parenthesized expression\n"); 8 | scanf("%s",input); 9 | if(parenthesesMatch(input)) 10 | printf("Valid\n"); 11 | else 12 | printf("Invalid\n"); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Unit2/4_Parentheses/parentheses.h: -------------------------------------------------------------------------------- 1 | //Stack: Array Implementation, Statically created array 2 | #define MAX 20 3 | typedef struct stack 4 | { 5 | char s[MAX]; 6 | int top; 7 | }STACK; 8 | 9 | int parenthesesMatch(char*); 10 | 11 | -------------------------------------------------------------------------------- /Unit2/4_Parentheses/parenthesesImpl.c: -------------------------------------------------------------------------------- 1 | #include"parentheses.h" 2 | void initStack(STACK *ps) 3 | { 4 | ps->top=-1; 5 | } 6 | 7 | int isFull(STACK *ps) 8 | { 9 | return ps->top==MAX-1; 10 | } 11 | void push(STACK *ps,char ele) 12 | { 13 | ps->top++; 14 | ps->s[ps->top]=ele; 15 | //instead of 2 lines above : ps->s[++ps->top]=ele; 16 | } 17 | int isEmpty(STACK *ps) 18 | { 19 | return ps->top==-1; 20 | } 21 | char pop(STACK *ps) 22 | { 23 | char res=ps->s[ps->top]; 24 | ps->top--; 25 | return res; 26 | //instead of 2 lines above : return ps->s[ps->top--]; 27 | } 28 | int parenthesesMatch(char *str) 29 | { 30 | STACK s; 31 | initStack(&s); 32 | 33 | for(int i=0;str[i]!='\0';i++) 34 | { 35 | if(str[i]=='(' || str[i]=='[' || str[i]=='{') 36 | push(&s,str[i]); 37 | else if(str[i]==')' || str[i]==']' || str[i]=='}') 38 | { 39 | if(isEmpty(&s)) 40 | return 0; 41 | switch(str[i]) 42 | { 43 | case ')':if(pop(&s)!='(') 44 | return 0; 45 | break; 46 | case ']':if(pop(&s)!='[') 47 | return 0; 48 | break; 49 | case '}':if(pop(&s)!='{') 50 | return 0; 51 | break; 52 | } 53 | } 54 | } 55 | return isEmpty(&s); 56 | } -------------------------------------------------------------------------------- /Unit2/5_PostfixEval/postfix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"postfix.h" 3 | 4 | int main() 5 | { 6 | char postfix[MAX]; 7 | 8 | printf("Enter a valid postfix expression\n"); 9 | scanf("%s",postfix); 10 | 11 | printf("Result=%f\n",postfixEval(postfix)); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Unit2/5_PostfixEval/postfix.h: -------------------------------------------------------------------------------- 1 | #define MAX 20 2 | typedef struct stack 3 | { 4 | float s[MAX]; 5 | int top; 6 | }STACK; 7 | 8 | float postfixEval(char*); -------------------------------------------------------------------------------- /Unit2/5_PostfixEval/postfixImpl.c: -------------------------------------------------------------------------------- 1 | #include"postfix.h" 2 | #include 3 | 4 | void initStack(STACK *ps) 5 | { 6 | ps->top=-1; 7 | } 8 | 9 | void push(STACK *ps,float ele) 10 | { 11 | ps->top++; 12 | ps->s[ps->top]=ele; 13 | //instead of 2 lines above : ps->s[++ps->top]=ele; 14 | } 15 | 16 | float pop(STACK *ps) 17 | { 18 | float res=ps->s[ps->top]; 19 | ps->top--; 20 | return res; 21 | //instead of 2 lines above : return ps->s[ps->top--]; 22 | } 23 | float postfixEval(char* pfix) 24 | { 25 | STACK s; 26 | initStack(&s); 27 | 28 | float op1,op2,res; 29 | for(int i=0;pfix[i]!='\0';i++) 30 | { 31 | if(isdigit(pfix[i])) 32 | push(&s,pfix[i]-'0'); 33 | else 34 | { 35 | op2=pop(&s); 36 | op1=pop(&s); 37 | 38 | switch(pfix[i]) 39 | { 40 | case '+':res=op1+op2; 41 | break; 42 | case '-':res=op1-op2; 43 | break; 44 | case '*':res=op1*op2; 45 | break; 46 | case '/':res=op1/op2; 47 | break; 48 | } 49 | push(&s,res); 50 | } 51 | } 52 | return pop(&s); 53 | } -------------------------------------------------------------------------------- /Unit2/6_InfixToPostfix/inToPost.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"inToPost.h" 3 | 4 | int main() 5 | { 6 | char infix[MAX],postfix[MAX]; 7 | printf("Enter a valid infix expression\n"); 8 | scanf("%s",infix); 9 | 10 | infixToPostfix(infix,postfix); 11 | printf("%s\n",postfix); 12 | } -------------------------------------------------------------------------------- /Unit2/6_InfixToPostfix/inToPost.h: -------------------------------------------------------------------------------- 1 | #define MAX 30 2 | typedef struct stack 3 | { 4 | char s[MAX]; 5 | int top; 6 | }STACK; 7 | 8 | void infixToPostfix(char*,char*); -------------------------------------------------------------------------------- /Unit2/6_InfixToPostfix/inToPostImpl.c: -------------------------------------------------------------------------------- 1 | #include"inToPost.h" 2 | #include 3 | void initStack(STACK *ps) 4 | { 5 | ps->top=-1; 6 | } 7 | void push(STACK *ps,int ele) 8 | { 9 | ps->top++; 10 | ps->s[ps->top]=ele; 11 | //instead of 2 lines above : ps->s[++ps->top]=ele; 12 | } 13 | int isEmpty(STACK *ps) 14 | { 15 | return ps->top==-1; 16 | } 17 | int pop(STACK *ps) 18 | { 19 | int res=ps->s[ps->top]; 20 | ps->top--; 21 | return res; 22 | //instead of 2 lines above : return ps->s[ps->top--]; 23 | } 24 | int stackTop(STACK *ps) 25 | { 26 | return ps->s[ps->top]; 27 | } 28 | int prcd(char st,char i) 29 | { 30 | switch(i) 31 | { 32 | case '+': 33 | case '-':if(st=='(') 34 | return 0; 35 | return 1; 36 | case '*': 37 | case '/':if(st=='(' || st=='+' || st=='-') 38 | return 0; 39 | return 1; 40 | case '(':return 0; 41 | case ')':if(st=='(') 42 | return 0; 43 | return 1; 44 | } 45 | } 46 | void infixToPostfix(char* infix,char *postfix) 47 | { 48 | STACK s; 49 | initStack(&s); 50 | char dummy; 51 | int k=0; 52 | for(int i=0;infix[i]!='\0';i++) 53 | { 54 | if(isdigit(infix[i])) 55 | postfix[k++]=infix[i]; 56 | else 57 | { 58 | while(!isEmpty(&s) && prcd(stackTop(&s),infix[i])) 59 | postfix[k++]=pop(&s); 60 | if(infix[i]==')') 61 | dummy=pop(&s); 62 | else 63 | push(&s,infix[i]); 64 | 65 | } 66 | } 67 | while(!isEmpty(&s)) 68 | postfix[k++]=pop(&s); 69 | postfix[k]='\0'; 70 | } -------------------------------------------------------------------------------- /Unit2/Queue/1_SimpleQ/ArrayImplementation/queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"queue_Array.h" 3 | 4 | int main() 5 | { 6 | QUEUE q; 7 | 8 | initQueue(&q); 9 | 10 | int choice,ele; 11 | 12 | printf("1.Enq 2.Deq 3.IsFull 4.IsEmpty 5.Display\n"); 13 | scanf("%d",&choice); 14 | 15 | do 16 | { 17 | switch(choice) 18 | { 19 | case 1:printf("Enter an ele\n"); 20 | scanf("%d",&ele); 21 | if(isFull(&q)) 22 | printf("Queue is already full\n"); 23 | else 24 | enqueue(&q,ele); 25 | break; 26 | case 2:if(isEmpty(&q)) 27 | printf("Queue is already empty\n"); 28 | else 29 | printf("deqd %d\n",dequeue(&q)); 30 | break; 31 | case 3:if(isFull(&q)) 32 | printf("Queue Full\n"); 33 | else 34 | printf("Queue can take ele's\n"); 35 | break; 36 | case 4:if(isEmpty(&q)) 37 | printf("Queue Empty\n"); 38 | else 39 | printf("Queue is not empty\n"); 40 | break; 41 | case 5:display(&q); 42 | break; 43 | } 44 | printf("1.Enq 2.Deq 3.IsFull 4.IsEmpty 5.Display\n"); 45 | scanf("%d",&choice); 46 | }while(choice<6); 47 | } -------------------------------------------------------------------------------- /Unit2/Queue/1_SimpleQ/ArrayImplementation/queueImpl.c: -------------------------------------------------------------------------------- 1 | #include"queue_Array.h" 2 | #include 3 | 4 | void initQueue(QUEUE *pq) 5 | { 6 | pq->front=0; 7 | pq->rear=-1; 8 | } 9 | 10 | void enqueue(QUEUE *pq,int ele) 11 | { 12 | pq->rear++; 13 | pq->q[pq->rear]=ele; 14 | } 15 | 16 | int dequeue(QUEUE *pq) 17 | { 18 | int ele=pq->q[pq->front]; 19 | pq->front++; 20 | 21 | if(pq->front>pq->rear) 22 | { 23 | pq->front=0; 24 | pq->rear=-1; 25 | } 26 | return ele; 27 | } 28 | 29 | int isFull(QUEUE *pq) 30 | { 31 | return pq->rear==MAX-1; 32 | } 33 | int isEmpty(QUEUE *pq) 34 | { 35 | return pq->front>pq->rear; 36 | } 37 | void display(QUEUE *pq) 38 | { 39 | if(isEmpty(pq)) 40 | { 41 | printf("Empty Queue\n"); 42 | return; 43 | } 44 | 45 | for(int i=pq->front;i<=pq->rear;i++) 46 | printf("%d ",pq->q[i]); 47 | printf("\n"); 48 | } -------------------------------------------------------------------------------- /Unit2/Queue/1_SimpleQ/ArrayImplementation/queue_Array.h: -------------------------------------------------------------------------------- 1 | #define MAX 5 2 | typedef struct queue 3 | { 4 | int q[MAX]; 5 | int front,rear; 6 | }QUEUE; 7 | 8 | void initQueue(QUEUE *pq); 9 | //Precondition: Queue is created and initialized 10 | //Precondn: Queue is not full 11 | //Postcondition: ele is successfully enqd 12 | void enqueue(QUEUE *pq,int ele); 13 | 14 | //Precondition: Queue is non empty 15 | //Postcondition:deqd ele is successfully returned back 16 | int dequeue(QUEUE *pq); 17 | 18 | int isFull(QUEUE *pq); 19 | int isEmpty(QUEUE *pq); 20 | void display(QUEUE *pq); -------------------------------------------------------------------------------- /Unit2/Queue/1_SimpleQ/LinkedListImplementation/qListImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }NODE; 8 | 9 | typedef struct queue 10 | { 11 | struct node *front,*rear; 12 | }QUEUE; 13 | 14 | void initQueue(QUEUE *pq); 15 | void enqueue(QUEUE *pq,int ele); 16 | int dequeue(QUEUE *pq); 17 | int isEmpty(QUEUE *pq); 18 | void display(QUEUE *pq); 19 | void destroyQueue(QUEUE *pq); 20 | 21 | int main() 22 | { 23 | int choice,ele; 24 | 25 | QUEUE qobj; 26 | initQueue(&qobj); 27 | 28 | printf("1.Enqueue 2.Dequeue 3.Display\n"); 29 | scanf("%d",&choice); 30 | do{ 31 | switch(choice) 32 | { 33 | case 1:printf("Enter an element\n"); 34 | scanf("%d",&ele); 35 | enqueue(&qobj,ele); 36 | break; 37 | case 2: if(!isEmpty(&qobj)) 38 | printf("Deleted element is %d\n",dequeue(&qobj)); 39 | else 40 | printf("Q already empty\n"); 41 | break; 42 | case 3:display(&qobj); 43 | break; 44 | } 45 | printf("1.Enqueue 2.Dequeue 3.Display\n"); 46 | scanf("%d",&choice); 47 | }while(choice<4); 48 | destroyQueue(&qobj); 49 | } 50 | 51 | void initQueue(QUEUE *pq) 52 | { 53 | pq->front=pq->rear=NULL; 54 | } 55 | void enqueue(QUEUE *pq,int ele) 56 | { 57 | NODE *temp=malloc(sizeof(NODE)); 58 | temp->data=ele; 59 | temp->next=NULL; 60 | 61 | if(pq->rear==NULL) 62 | { 63 | pq->rear=temp; 64 | pq->front=temp; 65 | return; 66 | } 67 | pq->rear->next=temp; 68 | pq->rear=temp; 69 | } 70 | 71 | int dequeue(QUEUE *pq) 72 | { 73 | int ele; 74 | if(pq->front==pq->rear) 75 | { 76 | ele=pq->front->data; 77 | free(pq->front); 78 | pq->front=pq->rear=NULL; 79 | return ele; 80 | } 81 | NODE *p=pq->front; 82 | ele=p->data; 83 | pq->front=p->next; 84 | free(p); 85 | return ele; 86 | } 87 | int isEmpty(QUEUE *pq) 88 | { 89 | return pq->front==NULL; 90 | } 91 | void display(QUEUE *pq) 92 | { 93 | NODE *p=pq->front; 94 | if(p==NULL) 95 | { 96 | printf("Empty Queue\n"); 97 | return; 98 | } 99 | 100 | while(p!=NULL) 101 | { 102 | printf("%d ",p->data); 103 | p=p->next; 104 | } 105 | printf("\n"); 106 | } 107 | void destroyQueue(QUEUE *pq) 108 | { 109 | NODE *p=pq->front; 110 | if(p==NULL) 111 | return; 112 | while(p!=NULL) 113 | { 114 | pq->front=p->next;printf("%d ",p->data); 115 | free(p); 116 | p=pq->front; 117 | } 118 | pq->rear=pq->front=NULL; 119 | } -------------------------------------------------------------------------------- /Unit2/Queue/2_CircularQ/ArrayImplementation/cirq.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"cirq.h" 3 | 4 | int main() 5 | { 6 | QUEUE q; 7 | 8 | initQueue(&q); 9 | 10 | int choice,ele; 11 | 12 | printf("1.Enq 2.Deq 3.IsFull 4.IsEmpty 5.Display\n"); 13 | scanf("%d",&choice); 14 | 15 | do 16 | { 17 | switch(choice) 18 | { 19 | case 1:printf("Enter an ele\n"); 20 | scanf("%d",&ele); 21 | if(isFull(&q)) 22 | printf("Queue is already full\n"); 23 | else 24 | enqueue(&q,ele); 25 | break; 26 | case 2:if(isEmpty(&q)) 27 | printf("Queue is already empty\n"); 28 | else 29 | printf("deqd %d\n",dequeue(&q)); 30 | break; 31 | case 3:if(isFull(&q)) 32 | printf("Queue Full\n"); 33 | else 34 | printf("Queue can take ele's\n"); 35 | break; 36 | case 4:if(isEmpty(&q)) 37 | printf("Queue Empty\n"); 38 | else 39 | printf("Queue is not empty\n"); 40 | break; 41 | case 5:display(&q); 42 | break; 43 | } 44 | printf("1.Enq 2.Deq 3.IsFull 4.IsEmpty 5.Display\n"); 45 | scanf("%d",&choice); 46 | }while(choice<6); 47 | } -------------------------------------------------------------------------------- /Unit2/Queue/2_CircularQ/ArrayImplementation/cirq.h: -------------------------------------------------------------------------------- 1 | #define MAX 5 2 | typedef struct queue 3 | { 4 | int q[MAX]; 5 | int front,rear; 6 | }QUEUE; 7 | 8 | void initQueue(QUEUE *pq); 9 | //Precondition: Queue is created and initialized 10 | //Precondn: Queue is not full 11 | //Postcondition: ele is successfully enqd 12 | void enqueue(QUEUE *pq,int ele); 13 | 14 | //Precondition: Queue is non empty 15 | //Postcondition:deqd ele is successfully returned back 16 | int dequeue(QUEUE *pq); 17 | 18 | int isFull(QUEUE *pq); 19 | int isEmpty(QUEUE *pq); 20 | void display(QUEUE *pq); -------------------------------------------------------------------------------- /Unit2/Queue/2_CircularQ/ArrayImplementation/cirqImpl.c: -------------------------------------------------------------------------------- 1 | #include"cirq.h" 2 | #include 3 | 4 | void initQueue(QUEUE *pq) 5 | { 6 | pq->front=pq->rear=MAX-1; 7 | } 8 | void enqueue(QUEUE *pq,int ele) 9 | { 10 | pq->rear=(pq->rear+1)%MAX; 11 | pq->q[pq->rear]=ele; 12 | } 13 | int dequeue(QUEUE *pq) 14 | { 15 | pq->front=(pq->front+1)%MAX; 16 | return pq->q[pq->front]; 17 | } 18 | 19 | int isFull(QUEUE *pq) 20 | { 21 | return (pq->rear+1)%MAX == pq->front; 22 | } 23 | int isEmpty(QUEUE *pq) 24 | { 25 | return pq->rear==pq->front; 26 | } 27 | void display(QUEUE *pq) 28 | { 29 | if(isEmpty(pq)) 30 | { 31 | printf("Queue is empty\n"); 32 | return; 33 | } 34 | 35 | int i=(pq->front+1)%MAX; 36 | while(i!=pq->rear) 37 | { 38 | printf("%d ",pq->q[i]); 39 | i=(i+1)%MAX; 40 | } 41 | printf("%d\n",pq->q[i]); 42 | } -------------------------------------------------------------------------------- /Unit2/Queue/2_CircularQ/LinkedListImplementation/cqListImpl.c: -------------------------------------------------------------------------------- 1 | /*Circular Queue: Linked list implementation*/ 2 | #include 3 | #include 4 | typedef struct node 5 | { 6 | int data; 7 | struct node *next; 8 | }NODE; 9 | 10 | typedef struct queue 11 | { 12 | struct node *front,*rear; 13 | }QUEUE; 14 | 15 | void initQueue(QUEUE *pq); 16 | void enqueue(QUEUE *pq,int ele); 17 | int dequeue(QUEUE *pq); 18 | int isEmpty(QUEUE *pq); 19 | void display(QUEUE *pq); 20 | void destroyQueue(QUEUE *pq); 21 | 22 | int main() 23 | { 24 | int choice,ele; 25 | 26 | QUEUE qobj; 27 | initQueue(&qobj); 28 | 29 | printf("1.Enqueue 2.Dequeue 3.Display\n"); 30 | scanf("%d",&choice); 31 | do{ 32 | switch(choice) 33 | { 34 | case 1:printf("Enter an element\n"); 35 | scanf("%d",&ele); 36 | enqueue(&qobj,ele); 37 | break; 38 | case 2: if(!isEmpty(&qobj)) 39 | printf("Deleted element is %d\n",dequeue(&qobj)); 40 | else 41 | printf("Q already empty\n"); 42 | break; 43 | case 3:display(&qobj); 44 | break; 45 | } 46 | printf("1.Enqueue 2.Dequeue 3.Display\n"); 47 | scanf("%d",&choice); 48 | }while(choice<4); 49 | destroyQueue(&qobj); 50 | } 51 | 52 | void initQueue(QUEUE *pq) 53 | { 54 | pq->front=pq->rear=NULL; 55 | } 56 | void enqueue(QUEUE *pq,int ele) 57 | { 58 | NODE *temp=malloc(sizeof(NODE)); 59 | temp->data=ele; 60 | temp->next=NULL; 61 | 62 | if(pq->rear==NULL) 63 | { 64 | pq->rear=temp; 65 | pq->front=temp; 66 | temp->next=temp; 67 | return; 68 | } 69 | temp->next=pq->rear->next; 70 | pq->rear->next=temp; 71 | pq->rear=temp; 72 | } 73 | 74 | int dequeue(QUEUE *pq) 75 | { 76 | int ele; 77 | 78 | if(pq->front==pq->rear) 79 | { 80 | ele=pq->front->data; 81 | free(pq->front); 82 | pq->front=pq->rear=NULL; 83 | return ele; 84 | } 85 | ele=pq->front->data; 86 | pq->rear->next=pq->front->next; 87 | free(pq->front); 88 | pq->front=pq->rear->next; 89 | return ele; 90 | } 91 | int isEmpty(QUEUE *pq) 92 | { 93 | return pq->front==NULL; 94 | } 95 | void display(QUEUE *pq) 96 | { 97 | NODE *p=pq->front; 98 | if(p==NULL) 99 | { 100 | printf("Empty Queue\n"); 101 | return; 102 | } 103 | 104 | while(p!=pq->rear) 105 | { 106 | printf("%d ",p->data); 107 | p=p->next; 108 | } 109 | printf("%d\n",p->data); 110 | } 111 | void destroyQueue(QUEUE *pq) 112 | { 113 | if(pq->front==NULL) 114 | return; 115 | while(pq->front!=pq->rear) 116 | { 117 | // printf("%d ",pq->front->data); 118 | pq->rear->next=pq->front->next; 119 | free(pq->front); 120 | pq->front=pq->rear->next; 121 | } 122 | // printf("%d ",pq->front->data); 123 | free(pq->front); 124 | pq->rear=pq->front=NULL; 125 | } -------------------------------------------------------------------------------- /Unit2/Queue/3_PriorityQ/ArrayImplementation_DescendingPriorityQueue/priQArray.c: -------------------------------------------------------------------------------- 1 | #include"priQArray.h" 2 | #include 3 | 4 | int main() 5 | { 6 | PRIQ pq[MAX]; 7 | 8 | int choice,ele,pri,count=0; 9 | PRIQ temp; 10 | printf("1.Enq 2.MaxDelete 3.Display\n"); 11 | scanf("%d",&choice); 12 | do{ 13 | 14 | switch(choice) 15 | { 16 | case 1:if(count!=MAX) 17 | { 18 | printf("Enter ele & pri\n"); 19 | scanf("%d%d",&ele,&pri); 20 | insert(pq,ele,pri,&count); 21 | } 22 | else 23 | printf("Queue is already full\n"); 24 | break; 25 | case 2:if(count) 26 | { 27 | temp=maxDelete(pq,&count); 28 | printf("Data:%d Pri:%d",temp.info,temp.pri); 29 | } 30 | else 31 | printf("Queue is already empty\n"); 32 | break; 33 | case 3:display(pq,&count); 34 | break; 35 | } 36 | printf("\n1.Enq 2.MaxDelete 3.Display\n"); 37 | scanf("%d",&choice); 38 | }while(choice<4); 39 | } 40 | -------------------------------------------------------------------------------- /Unit2/Queue/3_PriorityQ/ArrayImplementation_DescendingPriorityQueue/priQArray.h: -------------------------------------------------------------------------------- 1 | //Descending priority Queue 2 | #define MAX 5 3 | typedef struct priQ 4 | { 5 | int info; 6 | int pri; 7 | }PRIQ; 8 | 9 | void insert(PRIQ *pq,int ele,int pty,int *count); 10 | PRIQ maxDelete(PRIQ *pq,int *count); 11 | void display(PRIQ *pq,int *count); 12 | 13 | -------------------------------------------------------------------------------- /Unit2/Queue/3_PriorityQ/ArrayImplementation_DescendingPriorityQueue/priQArrayImpl.c: -------------------------------------------------------------------------------- 1 | #include"priQArray.h" 2 | #include 3 | 4 | //Ordered insertion 5 | void insert(PRIQ *pq,int ele,int pty,int *count) 6 | { 7 | int i=*count-1; 8 | 9 | PRIQ key; 10 | key.info=ele; 11 | key.pri=pty; 12 | 13 | while(i>=0 && key.pri>pq[i].pri) 14 | { 15 | pq[i+1]=pq[i]; 16 | i--; 17 | } 18 | pq[i+1]=key; 19 | (*count)++; 20 | } 21 | PRIQ maxDelete(PRIQ *pq,int *count) 22 | { 23 | PRIQ key=pq[0]; 24 | 25 | for(int i=1;i<*count;i++) 26 | pq[i-1]=pq[i]; 27 | (*count)--; 28 | 29 | return key; 30 | } 31 | void display(PRIQ *pq,int *count) 32 | { 33 | if(*count==0) 34 | { 35 | printf("Queue is empty\n"); 36 | return; 37 | } 38 | for(int i=0;i<*count;i++) 39 | printf("Data:%d Pri:%d|",pq[i].info,pq[i].pri); 40 | } -------------------------------------------------------------------------------- /Unit2/Queue/3_PriorityQ/LinkedListImplementation_AscendingPriorityQueue/priQll.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | int pri; 5 | struct node *next; 6 | }NODE; 7 | 8 | typedef struct priqueue 9 | { 10 | NODE *head; 11 | }PRIQ; 12 | 13 | void initQueue(PRIQ*); 14 | void pqInsert(PRIQ*,int ele,int pri); 15 | int isEmpty(PRIQ*); 16 | int minDelete(PRIQ*); 17 | void display(PRIQ*); 18 | void destroyQueue(PRIQ*); 19 | -------------------------------------------------------------------------------- /Unit2/Queue/3_PriorityQ/LinkedListImplementation_AscendingPriorityQueue/priQllImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"priQLL.h" 3 | #include 4 | 5 | void initQueue(PRIQ* pq) 6 | { 7 | pq->head=NULL; 8 | } 9 | void pqInsert(PRIQ* pq,int ele,int pri) 10 | { 11 | NODE *temp=malloc(sizeof(NODE)); 12 | temp->info=ele; 13 | temp->pri=pri; 14 | temp->next=NULL; 15 | 16 | NODE *p=pq->head,*q=NULL; 17 | 18 | while(p!=NULL && temp->pri>p->pri) 19 | { 20 | q=p; 21 | p=p->next; 22 | } 23 | if(q==NULL) //or if(p==pq->head) 24 | { 25 | temp->next=pq->head; 26 | pq->head=temp; 27 | return; 28 | } 29 | temp->next=p; 30 | q->next=temp; 31 | } 32 | 33 | int isEmpty(PRIQ* pq) 34 | { 35 | return pq->head==NULL; 36 | } 37 | int minDelete(PRIQ* pq) 38 | { 39 | NODE *p=pq->head; 40 | int ele=pq->head->info; 41 | 42 | pq->head=pq->head->next; 43 | free(p); 44 | return ele; 45 | } 46 | void display(PRIQ* pq) 47 | { 48 | NODE *p=pq->head; 49 | if(p==NULL) 50 | { 51 | printf("Queue is Empty\n"); 52 | return; 53 | } 54 | while(p!=NULL) 55 | { 56 | printf("%d %d |",p->info,p->pri); 57 | p=p->next; 58 | } 59 | printf("\n"); 60 | } 61 | 62 | void destroyQueue(PRIQ* pq) 63 | { 64 | NODE *p=pq->head; 65 | 66 | while(p!=NULL) 67 | { 68 | pq->head=pq->head->next; 69 | free(p); 70 | p=pq->head; 71 | } 72 | } -------------------------------------------------------------------------------- /Unit2/Queue/3_PriorityQ/LinkedListImplementation_AscendingPriorityQueue/priqll.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"priQLL.h" 3 | 4 | int main() 5 | { 6 | PRIQ q; 7 | 8 | initQueue(&q); 9 | int choice,ele,pri; 10 | 11 | printf("1.Insert 2.minDelete 3.Display\n"); 12 | scanf("%d",&choice); 13 | 14 | do 15 | { 16 | switch(choice) 17 | { 18 | case 1:printf("Enter the ele and pri\n"); 19 | scanf("%d%d",&ele,&pri); 20 | pqInsert(&q,ele,pri); 21 | break; 22 | case 2: if(!isEmpty(&q)) 23 | printf("Deqd %d\n",minDelete(&q)); 24 | else 25 | printf("Q is already empty\n"); 26 | break; 27 | 28 | case 3:display(&q); 29 | break; 30 | } 31 | printf("1.Insert 2.minDelete 3.Display\n"); 32 | scanf("%d",&choice); 33 | }while(choice<4); 34 | destroyQueue(&q); 35 | } -------------------------------------------------------------------------------- /Unit2/Queue/4_CircularQ_DEQUE_Array/1_cirQ.c: -------------------------------------------------------------------------------- 1 | //Circular Queue: Array Implmntn, front and rear set to -1 2 | #include 3 | #define MAX 5 4 | typedef struct queue 5 | { 6 | int q[MAX]; 7 | int front,rear; 8 | }QUEUE; 9 | 10 | void initQueue(QUEUE *pq) 11 | { 12 | pq->front=pq->rear=-1; 13 | } 14 | 15 | int isFull(QUEUE *pq) 16 | { 17 | return (pq->rear+1)%MAX == pq->front; 18 | } 19 | 20 | int isEmpty(QUEUE *pq) 21 | { 22 | return pq->rear==-1; //pq->front=-1 23 | } 24 | 25 | void enqueue(QUEUE *pq,int ele) 26 | { 27 | if(isEmpty(pq)) 28 | { 29 | pq->front=0; 30 | pq->rear=0; 31 | pq->q[pq->rear]=ele; 32 | return; 33 | } 34 | pq->rear=(pq->rear+1)%MAX; 35 | pq->q[pq->rear]=ele; 36 | } 37 | int dequeue(QUEUE *pq) 38 | { 39 | int ele=pq->q[pq->front]; 40 | if(pq->front==pq->rear) 41 | initQueue(pq); 42 | else 43 | pq->front=(pq->front+1)%MAX; 44 | return ele; 45 | } 46 | 47 | void display(QUEUE *pq) 48 | { 49 | if(isEmpty(pq)) 50 | { 51 | printf("Queue is empty\n"); 52 | return; 53 | } 54 | 55 | int i=(pq->front)%MAX; 56 | while(i!=pq->rear) 57 | { 58 | printf("%d ",pq->q[i]); 59 | i=(i+1)%MAX; 60 | } 61 | printf("%d\n",pq->q[i]); 62 | } 63 | 64 | int main() 65 | { 66 | QUEUE q; 67 | 68 | initQueue(&q); 69 | 70 | int choice,ele; 71 | 72 | printf("1.Enq 2.Deq 3.IsFull 4.IsEmpty 5.Display\n"); 73 | scanf("%d",&choice); 74 | 75 | do 76 | { 77 | switch(choice) 78 | { 79 | case 1:printf("Enter an ele\n"); 80 | scanf("%d",&ele); 81 | if(isFull(&q)) 82 | printf("Queue is already full\n"); 83 | else 84 | enqueue(&q,ele); 85 | break; 86 | case 2:if(isEmpty(&q)) 87 | printf("Queue is already empty\n"); 88 | else 89 | printf("deqd %d\n",dequeue(&q)); 90 | break; 91 | case 3:if(isFull(&q)) 92 | printf("Queue Full\n"); 93 | else 94 | printf("Queue can take ele's\n"); 95 | break; 96 | case 4:if(isEmpty(&q)) 97 | printf("Queue Empty\n"); 98 | else 99 | printf("Queue is not empty\n"); 100 | break; 101 | case 5:display(&q); 102 | break; 103 | } 104 | printf("1.Enq 2.Deq 3.IsFull 4.IsEmpty 5.Display\n"); 105 | scanf("%d",&choice); 106 | }while(choice<6); 107 | } -------------------------------------------------------------------------------- /Unit2/Queue/4_CircularQ_DEQUE_Array/2_DEQUE_CirArray_Mod.c: -------------------------------------------------------------------------------- 1 | //DEQUE using circular array 2 | #include 3 | #define MAX 5 4 | 5 | typedef struct DEQUE 6 | { 7 | int q[MAX]; 8 | int front,rear; 9 | }DEQUE; 10 | 11 | void initDeque(DEQUE* pq) 12 | { 13 | pq->front = -1; 14 | pq->rear = -1; 15 | } 16 | 17 | int isFull(DEQUE *pq) 18 | { 19 | return (pq->rear+1)%MAX==pq->front; 20 | } 21 | 22 | int isEmpty(DEQUE *pq) 23 | { 24 | return (pq->front == -1); // or return pq->rear==-1; 25 | } 26 | 27 | void insertRear(DEQUE *pq,int ele) 28 | { 29 | if (isEmpty(pq)) 30 | { 31 | pq->front = 0; 32 | pq->rear = 0; 33 | pq->q[pq->rear]=ele; 34 | return; 35 | } 36 | pq->rear = (pq->rear+1)%MAX; 37 | pq->q[pq->rear] = ele ; 38 | } 39 | 40 | void insertFront(DEQUE *pq,int ele) 41 | { 42 | if (isEmpty(pq)) 43 | { 44 | pq->front = 0; 45 | pq->rear = 0; 46 | pq->q[pq->front] = ele; 47 | return; 48 | } 49 | pq->front = (pq->front-1+MAX)%MAX; 50 | pq->q[pq->front] = ele; 51 | } 52 | 53 | int deleteFront(DEQUE *pq) 54 | { 55 | int ele=pq->q[pq->front]; 56 | if (pq->front==pq->rear) 57 | { 58 | pq->front = -1; 59 | pq->rear = -1; 60 | return ele; 61 | } 62 | pq->front = (pq->front+1)%MAX; 63 | return ele; 64 | } 65 | 66 | int deleteRear(DEQUE *pq) 67 | { 68 | int ele=pq->q[pq->rear]; 69 | if (pq->front==pq->rear) // DEQUEUE has only one element 70 | { 71 | pq->front = -1; 72 | pq->rear = -1; 73 | return ele; 74 | } 75 | pq->rear = (pq->rear-1+MAX)%MAX; 76 | return ele; 77 | } 78 | 79 | void display(DEQUE *pq) 80 | { 81 | if(isEmpty(pq)) 82 | { 83 | printf("Queue is Empty\n"); 84 | return; 85 | } 86 | int i; 87 | for(i=pq->front;i!=pq->rear;i=(i+1)%MAX) 88 | printf("%d ",pq->q[i]); 89 | printf("%d\n",pq->q[i]); 90 | } 91 | 92 | int main() 93 | { 94 | DEQUE dq; 95 | int choice,ele; 96 | 97 | initDeque(&dq); 98 | printf("1.insFront 2.insRear 3.delFront 4.delRear 5.Display\n"); 99 | scanf("%d",&choice); 100 | do{ 101 | switch(choice) 102 | { 103 | case 1:if(!isFull(&dq)) 104 | { 105 | printf("Enter an ele\n"); 106 | scanf("%d",&ele); 107 | insertFront(&dq,ele); 108 | } 109 | else 110 | printf("Queue is already full\n"); 111 | break; 112 | case 2:if(!isFull(&dq)) 113 | { 114 | printf("Enter an ele\n"); 115 | scanf("%d",&ele); 116 | insertRear(&dq,ele); 117 | } 118 | else 119 | printf("Queue is already full\n"); 120 | break; 121 | case 3:if(!isEmpty(&dq)) 122 | printf("%d deqd\n",deleteFront(&dq)); 123 | else 124 | printf("Queue is already empty\n"); 125 | break; 126 | case 4:if(!isEmpty(&dq)) 127 | printf("%d deqd\n",deleteRear(&dq)); 128 | else 129 | printf("Queue is already empty\n"); 130 | break; 131 | case 5:display(&dq);break; 132 | } 133 | printf("1.insFront 2.insRear 3.delFront 4.delRear 5.Display\n"); 134 | scanf("%d",&choice); 135 | }while(choice<6); 136 | 137 | return 0; 138 | } -------------------------------------------------------------------------------- /Unit2/Queue/4_CircularQ_DEQUE_Array/3_DEQUEUE_CirArray.c: -------------------------------------------------------------------------------- 1 | //DEQUE using circular array 2 | #include 3 | #define MAX 5 4 | 5 | typedef struct DEQUE 6 | { 7 | int q[MAX]; 8 | int front; 9 | int rear; 10 | }DEQUE; 11 | 12 | void initDeque(DEQUE* pq) 13 | { 14 | pq->front = -1; 15 | pq->rear = -1; 16 | } 17 | 18 | int isFull(DEQUE *pq) 19 | { 20 | return (pq->rear+1)%pq->size==pq->front; 21 | } 22 | 23 | int isEmpty(DEQUE *pq) 24 | { 25 | return (pq->front == -1); // or return pq->rear==-1; 26 | } 27 | 28 | void insertFront(DEQUE *pq,int ele) 29 | { 30 | if (pq->front == -1) 31 | { 32 | pq->front = 0; 33 | pq->rear = 0; 34 | } 35 | else if (pq->front == 0) 36 | pq->front = MAX - 1 ; 37 | else 38 | pq->front = pq->front-1; 39 | 40 | pq->q[pq->front] = ele; 41 | } 42 | 43 | void insertRear(DEQUE *pq,int ele) 44 | { 45 | if (pq->front == -1) 46 | { 47 | pq->front = 0; 48 | pq->rear = 0; 49 | } 50 | else if (pq->rear==MAX-1) 51 | pq->rear = 0; 52 | else 53 | pq->rear = pq->rear+1; 54 | 55 | pq->q[pq->rear] = ele ; 56 | } 57 | 58 | int deleteFront(DEQUE *pq) 59 | { 60 | int ele=pq->q[pq->front]; 61 | if (pq->front==pq->rear) 62 | { 63 | pq->front = -1; 64 | pq->rear = -1; 65 | } 66 | else if (pq->front == MAX -1) 67 | pq->front = 0; 68 | else 69 | pq->front = pq->front+1; 70 | return ele; 71 | } 72 | 73 | int deleteRear(DEQUE *pq) 74 | { 75 | int ele=pq->q[pq->rear]; 76 | if (pq->front==pq->rear) 77 | { 78 | pq->front = -1; 79 | pq->rear = -1; 80 | } 81 | else if (pq->rear == 0) 82 | pq->rear = MAX-1; 83 | else 84 | pq->rear = pq->rear-1; 85 | return ele; 86 | } 87 | 88 | void display(DEQUE *pq) 89 | { 90 | if(isEmpty(pq)) 91 | { 92 | printf("Queue is Empty\n"); 93 | return; 94 | } 95 | int i; 96 | for(i=pq->front;i!=pq->rear;i=(i+1)%MAX) 97 | printf("%d ",pq->q[i]); 98 | printf("%d\n",pq->q[i]); 99 | } 100 | 101 | int main() 102 | { 103 | DEQUE dq; 104 | int choice,ele; 105 | 106 | initDeque(&dq); 107 | printf("1.insFront 2.insRear 3.delFront 4.delRear 5.Display\n"); 108 | scanf("%d",&choice); 109 | do{ 110 | switch(choice) 111 | { 112 | case 1:if(!isFull(&dq)) 113 | { 114 | printf("Enter an ele\n"); 115 | scanf("%d",&ele); 116 | insertFront(&dq,ele); 117 | } 118 | else 119 | printf("Queue is already full\n"); 120 | break; 121 | case 2:if(!isFull(&dq)) 122 | { 123 | printf("Enter an ele\n"); 124 | scanf("%d",&ele); 125 | insertRear(&dq,ele); 126 | } 127 | else 128 | printf("Queue is already full\n"); 129 | break; 130 | case 3:if(!isEmpty(&dq)) 131 | printf("%d deqd\n",deleteFront(&dq)); 132 | else 133 | printf("Queue is already empty\n"); 134 | break; 135 | case 4:if(!isEmpty(&dq)) 136 | printf("%d deqd\n",deleteRear(&dq)); 137 | else 138 | printf("Queue is already empty\n"); 139 | break; 140 | case 5:display(&dq);break; 141 | } 142 | printf("1.insFront 2.insRear 3.delFront 4.delRear 5.Display\n"); 143 | scanf("%d",&choice); 144 | }while(choice<6); 145 | 146 | return 0; 147 | } -------------------------------------------------------------------------------- /Unit2/Queue/5_DEQUE_LinkedList/deq.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"deq.h" 3 | 4 | int main() 5 | { 6 | struct deque dq; 7 | initQueue(&dq); 8 | 9 | int choice,ele; 10 | printf("\n1.Insert Front 2.Insert Rear 3.Delete Front 4.Delete Rear 5.Display 6.EXIT"); 11 | scanf("%d",&choice); 12 | do 13 | { 14 | 15 | 16 | switch(choice) 17 | { 18 | case 1:printf("Enter the element\n"); 19 | scanf("%d",&ele); 20 | insertFront(&dq,ele); 21 | break; 22 | case 2:printf("Enter the value\n"); 23 | scanf("%d",&ele); 24 | insertLast(&dq,ele); 25 | break; 26 | case 3:if(!isEmpty(&dq)) 27 | printf("Deleted element=%d\n",deleteFront(&dq)); 28 | else 29 | printf("Empty Queue\n"); 30 | break; 31 | case 4:if(!isEmpty(&dq)) 32 | printf("Deleted element=%d\n",deleteLast(&dq)); 33 | else 34 | printf("Empty Queue\n"); 35 | break; 36 | case 5: display(&dq); 37 | break; 38 | } 39 | printf("\n1.Insert Front 2.Insert Rear 3.Delete Front 4.Delete Rear 5.Display 6.EXIT"); 40 | scanf("%d",&choice); 41 | }while(choice<6); 42 | destroyQueue(&dq); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Unit2/Queue/5_DEQUE_LinkedList/deq.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *prev,*next; 5 | }NODE; 6 | 7 | typedef struct deque 8 | { 9 | NODE *front,*rear; 10 | }DEQUE; 11 | 12 | void initQueue(DEQUE* pq); 13 | void insertFront(DEQUE* pq,int ele); 14 | void insertLast(DEQUE* pq,int ele); 15 | int isEmpty(DEQUE*pq); 16 | int deleteFront(DEQUE* pq); 17 | int deleteLast(DEQUE* pq); 18 | void display(DEQUE* pq); 19 | void destroyQueue(DEQUE* pq); -------------------------------------------------------------------------------- /Unit2/Queue/5_DEQUE_LinkedList/dequeImpl.c: -------------------------------------------------------------------------------- 1 | #include"deq.h" 2 | #include 3 | #include 4 | 5 | void initQueue(DEQUE* pq) 6 | { 7 | pq->front=pq->rear=NULL; 8 | } 9 | 10 | void insertFront(DEQUE* pq,int ele) 11 | { 12 | NODE *temp=malloc(sizeof(NODE)); 13 | temp->info=ele; 14 | temp->prev=NULL; 15 | temp->next=NULL; 16 | 17 | if(isEmpty(pq)) 18 | { 19 | pq->front=pq->rear=temp; 20 | return; 21 | } 22 | temp->next=pq->front; 23 | pq->front->prev=temp; 24 | pq->front=temp; 25 | } 26 | void insertLast(DEQUE* pq,int ele) 27 | { 28 | NODE *temp=malloc(sizeof(NODE)); 29 | temp->info=ele; 30 | temp->prev=NULL; 31 | temp->next=NULL; 32 | 33 | if(isEmpty(pq)) 34 | { 35 | pq->front=pq->rear=temp; 36 | return; 37 | } 38 | temp->prev=pq->rear; 39 | pq->rear->next=temp; 40 | pq->rear=temp; 41 | } 42 | int isEmpty(DEQUE* pq) 43 | { 44 | return pq->front==NULL; 45 | } 46 | int deleteFront(DEQUE* pq) 47 | { 48 | int ele=pq->front->info; 49 | if(pq->front==pq->rear) 50 | { 51 | free(pq->front); 52 | pq->front=pq->rear=NULL; 53 | return ele; 54 | } 55 | pq->front=pq->front->next; 56 | free(pq->front->prev); 57 | pq->front->prev=NULL; 58 | return ele; 59 | } 60 | int deleteLast(DEQUE* pq) 61 | { 62 | int ele=pq->rear->info; 63 | if(pq->front==pq->rear) 64 | { 65 | free(pq->rear); 66 | pq->front=pq->rear=NULL; 67 | return ele; 68 | } 69 | pq->rear=pq->rear->prev; 70 | free(pq->rear->next); 71 | pq->rear->next=NULL; 72 | return ele; 73 | } 74 | 75 | void display(DEQUE* pq) 76 | { 77 | NODE *p=pq->front; 78 | 79 | if(pq->front==NULL) 80 | { 81 | printf("Empty List\n"); 82 | return; 83 | } 84 | while(p!=NULL) 85 | { 86 | printf("%d ",p->info); 87 | p=p->next; 88 | } 89 | printf("\n"); 90 | } 91 | void destroyQueue(DEQUE* pq) 92 | { 93 | NODE *p=pq->front,*q=NULL; 94 | /* while(p!=NULL) 95 | { 96 | q=p; //printf("%d ",p->info); 97 | p=p->next; 98 | free(q); 99 | } 100 | pdl->head=NULL; */ 101 | if(p==NULL) 102 | return; 103 | while(p->next!=NULL) 104 | { 105 | // printf("%d ",p->info); 106 | p=p->next; 107 | free(p->prev); 108 | } 109 | // printf("%d ",p->info); 110 | free(p); 111 | pq->front=pq->rear=NULL; 112 | } -------------------------------------------------------------------------------- /Unit2/Queue/6_Josephus_usingCircularLL/josephus.c: -------------------------------------------------------------------------------- 1 | //Josephus problem using circular linked list 2 | #include 3 | #include"josephus.h" 4 | 5 | int main() 6 | { 7 | LIST l; 8 | int start,skip; 9 | 10 | initQueue(&l); 11 | createQueue(&l); 12 | display(&l); 13 | printf("Enter start value and skip value\n"); 14 | scanf("%d%d",&start,&skip); 15 | printf("Winner is %d\n",winner(&l,start,skip)); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Unit2/Queue/6_Josephus_usingCircularLL/josephus.h: -------------------------------------------------------------------------------- 1 | //Josephus problem using circular linked list 2 | typedef struct node 3 | { 4 | int info; 5 | struct node *next; 6 | }NODE; 7 | 8 | typedef struct list 9 | { 10 | NODE* head,*tail; 11 | }LIST; 12 | 13 | void initQueue(LIST*pq); 14 | void createQueue(LIST*pq); 15 | void display(LIST*pq); 16 | int winner(LIST*pq,int start,int skip); -------------------------------------------------------------------------------- /Unit2/Queue/6_Josephus_usingCircularLL/josephusImpl.c: -------------------------------------------------------------------------------- 1 | //Josephus problem using circular linked list 2 | #include 3 | #include"josephus.h" 4 | #include 5 | 6 | void initQueue(LIST*pl) 7 | { 8 | pl->head=pl->tail=NULL; 9 | } 10 | void createQueue(LIST*pl) 11 | { 12 | int n; 13 | printf("Enter the no. of soldiers\n"); 14 | scanf("%d",&n); 15 | 16 | NODE *temp=malloc(sizeof(NODE)); 17 | temp->info=1; 18 | temp->next=temp; 19 | pl->head=pl->tail=temp; 20 | 21 | for(int i=2;i<=n;i++) 22 | { 23 | NODE *temp=malloc(sizeof(NODE)); 24 | temp->info=i; 25 | temp->next=pl->head; 26 | pl->tail->next=temp; 27 | pl->tail=temp; 28 | } 29 | } 30 | void display(LIST*pl) 31 | { 32 | NODE *p=pl->head; 33 | do 34 | { 35 | printf("%d ",p->info); 36 | p=p->next; 37 | }while(p!=pl->head); 38 | printf("\n"); 39 | } 40 | int winner(LIST*pl,int start,int skip) 41 | { 42 | NODE *p=pl->head,*q=pl->tail; 43 | 44 | int count=1; 45 | while(count!=start) 46 | { 47 | count++; 48 | q=p; 49 | p=p->next; 50 | } 51 | while(p!=p->next) 52 | { 53 | for(int i=1;i!=skip;i++) 54 | { 55 | q=p; 56 | p=p->next; 57 | } 58 | printf("%d killed\n",p->info); 59 | q->next=p->next; 60 | free(p); 61 | p=q->next; 62 | } 63 | int ele=p->info; 64 | free(p); 65 | pl->head=pl->tail=NULL; 66 | return ele; 67 | } -------------------------------------------------------------------------------- /Unit3/1_BST_Creation_Traversal/bst.c: -------------------------------------------------------------------------------- 1 | #include"bst.h" 2 | #include 3 | 4 | int main() 5 | { 6 | TREE tobj; 7 | 8 | initTree(&tobj); //tobj.root=NULL; 9 | createTree(&tobj); 10 | 11 | printf("Inorder Traversal\n"); 12 | inorder(&tobj); 13 | 14 | printf("\nPreorder Traversal\n"); 15 | preorder(&tobj); 16 | 17 | printf("\nPostorder Traversal\n"); 18 | postorder(&tobj); 19 | 20 | destroyTree(&tobj); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Unit3/1_BST_Creation_Traversal/bst.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *left,*right; 5 | }NODE; 6 | 7 | typedef struct tree 8 | { 9 | NODE* root; 10 | }TREE; 11 | 12 | void initTree(TREE *pt); 13 | void createTree(TREE *pt); 14 | void inorder(TREE *pt); 15 | void preorder(TREE *pt); 16 | void postorder(TREE *pt); 17 | void destroyTree(TREE *pt); -------------------------------------------------------------------------------- /Unit3/1_BST_Creation_Traversal/bstImpl.c: -------------------------------------------------------------------------------- 1 | #include "bst.h" 2 | #include 3 | #include 4 | 5 | void initTree(TREE *pt) 6 | { 7 | pt->root = NULL; 8 | } 9 | 10 | void createTree(TREE *pt) 11 | { 12 | int choice; 13 | NODE *temp = malloc(sizeof(NODE)); 14 | temp->left = temp->right = NULL; 15 | 16 | printf("Enter root info\n"); 17 | scanf("%d", &temp->info); 18 | 19 | pt->root = temp; 20 | 21 | printf("Do you want to add one more node\n"); 22 | scanf("%d", &choice); 23 | while (choice) 24 | { 25 | temp = malloc(sizeof(NODE)); 26 | temp->left = temp->right = NULL; 27 | 28 | printf("Enter node info\n"); 29 | scanf("%d", &temp->info); 30 | 31 | NODE *p = pt->root; 32 | NODE *q = NULL; 33 | while (p != NULL) 34 | { 35 | if (temp->info <= p->info) 36 | { 37 | q = p; 38 | p = p->left; 39 | } 40 | else 41 | { 42 | q = p; 43 | p = p->right; 44 | } 45 | } 46 | if (temp->info <= q->info) 47 | q->left = temp; 48 | else 49 | q->right = temp; 50 | printf("Do you want to add one more node\n"); 51 | scanf("%d", &choice); 52 | } 53 | } 54 | void inord(NODE *r) 55 | { 56 | if (r == NULL) 57 | return; 58 | inord(r->left); 59 | printf("%d ", r->info); 60 | inord(r->right); 61 | } 62 | void inorder(TREE *pt) 63 | { 64 | inord(pt->root); 65 | } 66 | void preord(NODE *r) 67 | { 68 | if (r != NULL) 69 | { 70 | printf("%d ", r->info); 71 | preord(r->left); 72 | preord(r->right); 73 | } 74 | } 75 | void preorder(TREE *pt) 76 | { 77 | preord(pt->root); 78 | } 79 | void postord(NODE *r) 80 | { 81 | if (r != NULL) 82 | { 83 | postord(r->left); 84 | postord(r->right); 85 | printf("%d ", r->info); 86 | } 87 | } 88 | void postorder(TREE *pt) 89 | { 90 | postord(pt->root); 91 | } 92 | void destroyNode(NODE *r) 93 | { 94 | if (r != NULL) 95 | { 96 | destroyNode(r->left); 97 | destroyNode(r->right); 98 | // printf("\nFreeing%d",r->info); 99 | free(r); 100 | } 101 | } 102 | void destroyTree(TREE *pt) 103 | { 104 | if (pt->root != NULL) 105 | { 106 | destroyNode(pt->root); 107 | pt->root = NULL; 108 | } 109 | } -------------------------------------------------------------------------------- /Unit3/2_BST_Creatn_findMin_findMax_Search_Deletion/bst.c: -------------------------------------------------------------------------------- 1 | #include"bst.h" 2 | #include 3 | 4 | int main() 5 | { 6 | TREE tobj; 7 | 8 | initTree(&tobj); //tobj.root=NULL; 9 | createTree(&tobj); 10 | 11 | printf("Inorder Traversal\n"); 12 | inorder(&tobj); 13 | 14 | printf("\nPreorder Traversal\n"); 15 | preorder(&tobj); 16 | 17 | printf("\nPostorder Traversal\n"); 18 | postorder(&tobj); 19 | 20 | int min=findMin(&tobj); 21 | if(min==-1) 22 | printf("Empty Tree\n"); 23 | else 24 | printf("Min=%d\n",min); 25 | 26 | int max=findMax(&tobj); 27 | if(max==-1) 28 | printf("Empty Tree\n"); 29 | else 30 | printf("Max=%d\n",max); 31 | 32 | int ele; 33 | printf("Enter the element you want to delete\n"); 34 | scanf("%d",&ele); 35 | if(searchEle(&tobj,ele)) 36 | { 37 | // printf("%d found\n",ele); 38 | deleteNode(&tobj,ele); 39 | inorder(&tobj); 40 | } 41 | else 42 | { 43 | printf("%d not found or tree is empty\n",ele); 44 | } 45 | 46 | destroyTree(&tobj); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Unit3/2_BST_Creatn_findMin_findMax_Search_Deletion/bst.h: -------------------------------------------------------------------------------- 1 | typedef struct node 2 | { 3 | int info; 4 | struct node *left,*right; 5 | }NODE; 6 | 7 | typedef struct tree 8 | { 9 | NODE* root; 10 | }TREE; 11 | 12 | void initTree(TREE *pt); 13 | void createTree(TREE *pt); 14 | void inorder(TREE *pt); 15 | void preorder(TREE *pt); 16 | void postorder(TREE *pt); 17 | void destroyTree(TREE *pt); 18 | int findMin(TREE *pt); 19 | int findMax(TREE *pt); 20 | void deleteNode(TREE *pt,int ele); 21 | int searchEle(TREE *pt,int ele); -------------------------------------------------------------------------------- /Unit3/2_BST_Creatn_findMin_findMax_Search_Deletion/bstImpl.c: -------------------------------------------------------------------------------- 1 | #include "bst.h" 2 | #include 3 | #include 4 | 5 | void initTree(TREE *pt) 6 | { 7 | pt->root = NULL; 8 | } 9 | 10 | void createTree(TREE *pt) 11 | { 12 | int choice; 13 | NODE *temp = malloc(sizeof(NODE)); 14 | temp->left = temp->right = NULL; 15 | 16 | printf("Enter the info\n"); 17 | scanf("%d", &temp->info); 18 | 19 | pt->root = temp; 20 | 21 | printf("Do you want to add one more node\n"); 22 | scanf("%d", &choice); 23 | while (choice) 24 | { 25 | temp = malloc(sizeof(NODE)); 26 | temp->left = temp->right = NULL; 27 | 28 | printf("Enter the info\n"); 29 | scanf("%d", &temp->info); 30 | 31 | NODE *p = pt->root; 32 | NODE *q = NULL; 33 | while (p != NULL) 34 | { 35 | if (temp->info <= p->info) 36 | { 37 | q = p; 38 | p = p->left; 39 | } 40 | else 41 | { 42 | q = p; 43 | p = p->right; 44 | } 45 | } 46 | if (temp->info <= q->info) 47 | q->left = temp; 48 | else 49 | q->right = temp; 50 | printf("Do you want to add one more node\n"); 51 | scanf("%d", &choice); 52 | } 53 | } 54 | void inord(NODE *r) 55 | { 56 | if (r == NULL) 57 | return; 58 | inord(r->left); 59 | printf("%d ", r->info); 60 | inord(r->right); 61 | } 62 | void inorder(TREE *pt) 63 | { 64 | inord(pt->root); 65 | } 66 | void preord(NODE *r) 67 | { 68 | if (r != NULL) 69 | { 70 | printf("%d ", r->info); 71 | preord(r->left); 72 | preord(r->right); 73 | } 74 | } 75 | void preorder(TREE *pt) 76 | { 77 | preord(pt->root); 78 | } 79 | void postord(NODE *r) 80 | { 81 | if (r != NULL) 82 | { 83 | postord(r->left); 84 | postord(r->right); 85 | printf("%d ", r->info); 86 | } 87 | } 88 | void postorder(TREE *pt) 89 | { 90 | postord(pt->root); 91 | } 92 | void destroyNode(NODE *r) 93 | { 94 | if (r != NULL) 95 | { 96 | destroyNode(r->left); 97 | destroyNode(r->right); 98 | printf("\nFreeing%d", r->info); 99 | free(r); 100 | } 101 | } 102 | void destroyTree(TREE *pt) 103 | { 104 | if (pt->root != NULL) 105 | { 106 | destroyNode(pt->root); 107 | pt->root = NULL; 108 | } 109 | } 110 | int findMinimum(NODE *r) 111 | { 112 | if (r == NULL) 113 | return -1; 114 | 115 | while (r->left != NULL) 116 | r = r->left; 117 | 118 | return r->info; 119 | } 120 | int findMin(TREE *pt) 121 | { 122 | return findMinimum(pt->root); 123 | } 124 | int findMaximum(NODE *r) 125 | { 126 | if (r == NULL) 127 | return -1; 128 | 129 | while (r->right != NULL) 130 | r = r->right; 131 | 132 | return r->info; 133 | } 134 | int findMax(TREE *pt) 135 | { 136 | return findMaximum(pt->root); 137 | } 138 | 139 | NODE *delNode(NODE *r, int ele) 140 | { 141 | if (r == NULL) 142 | return r; 143 | 144 | NODE *temp = NULL; 145 | if (ele < r->info) 146 | r->left = delNode(r->left, ele); 147 | else if (ele > r->info) 148 | r->right = delNode(r->right, ele); 149 | else if (r->left == NULL) //single right child case and leaf node case 150 | { 151 | temp = r->right; 152 | free(r); 153 | return temp; 154 | } 155 | else if (r->right == NULL) //single left child case 156 | { 157 | temp = r->left; 158 | free(r); 159 | return temp; 160 | } 161 | else //replacing with inorder successor 162 | { 163 | temp = r->right; 164 | while (temp->left != NULL) 165 | temp = temp->left; 166 | r->info = temp->info; 167 | r->right = delNode(r->right, temp->info); 168 | } 169 | /* 170 | //Replacing with inorder predecessor 171 | else 172 | { 173 | temp=r->left; 174 | while(temp->right!=NULL) 175 | temp=temp->right; 176 | r->info=temp->info; 177 | r->left=delNode(r->left,temp->info); 178 | } 179 | 180 | */ 181 | return r; 182 | } 183 | 184 | /* 185 | //Few students logic, look for change in 2 children case 186 | NODE* delNode(NODE *r,int ele) 187 | { 188 | NODE *temp=NULL,*prev=NULL; 189 | if(r==NULL) 190 | return r; 191 | 192 | if(eleinfo) 193 | r->left=delNode(r->left,ele); 194 | else if(ele>r->info) 195 | r->right=delNode(r->right,ele); 196 | else if(r->left==NULL) //Single right child & Leaf node 197 | { 198 | temp=r->right; 199 | free(r); 200 | return temp; 201 | } 202 | else if(r->right==NULL)//Single left child 203 | { 204 | temp=r->left; 205 | free(r); 206 | return temp; 207 | } 208 | else //Two children 209 | { 210 | temp=r->right; 211 | 212 | while(temp->left!=NULL) 213 | { 214 | prev=temp; 215 | temp=temp->left; 216 | } 217 | 218 | r->info=temp->info; 219 | if(prev==NULL) 220 | { 221 | r->right=temp->right; 222 | free(temp); 223 | } 224 | else 225 | { 226 | prev->left=temp->right; 227 | free(prev->left); 228 | } 229 | } 230 | return r; 231 | } 232 | */ 233 | void deleteNode(TREE *pt, int ele) 234 | { 235 | pt->root = delNode(pt->root, ele); 236 | } 237 | /* 238 | int search(NODE *r,int ele) 239 | { 240 | if(r!=NULL) 241 | { 242 | if(ele==r->info) 243 | return 1; 244 | else if(eleinfo) 245 | return search(r->left,ele); 246 | else 247 | return search(r->right,ele); 248 | } 249 | return 0; 250 | } 251 | */ 252 | int search(NODE *r, int ele) 253 | { 254 | while (r != NULL) 255 | { 256 | if (ele == r->info) 257 | return 1; 258 | else if (ele < r->info) 259 | r = r->left; 260 | else 261 | r = r->right; 262 | } 263 | return 0; 264 | } 265 | 266 | int searchEle(TREE *pt, int ele) 267 | { 268 | return search(pt->root, ele); 269 | } -------------------------------------------------------------------------------- /Unit3/3_BST_ArrayImpl_Creation_Traversal/bstArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"bstArray.h" 3 | 4 | int main() 5 | { 6 | NODE bst[MAX]; 7 | 8 | initTree(bst); 9 | createTree(bst); 10 | 11 | printf("Tree in Level order Traversal\n"); 12 | levelorder(bst); 13 | 14 | printf("\nInorder Traversal\n"); 15 | inorder(bst,0); 16 | 17 | printf("\nPreorder Traversal\n"); 18 | preorder(bst,0); 19 | 20 | printf("\nPostorder Traversal\n"); 21 | postorder(bst,0); 22 | } -------------------------------------------------------------------------------- /Unit3/3_BST_ArrayImpl_Creation_Traversal/bstArray.h: -------------------------------------------------------------------------------- 1 | #define MAX 20 2 | typedef struct node 3 | { 4 | int info; 5 | int used; 6 | }NODE; 7 | 8 | void initTree(NODE bst[MAX]); 9 | void createTree(NODE bst[MAX]); 10 | void levelorder(NODE bst[MAX]); 11 | void inorder(NODE bst[MAX],int); 12 | void preorder(NODE bst[MAX],int); 13 | void postorder(NODE bst[MAX],int); -------------------------------------------------------------------------------- /Unit3/3_BST_ArrayImpl_Creation_Traversal/bstArrayImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "bstArray.h" 3 | 4 | void initTree(NODE bst[MAX]) 5 | { 6 | for (int i = 0; i < MAX; i++) 7 | bst[i].used = 0; 8 | } 9 | void createTree(NODE bst[MAX]) 10 | { 11 | int ele, choice; 12 | printf("Enter root info\n"); 13 | scanf("%d", &ele); 14 | 15 | bst[0].info = ele; 16 | bst[0].used = 1; 17 | 18 | printf("Do you want to add one more node\n"); 19 | scanf("%d", &choice); 20 | int i; 21 | while (choice) 22 | { 23 | printf("Enter node info\n"); 24 | scanf("%d", &ele); 25 | i = 0; 26 | while (i < MAX && bst[i].used) 27 | { 28 | if (ele < bst[i].info) 29 | i = 2 * i + 1; 30 | else 31 | i = 2 * i + 2; 32 | } 33 | if (i >= MAX) 34 | { 35 | printf("Can't insert, outside array bound\n"); 36 | break; 37 | } 38 | bst[i].info = ele; 39 | bst[i].used = 1; 40 | printf("Do you want to add one more node\n"); 41 | scanf("%d", &choice); 42 | } 43 | } 44 | void levelorder(NODE bst[MAX]) 45 | { 46 | for (int i = 0; i < MAX; i++) 47 | if (bst[i].used) 48 | printf("%d ", bst[i].info); 49 | } 50 | void inorder(NODE bst[MAX], int i) 51 | { 52 | if (i < MAX && bst[i].used) 53 | { 54 | inorder(bst, 2 * i + 1); 55 | printf("%d ", bst[i].info); 56 | inorder(bst, 2 * i + 2); 57 | } 58 | } 59 | void preorder(NODE bst[MAX], int i) 60 | { 61 | if (i < MAX && bst[i].used) 62 | { 63 | printf("%d ", bst[i].info); 64 | preorder(bst, 2 * i + 1); 65 | preorder(bst, 2 * i + 2); 66 | } 67 | } 68 | void postorder(NODE bst[MAX], int i) 69 | { 70 | if (i < MAX && bst[i].used) 71 | { 72 | postorder(bst, 2 * i + 1); 73 | postorder(bst, 2 * i + 2); 74 | printf("%d ", bst[i].info); 75 | } 76 | } -------------------------------------------------------------------------------- /Unit3/4_BinaryTree_dynamicAllocnImpl_ArrayImpl/binaryTree_ArrayImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 50 3 | 4 | typedef struct node 5 | { 6 | int info; 7 | int used; 8 | } NODE; 9 | 10 | void initTree(NODE bt[MAX]); 11 | void createTree(NODE bt[MAX], int i); 12 | void levelorder(NODE bt[MAX]); 13 | void inorder(NODE bt[MAX], int i); 14 | void preorder(NODE bt[MAX], int i); 15 | void postorder(NODE bt[MAX], int i); 16 | 17 | int main() 18 | { 19 | NODE bt[MAX]; 20 | 21 | initTree(bt); 22 | createTree(bt, 0); 23 | 24 | printf("Tree in Level order Traversal\n"); 25 | levelorder(bt); 26 | 27 | printf("\nInorder Traversal\n"); 28 | inorder(bt, 0); 29 | 30 | printf("\nPreorder Traversal\n"); 31 | preorder(bt, 0); 32 | 33 | printf("\nPostorder Traversal\n"); 34 | postorder(bt, 0); 35 | } 36 | 37 | void initTree(NODE bt[MAX]) 38 | { 39 | for (int i = 0; i < MAX; i++) 40 | bt[i].used = 0; 41 | } 42 | 43 | void createTree(NODE bt[MAX], int i) 44 | { 45 | int ele; 46 | printf("Enter data(-1 for no data):"); 47 | scanf("%d", &ele); 48 | 49 | if (ele == -1 || i >= MAX) 50 | { 51 | // if(i>=MAX) 52 | // printf("Outside array bound\n"); 53 | return; 54 | } 55 | 56 | bt[i].info = ele; 57 | bt[i].used = 1; 58 | 59 | printf("Enter left child of %d:\n", ele); 60 | createTree(bt, 2 * i + 1); 61 | 62 | printf("Enter right child of %d:\n", ele); 63 | createTree(bt, 2 * i + 2); 64 | } 65 | 66 | void levelorder(NODE bt[MAX]) 67 | { 68 | for (int i = 0; i < MAX; i++) 69 | if (bt[i].used) 70 | printf("%d ", bt[i].info); 71 | } 72 | 73 | void inorder(NODE bt[MAX], int i) 74 | { 75 | if (i < MAX && bt[i].used) 76 | { 77 | inorder(bt, 2 * i + 1); 78 | printf("%d ", bt[i].info); 79 | inorder(bt, 2 * i + 2); 80 | } 81 | } 82 | void preorder(NODE bt[MAX], int i) 83 | { 84 | if (i < MAX && bt[i].used) 85 | { 86 | printf("%d ", bt[i].info); 87 | preorder(bt, 2 * i + 1); 88 | preorder(bt, 2 * i + 2); 89 | } 90 | } 91 | void postorder(NODE bt[MAX], int i) 92 | { 93 | if (i < MAX && bt[i].used) 94 | { 95 | postorder(bt, 2 * i + 1); 96 | postorder(bt, 2 * i + 2); 97 | printf("%d ", bt[i].info); 98 | } 99 | } -------------------------------------------------------------------------------- /Unit3/4_BinaryTree_dynamicAllocnImpl_ArrayImpl/binaryTree_dynamicAllocn.c: -------------------------------------------------------------------------------- 1 | //Binary Tree, Linked List Implementation 2 | #include 3 | #include 4 | 5 | typedef struct node 6 | { 7 | int info; 8 | struct node *left, *right; 9 | } NODE; 10 | 11 | typedef struct tree 12 | { 13 | NODE *root; 14 | } TREE; 15 | void initTree(TREE *pt); 16 | void createTree(TREE *pt); 17 | void inorder(TREE *pt); 18 | void preorder(TREE *pt); 19 | void postorder(TREE *pt); 20 | void postorder1(NODE *r); 21 | void destroyTree(TREE *pt); 22 | 23 | int main() 24 | { 25 | TREE tobj; 26 | initTree(&tobj); 27 | 28 | createTree(&tobj); 29 | printf("Inorder BT traversal\n"); 30 | inorder(&tobj); 31 | printf("\nPreorder BT traversal\n"); 32 | preorder(&tobj); 33 | printf("\nPostorder BT traversal\n"); 34 | postorder(&tobj); 35 | printf("\nPostorder BT traversal\n"); 36 | postorder1(tobj.root); 37 | 38 | destroyTree(&tobj); 39 | 40 | return 0; 41 | } 42 | 43 | void initTree(TREE *pt) 44 | { 45 | pt->root = NULL; 46 | } 47 | NODE *create() 48 | { 49 | int ele; 50 | printf("Enter info (-1 for No node)\n"); 51 | scanf("%d", &ele); 52 | 53 | if (ele == -1) 54 | return NULL; 55 | 56 | NODE *temp = malloc(sizeof(NODE)); 57 | temp->info = ele; 58 | 59 | printf("Enter left child of %d\n", ele); 60 | temp->left = create(); 61 | 62 | printf("Enter right child of %d\n", ele); 63 | temp->right = create(); 64 | 65 | return temp; 66 | } 67 | void createTree(TREE *pt) 68 | { 69 | pt->root = create(); 70 | } 71 | 72 | void in(NODE *r) 73 | { 74 | if (r != NULL) 75 | { 76 | in(r->left); 77 | printf("%d ", r->info); 78 | in(r->right); 79 | } 80 | } 81 | void inorder(TREE *pt) 82 | { 83 | in(pt->root); 84 | } 85 | 86 | void pre(NODE *r) 87 | { 88 | if (r != NULL) 89 | { 90 | printf("%d ", r->info); 91 | pre(r->left); 92 | pre(r->right); 93 | } 94 | } 95 | void preorder(TREE *pt) 96 | { 97 | pre(pt->root); 98 | } 99 | void post(NODE *r) 100 | { 101 | if (r != NULL) 102 | { 103 | post(r->left); 104 | post(r->right); 105 | printf("%d ", r->info); 106 | } 107 | } 108 | void postorder(TREE *pt) 109 | { 110 | post(pt->root); 111 | } 112 | 113 | void postorder1(NODE *r) 114 | { 115 | if (r != NULL) 116 | { 117 | postorder1(r->left); 118 | postorder1(r->right); 119 | printf("%d ", r->info); 120 | } 121 | } 122 | void destroyNode(NODE *r) 123 | { 124 | if (r != NULL) 125 | { 126 | destroyNode(r->left); 127 | destroyNode(r->right); 128 | // printf("\nFreeing %d",r->info); 129 | free(r); 130 | } 131 | } 132 | void destroyTree(TREE *pt) 133 | { 134 | if (pt->root) 135 | { 136 | destroyNode(pt->root); 137 | pt->root = NULL; 138 | } 139 | } -------------------------------------------------------------------------------- /Unit3/5_ExpressionTree/exprTree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"exprTree.h" 3 | 4 | int main() 5 | { 6 | TREE tobj; 7 | initTree(&tobj); 8 | 9 | char expr[MAX]; 10 | printf("Enter a valid postfix expression\n"); 11 | scanf("%s",expr); 12 | 13 | createTree(&tobj,expr); 14 | printf("\nInorder:"); 15 | inorder(&tobj); 16 | printf("\nPreorder:"); 17 | preorder(&tobj); 18 | printf("\nPostorder:"); 19 | postorder(&tobj); 20 | 21 | printf("\nResult=%d\n",evalExprTree(&tobj)); 22 | } -------------------------------------------------------------------------------- /Unit3/5_ExpressionTree/exprTree.h: -------------------------------------------------------------------------------- 1 | #define MAX 50 2 | typedef struct node 3 | { 4 | char info; 5 | struct node *left,*right; 6 | }NODE; 7 | typedef struct tree 8 | { 9 | NODE *root; 10 | }TREE; 11 | typedef struct stack 12 | { 13 | int top; 14 | NODE *s[MAX]; 15 | }STACK; 16 | void initTree(TREE *pt); 17 | void createTree(TREE *pt,char expr[MAX]); 18 | void inorder(TREE *pt); 19 | void preorder(TREE *pt); 20 | void postorder(TREE *pt); 21 | int evalExprTree(TREE *pt); 22 | void destroyTree(TREE *pt); 23 | void initStack(STACK *ps); 24 | void push(STACK *ps,NODE* ele); 25 | NODE* pop(STACK *ps); -------------------------------------------------------------------------------- /Unit3/5_ExpressionTree/exprTreeImpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "exprTree.h" 3 | #include 4 | #include 5 | 6 | void initTree(TREE *pt) 7 | { 8 | pt->root = NULL; 9 | } 10 | void initStack(STACK *ps) 11 | { 12 | ps->top = -1; 13 | } 14 | void createTree(TREE *pt, char expr[MAX]) 15 | { 16 | STACK sobj; 17 | initStack(&sobj); 18 | 19 | for (int i = 0; expr[i] != '\0'; i++) 20 | { 21 | NODE *temp = malloc(sizeof(NODE)); 22 | temp->info = expr[i]; 23 | temp->left = temp->right = NULL; 24 | 25 | if (isdigit(expr[i])) 26 | push(&sobj, temp); 27 | else 28 | { 29 | temp->right = pop(&sobj); 30 | temp->left = pop(&sobj); 31 | push(&sobj, temp); 32 | } 33 | } 34 | pt->root = pop(&sobj); 35 | } 36 | void inord(NODE *r) 37 | { 38 | if (r == NULL) 39 | return; 40 | inord(r->left); 41 | printf("%c ", r->info); 42 | inord(r->right); 43 | } 44 | void inorder(TREE *pt) 45 | { 46 | inord(pt->root); 47 | } 48 | void preord(NODE *r) 49 | { 50 | if (r != NULL) 51 | { 52 | printf("%c ", r->info); 53 | preord(r->left); 54 | preord(r->right); 55 | } 56 | } 57 | void preorder(TREE *pt) 58 | { 59 | preord(pt->root); 60 | } 61 | void postord(NODE *r) 62 | { 63 | if (r != NULL) 64 | { 65 | postord(r->left); 66 | postord(r->right); 67 | printf("%c ", r->info); 68 | } 69 | } 70 | void postorder(TREE *pt) 71 | { 72 | postord(pt->root); 73 | } 74 | void destroyNode(NODE *r) 75 | { 76 | if (r != NULL) 77 | { 78 | destroyNode(r->left); 79 | destroyNode(r->right); 80 | printf("\nFreeing%d", r->info); 81 | free(r); 82 | } 83 | } 84 | void destroyTree(TREE *pt) 85 | { 86 | if (pt->root != NULL) 87 | { 88 | destroyNode(pt->root); 89 | pt->root = NULL; 90 | } 91 | } 92 | int eval(NODE *r) 93 | { 94 | switch (r->info) 95 | { 96 | case '+': 97 | return eval(r->left) + eval(r->right); 98 | break; 99 | case '-': 100 | return eval(r->left) - eval(r->right); 101 | break; 102 | case '*': 103 | return eval(r->left) * eval(r->right); 104 | break; 105 | case '/': 106 | return eval(r->left) / eval(r->right); 107 | break; 108 | default: 109 | return r->info - '0'; 110 | } 111 | } 112 | int evalExprTree(TREE *pt) 113 | { 114 | return eval(pt->root); 115 | } 116 | 117 | void push(STACK *ps, NODE *ele) 118 | { 119 | ps->top++; 120 | ps->s[ps->top] = ele; 121 | } 122 | NODE *pop(STACK *ps) 123 | { 124 | NODE *ele = ps->s[ps->top]; 125 | ps->top--; 126 | return ele; 127 | } -------------------------------------------------------------------------------- /Unit3/6_IterativeTraversal/bst_IterTraversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 20 4 | typedef struct node 5 | { 6 | int info; 7 | struct node *left, *right; 8 | } NODE; 9 | 10 | typedef struct tree 11 | { 12 | NODE *root; 13 | } TREE; 14 | 15 | typedef struct stack 16 | { 17 | int top; 18 | NODE *s[MAX]; 19 | } STACK; 20 | typedef struct queue 21 | { 22 | int rear, front; 23 | NODE *q[MAX]; 24 | } QUEUE; 25 | void initTree(TREE *pt) 26 | { 27 | pt->root = NULL; 28 | } 29 | 30 | void createTree(TREE *pt) 31 | { 32 | NODE *temp = malloc(sizeof(NODE)); 33 | temp->left = temp->right = NULL; 34 | 35 | printf("Enter root info\n"); 36 | scanf("%d", &temp->info); 37 | 38 | pt->root = temp; 39 | 40 | int choice; 41 | printf("Do you want to add one more node\n"); 42 | scanf("%d", &choice); 43 | 44 | while (choice) 45 | { 46 | NODE *temp = malloc(sizeof(NODE)); 47 | temp->left = temp->right = NULL; 48 | 49 | printf("Enter node info\n"); 50 | scanf("%d", &temp->info); 51 | 52 | NODE *p = pt->root; 53 | NODE *q; 54 | 55 | while (p != NULL) 56 | { 57 | q = p; 58 | if (temp->info < p->info) 59 | p = p->left; 60 | else 61 | p = p->right; 62 | } 63 | if (temp->info < q->info) 64 | q->left = temp; 65 | else 66 | q->right = temp; 67 | 68 | printf("Do you want to add one more node\n"); 69 | scanf("%d", &choice); 70 | } 71 | } 72 | void push(STACK *ps, NODE *ele) 73 | { 74 | ps->top++; 75 | ps->s[ps->top] = ele; 76 | } 77 | NODE *pop(STACK *ps) 78 | { 79 | NODE *temp = ps->s[ps->top]; 80 | ps->top--; 81 | return temp; 82 | } 83 | int isEmpty(STACK *ps) 84 | { 85 | return ps->top == -1; 86 | } 87 | void inorder(TREE *pt) 88 | { 89 | STACK s; 90 | s.top = -1; 91 | 92 | NODE *cur = pt->root; 93 | 94 | while (!isEmpty(&s) || cur != NULL) 95 | { 96 | if (cur != NULL) 97 | { 98 | push(&s, cur); 99 | cur = cur->left; 100 | } 101 | else 102 | { 103 | cur = pop(&s); 104 | printf("%d ", cur->info); 105 | cur = cur->right; 106 | } 107 | } 108 | } 109 | 110 | void preorder(TREE *pt) 111 | { 112 | STACK s; 113 | s.top = -1; 114 | NODE *cur = pt->root; 115 | 116 | if (cur == NULL) 117 | return; 118 | 119 | push(&s, cur); 120 | while (!isEmpty(&s)) 121 | { 122 | cur = pop(&s); 123 | printf("%d ", cur->info); 124 | if (cur->right != NULL) 125 | push(&s, cur->right); 126 | if (cur->left != NULL) 127 | push(&s, cur->left); 128 | } 129 | } 130 | void postorder(TREE *pt) 131 | { 132 | STACK s1, s2; 133 | s1.top = -1; 134 | s2.top = -1; 135 | 136 | NODE *cur = pt->root; 137 | 138 | if (cur == NULL) 139 | return; 140 | 141 | push(&s1, cur); 142 | while (!isEmpty(&s1)) 143 | { 144 | cur = pop(&s1); 145 | push(&s2, cur); 146 | 147 | if (cur->left != NULL) 148 | push(&s1, cur->left); 149 | if (cur->right != NULL) 150 | push(&s1, cur->right); 151 | } 152 | while (!isEmpty(&s2)) 153 | { 154 | cur = pop(&s2); 155 | printf("%d ", cur->info); 156 | } 157 | } 158 | void enqueue(QUEUE *pq, NODE *ele) 159 | { 160 | pq->rear++; 161 | pq->q[pq->rear] = ele; 162 | } 163 | 164 | NODE *dequeue(QUEUE *pq) 165 | { 166 | NODE *ele = pq->q[pq->front]; 167 | pq->front++; 168 | 169 | if (pq->front > pq->rear) 170 | { 171 | pq->front = 0; 172 | pq->rear = -1; 173 | } 174 | return ele; 175 | 176 | //return pq->q[pq->front++]; 177 | } 178 | 179 | int isEmptyQ(QUEUE *pq) 180 | { 181 | return pq->front > pq->rear; 182 | } 183 | void levelorder(TREE *pt) 184 | { 185 | QUEUE q; 186 | q.front = 0; 187 | q.rear = -1; 188 | NODE *cur = pt->root; 189 | 190 | if (cur == NULL) 191 | return; 192 | 193 | enqueue(&q, cur); 194 | while (!isEmptyQ(&q)) 195 | { 196 | cur = dequeue(&q); 197 | printf("%d ", cur->info); 198 | if (cur->left != NULL) 199 | enqueue(&q, cur->left); 200 | if (cur->right != NULL) 201 | enqueue(&q, cur->right); 202 | } 203 | } 204 | void destroyNode(NODE *r) 205 | { 206 | if (r != NULL) 207 | { 208 | destroyNode(r->left); 209 | destroyNode(r->right); 210 | printf("\nFreeing %d", r->info); 211 | free(r); 212 | } 213 | } 214 | void destroyTree(TREE *pt) 215 | { 216 | if (pt->root != NULL) 217 | { 218 | destroyNode(pt->root); 219 | pt->root = NULL; 220 | } 221 | } 222 | 223 | int main() 224 | { 225 | TREE tobj; 226 | initTree(&tobj); 227 | createTree(&tobj); 228 | printf("Inorder"); 229 | inorder(&tobj); 230 | printf("\nPreorder"); 231 | preorder(&tobj); 232 | printf("\nPostorder"); 233 | postorder(&tobj); 234 | printf("\nLevelorder"); 235 | levelorder(&tobj); 236 | printf("\n"); 237 | destroyTree(&tobj); 238 | } -------------------------------------------------------------------------------- /Unit3/7_Heap/botmUpMaxHeap.c: -------------------------------------------------------------------------------- 1 | //Max Heap 2 | //Bottom Up Heap Construction 3 | #include 4 | #define MAX 50 5 | void bottomUpMaxHeap(int h[MAX], int n); 6 | int main() 7 | { 8 | int h[MAX], n; 9 | 10 | printf("Enter the no. of elts\n"); 11 | scanf("%d", &n); 12 | 13 | printf("Enter the elts\n"); 14 | for (int i = 0; i < n; i++) 15 | scanf("%d", &h[i]); 16 | 17 | bottomUpMaxHeap(h, n); 18 | printf("Bottom Up heap:\n"); 19 | for (int i = 0; i < n; i++) 20 | { 21 | printf("%d ", h[i]); 22 | } 23 | } 24 | void bottomUpMaxHeap(int h[MAX], int n) 25 | { 26 | int c, p, key; 27 | 28 | for (int k = n / 2 - 1; k >= 0; k--) 29 | { 30 | p = k; 31 | key = h[p]; 32 | c = 2 * p + 1; 33 | 34 | while (c < n) //Until there is a left child 35 | { 36 | if (c + 1 < n) //If there exist right child 37 | if (h[c + 1] > h[c]) 38 | c = c + 1; 39 | //c will be having index of largest child 40 | if (key < h[c]) 41 | { 42 | h[p] = h[c]; 43 | p = c; 44 | c = 2 * p + 1; 45 | } 46 | else 47 | break; 48 | } 49 | h[p] = key; 50 | } 51 | } -------------------------------------------------------------------------------- /Unit3/7_Heap/topDown_MaxHeap.c: -------------------------------------------------------------------------------- 1 | /* #include 2 | #define MAX 50 3 | void topDownHeap(int h[MAX],int n); 4 | int main() 5 | { 6 | int h[MAX],n; 7 | 8 | printf("Enter the no. of elts\n"); 9 | scanf("%d",&n); 10 | 11 | topDownHeap(h,n); 12 | printf("Top down heap:\n"); 13 | for(int i=0;i0 && h[p] 41 | #define MAX 50 42 | void topDownHeap(int h[MAX],int n); 43 | int main() 44 | { 45 | int h[MAX],n; 46 | 47 | printf("Enter the no. of elts\n"); 48 | scanf("%d",&n); 49 | 50 | printf("Enter the elts\n"); 51 | for(int i=0;i0 && h[p] 2 | #include 3 | 4 | typedef struct node 5 | { 6 | int info; 7 | struct node *left, *right; 8 | int rthread; 9 | } NODE; 10 | 11 | typedef struct tree 12 | { 13 | NODE *root; 14 | } TREE; 15 | 16 | void initTree(TREE *pt) 17 | { 18 | pt->root = NULL; 19 | } 20 | void setLeft(NODE *q, NODE *temp) 21 | { 22 | q->left = temp; 23 | temp->right = q; 24 | } 25 | 26 | void setRight(NODE *p, NODE *temp) 27 | { 28 | temp->right = p->right; 29 | p->right = temp; 30 | p->rthread = 0; 31 | } 32 | 33 | NODE *createNode(int ele) 34 | { 35 | NODE *temp = malloc(sizeof(NODE)); 36 | temp->info = ele; 37 | temp->left = temp->right = NULL; 38 | temp->rthread = 1; 39 | 40 | return temp; 41 | } 42 | void createTree(TREE *pt) 43 | { 44 | int ele, choice; 45 | printf("Enter root info\n"); 46 | scanf("%d", &ele); 47 | pt->root = createNode(ele); 48 | 49 | printf("Do you want to add another node\n"); 50 | scanf("%d", &choice); 51 | 52 | NODE *p, *q, *temp; 53 | while (choice) 54 | { 55 | p = pt->root; 56 | q = NULL; 57 | printf("Enter node info\n"); 58 | scanf("%d", &ele); 59 | 60 | temp = createNode(ele); 61 | 62 | while (p != NULL) 63 | { 64 | q = p; 65 | if (temp->info < p->info) 66 | p = p->left; 67 | else 68 | { 69 | if (p->rthread) 70 | break; 71 | p = p->right; 72 | } 73 | } 74 | if (p == NULL) 75 | setLeft(q, temp); 76 | else 77 | setRight(p, temp); 78 | printf("Do you want to add one more node\n"); 79 | scanf("%d", &choice); 80 | } 81 | } 82 | 83 | void inorder(TREE *pt) 84 | { 85 | NODE *p = pt->root; 86 | NODE *q = NULL; 87 | do 88 | { 89 | q = NULL; 90 | while (p != NULL) 91 | { 92 | q = p; 93 | p = p->left; 94 | } 95 | if (q != NULL) 96 | { 97 | printf("%d ", q->info); 98 | p = q->right; 99 | while (q->rthread && p != NULL) 100 | { 101 | printf("%d ", p->info); 102 | q = p; 103 | p = p->right; 104 | } 105 | } 106 | } while (q != NULL); 107 | } 108 | 109 | int main() 110 | { 111 | TREE tobj; 112 | initTree(&tobj); 113 | createTree(&tobj); 114 | inorder(&tobj); 115 | } -------------------------------------------------------------------------------- /Unit3/9_PriorityQueueUsingHeap/descPq.c: -------------------------------------------------------------------------------- 1 | //Desc Priority Queue 2 | //Bottom Up Heap Construction 3 | #include 4 | #define MAX 50 5 | void bottomUpMaxHeap(int h[MAX], int n); 6 | int maxDelete(int h[MAX], int *n); 7 | int main() 8 | { 9 | int h[MAX], n; 10 | 11 | printf("Enter the no. of elts\n"); 12 | scanf("%d", &n); 13 | 14 | printf("Enter the elts\n"); 15 | for (int i = 0; i < n; i++) 16 | scanf("%d", &h[i]); 17 | 18 | bottomUpMaxHeap(h, n); 19 | printf("Bottom Up heap:\n"); 20 | for (int i = 0; i < n; i++) 21 | { 22 | printf("%d ", h[i]); 23 | } 24 | 25 | int del = maxDelete(h, &n); 26 | printf("Deleted %d\n", del); 27 | printf("Priority Q after deletion:\n"); 28 | for (int i = 0; i < n; i++) 29 | { 30 | printf("%d ", h[i]); 31 | } 32 | } 33 | void bottomUpMaxHeap(int h[MAX], int n) 34 | { 35 | int c, p, key; 36 | 37 | for (int k = n / 2 - 1; k >= 0; k--) 38 | { 39 | p = k; 40 | key = h[p]; 41 | c = 2 * p + 1; 42 | 43 | while (c < n) //Until there is a left child 44 | { 45 | if (c + 1 < n) //If there exist right child 46 | if (h[c + 1] > h[c]) 47 | c = c + 1; 48 | //c will be having index of largest child 49 | if (key < h[c]) 50 | { 51 | h[p] = h[c]; 52 | p = c; 53 | c = 2 * p + 1; 54 | } 55 | else 56 | break; 57 | } 58 | h[p] = key; 59 | } 60 | } 61 | int maxDelete(int h[MAX], int *n) 62 | { 63 | int ele = h[0]; 64 | h[0] = h[*n - 1]; 65 | (*n)--; 66 | bottomUpMaxHeap(h, *n); 67 | return ele; 68 | } -------------------------------------------------------------------------------- /Unit3/bstDictionary.c: -------------------------------------------------------------------------------- 1 | //Implementation of dictionary (Words with their meanings) 2 | #include 3 | #include 4 | #include 5 | #define MAX1 25 6 | #define MAX2 100 7 | 8 | typedef struct node 9 | { 10 | char key[MAX1], value[MAX2]; 11 | struct node *left, *right; 12 | } NODE; 13 | typedef struct tree 14 | { 15 | NODE *root; 16 | } TREE; 17 | 18 | void init(TREE *pt) 19 | { 20 | pt->root = NULL; 21 | } 22 | 23 | NODE *createNode(char word[MAX1], char meaning[MAX2]) 24 | { 25 | NODE *temp = malloc(sizeof(NODE)); 26 | strcpy(temp->key, word); 27 | strcpy(temp->value, meaning); 28 | temp->left = NULL; 29 | temp->right = NULL; 30 | return temp; 31 | } 32 | //BST: Iterative insert 33 | void insert(TREE *pt, char word[MAX1], char meaning[MAX2]) 34 | { 35 | NODE *temp = createNode(word, meaning); 36 | if (pt->root == NULL) 37 | { 38 | pt->root = temp; 39 | return; 40 | } 41 | NODE *p = pt->root; 42 | NODE *q = NULL; 43 | 44 | while (p != NULL) 45 | { 46 | q = p; 47 | if (strcmp(temp->key, p->key) < 0) 48 | p = p->left; 49 | else 50 | p = p->right; 51 | } 52 | if (strcmp(temp->key, q->key) < 0) 53 | q->left = temp; 54 | else 55 | q->right = temp; 56 | } 57 | 58 | //BST: Recursive Insert 59 | NODE *rinsert(NODE *r, NODE *temp) 60 | { 61 | if (r == NULL) 62 | return temp; 63 | if (strcmp(temp->key, r->key) < 0) 64 | r->left = rinsert(r->left, temp); 65 | else 66 | r->right = rinsert(r->right, temp); 67 | 68 | return r; 69 | } 70 | 71 | void recInsert(TREE *pt, char word[MAX1], char meaning[MAX2]) 72 | { 73 | NODE *temp = createNode(word, meaning); 74 | pt->root = rinsert(pt->root, temp); 75 | } 76 | 77 | void inorder(NODE *r) 78 | { 79 | if (r != NULL) 80 | { 81 | inorder(r->left); 82 | printf("%s,%s\n", r->key, r->value); 83 | inorder(r->right); 84 | } 85 | } 86 | 87 | NODE *delNode(NODE *r, char word[MAX1]) 88 | { 89 | NODE *temp = NULL, *p = NULL; 90 | if (r == NULL) 91 | return r; 92 | if (strcmp(word, r->key) < 0) 93 | r->left = delNode(r->left, word); 94 | else if (strcmp(word, r->key) > 0) 95 | r->right = delNode(r->right, word); 96 | else if (r->left == NULL) 97 | { 98 | temp = r->right; 99 | free(r); 100 | return temp; 101 | } 102 | else if (r->right == NULL) 103 | { 104 | temp = r->left; 105 | free(r); 106 | return temp; 107 | } 108 | else 109 | { 110 | p = r->right; 111 | while (p->left != NULL) 112 | p = p->left; 113 | strcpy(r->key, p->key); 114 | r->right = delNode(r->right, p->key); 115 | } 116 | return r; 117 | } 118 | 119 | //BST: Iterative search 120 | int search(NODE *r, char word[MAX1], char meaning[MAX2]) 121 | { 122 | while (r != NULL) 123 | { 124 | if (strcmp(word, r->key) == 0) 125 | { 126 | strcpy(meaning, r->value); 127 | return 1; 128 | } 129 | else if (strcmp(word, r->key) < 0) 130 | r = r->left; 131 | else 132 | r = r->right; 133 | } 134 | return 0; 135 | } 136 | 137 | void destroyNode(NODE *r) 138 | { 139 | if (r != NULL) 140 | { 141 | destroyNode(r->left); 142 | destroyNode(r->right); 143 | // printf("\nFreeing %s",r->key); 144 | free(r); 145 | } 146 | } 147 | 148 | void destroyTree(TREE *pt) 149 | { 150 | if (pt->root != NULL) 151 | { 152 | destroyNode(pt->root); 153 | pt->root = NULL; 154 | } 155 | } 156 | 157 | int main() 158 | { 159 | TREE tobj; 160 | init(&tobj); 161 | char word[MAX1], meaning[MAX2]; 162 | int choice; 163 | do 164 | { 165 | printf("\n1.Insert 2.Delete 3.Display 4.Search\n"); 166 | scanf("%d", &choice); 167 | switch (choice) 168 | { 169 | case 1: 170 | printf("enter the word and its meaning\n"); 171 | scanf("%s", word); 172 | fflush(stdin); 173 | scanf("%[^\n]", meaning); 174 | //insert(&tobj,word,meaning); 175 | recInsert(&tobj, word, meaning); 176 | break; 177 | case 2: 178 | printf("enter the word to be deleted\n"); 179 | scanf("%s", word); 180 | if (search(tobj.root, word, meaning)) 181 | { 182 | tobj.root = delNode(tobj.root, word); 183 | printf("After deletion\n"); 184 | inorder(tobj.root); 185 | } 186 | else 187 | printf("Word not present\n"); 188 | break; 189 | case 3: 190 | inorder(tobj.root); 191 | break; 192 | case 4: 193 | printf("enter the word to be searched\n"); 194 | scanf("%s", word); 195 | if (search(tobj.root, word, meaning)) 196 | printf("%s found and its meaning is %s\n", word, meaning); 197 | else 198 | printf("%s not found\n", word); 199 | break; 200 | 201 | case 5: 202 | break; 203 | } 204 | } while (choice < 5); 205 | destroyTree(&tobj); 206 | } -------------------------------------------------------------------------------- /Unit3/bstMenudriven.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct node 4 | { 5 | int info; 6 | struct node *left, *right; 7 | } NODE; 8 | typedef struct tree 9 | { 10 | NODE *root; 11 | } TREE; 12 | 13 | void init(TREE *pt) 14 | { 15 | pt->root = NULL; 16 | } 17 | 18 | NODE *createNode(int ele) 19 | { 20 | NODE *temp = malloc(sizeof(NODE)); 21 | temp->info = ele; 22 | temp->left = NULL; 23 | temp->right = NULL; 24 | return temp; 25 | } 26 | //BST: Iterative insert 27 | void insert(TREE *pt, int ele) 28 | { 29 | NODE *temp = createNode(ele); 30 | if (pt->root == NULL) 31 | { 32 | pt->root = temp; 33 | return; 34 | } 35 | NODE *p = pt->root; 36 | NODE *q = NULL; 37 | 38 | while (p != NULL) 39 | { 40 | q = p; 41 | if (temp->info < p->info) 42 | p = p->left; 43 | else 44 | p = p->right; 45 | } 46 | if (temp->info < q->info) 47 | q->left = temp; 48 | else 49 | q->right = temp; 50 | } 51 | 52 | //BST: Recursive Insert 53 | NODE *rinsert(NODE *r, NODE *temp) 54 | { 55 | if (r == NULL) 56 | return temp; 57 | if (temp->info < r->info) 58 | r->left = rinsert(r->left, temp); 59 | else 60 | r->right = rinsert(r->right, temp); 61 | 62 | return r; 63 | } 64 | 65 | void recInsert(TREE *pt, int ele) 66 | { 67 | NODE *temp = createNode(ele); 68 | pt->root = rinsert(pt->root, temp); 69 | } 70 | 71 | void inorder(NODE *r) 72 | { 73 | if (r != NULL) 74 | { 75 | inorder(r->left); 76 | printf("%d ", r->info); 77 | inorder(r->right); 78 | } 79 | } 80 | 81 | void preorder(NODE *r) 82 | { 83 | if (r != NULL) 84 | { 85 | printf("%d ", r->info); 86 | preorder(r->left); 87 | preorder(r->right); 88 | } 89 | } 90 | 91 | void postorder(NODE *r) 92 | { 93 | if (r != NULL) 94 | { 95 | postorder(r->left); 96 | postorder(r->right); 97 | printf("%d ", r->info); 98 | } 99 | } 100 | 101 | NODE *delNode(NODE *r, int ele) 102 | { 103 | NODE *temp = NULL, *p = NULL; 104 | if (r == NULL) 105 | return r; 106 | if (ele < r->info) 107 | r->left = delNode(r->left, ele); 108 | else if (ele > r->info) 109 | r->right = delNode(r->right, ele); 110 | else if (r->left == NULL) 111 | { 112 | temp = r->right; 113 | free(r); 114 | return temp; 115 | } 116 | else if (r->right == NULL) 117 | { 118 | temp = r->left; 119 | free(r); 120 | return temp; 121 | } 122 | else 123 | { 124 | p = r->right; 125 | while (p->left != NULL) 126 | p = p->left; 127 | r->info = p->info; 128 | r->right = delNode(r->right, p->info); 129 | } 130 | return r; 131 | } 132 | 133 | //BST: Recursive search 134 | int rsearch(NODE *r, int ele) 135 | { 136 | if (r == NULL) 137 | return 0; 138 | if (ele == r->info) 139 | return 1; 140 | if (ele < r->info) 141 | return rsearch(r->left, ele); 142 | else 143 | return rsearch(r->right, ele); 144 | } 145 | 146 | //BST: Iterative search 147 | int search(NODE *r, int ele) 148 | { 149 | while (r != NULL) 150 | { 151 | if (ele == r->info) 152 | return 1; 153 | else if (ele < r->info) 154 | r = r->left; 155 | else 156 | r = r->right; 157 | } 158 | return 0; 159 | } 160 | //BST: find min ele (Iterative approach) 161 | int min(NODE *r) 162 | { 163 | while (r->left != NULL) 164 | r = r->left; 165 | return r->info; 166 | } 167 | 168 | /* 169 | //BST: find min ele (Recursive approach) 170 | int min(NODE* r) 171 | { 172 | if(r->left == NULL) 173 | return r->info; 174 | return min(r->left); 175 | } 176 | */ 177 | 178 | /* 179 | //BST: find max ele (Iterative approach) 180 | int max(NODE* r) 181 | { 182 | while(r->right != NULL) 183 | r=r->right; 184 | return r->info; 185 | } 186 | */ 187 | 188 | //BST: find max elt (Recursive approach) 189 | int max(NODE *r) 190 | { 191 | if (r->right == NULL) 192 | return r->info; 193 | return max(r->right); 194 | } 195 | int maximum(int a, int b) 196 | { 197 | return a > b ? a : b; 198 | } 199 | int height(NODE *r) 200 | { 201 | if (r == NULL) 202 | return -1; 203 | return maximum(height(r->left), height(r->right)) + 1; 204 | } 205 | void destroyNode(NODE *r) 206 | { 207 | if (r != NULL) 208 | { 209 | destroyNode(r->left); 210 | destroyNode(r->right); 211 | // printf("\nFreeing %d",r->info); 212 | free(r); 213 | } 214 | } 215 | 216 | void destroyTree(TREE *pt) 217 | { 218 | if (pt->root != NULL) 219 | { 220 | destroyNode(pt->root); 221 | pt->root = NULL; 222 | } 223 | } 224 | 225 | int main() 226 | { 227 | TREE tobj; 228 | init(&tobj); 229 | int choice, ele; 230 | do 231 | { 232 | printf("\n1.Insert 2.Recursiveinsert 3.Inorder 4.Preorder 5.Postorder\n"); 233 | printf("6.DeleteNode 7.Srch 8.MinElt 9.MaxElt 10.Tree height\n"); 234 | printf("11.Tree Depth 12.exit\n"); 235 | scanf("%d", &choice); 236 | switch (choice) 237 | { 238 | case 1: 239 | printf("enter an element\n"); 240 | scanf("%d", &ele); 241 | insert(&tobj, ele); 242 | break; 243 | case 2: 244 | printf("enter an element\n"); 245 | scanf("%d", &ele); 246 | recInsert(&tobj, ele); 247 | break; 248 | case 3: 249 | inorder(tobj.root); 250 | break; 251 | case 4: 252 | preorder(tobj.root); 253 | break; 254 | case 5: 255 | postorder(tobj.root); 256 | break; 257 | case 6: 258 | printf("enter the element to be deleted\n"); 259 | scanf("%d", &ele); 260 | if (search(tobj.root, ele)) 261 | { 262 | tobj.root = delNode(tobj.root, ele); 263 | printf("Inorder Traversal after deletion\n"); 264 | inorder(tobj.root); 265 | } 266 | else 267 | printf("Element not present\n"); 268 | break; 269 | case 7: 270 | printf("enter the info to be searched\n"); 271 | scanf("%d", &ele); 272 | /* recursion is on node object,so passing root node's addr. 273 | if addr of tree object is passed Eg: rsearch(&tobj,ele) 274 | then use another recursive helper function 275 | to recurse through the nodes 276 | Eg1: see recInsert and rinsert functions 277 | Eg2: see destroyTree and destroyNode functions 278 | */ 279 | if (rsearch(tobj.root, ele)) 280 | printf("%d found\n", ele); 281 | else 282 | printf("%d not found\n", ele); 283 | break; 284 | case 8: 285 | if (tobj.root == NULL) 286 | printf("Tree is empty\n"); 287 | else 288 | printf("Minimum element = %d\n", min(tobj.root)); 289 | break; 290 | case 9: 291 | if (tobj.root == NULL) 292 | printf("Tree is empty\n"); 293 | else 294 | printf("Maximum element = %d\n", max(tobj.root)); 295 | break; 296 | case 10: 297 | printf("Tree height=%d", height(tobj.root)); 298 | break; 299 | case 11: 300 | printf("Tree Depth=%d", height(tobj.root)); 301 | break; 302 | case 12: 303 | break; 304 | } 305 | } while (choice < 12); 306 | /* 307 | Here address of tree obj is passed to destroyTree function 308 | so one more recursive helper function is used to 309 | recurse through the nodes in a non empty tree 310 | and delete the nodes in the following order 311 | left child (if it exists), right child (if it exists) and then parent 312 | */ 313 | destroyTree(&tobj); 314 | } -------------------------------------------------------------------------------- /Unit4/10_chkForPathUsingDFS.c: -------------------------------------------------------------------------------- 1 | //Chk if there exists a path from source to dest using DFS 2 | //Adjacency Matrix representation 3 | #define MAX 10 4 | #include 5 | 6 | void readGraph(int a[MAX][MAX], int n); 7 | int dfs(int a[MAX][MAX], int n, int visited[MAX], int source, int dest); 8 | 9 | int main() 10 | { 11 | int a[MAX][MAX], n, visited[MAX] = {0}, source, dest; 12 | 13 | printf("Enter the no. of vertices\n"); 14 | scanf("%d", &n); 15 | 16 | printf("Enter the values to adj matrix\n"); 17 | readGraph(a, n); 18 | 19 | printf("Enter the source and dest\n"); 20 | scanf("%d%d", &source, &dest); 21 | 22 | if (dfs(a, n, visited, source, dest)) 23 | printf("There exists path from %d to %d\n", source, dest); 24 | else 25 | printf("There exists no path from %d to %d\n", source, dest); 26 | } 27 | 28 | void readGraph(int a[MAX][MAX], int n) 29 | { 30 | for (int i = 0; i < n; i++) 31 | for (int j = 0; j < n; j++) 32 | scanf("%d", &a[i][j]); 33 | } 34 | 35 | int dfs(int a[MAX][MAX], int n, int visited[MAX], int source, int dest) 36 | { 37 | visited[source] = 1; 38 | // printf("%d ",source); 39 | 40 | for (int i = 0; i < n; i++) 41 | { 42 | if (a[source][i] && visited[i] == 0) 43 | { 44 | if (dest == i || dfs(a, n, visited, i, dest)) 45 | return 1; 46 | } 47 | } 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Unit4/1_graphRepresentation.c: -------------------------------------------------------------------------------- 1 | //Graph Representation: Adjacency Matrix 2 | #include 3 | #define MAX 5 4 | void readGraph(int a[MAX][MAX], int n); 5 | void printGraph(int a[MAX][MAX], int n); 6 | int main() 7 | { 8 | int a[MAX][MAX], n; 9 | 10 | printf("Enter the no. of vertices\n"); 11 | scanf("%d", &n); 12 | 13 | printf("Enter the values to adj matrix\n"); 14 | readGraph(a, n); 15 | printf("Graph displayed as adj matrix\n"); 16 | printGraph(a, n); 17 | } 18 | void readGraph(int a[MAX][MAX], int n) 19 | { 20 | for (int i = 0; i < n; i++) 21 | for (int j = 0; j < n; j++) 22 | scanf("%d", &a[i][j]); 23 | } 24 | void printGraph(int a[MAX][MAX], int n) 25 | { 26 | for (int i = 0; i < n; i++) 27 | { 28 | for (int j = 0; j < n; j++) 29 | printf("%d ", a[i][j]); 30 | printf("\n"); 31 | } 32 | } 33 | 34 | //Graph Representation: Adjacency Matrix 35 | #include 36 | #define MAX 5 37 | void readGraph(int a[MAX][MAX]); 38 | void printGraph(int a[MAX][MAX], int n); 39 | int main() 40 | { 41 | int a[MAX][MAX] = {0}; //initialize all entries in adj matrix to zero 42 | int n; 43 | 44 | printf("Enter the no. of vertices\n"); 45 | scanf("%d", &n); 46 | 47 | printf("Enter the values to adj matrix\n"); 48 | readGraph(a); 49 | 50 | printGraph(a, n); 51 | } 52 | void readGraph(int a[MAX][MAX]) 53 | { 54 | int s, d; 55 | printf("Enter -1 -1 to stop adding edges\n"); 56 | while (1) 57 | { 58 | printf("Enter the source and dest vertex for the edge to be added\n"); 59 | scanf("%d%d", &s, &d); 60 | if (s == -1 && d == -1) 61 | break; 62 | a[s][d] = 1; 63 | } 64 | } 65 | void printGraph(int a[MAX][MAX], int n) 66 | { 67 | for (int i = 0; i < n; i++) 68 | { 69 | for (int j = 0; j < n; j++) 70 | printf("%d ", a[i][j]); 71 | printf("\n"); 72 | } 73 | } 74 | 75 | //Graph Representation: Adjacency List 76 | #include 77 | #define MAX 10 78 | #include 79 | typedef struct node 80 | { 81 | int info; 82 | struct node *next; 83 | } NODE; 84 | 85 | void createAdjList(NODE *V[MAX]); 86 | void append(NODE *V[MAX], int source, int dest); 87 | void display(NODE *V[MAX], int n); 88 | 89 | int main() 90 | { 91 | int n; 92 | printf("Enter the no. of nodes\n"); 93 | scanf("%d", &n); 94 | 95 | NODE *V[MAX] = {NULL}; 96 | createAdjList(V); 97 | display(V, n); 98 | } 99 | void createAdjList(NODE *V[MAX]) 100 | { 101 | int choice; 102 | int source, dest; 103 | do 104 | { 105 | printf("Enter the source and dest vertex of the edge to be added\n"); 106 | scanf("%d%d", &source, &dest); 107 | append(V, source, dest); 108 | append(V, dest, source); //for undirected graph 109 | 110 | printf("Do you want to add one more edge\n"); 111 | scanf("%d", &choice); 112 | } while (choice); 113 | } 114 | void append(NODE *V[MAX], int source, int dest) 115 | { 116 | NODE *temp = malloc(sizeof(NODE)); 117 | temp->info = dest; 118 | temp->next = NULL; 119 | 120 | //for insert Last 121 | if (V[source] == NULL) 122 | { 123 | V[source] = temp; 124 | return; 125 | } 126 | 127 | NODE *p = V[source]; 128 | while (p->next != NULL) 129 | p = p->next; 130 | p->next = temp; 131 | //insertLast ends here 132 | 133 | //insert node at front 134 | // temp->next=V[source]; 135 | // V[source]=temp; 136 | //insertFront ends here 137 | } 138 | void display(NODE *V[MAX], int n) 139 | { 140 | NODE *p; 141 | for (int i = 0; i < n; i++) 142 | { 143 | p = V[i]; 144 | printf("V[%d]->", i); 145 | while (p != NULL) 146 | { 147 | printf("%d->", p->info); 148 | p = p->next; 149 | } 150 | printf("NULL\n"); 151 | } 152 | } 153 | void destroyList(NODE *V[MAX], int n) 154 | { 155 | NODE *p, *q; 156 | for (int i = 0; i < n; i++) 157 | { 158 | p = V[i]; 159 | while (p != NULL) 160 | { 161 | q = p; 162 | p = p->next; 163 | free(q); 164 | } 165 | V[i] = NULL; 166 | } 167 | } -------------------------------------------------------------------------------- /Unit4/2_bfsTraversal.c: -------------------------------------------------------------------------------- 1 | //Pgm1 2 | //Visits all the vertices from the source in a connected graph using BFS 3 | //i.e., print all vertices reachable from the given source 4 | //Adjacency Matrix representation 5 | #define MAX 10 6 | #include 7 | 8 | void readGraph(int a[MAX][MAX],int n); 9 | void bfs(int a[MAX][MAX],int n,int visited[MAX],int source); 10 | 11 | int main() 12 | { 13 | int a[MAX][MAX],n,visited[MAX]={0},source; 14 | 15 | printf("Enter the no. of vertices\n"); 16 | scanf("%d",&n); 17 | 18 | printf("Enter the values to adj matrix\n"); 19 | readGraph(a,n); 20 | 21 | printf("Enter the source\n"); 22 | scanf("%d",&source); 23 | 24 | printf("BFS traversal or vertices reachable from %d\n",source); 25 | bfs(a,n,visited,source); 26 | } 27 | 28 | void readGraph(int a[MAX][MAX],int n) 29 | { 30 | for(int i=0;i 67 | typedef struct queue 68 | { 69 | int q[MAX]; 70 | int f,r; 71 | }QUEUE; 72 | void readGraph(int a[MAX][MAX],int n); 73 | void initQueue(QUEUE *pq); 74 | int isEmpty(QUEUE *pq); 75 | void enqueue(QUEUE *pq,int ele); 76 | int dequeue(QUEUE *pq); 77 | void bfs(int a[MAX][MAX],int n,int visited[MAX],int source); 78 | 79 | int main() 80 | { 81 | int a[MAX][MAX],n,visited[MAX]={0},source; 82 | 83 | printf("Enter the no. of vertices\n"); 84 | scanf("%d",&n); 85 | 86 | printf("Enter the values to adj matrix\n"); 87 | readGraph(a,n); 88 | 89 | printf("Enter the source\n"); 90 | scanf("%d",&source); 91 | 92 | printf("BFS traversal or vertices reachable from %d\n",source); 93 | bfs(a,n,visited,source); 94 | } 95 | 96 | void readGraph(int a[MAX][MAX],int n) 97 | { 98 | for(int i=0;if=0; 105 | pq->r=-1; 106 | } 107 | int isEmpty(QUEUE *pq) 108 | { 109 | return pq->f > pq->r; 110 | } 111 | void enqueue(QUEUE *pq,int ele) 112 | { 113 | pq->r++; 114 | pq->q[pq->r]=ele; 115 | } 116 | 117 | int dequeue(QUEUE *pq) 118 | { 119 | int ele=pq->q[pq->f]; 120 | pq->f++; 121 | return ele; 122 | } 123 | void bfs(int a[MAX][MAX],int n,int visited[MAX],int source) 124 | { 125 | QUEUE q; //queue 126 | initQueue(&q); 127 | 128 | enqueue(&q,source); //enqueue source 129 | visited[source]=1; //mark source visited 130 | 131 | int v; 132 | while(!isEmpty(&q)) //until q is not empty 133 | { 134 | v=dequeue(&q); //dequeue 135 | printf("%d ",v); 136 | 137 | for(int i=0;i 156 | 157 | void readGraph(int a[MAX][MAX],int n); 158 | void bfs(int a[MAX][MAX],int n,int visited[MAX],int source); 159 | 160 | int main() 161 | { 162 | int a[MAX][MAX],n,visited[MAX]={0},source; 163 | 164 | printf("Enter the no. of vertices\n"); 165 | scanf("%d",&n); 166 | 167 | printf("Enter the values to adj matrix\n"); 168 | readGraph(a,n); 169 | 170 | printf("Enter the source\n"); 171 | scanf("%d",&source); 172 | 173 | printf("BFS traversal\n"); 174 | bfs(a,n,visited,source); 175 | for(int i=0;i 221 | #include 222 | #define MAX 10 223 | typedef struct node 224 | { 225 | int info; 226 | struct node *next; 227 | }NODE; 228 | 229 | void createAdjList(NODE *V[MAX]); 230 | void append(NODE *V[MAX],int source,int dest); 231 | void bfs(NODE* V[MAX],int n,int visited[MAX],int source); 232 | void destroyList(NODE *V[MAX],int n); 233 | 234 | int main() 235 | { 236 | int n; 237 | printf("Enter the no. of nodes\n"); 238 | scanf("%d",&n); 239 | 240 | NODE *V[MAX]={NULL}; 241 | createAdjList(V); 242 | 243 | int src,visited[MAX]={0}; 244 | printf("Enter the source vertex\n"); 245 | scanf("%d",&src); 246 | 247 | printf("BFS Traversal: Nodes reachable from %d\n",src); 248 | bfs(V,n,visited,src); 249 | destroyList(V,n); 250 | } 251 | 252 | void bfs(NODE* V[MAX],int n,int visited[MAX],int source) 253 | { 254 | int q[n]; //Queue 255 | int f=0,r=-1; //InitQueue 256 | q[++r]=source; 257 | visited[source]=1; 258 | int v; 259 | while(f<=r) 260 | { 261 | v=q[f++]; 262 | printf("%d ",v); 263 | 264 | NODE *p=V[v]; 265 | while(p!=NULL) 266 | { 267 | if(!visited[p->info]) 268 | { 269 | q[++r]=p->info; 270 | visited[p->info]=1; 271 | } 272 | p=p->next; 273 | } 274 | } 275 | } 276 | void createAdjList(NODE *V[MAX]) 277 | { 278 | int choice; 279 | int source,dest; 280 | do{ 281 | printf("Enter the source and dest vertex of the edge to be added\n"); 282 | scanf("%d%d",&source,&dest); 283 | append(V,source,dest); 284 | append(V,dest,source); //undirected graph 285 | 286 | printf("Do you want to add one more edge\n"); 287 | scanf("%d",&choice); 288 | }while(choice); 289 | } 290 | void append(NODE *V[MAX],int source,int dest) 291 | { 292 | NODE *temp=malloc(sizeof(NODE)); 293 | temp->info=dest; 294 | temp->next=NULL; 295 | 296 | //insert Last 297 | if(V[source]==NULL) 298 | { 299 | V[source]=temp; 300 | return; 301 | } 302 | 303 | NODE *p=V[source]; 304 | while(p->next!=NULL) 305 | p=p->next; 306 | p->next=temp; 307 | //insertLast ends here 308 | 309 | //insert node at front 310 | // temp->next=V[source]; 311 | // V[source]=temp; 312 | //insertFront ends here 313 | } 314 | 315 | void destroyList(NODE *V[MAX],int n) 316 | { 317 | NODE *p,*q; 318 | for(int i=0;inext; 325 | // printf("Freeing %d\n",q->info); 326 | free(q); 327 | } 328 | V[i]=NULL; 329 | } 330 | } 331 | 332 | 333 | 334 | //Pgm5 335 | //If the graph is disconnected 336 | //start the bfs traversal from one of the unvisited vertices 337 | //adjacency list representation 338 | #include 339 | #include 340 | #define MAX 10 341 | typedef struct node 342 | { 343 | int info; 344 | struct node *next; 345 | }NODE; 346 | 347 | void createAdjList(NODE *V[MAX]); 348 | void append(NODE *V[MAX],int source,int dest); 349 | void bfs(NODE* V[MAX],int n,int visited[MAX],int source); 350 | void destroyList(NODE *V[MAX],int n); 351 | 352 | int main() 353 | { 354 | int n; 355 | printf("Enter the no. of nodes\n"); 356 | scanf("%d",&n); 357 | 358 | NODE *V[MAX]={NULL}; 359 | createAdjList(V); 360 | 361 | int src,visited[MAX]={0}; 362 | printf("Enter the source vertex\n"); 363 | scanf("%d",&src); 364 | 365 | printf("BFS Traversal\n"); 366 | bfs(V,n,visited,src); 367 | for(int i=0;iinfo]) 394 | { 395 | q[++r]=p->info; 396 | visited[p->info]=1; 397 | } 398 | p=p->next; 399 | } 400 | } 401 | } 402 | void createAdjList(NODE *V[MAX]) 403 | { 404 | int choice; 405 | int source,dest; 406 | do{ 407 | printf("Enter the source and dest vertex of the edge to be added\n"); 408 | scanf("%d%d",&source,&dest); 409 | append(V,source,dest); 410 | append(V,dest,source); //undirected graph 411 | 412 | printf("Do you want to add one more edge\n"); 413 | scanf("%d",&choice); 414 | }while(choice); 415 | } 416 | void append(NODE *V[MAX],int source,int dest) 417 | { 418 | NODE *temp=malloc(sizeof(NODE)); 419 | temp->info=dest; 420 | temp->next=NULL; 421 | 422 | //insert Last 423 | if(V[source]==NULL) 424 | { 425 | V[source]=temp; 426 | return; 427 | } 428 | 429 | NODE *p=V[source]; 430 | while(p->next!=NULL) 431 | p=p->next; 432 | p->next=temp; 433 | //insertLast ends here 434 | 435 | //insert node at front 436 | // temp->next=V[source]; 437 | // V[source]=temp; 438 | //insertFront ends here 439 | } 440 | 441 | void destroyList(NODE *V[MAX],int n) 442 | { 443 | NODE *p,*q; 444 | for(int i=0;inext; 451 | // printf("Freeing %d\n",q->info); 452 | free(q); 453 | } 454 | V[i]=NULL; 455 | } 456 | } 457 | 458 | //Pgm6: same as Pgm5, but queue is part of a structure 459 | //If the graph is disconnected 460 | //start the bfs traversal from one of the unvisited vertices 461 | //adjacency list representation 462 | #include 463 | #include 464 | #define MAX 10 465 | typedef struct node 466 | { 467 | int info; 468 | struct node *next; 469 | }NODE; 470 | 471 | typedef struct queue 472 | { 473 | int q[MAX]; 474 | int f,r; 475 | }QUEUE; 476 | 477 | void initQueue(QUEUE *pq); 478 | int isEmpty(QUEUE *pq); 479 | void enqueue(QUEUE *pq,int ele); 480 | int dequeue(QUEUE *pq); 481 | void createAdjList(NODE *V[MAX]); 482 | void append(NODE *V[MAX],int source,int dest); 483 | void bfs(NODE* V[MAX],int visited[MAX],int source); 484 | void destroyList(NODE *V[MAX],int n); 485 | 486 | int main() 487 | { 488 | int n; 489 | printf("Enter the no. of nodes\n"); 490 | scanf("%d",&n); 491 | 492 | NODE *V[MAX]={NULL}; 493 | createAdjList(V); 494 | 495 | int src,visited[MAX]={0}; 496 | printf("Enter the source vertex\n"); 497 | scanf("%d",&src); 498 | 499 | printf("BFS Traversal\n"); 500 | bfs(V,visited,src); 501 | for(int i=0;if=0; 514 | pq->r=-1; 515 | } 516 | int isEmpty(QUEUE *pq) 517 | { 518 | return pq->f > pq->r; 519 | } 520 | void enqueue(QUEUE *pq,int ele) 521 | { 522 | pq->r++; 523 | pq->q[pq->r]=ele; 524 | } 525 | 526 | int dequeue(QUEUE *pq) 527 | { 528 | int ele=pq->q[pq->f]; 529 | pq->f++; 530 | return ele; 531 | } 532 | 533 | void bfs(NODE* V[MAX],int visited[MAX],int source) 534 | { 535 | QUEUE q; //Queue 536 | initQueue(&q); //InitQueue 537 | 538 | enqueue(&q,source); 539 | visited[source]=1; 540 | int v; 541 | while(!isEmpty(&q)) 542 | { 543 | v=dequeue(&q); 544 | printf("%d ",v); 545 | 546 | NODE *p=V[v]; 547 | while(p!=NULL) 548 | { 549 | if(!visited[p->info]) 550 | { 551 | enqueue(&q,p->info); 552 | visited[p->info]=1; 553 | } 554 | p=p->next; 555 | } 556 | } 557 | } 558 | void createAdjList(NODE *V[MAX]) 559 | { 560 | int choice; 561 | int source,dest; 562 | do{ 563 | printf("Enter the source and dest vertex of the edge to be added\n"); 564 | scanf("%d%d",&source,&dest); 565 | append(V,source,dest); 566 | append(V,dest,source); //undirected graph 567 | 568 | printf("Do you want to add one more edge\n"); 569 | scanf("%d",&choice); 570 | }while(choice); 571 | } 572 | void append(NODE *V[MAX],int source,int dest) 573 | { 574 | NODE *temp=malloc(sizeof(NODE)); 575 | temp->info=dest; 576 | temp->next=NULL; 577 | 578 | //insert Last 579 | if(V[source]==NULL) 580 | { 581 | V[source]=temp; 582 | return; 583 | } 584 | 585 | NODE *p=V[source]; 586 | while(p->next!=NULL) 587 | p=p->next; 588 | p->next=temp; 589 | //insertLast ends here 590 | 591 | //insert node at front 592 | // temp->next=V[source]; 593 | // V[source]=temp; 594 | //insertFront ends here 595 | } 596 | 597 | void destroyList(NODE *V[MAX],int n) 598 | { 599 | NODE *p,*q; 600 | for(int i=0;inext; 607 | // printf("Freeing %d\n",q->info); 608 | free(q); 609 | } 610 | V[i]=NULL; 611 | } 612 | } -------------------------------------------------------------------------------- /Unit4/3_UndirectedGraphConnectivityUsingBFS.c: -------------------------------------------------------------------------------- 1 | //Pgm1 2 | //Determine if the undirected graph is connected or not using BFS 3 | //Adjacency Matrix representation 4 | #define MAX 10 5 | #include 6 | 7 | void readGraph(int a[MAX][MAX], int n); 8 | void bfs(int a[MAX][MAX], int n, int visited[MAX], int source); 9 | int connected(int a[MAX][MAX], int n); 10 | int main() 11 | { 12 | int a[MAX][MAX], n; 13 | 14 | printf("Enter the no. of vertices\n"); 15 | scanf("%d", &n); 16 | 17 | printf("Enter the values to adj matrix\n"); 18 | readGraph(a, n); 19 | 20 | if (connected(a, n)) 21 | printf("Graph is connected\n"); 22 | else 23 | printf("Graph is disconnected\n"); 24 | } 25 | 26 | void readGraph(int a[MAX][MAX], int n) 27 | { 28 | for (int i = 0; i < n; i++) 29 | for (int j = 0; j < n; j++) 30 | scanf("%d", &a[i][j]); 31 | } 32 | void bfs(int a[MAX][MAX], int n, int visited[MAX], int source) 33 | { 34 | int q[n]; //Queue 35 | int f = 0, r = -1; 36 | 37 | q[++r] = source; 38 | visited[source] = 1; 39 | 40 | int v; 41 | while (f <= r) //until q is not empty 42 | { 43 | v = q[f++]; 44 | // printf("%d ",v); 45 | 46 | for (int i = 0; i < n; i++) 47 | { 48 | if (a[v][i] && visited[i] == 0) 49 | { 50 | q[++r] = i; 51 | visited[i] = 1; 52 | } 53 | } 54 | } 55 | } 56 | int connected(int a[MAX][MAX], int n) 57 | { 58 | int visited[MAX] = {0}, source = 0; 59 | bfs(a, n, visited, source); 60 | for (int i = 0; i < n; i++) 61 | { 62 | if (visited[i] == 0) 63 | { 64 | return 0; 65 | } 66 | } 67 | return 1; 68 | } 69 | 70 | //Pgm2 71 | //Print the no. of connected components 72 | #define MAX 10 73 | #include 74 | 75 | void readGraph(int a[MAX][MAX], int n); 76 | void bfs(int a[MAX][MAX], int n, int visited[MAX], int source); 77 | int connected(int a[MAX][MAX], int n); 78 | int main() 79 | { 80 | int a[MAX][MAX], n; 81 | 82 | printf("Enter the no. of vertices\n"); 83 | scanf("%d", &n); 84 | 85 | printf("Enter the values to adj matrix\n"); 86 | readGraph(a, n); 87 | 88 | int res = connected(a, n); 89 | printf("No.of connected components is %d\n", res); 90 | } 91 | 92 | void readGraph(int a[MAX][MAX], int n) 93 | { 94 | for (int i = 0; i < n; i++) 95 | for (int j = 0; j < n; j++) 96 | scanf("%d", &a[i][j]); 97 | } 98 | void bfs(int a[MAX][MAX], int n, int visited[MAX], int source) 99 | { 100 | int q[n]; //Queue 101 | int f = 0, r = -1; 102 | 103 | q[++r] = source; 104 | visited[source] = 1; 105 | 106 | int v; 107 | while (f <= r) //until q is not empty 108 | { 109 | v = q[f++]; 110 | // printf("%d ",v); 111 | 112 | for (int i = 0; i < n; i++) 113 | { 114 | if (a[v][i] && visited[i] == 0) 115 | { 116 | q[++r] = i; 117 | visited[i] = 1; 118 | } 119 | } 120 | } 121 | } 122 | int connected(int a[MAX][MAX], int n) 123 | { 124 | int visited[MAX] = {0}; 125 | int con = 1; 126 | bfs(a, n, visited, 0); 127 | for (int i = 0; i < n; i++) 128 | { 129 | if (visited[i] == 0) 130 | { 131 | printf("\n"); 132 | con++; 133 | bfs(a, n, visited, i); 134 | } 135 | } 136 | return con; 137 | } -------------------------------------------------------------------------------- /Unit4/4_DigraphConnectivityUsingBFS.c: -------------------------------------------------------------------------------- 1 | //Determine if the digraph is strongly or weakly connected 2 | //or not connected using BFS 3 | //Adjacency Matrix representation 4 | /* 5 | Strongly conctd 6 | 0 1 1 0 7 | 1 0 0 0 8 | 0 0 0 1 9 | 1 0 0 0 10 | Weakly conctd 11 | 0 1 0 0 12 | 0 0 0 0 13 | 0 0 0 1 14 | 1 0 0 0 15 | Disconctd 16 | 0 0 1 0 17 | 0 0 0 0 18 | 0 0 0 1 19 | 1 0 0 0 20 | */ 21 | #define MAX 10 22 | #include 23 | 24 | void readGraph(int a[MAX][MAX], int n); 25 | void bfs(int a[MAX][MAX], int n, int visited[MAX], int source); 26 | int strongConnected(int a[MAX][MAX], int n); 27 | int weakConnected(int a[MAX][MAX], int n); 28 | void removeEdgeDirection(int a[MAX][MAX], int b[MAX][MAX], int n); 29 | int main() 30 | { 31 | int a[MAX][MAX], n; 32 | 33 | printf("Enter the no. of vertices\n"); 34 | scanf("%d", &n); 35 | 36 | printf("Enter the values to adj matrix\n"); 37 | readGraph(a, n); 38 | 39 | if (strongConnected(a, n)) 40 | printf("Graph is strongly connected\n"); 41 | else 42 | { 43 | int b[MAX][MAX] = {0}; 44 | removeEdgeDirection(a, b, n); 45 | if (weakConnected(b, n)) 46 | printf("Graph is weakly connected\n"); 47 | else 48 | printf("Graph is disconnected\n"); 49 | } 50 | } 51 | 52 | void readGraph(int a[MAX][MAX], int n) 53 | { 54 | for (int i = 0; i < n; i++) 55 | for (int j = 0; j < n; j++) 56 | scanf("%d", &a[i][j]); 57 | } 58 | void bfs(int a[MAX][MAX], int n, int visited[MAX], int source) 59 | { 60 | int q[n]; //Queue 61 | int f = 0, r = -1; 62 | 63 | q[++r] = source; 64 | visited[source] = 1; 65 | 66 | int v; 67 | while (f <= r) //until q is not empty 68 | { 69 | v = q[f++]; 70 | // printf("%d ",v); 71 | 72 | for (int i = 0; i < n; i++) 73 | { 74 | if (a[v][i] && visited[i] == 0) 75 | { 76 | q[++r] = i; 77 | visited[i] = 1; 78 | } 79 | } 80 | } 81 | } 82 | int strongConnected(int a[MAX][MAX], int n) 83 | { 84 | int visited[MAX] = {0}; 85 | 86 | for (int v = 0; v < n; v++) 87 | { 88 | for (int j = 0; j < n; j++) 89 | visited[j] = 0; 90 | bfs(a, n, visited, v); 91 | for (int i = 0; i < n; i++) 92 | { 93 | if (visited[i] == 0) 94 | { 95 | return 0; 96 | } 97 | } 98 | } 99 | return 1; 100 | } 101 | 102 | void removeEdgeDirection(int a[MAX][MAX], int b[MAX][MAX], int n) 103 | { 104 | int i, j; 105 | for (i = 0; i < n; i++) 106 | { 107 | for (j = 0; j < n; j++) 108 | { 109 | if (a[i][j] == 1) 110 | b[i][j] = b[j][i] = 1; 111 | } 112 | } 113 | } 114 | int weakConnected(int b[MAX][MAX], int n) 115 | { 116 | int visited[MAX] = {0}; 117 | 118 | bfs(b, n, visited, 0); 119 | for (int i = 0; i < n; i++) 120 | { 121 | if (visited[i] == 0) 122 | { 123 | return 0; 124 | } 125 | } 126 | return 1; 127 | } -------------------------------------------------------------------------------- /Unit4/5_chkForPathUsingBFS.c: -------------------------------------------------------------------------------- 1 | //Chk if there exists a path from source to dest using BFS 2 | //Adjacency Matrix representation 3 | #define MAX 10 4 | #include 5 | 6 | void readGraph(int a[MAX][MAX], int n); 7 | int bfs(int a[MAX][MAX], int n, int visited[MAX], int source, int dest); 8 | 9 | int main() 10 | { 11 | int a[MAX][MAX], n, visited[MAX] = {0}, source, dest; 12 | 13 | printf("Enter the no. of vertices\n"); 14 | scanf("%d", &n); 15 | 16 | printf("Enter the values to adj matrix\n"); 17 | readGraph(a, n); 18 | 19 | printf("Enter the source and dest\n"); 20 | scanf("%d%d", &source, &dest); 21 | 22 | if (bfs(a, n, visited, source, dest)) 23 | printf("There exists path from %d to %d\n", source, dest); 24 | else 25 | printf("There exists no path from %d to %d\n", source, dest); 26 | } 27 | 28 | void readGraph(int a[MAX][MAX], int n) 29 | { 30 | for (int i = 0; i < n; i++) 31 | for (int j = 0; j < n; j++) 32 | scanf("%d", &a[i][j]); 33 | } 34 | int bfs(int a[MAX][MAX], int n, int visited[MAX], int source, int dest) 35 | { 36 | int q[n]; //Queue 37 | int f = 0, r = -1; 38 | 39 | q[++r] = source; 40 | visited[source] = 1; 41 | 42 | int v; 43 | while (f <= r) //until q is not empty 44 | { 45 | v = q[f++]; 46 | // printf("%d ",v); 47 | 48 | for (int i = 0; i < n; i++) 49 | { 50 | if (a[v][i] && visited[i] == 0) 51 | { 52 | if (dest == i) 53 | return 1; 54 | q[++r] = i; 55 | visited[i] = 1; 56 | } 57 | } 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Unit4/6_dfsTraversal_Iterative.c: -------------------------------------------------------------------------------- 1 | //Pgm1 2 | //DFS (Iterative): print all vertices reachable from source 3 | //Adjacency Matrix representation 4 | #define MAX 10 5 | #include 6 | 7 | void readGraph(int a[MAX][MAX], int n); 8 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source); 9 | 10 | int main() 11 | { 12 | int a[MAX][MAX], n, visited[MAX] = {0}, source; 13 | 14 | printf("Enter the no. of vertices\n"); 15 | scanf("%d", &n); 16 | 17 | printf("Enter the values to adj matrix\n"); 18 | readGraph(a, n); 19 | 20 | printf("Enter the source\n"); 21 | scanf("%d", &source); 22 | 23 | printf("DFS traversal:Vertices reachable from %d\n", source); 24 | dfs(a, n, visited, source); 25 | } 26 | 27 | void readGraph(int a[MAX][MAX], int n) 28 | { 29 | for (int i = 0; i < n; i++) 30 | for (int j = 0; j < n; j++) 31 | scanf("%d", &a[i][j]); 32 | } 33 | 34 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source) 35 | { 36 | int s[n]; //stack created 37 | int top = -1; //init stack 38 | 39 | int flag = 0, i; 40 | s[++top] = source; 41 | visited[source] = 1; 42 | printf("%d ", source); 43 | 44 | while (top != -1) //until stack is not empty 45 | { 46 | flag = 0; 47 | source = s[top]; //stacktop or peek opn 48 | for (i = 0; i < n; i++) 49 | { 50 | if (a[source][i] && !visited[i]) 51 | { 52 | flag = 1; 53 | break; 54 | } 55 | } 56 | if (flag) 57 | { 58 | s[++top] = i; 59 | visited[i] = 1; 60 | printf("%d ", i); 61 | } 62 | else 63 | top--; 64 | } 65 | } 66 | 67 | //Pgm2 68 | //print all vertices in the graph using DFS traversal (Iterative) 69 | //Adjacency Matrix representation 70 | #define MAX 10 71 | #include 72 | 73 | void readGraph(int a[MAX][MAX], int n); 74 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source); 75 | 76 | int main() 77 | { 78 | int a[MAX][MAX], n, visited[MAX] = {0}, source; 79 | 80 | printf("Enter the no. of vertices\n"); 81 | scanf("%d", &n); 82 | 83 | printf("Enter the values to adj matrix\n"); 84 | readGraph(a, n); 85 | 86 | printf("Enter the source\n"); 87 | scanf("%d", &source); 88 | 89 | printf("DFS traversal\n"); 90 | dfs(a, n, visited, source); 91 | for (int i = 0; i < n; i++) 92 | { 93 | if (visited[i] == 0) 94 | { 95 | printf("\n"); 96 | dfs(a, n, visited, i); 97 | } 98 | } 99 | } 100 | 101 | void readGraph(int a[MAX][MAX], int n) 102 | { 103 | for (int i = 0; i < n; i++) 104 | for (int j = 0; j < n; j++) 105 | scanf("%d", &a[i][j]); 106 | } 107 | 108 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source) 109 | { 110 | int s[n]; //stack created 111 | int top = -1; //init stack 112 | 113 | int flag = 0, i; 114 | s[++top] = source; 115 | visited[source] = 1; 116 | printf("%d ", source); 117 | 118 | while (top != -1) //until stack is not empty 119 | { 120 | flag = 0; 121 | source = s[top]; //stacktop or peek opn 122 | for (i = 0; i < n; i++) 123 | { 124 | if (a[source][i] && !visited[i]) 125 | { 126 | flag = 1; 127 | break; 128 | } 129 | } 130 | if (flag) 131 | { 132 | s[++top] = i; 133 | visited[i] = 1; 134 | printf("%d ", i); 135 | } 136 | else 137 | top--; 138 | } 139 | } -------------------------------------------------------------------------------- /Unit4/7_dfsTraversalRecursive.c: -------------------------------------------------------------------------------- 1 | //Pgm1 2 | //DFS (Recursive): print all vertices reachable from source 3 | //Adjacency Matrix representation 4 | 5 | #define MAX 10 6 | #include 7 | 8 | void readGraph(int a[MAX][MAX], int n); 9 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source); 10 | 11 | int main() 12 | { 13 | int a[MAX][MAX], n, visited[MAX] = {0}, source; 14 | 15 | printf("Enter the no. of vertices\n"); 16 | scanf("%d", &n); 17 | 18 | printf("Enter the values to adj matrix\n"); 19 | readGraph(a, n); 20 | 21 | printf("Enter the source\n"); 22 | scanf("%d", &source); 23 | 24 | printf("DFS traversal\n"); 25 | dfs(a, n, visited, source); 26 | } 27 | 28 | void readGraph(int a[MAX][MAX], int n) 29 | { 30 | for (int i = 0; i < n; i++) 31 | for (int j = 0; j < n; j++) 32 | scanf("%d", &a[i][j]); 33 | } 34 | 35 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source) 36 | { 37 | visited[source] = 1; 38 | printf("%d ", source); 39 | 40 | for (int i = 0; i < n; i++) 41 | { 42 | if (a[source][i] && visited[i] == 0) 43 | dfs(a, n, visited, i); 44 | } 45 | } 46 | 47 | //Pgm2 48 | //DFS (Recursive): print all vertices in the graph using DFS traversal 49 | //Adjacency Matrix representation 50 | 51 | #define MAX 10 52 | #include 53 | 54 | void readGraph(int a[MAX][MAX], int n); 55 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source); 56 | 57 | int main() 58 | { 59 | int a[MAX][MAX], n, visited[MAX] = {0}, source; 60 | 61 | printf("Enter the no. of vertices\n"); 62 | scanf("%d", &n); 63 | 64 | printf("Enter the values to adj matrix\n"); 65 | readGraph(a, n); 66 | 67 | printf("Enter the source\n"); 68 | scanf("%d", &source); 69 | 70 | printf("DFS traversal\n"); 71 | dfs(a, n, visited, source); 72 | for (int i = 0; i < n; i++) 73 | { 74 | if (visited[i] == 0) 75 | { 76 | printf("\n"); 77 | dfs(a, n, visited, i); 78 | } 79 | } 80 | } 81 | 82 | void readGraph(int a[MAX][MAX], int n) 83 | { 84 | for (int i = 0; i < n; i++) 85 | for (int j = 0; j < n; j++) 86 | scanf("%d", &a[i][j]); 87 | } 88 | 89 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source) 90 | { 91 | visited[source] = 1; 92 | printf("%d ", source); 93 | 94 | for (int i = 0; i < n; i++) 95 | { 96 | if (a[source][i] && visited[i] == 0) 97 | dfs(a, n, visited, i); 98 | } 99 | } 100 | 101 | //Pgm3 102 | //print all vertices reachable from the given source using DFS 103 | //adjacency list representation 104 | #include 105 | #include 106 | #define MAX 10 107 | typedef struct node 108 | { 109 | int info; 110 | struct node *next; 111 | } NODE; 112 | void createAdjList(NODE *V[MAX]); 113 | void append(NODE *V[MAX], int source, int dest); 114 | void dfs(NODE *V[MAX], int visited[MAX], int source); 115 | void destroyList(NODE *V[MAX], int n); 116 | 117 | int main() 118 | { 119 | int n; 120 | printf("Enter the no. of nodes\n"); 121 | scanf("%d", &n); 122 | 123 | NODE *V[MAX] = {NULL}; 124 | createAdjList(V); 125 | 126 | int src, visited[MAX] = {0}; 127 | printf("Enter the source vertex\n"); 128 | scanf("%d", &src); 129 | 130 | printf("DFS Traversal: Nodes reachable from %d\n", src); 131 | dfs(V, visited, src); 132 | destroyList(V, n); 133 | } 134 | void createAdjList(NODE *V[MAX]) 135 | { 136 | int choice; 137 | int source, dest; 138 | do 139 | { 140 | printf("Enter the source and dest vertex of the edge to be added\n"); 141 | scanf("%d%d", &source, &dest); 142 | append(V, source, dest); 143 | append(V, dest, source); //undirected graph 144 | 145 | printf("Do you want to add one more edge\n"); 146 | scanf("%d", &choice); 147 | } while (choice); 148 | } 149 | void append(NODE *V[MAX], int source, int dest) 150 | { 151 | NODE *temp = malloc(sizeof(NODE)); 152 | temp->info = dest; 153 | temp->next = NULL; 154 | 155 | //insert Last 156 | if (V[source] == NULL) 157 | { 158 | V[source] = temp; 159 | return; 160 | } 161 | 162 | NODE *p = V[source]; 163 | while (p->next != NULL) 164 | p = p->next; 165 | p->next = temp; 166 | //insertLast ends here 167 | 168 | //insert node at front 169 | // temp->next=V[source]; 170 | // V[source]=temp; 171 | //insertFront ends here 172 | } 173 | 174 | void dfs(NODE *V[MAX], int visited[MAX], int cv) 175 | { 176 | visited[cv] = 1; 177 | printf("%d ", cv); 178 | NODE *p = V[cv]; 179 | while (p != NULL) 180 | { 181 | if (visited[p->info] == 0) 182 | dfs(V, visited, p->info); 183 | p = p->next; 184 | } 185 | } 186 | void destroyList(NODE *V[MAX], int n) 187 | { 188 | NODE *p, *q; 189 | for (int i = 0; i < n; i++) 190 | { 191 | p = V[i]; 192 | while (p != NULL) 193 | { 194 | q = p; 195 | p = p->next; 196 | // printf("Freeing %d\n",q->info); 197 | free(q); 198 | } 199 | V[i] = NULL; 200 | } 201 | } 202 | 203 | //Pgm4 204 | //print all vertices in the graph using DFS 205 | //adjacency list representation 206 | #include 207 | #include 208 | #define MAX 10 209 | typedef struct node 210 | { 211 | int info; 212 | struct node *next; 213 | } NODE; 214 | void createAdjList(NODE *V[MAX]); 215 | void append(NODE *V[MAX], int source, int dest); 216 | void dfs(NODE *V[MAX], int visited[MAX], int source); 217 | void destroyList(NODE *V[MAX], int n); 218 | 219 | int main() 220 | { 221 | int n; 222 | printf("Enter the no. of nodes\n"); 223 | scanf("%d", &n); 224 | 225 | NODE *V[MAX] = {NULL}; 226 | createAdjList(V); 227 | 228 | int src, visited[MAX] = {0}; 229 | printf("Enter the source vertex\n"); 230 | scanf("%d", &src); 231 | 232 | printf("DFS Traversal\n"); 233 | dfs(V, visited, src); 234 | for (int i = 0; i < n; i++) 235 | { 236 | if (visited[i] == 0) 237 | { 238 | printf("\n"); 239 | dfs(V, visited, i); 240 | } 241 | } 242 | destroyList(V, n); 243 | } 244 | void createAdjList(NODE *V[MAX]) 245 | { 246 | int choice; 247 | int source, dest; 248 | do 249 | { 250 | printf("Enter the source and dest vertex of the edge to be added\n"); 251 | scanf("%d%d", &source, &dest); 252 | append(V, source, dest); 253 | append(V, dest, source); //undirected graph 254 | 255 | printf("Do you want to add one more edge\n"); 256 | scanf("%d", &choice); 257 | } while (choice); 258 | } 259 | void append(NODE *V[MAX], int source, int dest) 260 | { 261 | NODE *temp = malloc(sizeof(NODE)); 262 | temp->info = dest; 263 | temp->next = NULL; 264 | 265 | //insert Last 266 | if (V[source] == NULL) 267 | { 268 | V[source] = temp; 269 | return; 270 | } 271 | 272 | NODE *p = V[source]; 273 | while (p->next != NULL) 274 | p = p->next; 275 | p->next = temp; 276 | //insertLast ends here 277 | 278 | //insert node at front 279 | // temp->next=V[source]; 280 | // V[source]=temp; 281 | //insertFront ends here 282 | } 283 | 284 | void dfs(NODE *V[MAX], int visited[MAX], int cv) 285 | { 286 | visited[cv] = 1; 287 | printf("%d ", cv); 288 | NODE *p = V[cv]; 289 | while (p != NULL) 290 | { 291 | if (visited[p->info] == 0) 292 | dfs(V, visited, p->info); 293 | p = p->next; 294 | } 295 | } 296 | void destroyList(NODE *V[MAX], int n) 297 | { 298 | NODE *p, *q; 299 | for (int i = 0; i < n; i++) 300 | { 301 | p = V[i]; 302 | while (p != NULL) 303 | { 304 | q = p; 305 | p = p->next; 306 | // printf("Freeing %d\n",q->info); 307 | free(q); 308 | } 309 | V[i] = NULL; 310 | } 311 | } -------------------------------------------------------------------------------- /Unit4/8_UndirectedGraphConnectivityUsingDFS.c: -------------------------------------------------------------------------------- 1 | //Pgm1 2 | //Determine if the undirected graph is connected or not using DFS 3 | //Adjacency Matrix representation 4 | #define MAX 10 5 | #include 6 | 7 | void readGraph(int a[MAX][MAX], int n); 8 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source); 9 | int connected(int a[MAX][MAX], int n); 10 | 11 | int main() 12 | { 13 | int a[MAX][MAX], n; 14 | 15 | printf("Enter the no. of vertices\n"); 16 | scanf("%d", &n); 17 | 18 | printf("Enter the values to adj matrix\n"); 19 | readGraph(a, n); 20 | 21 | if (connected(a, n)) 22 | printf("Graph is connected\n"); 23 | else 24 | printf("Graph is disconnected\n"); 25 | } 26 | 27 | void readGraph(int a[MAX][MAX], int n) 28 | { 29 | for (int i = 0; i < n; i++) 30 | for (int j = 0; j < n; j++) 31 | scanf("%d", &a[i][j]); 32 | } 33 | 34 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source) 35 | { 36 | visited[source] = 1; 37 | // printf("%d ",source); //To print path 38 | 39 | for (int i = 0; i < n; i++) 40 | { 41 | if (a[source][i] && visited[i] == 0) 42 | dfs(a, n, visited, i); 43 | } 44 | } 45 | 46 | int connected(int a[MAX][MAX], int n) 47 | { 48 | int visited[MAX] = {0}, source = 0; 49 | dfs(a, n, visited, source); 50 | for (int i = 0; i < n; i++) 51 | { 52 | if (visited[i] == 0) 53 | { 54 | return 0; 55 | } 56 | } 57 | return 1; 58 | } 59 | 60 | //Pgm2 61 | //Print the no. of connected components using DFS 62 | #define MAX 10 63 | #include 64 | 65 | void readGraph(int a[MAX][MAX], int n); 66 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source); 67 | int connected(int a[MAX][MAX], int n); 68 | int main() 69 | { 70 | int a[MAX][MAX], n; 71 | 72 | printf("Enter the no. of vertices\n"); 73 | scanf("%d", &n); 74 | 75 | printf("Enter the values to adj matrix\n"); 76 | readGraph(a, n); 77 | 78 | int res = connected(a, n); 79 | printf("No.of connected components is %d\n", res); 80 | } 81 | 82 | void readGraph(int a[MAX][MAX], int n) 83 | { 84 | for (int i = 0; i < n; i++) 85 | for (int j = 0; j < n; j++) 86 | scanf("%d", &a[i][j]); 87 | } 88 | 89 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source) 90 | { 91 | visited[source] = 1; 92 | // printf("%d ",source); 93 | 94 | for (int i = 0; i < n; i++) 95 | { 96 | if (a[source][i] && visited[i] == 0) 97 | dfs(a, n, visited, i); 98 | } 99 | } 100 | 101 | int connected(int a[MAX][MAX], int n) 102 | { 103 | int visited[MAX] = {0}; 104 | int con = 1; 105 | dfs(a, n, visited, 0); 106 | for (int i = 0; i < n; i++) 107 | { 108 | if (visited[i] == 0) 109 | { 110 | printf("\n"); 111 | con++; 112 | dfs(a, n, visited, i); 113 | } 114 | } 115 | return con; 116 | } -------------------------------------------------------------------------------- /Unit4/9_DigraphConnectivityUsingDFS.c: -------------------------------------------------------------------------------- 1 | //Determine if the digraph is strongly or weakly connected 2 | //or not connected using DFS 3 | //Adjacency Matrix representation 4 | /* 5 | Strongly conctd 6 | 0 1 1 0 7 | 1 0 0 0 8 | 0 0 0 1 9 | 1 0 0 0 10 | Weakly conctd 11 | 0 1 0 0 12 | 0 0 0 0 13 | 0 0 0 1 14 | 1 0 0 0 15 | Disconctd 16 | 0 0 1 0 17 | 0 0 0 0 18 | 0 0 0 1 19 | 1 0 0 0 20 | */ 21 | #define MAX 10 22 | #include 23 | 24 | void readGraph(int a[MAX][MAX], int n); 25 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source); 26 | int strongConnected(int a[MAX][MAX], int n); 27 | int weakConnected(int a[MAX][MAX], int n); 28 | void removeEdgeDirection(int a[MAX][MAX], int b[MAX][MAX], int n); 29 | int main() 30 | { 31 | int a[MAX][MAX], n; 32 | 33 | printf("Enter the no. of vertices\n"); 34 | scanf("%d", &n); 35 | 36 | printf("Enter the values to adj matrix\n"); 37 | readGraph(a, n); 38 | 39 | if (strongConnected(a, n)) 40 | printf("Graph is strongly connected\n"); 41 | else 42 | { 43 | int b[MAX][MAX] = {0}; 44 | removeEdgeDirection(a, b, n); 45 | if (weakConnected(b, n)) 46 | printf("Graph is weakly connected\n"); 47 | else 48 | printf("Graph is disconnected\n"); 49 | } 50 | } 51 | 52 | void readGraph(int a[MAX][MAX], int n) 53 | { 54 | for (int i = 0; i < n; i++) 55 | for (int j = 0; j < n; j++) 56 | scanf("%d", &a[i][j]); 57 | } 58 | 59 | void dfs(int a[MAX][MAX], int n, int visited[MAX], int source) 60 | { 61 | visited[source] = 1; 62 | // printf("%d ",source); 63 | 64 | for (int i = 0; i < n; i++) 65 | { 66 | if (a[source][i] && visited[i] == 0) 67 | dfs(a, n, visited, i); 68 | } 69 | } 70 | 71 | int strongConnected(int a[MAX][MAX], int n) 72 | { 73 | int visited[MAX] = {0}; 74 | 75 | for (int v = 0; v < n; v++) 76 | { 77 | for (int j = 0; j < n; j++) 78 | visited[j] = 0; 79 | dfs(a, n, visited, v); 80 | for (int i = 0; i < n; i++) 81 | { 82 | if (visited[i] == 0) 83 | { 84 | return 0; 85 | } 86 | } 87 | } 88 | return 1; 89 | } 90 | 91 | void removeEdgeDirection(int a[MAX][MAX], int b[MAX][MAX], int n) 92 | { 93 | int i, j; 94 | for (i = 0; i < n; i++) 95 | { 96 | for (j = 0; j < n; j++) 97 | { 98 | if (a[i][j] == 1) 99 | b[i][j] = b[j][i] = 1; 100 | } 101 | } 102 | } 103 | int weakConnected(int b[MAX][MAX], int n) 104 | { 105 | int visited[MAX] = {0}; 106 | 107 | dfs(b, n, visited, 0); 108 | for (int i = 0; i < n; i++) 109 | { 110 | if (visited[i] == 0) 111 | { 112 | return 0; 113 | } 114 | } 115 | return 1; 116 | } -------------------------------------------------------------------------------- /Unit5/Hashing/hashChaining.c: -------------------------------------------------------------------------------- 1 | //Open Hashing 2 | //Hash function: rNo%tableSize 3 | //Collision Resolution: Open hashing/Separate Chaining 4 | #include 5 | #include 6 | #include 7 | #define SIZE 7 8 | 9 | typedef struct element 10 | { 11 | int rNo; 12 | char name[30]; 13 | struct element *next; 14 | } NODE; 15 | 16 | void initTable(NODE *ht[SIZE]); 17 | void insert(NODE *ht[SIZE], int rNo, char name[30]); 18 | int delete (NODE *ht[SIZE], int rNo); 19 | int search(NODE *ht[SIZE], int rNo, char name[30]); 20 | void display(NODE *ht[SIZE]); 21 | void destroyTable(NODE *ht[SIZE]); 22 | 23 | int main() 24 | { 25 | NODE *ht[SIZE]; //create hashtable 26 | initTable(ht); //initialize hashtable 27 | 28 | int choice, rNo; 29 | char name[30]; 30 | printf("1.Insert 2.Delete 3.Search 4.Display 5.Exit\n"); 31 | scanf("%d", &choice); 32 | do 33 | { 34 | switch (choice) 35 | { 36 | case 1: 37 | printf("Enter the rNo\n"); 38 | scanf("%d", &rNo); 39 | printf("Enter the name\n"); 40 | scanf("%s", name); 41 | insert(ht, rNo, name); 42 | break; 43 | case 2: 44 | printf("Enter the rNo\n"); 45 | scanf("%d", &rNo); 46 | if (delete (ht, rNo)) 47 | printf("Record with rNo %d deleted\n", rNo); 48 | else 49 | printf("Record not found\n"); 50 | break; 51 | case 3: 52 | printf("Enter the rNo\n"); 53 | scanf("%d", &rNo); 54 | 55 | if (search(ht, rNo, name)) 56 | printf("Rcrdfound:RollNo:%d,Name:%s\n", rNo, name); 57 | else 58 | printf("Record not found\n"); 59 | break; 60 | case 4: 61 | display(ht); 62 | break; 63 | case 5: 64 | destroyTable(ht); 65 | return 0; 66 | } 67 | printf("1.Insert 2.Delete 3.Search 4.Display 5.Exit\n"); 68 | scanf("%d", &choice); 69 | } while (choice < 6); 70 | destroyTable(ht); 71 | } 72 | 73 | void initTable(NODE *ht[SIZE]) 74 | { 75 | for (int i = 0; i < SIZE; i++) 76 | ht[i] = NULL; 77 | } 78 | 79 | void insert(NODE *ht[SIZE], int rNo, char name[30]) 80 | { 81 | int index = rNo % SIZE; 82 | NODE *temp = malloc(sizeof(NODE)); 83 | temp->rNo = rNo; 84 | strcpy(temp->name, name); 85 | temp->next = NULL; 86 | 87 | if (ht[index] == NULL) 88 | { 89 | ht[index] = temp; 90 | return; 91 | } 92 | 93 | NODE *cur = ht[index]; 94 | while (cur->next != NULL) 95 | cur = cur->next; 96 | cur->next = temp; 97 | } 98 | 99 | int delete (NODE *ht[SIZE], int rNo) 100 | { 101 | int index = rNo % SIZE; 102 | 103 | NODE *cur = ht[index]; 104 | NODE *prev = NULL; 105 | 106 | while (cur != NULL && cur->rNo != rNo) 107 | { 108 | prev = cur; 109 | cur = cur->next; 110 | } 111 | if (cur != NULL) 112 | { 113 | if (prev == NULL) 114 | ht[index] = cur->next; 115 | else 116 | prev->next = cur->next; 117 | free(cur); 118 | return 1; 119 | } 120 | return 0; 121 | } 122 | int search(NODE *ht[SIZE], int rNo, char name[30]) 123 | { 124 | int index = rNo % SIZE; 125 | 126 | NODE *cur = ht[index]; 127 | 128 | while (cur != NULL) 129 | { 130 | if (cur->rNo == rNo) 131 | { 132 | strcpy(name, cur->name); 133 | return 1; 134 | } 135 | cur = cur->next; 136 | } 137 | return 0; 138 | } 139 | void display(NODE *ht[SIZE]) 140 | { 141 | NODE *cur; 142 | for (int i = 0; i < SIZE; i++) 143 | { 144 | cur = ht[i]; 145 | printf("h[%d]->", i); 146 | while (cur != NULL) 147 | { 148 | printf("%d %s-> ", cur->rNo, cur->name); 149 | cur = cur->next; 150 | } 151 | printf("NULL\n"); 152 | } 153 | } 154 | void destroyTable(NODE *ht[SIZE]) 155 | { 156 | NODE *cur, *prev; 157 | for (int i = 0; i < SIZE; i++) 158 | { 159 | cur = ht[i]; 160 | while (cur != NULL) 161 | { 162 | prev = cur; 163 | cur = cur->next; 164 | // printf("Freeing %d\n",prev->rNo); 165 | free(prev); 166 | } 167 | ht[i] = NULL; 168 | } 169 | } -------------------------------------------------------------------------------- /Unit5/Hashing/linearProbUpdated.c: -------------------------------------------------------------------------------- 1 | //Closed Hashing (Open addressing) 2 | //Hash function: rNo%tableSize 3 | //Collision Resolution: Linear Probing 4 | #include 5 | #define SIZE 7 6 | #include 7 | typedef struct stud 8 | { 9 | int rNo; 10 | char name[30]; 11 | int mark; //indicates if record exists or not 12 | } NODE; 13 | 14 | void initTable(NODE ht[SIZE], int *n); 15 | int insert(NODE ht[SIZE], int rNo, char name[30], int *n); 16 | int delete (NODE ht[SIZE], int rNo, int *n); 17 | int search(NODE ht[SIZE], int rNo, char name[30], int n); 18 | void display(NODE ht[SIZE], int n); 19 | 20 | int main() 21 | { 22 | int n = 0; 23 | NODE ht[SIZE]; //create hashtable 24 | initTable(ht, &n); //initialize hashtable 25 | 26 | int choice, rNo; 27 | char name[30]; 28 | printf("1.Insert 2.Delete 3.Search 4.Display 5.Exit\n"); 29 | scanf("%d", &choice); 30 | do 31 | { 32 | switch (choice) 33 | { 34 | case 1: 35 | printf("Enter the rNo\n"); 36 | scanf("%d", &rNo); 37 | printf("Enter the name\n"); 38 | scanf("%s", name); 39 | if (!insert(ht, rNo, name, &n)) 40 | printf("Table already full, cannot insert\n"); 41 | break; 42 | case 2: 43 | printf("Enter the rNo\n"); 44 | scanf("%d", &rNo); 45 | if (delete (ht, rNo, &n)) 46 | printf("Record with rNo %d deleted\n", rNo); 47 | else 48 | printf("Record not found\n"); 49 | break; 50 | case 3: 51 | printf("Enter the rNo\n"); 52 | scanf("%d", &rNo); 53 | 54 | if (search(ht, rNo, name, n)) 55 | printf("Record found: Details %d,%s\n", rNo, name); 56 | else 57 | printf("Record not found\n"); 58 | break; 59 | case 4: 60 | display(ht, n); 61 | break; 62 | case 5: 63 | return 0; 64 | } 65 | printf("1.Insert 2.Delete 3.Search 4.Display 5.Exit\n"); 66 | scanf("%d", &choice); 67 | } while (choice < 6); 68 | } 69 | 70 | void initTable(NODE ht[SIZE], int *n) 71 | { 72 | for (int i = 0; i < SIZE; i++) 73 | ht[i].mark = -1; 74 | (*n) = 0; 75 | } 76 | int insert(NODE ht[SIZE], int rNo, char name[30], int *n) 77 | { 78 | if (*n == SIZE) 79 | return 0; 80 | 81 | int index = rNo % SIZE; //hash fn 82 | 83 | while (ht[index].mark != -1) 84 | index = (index + 1) % SIZE; 85 | 86 | ht[index].rNo = rNo; 87 | strcpy(ht[index].name, name); 88 | ht[index].mark = 1; 89 | (*n)++; 90 | return 1; 91 | } 92 | int delete (NODE ht[SIZE], int rNo, int *n) 93 | { 94 | if (*n == 0) 95 | return 0; 96 | 97 | int index = rNo % SIZE; //hash fn 98 | 99 | int i = 0; 100 | while (ht[index].rNo != rNo && i < *n) 101 | { 102 | index = (index + 1) % SIZE; 103 | if (ht[index].mark == 1) 104 | i++; 105 | } 106 | if (ht[index].rNo == rNo && ht[index].mark == 1) 107 | { 108 | ht[index].mark = -1; 109 | (*n)--; 110 | return 1; 111 | } 112 | return 0; 113 | } 114 | int search(NODE ht[SIZE], int rNo, char name[30], int n) 115 | { 116 | if (n == 0) 117 | return 0; 118 | 119 | int index = rNo % SIZE; //hash fn 120 | 121 | int i = 0; 122 | while (ht[index].rNo != rNo && i < n) 123 | { 124 | index = (index + 1) % SIZE; 125 | if (ht[index].mark == 1) 126 | i++; 127 | } 128 | if (ht[index].rNo == rNo && ht[index].mark == 1) 129 | { 130 | strcpy(ht[index].name, name); 131 | return 1; 132 | } 133 | return 0; 134 | } 135 | void display(NODE ht[SIZE], int n) 136 | { 137 | if (n == 0) 138 | { 139 | printf("Empty hash table\n"); 140 | return; 141 | } 142 | for (int i = 0; i < SIZE; i++) 143 | { 144 | if (ht[i].mark == 1) 145 | printf("h[%d]: %d, %s\n", i, ht[i].rNo, ht[i].name); 146 | else 147 | printf("h[%d]\n", i); 148 | } 149 | } -------------------------------------------------------------------------------- /Unit5/dictionaryusingTrie.c: -------------------------------------------------------------------------------- 1 | //creation of a dictionary using trie data structure 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef struct trie 8 | { 9 | int isLeaf; 10 | struct trie *child[26]; 11 | char *synonym; 12 | } TRIE; 13 | 14 | TRIE *createNode(); 15 | void insert(TRIE *root, char *pattern, char *meaning); 16 | int search(TRIE *root, char *pattern, char *meaning); 17 | 18 | int main() 19 | { 20 | TRIE *root = createNode(); 21 | 22 | insert(root, "act", "pretend"); 23 | 24 | char meaning[100]; 25 | if (search(root, "act", meaning)) 26 | printf("act is present and its meaning is %s\n", meaning); 27 | else 28 | printf("act is not present\n"); 29 | if (search(root, "ac", meaning)) 30 | printf("ac is present and its meaning is %s\n", meaning); 31 | else 32 | printf("ac is not present\n"); 33 | } 34 | 35 | TRIE *createNode() 36 | { 37 | TRIE *temp = malloc(sizeof(TRIE)); 38 | temp->isLeaf = 0; 39 | for (int i = 0; i < 26; i++) 40 | temp->child[i] = NULL; 41 | 42 | return temp; 43 | } 44 | 45 | void insert(TRIE *root, char *pattern, char *meaning) 46 | { 47 | TRIE *cur = root; 48 | 49 | while (*pattern) 50 | { 51 | if (cur->child[*pattern - 'a'] == NULL) 52 | cur->child[*pattern - 'a'] = createNode(); 53 | cur = cur->child[*pattern - 'a']; 54 | pattern++; 55 | } 56 | cur->isLeaf = 1; 57 | cur->synonym = malloc(strlen(meaning) + 1); 58 | strcpy(cur->synonym, meaning); 59 | } 60 | int search(TRIE *root, char *pattern, char *meaning) 61 | { 62 | TRIE *cur = root; 63 | 64 | while (*pattern) 65 | { 66 | if (cur->child[*pattern - 'a'] == NULL) 67 | return 0; 68 | cur = cur->child[*pattern - 'a']; 69 | pattern++; 70 | } 71 | if (cur->isLeaf) 72 | { 73 | strcpy(meaning, cur->synonym); 74 | return 1; 75 | } 76 | return 0; 77 | } -------------------------------------------------------------------------------- /Unit5/trie.c: -------------------------------------------------------------------------------- 1 | //TRIE data structure: prefix tree 2 | //houses lower case english alphabets 3 | #include 4 | #include 5 | 6 | typedef struct trie 7 | { 8 | int isLeaf; 9 | struct trie *child[26]; 10 | } TRIE; 11 | 12 | TRIE *createNode(); 13 | void insert(TRIE *root, char *pattern); 14 | int search(TRIE *root, char *pattern); 15 | TRIE *destroy(TRIE *root); 16 | void display(TRIE *root, char *str, int level); 17 | void displayOfLen(TRIE *root, char *str, int level, int length); 18 | int isEmpty(TRIE *root); 19 | TRIE *delete (TRIE *root, char *str, int level, int length); 20 | int main() 21 | { 22 | TRIE *root = createNode(); 23 | /* 24 | insert(root,"ant"); 25 | insert(root,"antenna"); 26 | 27 | if(search(root,"ant")) 28 | printf("ant is present\n"); 29 | else 30 | printf("ant is not present\n"); 31 | 32 | if(search(root,"antenna")) 33 | printf("antenna is present\n"); 34 | else 35 | printf("antenna is not present\n"); 36 | 37 | if(search(root,"ten")) 38 | printf("ten is present\n"); 39 | else 40 | printf("ten is not present\n"); 41 | 42 | if(search(root,"an")) 43 | printf("an is present\n"); 44 | else 45 | printf("an is not present\n"); */ 46 | 47 | insert(root, "app"); 48 | insert(root, "apple"); 49 | insert(root, "ant"); 50 | 51 | char str[100]; 52 | printf("Patterns present in TRIE\n"); 53 | display(root, str, 0); 54 | printf("Patterns of length 2 in TRIE\n"); 55 | displayOfLen(root, str, 0, 2); 56 | delete (root, "app", 0, 3); 57 | printf("Patterns present in TRIE\n"); 58 | display(root, str, 0); 59 | destroy(root); 60 | } 61 | 62 | TRIE *createNode() 63 | { 64 | TRIE *temp = malloc(sizeof(TRIE)); 65 | temp->isLeaf = 0; 66 | for (int i = 0; i < 26; i++) 67 | temp->child[i] = NULL; 68 | 69 | return temp; 70 | } 71 | 72 | void insert(TRIE *root, char *pattern) 73 | { 74 | TRIE *cur = root; 75 | 76 | while (*pattern) 77 | { 78 | if (cur->child[*pattern - 'a'] == NULL) 79 | cur->child[*pattern - 'a'] = createNode(); 80 | cur = cur->child[*pattern - 'a']; 81 | pattern++; 82 | } 83 | cur->isLeaf = 1; 84 | } 85 | int search(TRIE *root, char *pattern) 86 | { 87 | TRIE *cur = root; 88 | 89 | while (*pattern) 90 | { 91 | if (cur->child[*pattern - 'a'] == NULL) 92 | return 0; 93 | cur = cur->child[*pattern - 'a']; 94 | pattern++; 95 | } 96 | return cur->isLeaf; 97 | } 98 | 99 | TRIE *destroy(TRIE *root) 100 | { 101 | for (int i = 0; i < 26; i++) 102 | { 103 | if (root->child[i] != NULL) 104 | { 105 | root->child[i] = destroy(root->child[i]); 106 | // printf("%c is freed\n",i+'a'); 107 | free(root->child[i]); 108 | root->child[i] = NULL; 109 | } 110 | } 111 | return root; 112 | } 113 | 114 | void display(TRIE *root, char *str, int level) 115 | { 116 | if (root->isLeaf) 117 | { 118 | str[level] = '\0'; 119 | printf("%s\n", str); 120 | } 121 | for (int i = 0; i < 26; i++) 122 | { 123 | if (root->child[i] != NULL) 124 | { 125 | str[level] = i + 'a'; 126 | display(root->child[i], str, level + 1); 127 | } 128 | } 129 | } 130 | void displayOfLen(TRIE *root, char *str, int level, int length) 131 | { 132 | if (root->isLeaf && level == length) 133 | { 134 | str[level] = '\0'; 135 | printf("%s\n", str); 136 | } 137 | for (int i = 0; i < 26; i++) 138 | { 139 | if (root->child[i] != NULL) 140 | { 141 | str[level] = i + 'a'; 142 | displayOfLen(root->child[i], str, level + 1, length); 143 | } 144 | } 145 | } 146 | int isEmpty(TRIE *root) 147 | { 148 | for (int i = 0; i < 26; i++) 149 | if (root->child[i] != NULL) 150 | return 0; 151 | 152 | return 1; 153 | } 154 | TRIE *delete (TRIE *root, char *str, int level, int length) 155 | { 156 | if (root == NULL) 157 | return NULL; 158 | 159 | if (length == level) 160 | { 161 | if (root->isLeaf) 162 | root->isLeaf = 0; 163 | if (isEmpty(root)) 164 | { 165 | free(root); 166 | root = NULL; //return NULL; 167 | } 168 | return root; 169 | } 170 | 171 | int index = str[level] - 'a'; 172 | 173 | root->child[index] = delete (root->child[index], str, level + 1, length); 174 | if (root->isLeaf == 0 && isEmpty(root)) 175 | { 176 | free(root); 177 | root = NULL; //return NULL; 178 | } 179 | return root; 180 | } --------------------------------------------------------------------------------