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 > Data Structures Code Examples

Linked List implementation

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
/* Linked List implementation */ #include"m_list.h" void main() { list *first=NULL,*second=NULL,*third=NULL; int choice,i; char ch='y'; while(1) { clrscr(); printf(" case 1: Create list"); printf(" case 2: Add in the list"); printf(" case 3: Delete in the list"); printf(" case 4: Append two list"); printf(" case 5: show list"); printf(" case 6: Exit"); printf(" Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: //create list while(ch!='n') { printf(" Enter element : "); scanf("%d",&i); create(&first,i); printf(" Enter element (y/n) : "); fflush(stdin); scanf("%c",&ch); } break; case 2: //add in the list int c; clrscr(); printf(" case 1: Add in Beginning"); printf(" case 2: Add in End"); printf(" case 3: Add After a given element"); printf(" case 4: Return to main menu"); printf(" Enter your choice : "); scanf("%d",&c); switch(c) { case 1: add_at_beg(&first); break; case 2: add_at_end(&first); break; case 3: add_after_given_element(&first); break; case 4: break; } break; case 3: clrscr(); printf(" case 1: Delete in Beginning"); printf(" case 2: Delete in End"); printf(" case 3: Delete a specified element"); printf(" case 4: Return to main menu"); printf(" Enter your choice : "); scanf("%d",&c); switch(c) { case 1: del_at_beg(&first); break; case 2: del_at_end(&first); break; case 3: del_specified_element(&first); break; case 4: break; } break; case 4: char ch='y'; printf("Enter element in second list : "); while(ch!='n') { printf(" Enter element : "); scanf("%d",&i); create(&second,i); printf(" Enter element (y/n) : "); fflush(stdin); scanf("%c",&ch); } append(&third,first,second); break; case 5: //show list clrscr(); printf(" case 1: List 1"); printf(" case 2: List 2"); printf(" case 3: List 3"); printf(" Enter choice : "); scanf("%d",&choice); switch(choice) { case 1: show(first);break; case 2: show(second);break; case 3: show(third);break; } break; case 6: exit(0); } } } #include<conio.h> #include<stdio.h> #include<alloc.h> #include<stdlib.h> typedef struct list { int info; struct list *next; }; //.................Function Declaration ........... void create(struct list **p,int i) { struct list *temp,*q=*p; temp=(struct list*)malloc(sizeof(struct list)); temp->info=i; temp->next=NULL; if(*p==NULL) *p=temp; else { while(q->next!=NULL) q=q->next; q->next=temp; } } int append(struct list **t,struct list *f,struct list *s) { struct list *temp=*t; if(f==NULL && s==NULL) return 0; while(f) { create(t,f->info); f=f->next; } while(s) { create(t,s->info); s=s->next; } return 0; } void show(struct list *p) { if(p==NULL) printf(" List is Empty"); else while(p) { printf("%d ",p->info); p=p->next; } getch(); } void add_at_beg(struct list **l) { struct list *temp=(struct list *)malloc(sizeof(struct list)); printf(" Enter element : "); scanf("%d",&temp->info); temp->next=NULL; if(*l==NULL) *l=temp; else { temp->next=*l; *l=temp; } } void del_at_beg(struct list **l) { list *temp; if(*l==NULL) { printf(" List is empty"); getch(); } else { temp=*l; *l=(*l)->next; free(temp); } } void add_at_end(struct list **l) { list *temp,*p; temp=(struct list *)malloc(sizeof(struct list)); printf(" Enter element : "); scanf("%d",&temp->info); temp->next=NULL; if(*l==NULL) *l=temp; else { p=*l; while(p->next!=NULL) p=p->next; p->next=temp; } } void del_at_end(struct list **l) { list *temp,*p; if(*l==NULL) { printf(" List is Empty"); getch(); } else if((*l)->next==NULL) { temp=*l; *l=NULL; free(temp); } else { p=*l; while(p->next->next!=NULL) p=p->next; temp=p->next->next; p->next=NULL; free(temp); } } void add_after_given_element(list **l) { list *temp,*p; int m; temp=(struct list *)malloc(sizeof(struct list)); printf(" Enter element : "); scanf("%d",&temp->info); printf(" Enter position after which element inserted : "); scanf("%d",&m); temp->next=NULL; if(*l==NULL) *l=temp; else { p=*l; while(p->next!=NULL) if(p->info==m) break; else p=p->next; temp->next=p->next; p->next=temp; } } void del_specified_element(list **l) { list *temp,*p,*q; int m; printf(" Enter element which is deleted : "); scanf("%d",&m); if(*l==NULL) { printf(" List is Empty"); getch(); } else if((*l)->next!=NULL && (*l)->info==m) { temp=*l; *l=(*l)->next; free(temp); } else if((*l)->next==NULL && (*l)->info==m) { temp=*l; *l=NULL; free(temp); } else { p=*l; while(p!=NULL) if(p->info==m) break; else { q=p; p=p->next; } temp=p; q->next=p->next; free(temp); } }

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.

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.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block.

The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program. The exit() function is the standard library function of the C, which is defined in the stdlib.h header file. So, we can say it is the function that forcefully terminates the current program and transfers the control to the operating system to exit the program. The exit(0) function determines the program terminates without any error message, and then the exit(1) function determines the program forcefully terminates the execution process.

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.

#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:

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.

Function clrscr() clears the screen and moves the cursor to the upper left-hand corner of the screen. If you are using the GCC compiler, use system function to execute the clear/cls command. clrscr() function is also a non-standard function defined in "conio.h" header. This function is used to clear the console screen. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.

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.

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.

Flush stream. If the given stream was open for writing (or if it was open for updating and the last i/o operation was an output operation) any unwritten data in its output buffer is written to the file. If stream is a null pointer, all such streams are flushed. In all other cases, the behavior depends on the specific library implementation. In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior).

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.

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.

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 typedef is a keyword used in C programming to provide some meaningful names to the already existing variable in the C program. It behaves similarly as we define the alias for the commands. In short, we can say that this keyword is used to redefine the name of an already existing variable.

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,


C program code input a character and check whether the character is alphabet or not using Conditional/Ternary operator ?:. How to check alphabets by conditional operator...







C Code to check if the "array is sorted" or not. Shuffle the "Array Elements" and check if the array is sorted or not & if not sorted calls the shuffle function to shuffle the array elements


If the ASCII value of the character entered by the user lies in the range 97 to 122 or from 65 to 90, that number is an alphabet. 'a' is used instead of 97 and 'z' is used instead of 122. 'A'