├── M10 Doubly Linked List ├── M10_T3_E7 │ └── m10_t3_e7.c └── M10_T4_E7 │ └── m10_t4_e7.c ├── M3 Single & Double Dimensional Arrays ├── M3_T2_E5 │ └── m3_t2_e5.c └── M3_T4_E3 │ └── m3_t4_e3.c ├── M4 Searching & Sorting ├── M4_T1_E7 │ └── m4_t1_e7.c ├── M4_T1_E8 │ └── m4_t1_e8.c ├── M4_T2_E8 │ └── m4_t2_e8.c └── M4_T2_E9 │ └── m4t2e9.c ├── M6 Revising relavent topics in C ├── M6_T1_E13 │ └── m6_t1_e_13.c ├── M6_T2_E15 │ └── m6_t2_e_15.c ├── M6_T2_E19 │ └── m6_t2_e_19.c ├── M6_T2_E21 │ └── m6_t2_e_21.c ├── M6_T2_E6 │ └── m6_t2_e_6.c ├── M6_T2_E7 │ └── m6_t2_e_7.c ├── M6_T2_E8 │ └── m6_t2_e_8.c ├── M6_T3_E11 │ └── m6_t3_e11.c └── M6_T3_E12 │ └── m6_t3_e12.c ├── M7 Implementation programs of ├── M7_T2_E3 │ └── m7_t2_e3.c └── M7_T2_E4 │ └── m7_t2_e4.c ├── M8 Linear Linked List ├── M8_T4_E7 │ └── m8_t4_e7.c └── M8_T5_E4 │ └── m8_t5_e4.c └── M9 Circular Linked List ├── M9_T3_E5 └── m9_t3_e5.c └── M9_T4_E3 └── m9_t4_e3.c /M10 Doubly Linked List/M10_T3_E7/m10_t3_e7.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // following is the structure specification for doubly linked list 5 | typedef struct dlink 6 | { 7 | int no; 8 | struct dlink *next; 9 | struct dlink *prev; 10 | } node; 11 | 12 | // following is the definition of the function create() 13 | void create(node **start,node **end) 14 | { 15 | node *temp,*p; 16 | char ch='y'; 17 | system("cls"); 18 | if(*start != NULL) 19 | { 20 | printf("\n\n\t **** LIST ALREADY EXISTS *****"); 21 | getch(); 22 | return; 23 | } 24 | fflush(stdin); 25 | printf("\n\n\t\t **** INPUT BLOCK ****\n"); 26 | while(ch == 'y') // loop to insert nodes 27 | { 28 | temp=(node *)malloc(sizeof(node)); 29 | printf("\n\t Enter the no : ==> " ); 30 | scanf("%d",&temp->no); 31 | temp->next = NULL; 32 | if(*start==NULL) 33 | { 34 | p=*start=temp; 35 | temp->prev=NULL; 36 | } 37 | else 38 | { 39 | p->next=temp; 40 | temp->prev=p; 41 | p=temp; 42 | } 43 | *end=temp ; 44 | fflush(stdin); 45 | printf("\n\t Do you want to continue (y/n) ? : "); 46 | ch=getchar(); 47 | } 48 | return; 49 | } 50 | // definition of the function counting_fnc() that counts number of nodes 51 | int counting_fnc(node *end) 52 | { 53 | int total=0 ; 54 | node *temp; 55 | system("cls") ; 56 | for(temp=end;temp!=NULL;temp=temp->prev) // applying the loop in reverse direction 57 | ++total ; // keep adding 1 to the counter variable 58 | return total; // returning total number of nodes back to the calling function 59 | } 60 | 61 | int main() 62 | { 63 | node *start,*end; 64 | int tot; 65 | start=end=NULL; // assigining NULL to start and end indicating doubly linked list is empty 66 | create(&start,&end); 67 | 68 | tot=counting_fnc(end); 69 | printf("Total number of nodes in doubly linked list = %d",tot); 70 | return 0 ; 71 | } 72 | 73 | 74 | /******************** End of the Program ****************************/ -------------------------------------------------------------------------------- /M10 Doubly Linked List/M10_T4_E7/m10_t4_e7.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /* following is the specification of the structure with alias node */ 5 | typedef struct dlink 6 | { 7 | int no; 8 | struct dlink *next; 9 | struct dlink *prev; 10 | } node; 11 | /* definition of the function create() that creates the nodes of a doubly linked list */ 12 | void create(node **start,node **end) 13 | { 14 | node *temp,*p; 15 | char ch='y'; 16 | system("cls"); 17 | if(*start != NULL) // if start is not NULL then error as list is not empty 18 | { 19 | printf("\n\n\t **** LIST ALREADY EXISTS *****"); 20 | getch(); 21 | return; 22 | } 23 | fflush(stdin); 24 | printf("\n\n\t\t **** INPUT BLOCK ****\n"); 25 | // following is the loop block to create nodes and insert into doubly linked list 26 | while(ch == 'y') 27 | { 28 | temp=(node *)malloc(sizeof(node)); // creating node dynamically 29 | printf("\n\t Enter the no : ==> " ); 30 | scanf("%d",&temp->no); // reading value into the node 31 | temp->next = NULL; 32 | if(*start==NULL) 33 | { 34 | p=*start=temp; 35 | temp->prev=NULL; 36 | } 37 | else 38 | { 39 | p->next=temp; 40 | temp->prev=p; 41 | p=temp; 42 | } 43 | *end=temp ; 44 | fflush(stdin); 45 | printf("\n\t Do you want to continue (y/n) ? : "); 46 | ch=getchar(); 47 | } 48 | return; 49 | } 50 | // definition of the function print() that displays all the nodes 51 | void print(node *start,node *end) 52 | { 53 | node *temp; 54 | //system("cls") ; 55 | 56 | 57 | printf("\n\n\t End address Number Prev Link"); 58 | printf("\n\t =================================="); 59 | for(temp=start;temp!=NULL;temp=temp->next) 60 | printf("\n %10u %10d %10u",temp,temp->no,temp->next); 61 | printf("\n\n\t Press any key to goto MAIN BLOCK....."); 62 | getch(); 63 | return; 64 | } 65 | 66 | 67 | node *dele(node *end) 68 | { 69 | int item; 70 | item=end->no; 71 | end=end->prev; 72 | end->next=NULL; 73 | printf("\n\n\t Element [%d] is deleted from Linklist",item); 74 | getch(); 75 | return end; 76 | } 77 | 78 | 79 | node* delf(node *start) 80 | { 81 | int item; 82 | node *tp ; 83 | tp=start ; // preserving address of first node into tp 84 | item=start->no; 85 | start=start->next; 86 | start->prev=NULL; 87 | printf("\n\n\t first Element [ %d ] is successfully deleted from Linklist",item); 88 | free(tp) ; 89 | getch(); 90 | return start; 91 | } 92 | 93 | 94 | 95 | /* Following is the definition of the function delp() that checks whether any of the node values in the doubly linked list is a palindrome or not. If yes, then delete it. Also if node happens to be the first one, then call delf() function. If node happens to be the last one then call dele() */ 96 | void delp(node **start,node **end) 97 | { 98 | node *temp,*temp1; 99 | int num,rev,r ; 100 | // following outer loop traverses nodes one by one 101 | for(temp=*start;temp!=NULL;temp=temp->next) 102 | { 103 | temp1=temp->prev; 104 | rev=0 ; 105 | num=temp->no ; 106 | /* Following inner loop checks the current node whether it is palindrome or not. If it is , then takes the appropriate steps to delete it from the list */ 107 | while(num!=0) 108 | { 109 | r=num%10; 110 | rev=rev*10+r ; 111 | num=num/10 ; 112 | } // end of loop 113 | if (temp->no==rev) // node value is palindrome 114 | { 115 | printf("\n Condition is true %d\t%d",rev,temp->no); getch() ; 116 | break ; // terminate the loop if condition is true 117 | } 118 | } // end of loop 119 | if(temp==NULL) 120 | { 121 | printf("\n\n\t No palindrome node value found in the Linklist "); 122 | getch(); 123 | return; 124 | } 125 | 126 | if(temp->prev == NULL) // only if it is the first node 127 | *start=delf(*start); // invoking the delf() function that deletes the first node 128 | else if(temp->next == NULL) // only if it is the last node 129 | *end=dele(*end); /* invoking the dele() function that delete the last node and updates the value of end */ 130 | 131 | else 132 | { 133 | temp1->next=temp->next; 134 | temp->next->prev=temp1; 135 | 136 | } 137 | return; 138 | } 139 | 140 | int main() 141 | { 142 | node *start,*end; 143 | start=end=NULL; // Assigning NULL to start and end to indicate doubly linked list is empty 144 | create(&start,&end); // calling the create() function 145 | system("cls"); 146 | print(start,end); // calling the function print() to display() nodes before deletion 147 | 148 | delp(&start,&end); 149 | printf("\n **** After deleting the palindrome node value *****"); 150 | print(start,end) ; //printing after deletion of palindrome node value,if there was any 151 | getch() ; 152 | return 0 ; 153 | } 154 | 155 | 156 | /******************** End of the Program ****************************/ -------------------------------------------------------------------------------- /M3 Single & Double Dimensional Arrays/M3_T2_E5/m3_t2_e5.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | float num[10]; // declaration of array 5 | float smallest, sec_smallest ; 6 | int c ; 7 | for (c=0;c<10;++c) // loop for reading values into the array 8 | { 9 | printf("num[%d] = ",c); 10 | scanf("%f",&num[c]) ; 11 | } 12 | smallest=num[0]; // storing the first element into variable smallest 13 | 14 | /*following is the logic to find out the smallest value in the array. 15 | The counter variable for subscript c starts from 1 and not 0 as 16 | first element has already been extracted and stored into variable smallest */ 17 | for (c=1;c<10;++c) 18 | { 19 | if (num[c] 2 | 3 | /* following is the definition of the function transpose() 4 | that receives input array passed from the calling function */ 5 | void transpose(int x[][3]) 6 | { 7 | int y[3][3],i,j; // declaration of output array y and other variables 8 | for (i=0;i<3;++i) // outer loop for rows of x 9 | { 10 | for (j=0;j<3;++j) // inner loop for columns of x 11 | { 12 | 13 | y[i][j]=x[j][i]; // assigning transpose values into y 14 | } // end of inner loop 15 | } // end of outer loop 16 | // printing the output matrix 17 | for (i=0;i<3;++i) 18 | { 19 | for(j=0;j<3;++j) 20 | { 21 | printf("%d\t",y[i][j]); 22 | } 23 | printf("\n"); //newline feed as next row will be printed 24 | } 25 | 26 | } 27 | // definition of the function main() 28 | int main() 29 | { 30 | /* following is the declaration and initialization of input matrix arr */ 31 | int arr[][3]={ 32 | {1,2,3}, 33 | {3,4,5}, 34 | {5,6,7} 35 | } ; 36 | transpose(arr); // calling the function by passing 2-d array arr 37 | 38 | } //end of main() 39 | -------------------------------------------------------------------------------- /M4 Searching & Sorting/M4_T1_E7/m4_t1_e7.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MX 10 // defining a constant MX 3 | int main() 4 | { 5 | int arr[MX],val, i,occ=0 ; // declaration of array and variables 6 | for (i=0;i 2 | #define MX 10 // defining a constant MX 3 | int main() 4 | { 5 | int arr[MX]={100,98,95,90,80,76,75,70,65,62} ; // declaration + initialization of array 6 | int val, lft=0,rt=MX-1,md=(lft+rt)/2 ; /* declaration of array and variables. Here lft will be used for storing the left most subscript and rt for right most subscript. md variable stores middle subscript */ 7 | printf("Enter the value to be searched") ; 8 | scanf("%d",&val) ; // reading the value to be searched into val 9 | while(lft<=rt) 10 | { 11 | if (valarr[md]) /* if value is higher than the middle element, then obviously the chance of finding it lies only on the left subarray. Hence we will update rt */ 15 | rt=md-1 ; // updating rt so that only left subarray is searched 16 | else 17 | { 18 | printf("Found at location number %d\n",md+1); // success message with location 19 | break ; // takes the control out of the loop block 20 | } 21 | md=(lft + rt)/2 ; //updating md before continuing with the search 22 | } // end of loop 23 | if (lft>rt) /* once the control is out of the loop, value of lft being bigger than that of rt, indicates no such value exists. */ 24 | printf("No such value exists in the array") ; 25 | } 26 | -------------------------------------------------------------------------------- /M4 Searching & Sorting/M4_T2_E8/m4_t2_e8.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MX 10 // defining a constant MX for array size 3 | int main() 4 | { 5 | int marks[MX],i,j,temp,flag=0; // declaration of array with size MX and other variables 6 | // reading values in array 7 | for (i=0;imarks[j+1]) // interchange if condition is true 17 | { 18 | temp=marks[j]; // assigning value of first element into temp 19 | marks[j]=marks[j+1]; // assigning value of second element into first one 20 | marks[j+1]=temp ; // assigning value of temp into second element 21 | flag=1 ; // make the value of flag 1 indicating that interchanging took place in the current pass 22 | } // end of if 23 | } // end of inner loop 24 | if(flag==0) /* no interchanging took place in the pass just concluded 25 | thus indicating that array has turned sorted, no need to go for more passes */ 26 | break ; // terminates the outer loop 27 | else 28 | flag=0; // reinitialising value of flag with 0 29 | } // end of outer loop 30 | // lets print the sorted array 31 | 32 | for (i=0;i 2 | #define MX 10 // defining a constant MX for array size 3 | int main() 4 | { 5 | int sales[MX],i,j,temp; // declaration of array with size MX and other variables 6 | // reading values in array 7 | for (i=0;i 2 | #include // required for exit() function 3 | int main() 4 | { 5 | int n,i ; 6 | long double *GDP ; /* declaration of array through pointer 7 | as array name is pointer to base address */ 8 | printf("Enter the number of countries") ; 9 | scanf("%d",&n); // reading he number of countries 10 | /* in the following statement we are using malloc() function to allocate 11 | space dynamically for the array GDP */ 12 | GDP=(long double *) malloc(sizeof(long double)* n); 13 | if (GDP==NULL) // if dynamic space allocation encounters failure 14 | { 15 | printf("dynamic memory allocation resulted into failure....exiting") ; 16 | exit(0); // terminates the program …. Note that in some compilers argument 0 is not given 17 | } 18 | // using loop to read GDP's of n number of countries into array 19 | for (i=0;i 2 | 3 | int tsc1, tsc2,tsc3 ; /* Global declaration of 3 test scores 4 | so that they can be accessed directly in any function */ 5 | 6 | //following is the definition of the function first() ; 7 | void first() // Function data type is void() as no value to be returned 8 | { 9 | float avg ; 10 | avg=(tsc1 + tsc2 + tsc3)/3; 11 | printf("Average score = %.2f\n",avg); 12 | second(); // calling the function second() ; 13 | } 14 | //following is the definition of the function second() ; 15 | void second() // Function data type is void() as no value to be returned 16 | { 17 | if (tsc1 2 | int main() 3 | { 4 | /* 1.following is the function prototype of the function updation() 5 | As we are calling by reference we have used * with the data types 6 | 2. function will not return any value. Hence it's data type is void 7 | */ 8 | void updation(int *, float *,int *, float *;); 9 | int a,b ; 10 | float c,d ; 11 | printf("Enter the values of a,b,c and d\n"); 12 | scanf("%d%d%f%f",&a,&b,&c,&d); // reading the values into 4 variables 13 | updation(&a,&c,&b,&d); // calling the function by passing 4 values by reference 14 | printf("\n Updated values in the calling function : \n"); 15 | printf("a = %d\nc= %.2f\nb = %d\nd = %.2f",a,c,b,d); 16 | } // end of definition of main() 17 | 18 | // following is the definition of the function updation() 19 | void updation(int *ap, float *cp, int*bp, float *dp) 20 | { 21 | /* raising the values of first and last variables 4 times*/ 22 | *ap= *ap * 4 ; 23 | *dp= *dp * 4 ; 24 | /* lowering the values of second and third variables by half */ 25 | *bp= *bp/2 ; 26 | *cp= *cp/2 ; 27 | } // end of definition of the function uddation() 28 | -------------------------------------------------------------------------------- /M6 Revising relavent topics in C/M6_T2_E21/m6_t2_e_21.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SZ 15 // declaration of a named constant 3 | /* following is the global definition of the function howmanyevens() 4 | darr[] is the formal parameter(dummy) that receives the array from calling function */ 5 | 6 | int howmanyevens(int darr[]) 7 | { 8 | int n,count=0 ; 9 | for (n=0;n 2 | int main() 3 | { 4 | /*decl of variables for principal amount,rate of interest, 5 | time and simple interest*/ 6 | float pamt,roi,sint ; 7 | int tm ; 8 | float getinterest(float,int,float) ; // function prototype 9 | printf("Enter the principal amount,rate of interest and time") ; 10 | scanf("%f%f%d",&pamt,&roi,&tm) ; // reading the values 11 | sint=getinterest(pamt,tm,roi); // calling the function by passing 3 arguments 12 | printf("Simple Interest = %.2f",sint); // getting the returned value printed 13 | } 14 | 15 | // following is the definition(body) of the function getinterest() 16 | float getinterest(float pamt1,int tm1,float roi1) 17 | { 18 | float s=(pamt1 * tm1 * roi1)/100 ; // calculating the simple interest 19 | return s ; // returning the simple interest stored in s 20 | } 21 | -------------------------------------------------------------------------------- /M6 Revising relavent topics in C/M6_T2_E7/m6_t2_e_7.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | float temp1,temp2,temp3 ; // declaration of variables to store temperatures of 3 days 5 | void mintemp(float,float,float) ; // function prototype 6 | printf("Enter the temperatures of 3 consecutive days"); 7 | scanf("%f%f%f",&temp1,&temp2,&temp3); // reading temperatures into variables 8 | mintemp(temp1,temp2,temp3); // calling the funtion by passing 3 arguments 9 | } // end of definition (body) of main() 10 | 11 | // following is the definition (body) of function mintemp() function 12 | void mintemp(float t1,float t2,float t3) 13 | { 14 | if (t1 2 | int main() 3 | { 4 | int num ; char ch ; 5 | char divby6(int) ; // function prototype 6 | printf("Enter the number") ; 7 | scanf("%d",&num); // reading the value of num 8 | ch=divby6(num); // calling the function by passing the number num 9 | if (ch=='y') // if 'y' has been returned into ch 10 | printf("Divisible by 6"); 11 | else // if 'y' has not been returned into ch 12 | printf("Not divisible by 6"); 13 | } 14 | 15 | // following is the definition(body) of the called function divby6() 16 | char divby6(int val) 17 | { 18 | if (val%6==0) // if divisible by 6, remainder will be 0 19 | return 'y' ; // then return 'y' 20 | else 21 | return 'n' ; // if remainder is not 0, then return 'n' 22 | 23 | } 24 | -------------------------------------------------------------------------------- /M6 Revising relavent topics in C/M6_T3_E11/m6_t3_e11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // following is the structure specification(GLOBAL SCOPE) with declaration of 4 members 4 | struct IELTS 5 | { 6 | char reg_no[15]; 7 | float bnd1,bnd2,bnd3 ; 8 | } ; 9 | int main() 10 | { 11 | struct IELTS ivar ; // declaration of structure variable 12 | 13 | /* following is the prototype of the function score_avg() 14 | float is the return type of the function as we will be returning average 15 | score band and "struct IELTS" is the data type of the passing argument */ 16 | float score_avg(struct IELTS); 17 | float band_avg ; // declaration of variable that will collect average 18 | // reading the registration number & bands scored in 3 IELTS tests 19 | printf("Enter the registration number & IELTS bands scored in 3 tests"); 20 | gets(ivar.reg_no); 21 | scanf("%f%f%f",&ivar.bnd1,&ivar.bnd2,&ivar.bnd3); 22 | band_avg=score_avg(ivar) ; // calling the function by passing structure ivar 23 | printf("Average of bans scored = %.2f",band_avg); 24 | } //end of definition of the function main() 25 | 26 | // following is the definition of the function score_avg() 27 | float score_avg(struct IELTS dvar) // dvar is the formal parameter 28 | { 29 | float avg = (dvar.bnd1 + dvar.bnd2 + dvar.bnd3)/3 ; 30 | return avg ; 31 | } 32 | -------------------------------------------------------------------------------- /M6 Revising relavent topics in C/M6_T3_E12/m6_t3_e12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // following is the structure specification(GLOBAL SCOPE) with declaration of 4 members 4 | struct IELTS 5 | { 6 | char reg_no[15]; 7 | float bnd1,bnd2,bnd3 ; 8 | } ; 9 | int main() 10 | { 11 | struct IELTS ivar ; // declaration of structure variable 12 | 13 | /* following is the prototype of the function score_avg() 14 | float is the return type of the function as we will be returning average 15 | score band and "struct IELTS *" is the data type of the passing argument 16 | as we will be passing the address of the structure and not value 17 | */ 18 | float score_avg(struct IELTS *); 19 | float band_avg ; // declaration of variable that will collect average 20 | // reading the registration number & bands scored in 3 IELTS tests 21 | printf("Enter the registration number & IELTS bands scored in 3 tests"); 22 | gets(ivar.reg_no); 23 | scanf("%f%f%f",&ivar.bnd1,&ivar.bnd2,&ivar.bnd3); 24 | band_avg=score_avg(&ivar) ; // calling the function by reference 25 | printf("Average of bans scored = %.2f",band_avg); 26 | } //end of definition of the function main() 27 | 28 | // following is the definition of the function score_avg() 29 | float score_avg(struct IELTS *dvar) // dvar is the pointer formal parameter as this is call by reference 30 | { 31 | /* in the following statement we are using arrow operator as dvar is not a strcuture 32 | but 'pointer to structure' */ 33 | float avg = (dvar->bnd1 + dvar->bnd2 + dvar->bnd3)/3 ; 34 | return avg ; 35 | } -------------------------------------------------------------------------------- /M7 Implementation programs of/M7_T2_E3/m7_t2_e3.c: -------------------------------------------------------------------------------- 1 | /*********** Program to copy one Stack to another ************/ 2 | 3 | #include 4 | #include 5 | #define MX 5 6 | // following is the structure specification.typedef keyword is being used to give an alias to the strcuture 7 | typedef struct stack_arr 8 | { 9 | int arr[MX*2]; 10 | int top; 11 | } node; 12 | 13 | /* following is the definition of the function initialise() 14 | this function initialises all the elements to zero 15 | and top is set to -1 */ 16 | void initialise(node *ptr) 17 | { 18 | int i; 19 | ptr->top = -1; 20 | for(i=0;iarr[i]=0; 22 | return; 23 | } 24 | /* following is the definition of the function push() that 25 | is instrumental in inserting the elements into the stack */ 26 | void push(int item,node *ptr) 27 | { 28 | //if(ptr->top == MX-1 29 | // return; 30 | ptr->top++; 31 | ptr->arr[ptr->top]=item; 32 | return; 33 | } 34 | 35 | /* following is the definition of the function pop() that 36 | is instrumental in extracting(deleting) the elements from the stack */ 37 | 38 | int pop(node *ptr) 39 | { 40 | int item; 41 | if(ptr->top == -1) 42 | return(NULL); 43 | item=ptr->arr[ptr->top]; 44 | //ptr->arr[ptr->top]=0; 45 | ptr->top--; 46 | return(item); 47 | } 48 | /* following is the definition of the function display() that 49 | shows all the stack elements on the screen */ 50 | void display(node *ptr) 51 | { 52 | int i; 53 | for(i=(MX*2)-1;i>=0;i--) 54 | printf("\n ==> %d",ptr->arr[i]); 55 | return; 56 | } 57 | /* definition of the function main() */ 58 | int main() 59 | { 60 | 61 | char ch; int i,item; 62 | node stack1,stack2,stack3; // declaring three stacks, two input ones and one output one 63 | initialise(&stack1); // initializing the first input stack 64 | initialise(&stack2); // initializing the second input stack 65 | initialise(&stack3); // initiating the output stack 66 | //clrscr(); 67 | printf("\n\n\t Program to copy one Stack to another "); 68 | printf("\n\t ====================================\n"); 69 | // populating the first input stack 70 | while(1) 71 | { 72 | printf("\n\t Enter the element in Ist Stack : "); 73 | scanf("%d",&item); 74 | push(item,&stack1); 75 | if (stack1.top==MX-1) // overflow (no space left) 76 | break ; 77 | 78 | } // end of loop 79 | // popluating the second input stack 80 | while(1) 81 | { 82 | printf("\n\t Enter the element in 2nd Stack : "); 83 | scanf("%d",&item); 84 | push(item,&stack2); // overflow (no space left) 85 | if (stack2.top==MX-1) 86 | break ; 87 | 88 | } // end of loop 89 | /* popping the elements from first input stack stack1 90 | and pushing them into the output stack stack3 */ 91 | while(1) 92 | { 93 | item=pop(&stack1); 94 | printf("item of stack1 = %d\n",item); 95 | if(item == NULL) 96 | break; 97 | push(item,&stack3); 98 | } 99 | /* popping the elements from second input stack stack2 100 | and pushing them into the output stack stack3 */ 101 | while(1) 102 | { 103 | item=pop(&stack2); 104 | printf("item of stack2 = %d\n",item); 105 | 106 | if(item == NULL) 107 | break; 108 | push(item,&stack3); 109 | } 110 | 111 | 112 | printf("\n*******"); 113 | display(&stack3); //calling the function to display the elements of output stack 114 | printf("\n*******"); 115 | getch(); 116 | } 117 | 118 | /******************** End of the program ************************/ 119 | -------------------------------------------------------------------------------- /M7 Implementation programs of/M7_T2_E4/m7_t2_e4.c: -------------------------------------------------------------------------------- 1 | /* following the specification of the structure 2 | typedef used to give an alias 'node' to this structure */ 3 | typedef struct queue_type 4 | { 5 | int arr[MX] ; // array representing queue with size MX 6 | int front ; 7 | int rear ; 8 | } node; 9 | 10 | /* following is the definition of the function insert(). q pointer formal parameter represents 11 | the queue and item is the value to be inserted */ 12 | int insert(node *q, int item) 13 | { 14 | 15 | if ((q->front==0 && q->rear==MX-1) || q->front==q->rear+1) 16 | { 17 | printf("Queue overflow error.....") ; 18 | 19 | getch(); 20 | return 0 ; // returns 0 if overflow error 21 | } 22 | if (q->front==-1) // if queue is empty 23 | q->front=q->rear=0 ; 24 | else 25 | if (q->rear==MX-1) 26 | q->rear=0 ; 27 | else 28 | q->rear=q->rear+1 ; 29 | 30 | q->arr[q->rear]=item ; // inserting new value in queue 31 | return 1 ; // returns 1 if no overflow error 32 | } 33 | /* following is the definition of the function delete(). q pointer formal 34 | parameter represents queue */ 35 | void delete(node *q) 36 | { 37 | int val,count=0 ; 38 | while(1) 39 | 40 | { 41 | if (q->front==-1) 42 | { 43 | printf("Queue underflow error ......") ; 44 | getch() ; 45 | break ; // if queue is empty terminate the loop 46 | } 47 | val=q->arr[q->front] ; // extracting the element from front 48 | /* following condition checks whether extracted value is divisible 49 | by 3 but not by 5, if yes then add 1 to count */ 50 | if (val%3==0 && val%5!=0) 51 | ++count ; 52 | 53 | if (q->front==q->rear) // only one element was there in the queue 54 | q->front=q->rear=-1 ; // now queue is empty 55 | else 56 | if (q->front==MX-1) 57 | q->front=0 ; 58 | else 59 | q->front++ ; 60 | } // end of loop 61 | printf("\n Total queue elements divisible by 3 but not by 5 =%d",count); 62 | } 63 | 64 | 65 | int main() 66 | { 67 | char ch ; 68 | int val,item,count; 69 | node queue ; //declaring structure variable 70 | queue.front=-1 ; // initializing front with 1 indicating queue is empty 71 | queue.rear=-1 ; // // initializing rear also with 1 indicating queue is empty 72 | int flag=1 ; 73 | while(1) 74 | { 75 | 76 | printf("\n\n\t Enter the Number to Push :=> "); 77 | scanf("%d",&item); 78 | flag=insert(&queue,item); /* calling the function insert() 79 | by passing address of queue and value item */ 80 | if (flag==0) // overflow error reported by the insert() function 81 | break ; //then terminate the loop 82 | } 83 | 84 | delete(&queue) ; /* calling the delete function that extracts 85 | queue element one by one */ 86 | } 87 | /******************** End of the Program ****************************/ 88 | -------------------------------------------------------------------------------- /M8 Linear Linked List/M8_T4_E7/m8_t4_e7.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /* following is the specification of the strcuture */ 5 | typedef struct queue_type 6 | { 7 | int info; 8 | struct queue_type *next; // next would contain address of next node in the queue 9 | } node; 10 | /* definition of the function create() that creates the queue */ 11 | void create(node **front ,node **rear) 12 | { 13 | node *temp,*ptr; 14 | char ch; 15 | while(1) 16 | { 17 | temp=(node *)malloc(sizeof(node)) ; 18 | temp->next=NULL; 19 | if(temp == NULL) 20 | { 21 | printf("\n\t ******* Overflow *******"); 22 | return; 23 | } 24 | printf("\n\t Enter the number :=> "); 25 | scanf("%d",&temp->info); 26 | if(*front == NULL) 27 | *front=*rear=ptr=temp; 28 | else 29 | { 30 | ptr->next=temp; 31 | ptr=temp; 32 | *rear=temp; 33 | } 34 | fflush(stdin); 35 | printf("\n\t Want to add more(y/n) ? : "); 36 | if(getchar()!='y') 37 | break; 38 | } 39 | return; 40 | } 41 | /* definition of the function insert() that inserts new nodes into the queue */ 42 | node *insert(int item,node *rear) 43 | { 44 | node *temp; 45 | temp=(node *)malloc(sizeof(node)); 46 | temp->next=NULL; 47 | temp->info=item; 48 | rear->next=temp; 49 | rear=temp; 50 | return(rear); 51 | } 52 | /* definition of the function delete1() that removes the nodes from the queue 53 | in FIFO manne */ 54 | node* delete1(node **front) 55 | { 56 | node *temp; 57 | temp=*front; 58 | *front=(*front)->next; 59 | return(temp); 60 | } 61 | 62 | /* definition of the function to display all 63 | nodes of the queue with one single go*/ 64 | 65 | void display(node *front,node *rear) 66 | { 67 | node *temp; 68 | printf("\n\n\t Base address Number Link"); 69 | printf("\n\t ==============================="); 70 | printf("\n Front= %5u %10d %10u",front,front->info,front->next); 71 | for(temp=front->next;temp != rear;temp=temp->next) 72 | printf("\n %10u %10d %10u",temp,temp->info,temp->next); 73 | printf("\n Rear = %5u %10d %10u",rear,rear->info,rear->next); 74 | printf("\n\n\t Press any key to goto MAIN BLOCK....."); 75 | getch(); 76 | return; 77 | } 78 | 79 | 80 | int main() 81 | { 82 | char ch; 83 | int item; 84 | // void create(node **,node **),display(node *,node *); 85 | node *front,*rear,*temp ; 86 | //*insert(int a,node *t),*delete1(node **k); 87 | 88 | front = rear = NULL; 89 | while(1) 90 | { 91 | //clrscr(); 92 | printf("\n\t *** Main Block ***\n"); 93 | printf("\n\tLink list representation of Queue"); 94 | printf("\n\t================================="); 95 | printf("\n\n\t 1) Creation of Queue"); 96 | printf("\n\n\t 2) Inserting element into Queue"); 97 | printf("\n\n\t 3) Deleting element from Queue"); 98 | printf("\n\n\t 4) Display the elements of Queue"); 99 | printf("\n\n\t 5) Exit from program"); 100 | printf("\n\n\t Enter your Choice :==> "); 101 | fflush(stdin); 102 | scanf("%c",&ch); 103 | switch(ch) 104 | { 105 | case '1': 106 | //clrscr(); 107 | printf("\n\t *** Create Block ***\n"); 108 | create(&front,&rear); 109 | break; 110 | case '2': 111 | ch='y'; 112 | //clrscr(); 113 | printf("\n\t *** Insert Block ***\n"); 114 | while(ch == 'y') 115 | { 116 | printf("\n\n\t Enter the Number to Push :=> "); 117 | scanf("%d",&item); 118 | rear = insert(item,rear); 119 | printf("\n\t Want to Push more (y/n) => "); 120 | fflush(stdin); 121 | ch=getchar(); 122 | if(ch != 'y') 123 | break; 124 | } 125 | break; 126 | case '3': 127 | ch='y'; 128 | //clrscr(); 129 | printf("\n\t *** Delete Block ***\n"); 130 | while(ch == 'y') 131 | { 132 | temp=delete1(&front); 133 | if(temp != NULL) 134 | { 135 | printf("\n\tThe Poped no.is [ %d ]",temp->info); 136 | printf(" & Address is = %u",temp); 137 | free(temp) ; // Garbage Collection 138 | printf("\n\n\tWant to pop more (y/n) => "); 139 | fflush(stdin); 140 | ch=getchar(); 141 | } 142 | else 143 | break; 144 | } 145 | break; 146 | case '4': 147 | system("cls") ; 148 | printf("\n\t *** Display Block ***\n"); 149 | display(front,rear); 150 | break; 151 | case '5': 152 | exit(0); 153 | } 154 | } 155 | } 156 | 157 | 158 | /******************** End of the Program ****************************/ 159 | -------------------------------------------------------------------------------- /M8 Linear Linked List/M8_T5_E4/m8_t5_e4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // following is the specification of the structure 5 | typedef struct linklist 6 | { 7 | int no; 8 | struct linklist *link; 9 | } node; 10 | char ch; 11 | /* following is the definition of the create() function that creates nodes for linked list 12 | node *create(node *start) 13 | { 14 | node *temp,*p ; 15 | ch='y'; 16 | // clrscr(); 17 | printf("\n\t\t **** INPUT BLOCK ****\n"); 18 | if(start != NULL) 19 | { 20 | printf("\n\n\t **** LIST ALREADY EXIST *****"); 21 | getch(); 22 | return(start); 23 | } 24 | fflush(stdin); 25 | while(ch=='y') 26 | { 27 | temp=(node *)malloc(sizeof(node)); 28 | printf("\n\t Enter the no :==> " ); 29 | scanf("%d",&temp->no); 30 | temp->link = NULL; 31 | if(start == NULL) 32 | p=start=temp ; 33 | else 34 | { 35 | p->link=temp; 36 | p=temp; 37 | } 38 | fflush(stdin); 39 | printf("\t Do you want to continue (y/n) ? : "); 40 | ch=getchar(); 41 | } // end of loop 42 | return(start); 43 | } 44 | 45 | /* following is the definition of the function that displays all the nodes of the linked list */ 46 | void print(node *start) 47 | { 48 | node *temp; 49 | // clrscr(); system("cls") ; 50 | printf("\n\nBase address Number Link"); 51 | printf("\n==============================="); 52 | for(temp=start;temp != NULL;temp=temp->link) 53 | printf("\n%10u %10d %10u",temp,temp->no,temp->link); 54 | printf("\n\n\t Press any key to goto MAIN BLOCK....."); 55 | getch(); 56 | return; 57 | } 58 | 59 | /* definition of the function delf() that will be invoked to delete the first node */ 60 | node *delf(node *start) 61 | { 62 | node *tmp=start ; 63 | start=start->link; 64 | printf("\n\n\t Element is successfully deleted "); 65 | getch(); 66 | free(tmp) ; // garbage collection - deallocation of memory - reclaiming space 67 | return(start); 68 | } 69 | /* following is the definition of the function del_negative_node() 70 | that finds a node with negative value and deletes it */ 71 | 72 | node *del_negative_node(node *start) 73 | { 74 | node *temp,*temp1; 75 | 76 | for(temp=start;(!(temp->no < 0))&& (temp != NULL);temp=temp->link) 77 | temp1=temp; 78 | 79 | if(temp==NULL) // no node with -ve value found in the linked list 80 | { 81 | printf("\n\n\t No node with negative value found in the Linklist"); 82 | getch(); 83 | return(start); 84 | } 85 | 86 | if(temp==start) // first node is -ve 87 | start=delf(start); 88 | 89 | else 90 | { 91 | temp1->link=temp1->link->link; /* temp1->link=temp->link */ 92 | printf("\n\n\t Element with negative value is successfully deleted "); 93 | getch(); 94 | free(temp) ; // garbage collection - deallocation of memory - reclaiming space 95 | return(start); 96 | } 97 | return(start); 98 | } 99 | 100 | 101 | /* following is the definition of the function main() */ 102 | int main() 103 | { 104 | node *start,*temp; // declaration of pointer variables 105 | int op; 106 | start=NULL; // assigning NULL to start indicating linked list is empty 107 | while(1) // infinite loop 108 | { 109 | system("cls") ; // clears the screen 110 | printf("\n**************** Menu ***************"); 111 | printf("\n1) Create the linked list"); 112 | printf("\n2) Delete a node with negative value") ; 113 | printf("\n3) Display the linked list"); 114 | printf("\n4) Exit"); 115 | printf("\nEnter your option please "); 116 | scanf("%d",&op); // reading choice from user 117 | switch(op) 118 | { 119 | case 1: 120 | start=create(start) ; // calling the create() function 121 | break ; 122 | case 2: 123 | start=del_negative_node(start); // calling the function to delete a node with negative value 124 | break; 125 | 126 | case 3: 127 | print(start) ; // calling the print() function() 128 | break ; 129 | case 4: 130 | exit(0) ; // terminates the program 131 | 132 | } // end of switch case 133 | } // end of loop 134 | } // end of main() 135 | -------------------------------------------------------------------------------- /M9 Circular Linked List/M9_T3_E5/m9_t3_e5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // following is the structure specification for circular linked list 5 | struct clist 6 | { 7 | int data; 8 | struct clist *link; 9 | }; 10 | typedef struct clist node; // using typedef keyword to give alias to structure 11 | 12 | // following is the definition of the function create() 13 | node * create(node *start) 14 | { 15 | node *temp,*ptr; // ptr will be used to store the address of previous node 16 | char ch; 17 | int num; 18 | do 19 | { 20 | printf("\n\t Enter the value of number : "); 21 | scanf("%d",&num); 22 | temp = (node*) malloc(sizeof(node)); 23 | temp->data = num ; 24 | if(start==NULL) 25 | { 26 | start = temp; 27 | ptr = start; 28 | } 29 | else 30 | { 31 | ptr->link = temp; 32 | ptr = ptr->link; // ptr=temp 33 | } 34 | temp->link = start; // assigning address of the first node to the link of the last node 35 | printf("\n\t Do you want to add more nodes (y/n) : "); 36 | fflush(stdin); 37 | scanf("%c",&ch); 38 | } while(ch=='y'||ch=='Y'); 39 | return(start); 40 | } 41 | 42 | void display(node *start) 43 | { 44 | node *temp; 45 | printf("\n\n Base address Number Link"); 46 | printf("\n ==============================="); 47 | if (start->data>=120 && start->data<=255) // checking the value of the very first node 48 | printf("\n%10u %10d %10u",start,start->data,start->link); 49 | for(temp=start->link;temp != start;temp=temp->link) 50 | if (temp->data>=120 && temp->data<=255) 51 | printf("\n%10u %10d %10u",temp,temp->data,temp->link); 52 | getch() ; 53 | return; 54 | } 55 | 56 | int main() 57 | { 58 | node *start; 59 | int choice,num,data; 60 | char ch; 61 | start = NULL; 62 | system("cls") ; 63 | printf("\n\t\t **** CREATE BLOCK ****\n"); 64 | start = create(start); 65 | printf("\n\t\t **** DISPLAY BLOCK ****\n"); 66 | display(start); 67 | } 68 | /**** End of the Program**********/ 69 | -------------------------------------------------------------------------------- /M9 Circular Linked List/M9_T4_E3/m9_t4_e3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct clist 5 | { 6 | int data; 7 | struct clist *link; 8 | }; 9 | typedef struct clist node; 10 | 11 | node * create(node *start) 12 | { 13 | node *temp,*ptr; 14 | char ch; 15 | int num; 16 | do 17 | { 18 | printf("\n\t Enter the value of number : "); 19 | scanf("%d",&num); 20 | temp = (node*) malloc(sizeof(node)); 21 | temp->data = num ; 22 | if(start==NULL) 23 | { 24 | start = temp; 25 | ptr = start; 26 | } 27 | else 28 | { 29 | ptr->link = temp; 30 | ptr = ptr->link; // ptr=temp 31 | } 32 | temp->link = start; 33 | printf("\n\t Do you want to add more nodes (y/n) : "); 34 | fflush(stdin); 35 | scanf("%c",&ch); 36 | } while(ch=='y'||ch=='Y'); 37 | return(start); 38 | } 39 | 40 | void display(node *start) 41 | { 42 | node *temp; 43 | printf("\n\n Base address Number Link"); 44 | printf("\n ==============================="); 45 | printf("\n%10u %10d %10u",start,start->data,start->link); 46 | for(temp=start->link;temp != start;temp=temp->link) 47 | printf("\n%10u %10d %10u",temp,temp->data,temp->link); 48 | getch() ; 49 | return; 50 | } 51 | 52 | 53 | 54 | node* del_if_zero(node *start) 55 | { 56 | node *temp,*ptr,*end,*loc,*i; 57 | char ch; 58 | int num; 59 | loc = start; 60 | // following post-tested loop get holdof the address of the last node 61 | do 62 | { 63 | end = loc; 64 | loc = loc->link; 65 | }while(loc!=start); 66 | 67 | 68 | // temp = start; // preseving address of first node into temp 69 | 70 | if( start->link==start && start->data==0) // only one node in list and contains 0 71 | { 72 | free(start) ; // garbage collection.... freeing up space 73 | start = NULL; // circular linked list is empty 74 | 75 | } 76 | else if(start->data==0) // first node but not only single node 77 | { 78 | ptr = start; 79 | start = start->link; 80 | end->link = start; // assigning address of new first node into link of last node 81 | free(ptr); // deallocation ..... freeing up space 82 | } 83 | 84 | return(start); // retunung the updated value of start 85 | } 86 | 87 | 88 | // following is the definitionof main() 89 | int main() 90 | { 91 | node *start; 92 | int choice,num,data; 93 | char ch; 94 | start = NULL; 95 | system("cls"); 96 | printf("\n\t\t **** CREATE BLOCK ****\n"); 97 | start = create(start); 98 | printf("\n Nodes before deletion"); 99 | display(start); 100 | start = del_if_zero(start); 101 | printf("\n Nodes after deletion, if took place"); 102 | display(start); 103 | } 104 | /******************** End of the Program ****************************/ 105 | --------------------------------------------------------------------------------