C Programming Code Examples
C > Linked Lists Code Examples
Combine Two Doubly Linked Lists
/* Combine Two Doubly Linked Lists */
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
struct node *list = NULL;
struct node *list_last = NULL;
struct node *even = NULL;
struct node *even_last = NULL;
struct node *odd = NULL;
struct node *odd_last = NULL;
struct node *current = NULL;
//Create Linked List
void insert(int data) {
// Allocate memory for new node;
struct node *link = (struct node*) malloc(sizeof(struct node));
link->data = data;
link->prev = NULL;
link->next = NULL;
// If head is empty, create new list
if(data%2 == 0) {
if(even == NULL) {
even = link;
return;
}else {
current = even;
while(current->next != NULL)
current = current->next;
// Insert link at the end of the list
current->next = link;
even_last = link;
link->prev = current;
}
}else {
if(odd == NULL) {
odd = link;
return;
}else {
current = odd;
while(current->next!=NULL)
current = current->next;
// Insert link at the end of the list
current->next = link;
odd_last = link;
link->prev = current;
}
}
}
//display the list
void print_backward(struct node *head) {
struct node *ptr = head;
printf("\n[last] <=>");
//start from the beginning
while(ptr != NULL) {
printf(" %d <=>",ptr->data);
ptr = ptr->prev;
}
printf(" [head]\n");
}
//display the list
void printList(struct node *head) {
struct node *ptr = head;
printf("\n[head] <=>");
//start from the beginning
while(ptr != NULL) {
printf(" %d <=>",ptr->data);
ptr = ptr->next;
}
printf(" [last]\n");
}
void combine() {
struct node *link;
list = even;
link = list;
while(link->next!= NULL) {
link = link->next;
}
link->next = odd;
odd->prev = link;
// assign link_last to last node of new list
while(link->next!= NULL) {
link = link->next;
}
list_last = link;
}
int main() {
int i;
for(i=1; i<=10; i++)
insert(i);
printf("Even : ");
printList(even);
printf("Even (R): ");
print_backward(even_last);
printf("Odd : ");
printList(odd);
printf("Odd (R) : ");
print_backward(odd_last);
combine();
printf("Combined List :\n");
printList(list);
printf("Combined List (R):\n");
print_backward(list_last);
return 0;
}
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:
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,
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 for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.
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.
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.
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.
The first expression conditionalExpression is evaluated first. This expression evaluates to 1 if it's true and evaluates to 0 if it's false. If the conditionalExpression is true, expression1 is
C program to Count total number of Vowel or Consonant in a string using switch case. Input String from user, store it in some variable say 'str'. Initialize 2 other variables to store vowel
We have declared one pointer variable and one array. Address of first element of array is stored inside pointer variable. Accept Size of an Array. Now we have accepted element...