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

C Program to Display the Nodes of a Linked List in Reverse using Recursion

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
/* C Program to Display the Nodes of a Linked List in Reverse using Recursion This C Program uses recursive function & reverses the nodes in a linked list and displays the list. A linked list is an ordered set of data elements, each containing a link to its successor. This program makes each data element to link to its predecessor. */ #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; void print_reverse_recursive (struct node *); void print (struct node *); void create_new_node (struct node *, int ); //Driver Function int main () { struct node *head = NULL; insert_new_node (&head, 1); insert_new_node (&head, 2); insert_new_node (&head, 3); insert_new_node (&head, 4); printf ("LinkedList : "); print (head); printf ("\nLinkedList in reverse order : "); print_reverse_recursive (head); printf ("\n"); return 0; } //Recursive Reverse void print_reverse_recursive (struct node *head) { if (head == NULL) { return; } //Recursive call first print_reverse (head -> next); //Print later printf ("%d ", head -> data); } //Print the linkedlist normal void print (struct node *head) { if (head == NULL) { return; } printf ("%d ", head -> data); print (head -> next); } //New data added in the start void insert_new_node (struct node ** head_ref, int new_data) { struct node * new_node = (struct node *) malloc (sizeof (struct node)); new_node -> data = new_data; new_node -> next = (*head_ref); (*head_ref) = new_node; }

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

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.

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

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,

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.



C program code ask the user for operation like insert, delete, display and exit. According to the option entered, access its respective function using switch statement and use the


This C Program code calculates volume and surface area of cylinder. The formula used in this C Program 'Surface_area' = 2 * Pi * r * (r + h) & Volume = Pi * r * r * h where Pi = 22/7.

C Program to find the nth 'fibonacci number' using recursion. Program prints the fibonacci of a number. In fibonacci series, each number is the sum of the 2 preceding numbers. 0, 1, 1,

Take a string and a character as input. Store it in the array "str[]" & variable key respectively. Using for loop search for the variable key. If it is found then increment the variable count. If

Check if string is a 'Palindrome' without using the "Built-In function". "Take a string" as input & store it in the array 'string[]'. Then store the same string into the another array 'reverse_s'

C Programming language code to search for a palindrome. Please enter a word, phrase or sentence below(type END to exit the program). String is a palindrome or it is not...


Fibonacci series is a series of numbers where the current number is the sum of previous 2 terms. For Example: 0, 1, 1, 2, 3, 5, 8, 13, 21,... Here c is the current term, b is the n-1th term