Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > Linked Lists Code Examples

Convert a Binary Tree into a Singly Linked List by Traversing Level by Level

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
/* Convert a Binary Tree into a Singly Linked List by Traversing Level by Level This C Program converts a binary tree into a singly linked list by Breadth first search algorithm. We use this algorithm for traversing level by level */ #include <stdio.h> #include <stdlib.h> /*structure type to create a tree*/ struct node { int j; struct node *left; struct node *right; }; /* * structure type to point to the nodes of a tree * and also create self-referential list used for * queueing. */ struct queue { struct node *nodeptr; struct queue *next; }; /* resulting singly linked list */ struct list { int j; struct list *next; }; void createTree(struct node **); void createlistbfs(struct node *, struct list **); void delete(struct node **); void display(struct list *); void deleteList(struct list **); int main() { struct node *root = NULL; struct list *head = NULL; createTree(&root); createlistbfs(root, &head); printf("Displaying the list generated at node by node level of the tree: "); display(head); deleteList(&head); delete(&root); return 0; } void createTree(struct node **root) { struct node *temp, *p, *q; int a, ch; do { printf("Enter a number for a node: "); scanf("%d", &a); temp = (struct node *)malloc(sizeof(struct node)); temp->j = a; temp->left = NULL; temp->right = NULL; p = q = *root; if (*root == NULL) { *root = temp; } else { while (1) { q = p; if (p->j >= temp->j) { p = p->left; } else { p = p->right; } if (p == NULL) { break; } } if (q->j >= temp->j) q->left = temp; else q->right = temp; } printf("Do you want to continue? [1/0]: "); scanf("%d", &ch); } while (ch != 0); } void createlistbfs(struct node *root, struct list **head) { struct queue *qhead, *qrear, *qtemp, *qrelease; struct list *temp, *rear; if (root == NULL) { return; } qhead = (struct queue *)malloc(sizeof(struct queue)); qhead->nodeptr = root; qhead->next = NULL; qrear = qhead; while (qhead != NULL) { temp = (struct list *)malloc(sizeof(struct list)); temp->j = qhead->nodeptr->j; temp->next = NULL; if (*head == NULL) { *head = temp; } else { rear->next = temp; } rear = temp; if (qhead->nodeptr->left != NULL) { qtemp = (struct queue *)malloc(sizeof(struct queue)); qtemp->nodeptr = qhead->nodeptr->left; qtemp->next = NULL; qrear->next = qtemp; qrear = qtemp; } if (qhead->nodeptr->right != NULL) { qtemp = (struct queue *)malloc(sizeof(struct queue)); qtemp->nodeptr = qhead->nodeptr->right; qtemp->next = NULL; qrear->next = qtemp; qrear = qtemp; } qrelease = qhead; qhead = qhead->next; free(qrelease); } } void delete(struct node **root) { if (*root == NULL) { return; } else { if ((*root)->left != NULL) { delete(&((*root)->left)); } if ((*root)->right != NULL) { delete(&((*root)->right)); } } } void display(struct list *head) { while (head != NULL) { printf("%d ", head->j); head = head->next; } } void deleteList(struct list **head) { struct list *temp; temp = *head; while (temp != NULL) { *head = (*head)->next; free(temp); temp = *head; } }

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.

Allocate memory block. Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced. The "malloc" or "memory allocation" method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It doesn't Iniatialize memory at execution time so that it has initializes each block with the default garbage value initially.

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

The free() function in C library allows you to release or deallocate the memory blocks which are previously allocated by calloc(), malloc() or realloc() functions. It frees up the memory blocks and returns the memory to heap. It helps freeing the memory in your program which will be available for later use. In C, the memory for variables is automatically deallocated at compile time. For dynamic memory allocation in C, you have to deallocate the memory explicitly. If not done, you may encounter out of memory error.

The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.

The sizeof() operator is commonly used in C. It determines the size of the expression or the data type specified in the number of char-sized storage units. The sizeof() operator contains a single operand which can be either an expression or a data typecast where the cast is data type enclosed within parenthesis. The data type cannot only be primitive data types such as integer or floating data types, but it can also be pointer data types and compound data types such as unions and structs.

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

C file operations: Open, read, write and close operation in C. The fclose( ) function is used for closing an opened file. As argument you must provide a pointer to the file that you...

Take the integer which you want to check as input and store it in a variable number. Using if, else statements check whether the integer is Greater or Lesser than zero. If it is greater...



Write a Recursive Function in C Language to calculate sum of digits of a number. Declare Recursive Function to find sum of digits of a number. First give a meaningful name to the

C Program to input upper limit to find sum of even number. Store it in some variable say N. Initialize another variable to store sum with 0 say sum = 0. To find sum of even numbers we



Status epilepticus, print help and exit with exval. Print version and exit with exval. Print this help and exit. Print program version and exit. Option needs an argument. Open file...

Pointer var1 is pointing to the first element of the array and the var2 is the size of the array. In the loop we are incrementing pointer so that it points to the next element of the array

Compare 2 strings, 'Concatenate Strings' and copy one string to another & perform various 'string manipulation operations'. We perform such operations using 'pre-defined' functions