Question: Given a Linked List, we need to find out if the number of nodes in the Link List are odd or even? Input: 4, 8, 15, 16, 23, 42 Output: EVEN The most easy method to solve this problem would be by traversing the entire Linked List and counting the number of nodes as we go. As the loop is finished, we can check if the count is even or odd. A Simpler Way: Another way of solving this problem in less time would be advancing 2 nodes at…
nikoo28
nikoo28
a tech-savvy guy and a design buff... I was born with the love for exploring and want to do my best to give back to the community. I also love taking photos with my phone to capture moments in my life. It's my pleasure to have you here.
-
-
Question: Print the Linked List in reverse order. The actual structure of the Linked List must remain intact. Input: 4, 8, 15, 16, 23, 42 Output: 42, 23, 16, 15, 8, 4 We just discussed how to reverse a Linked List in an older post. Reverse a singly Linked List. But, in the above described method we changed the entire structure of the Linked List, the last node was no longer the last node and the head node had also changed. We might encounter cases, where we just need to…
-
Question: How will you reverse a singly Linked List? Input:– 4 -> 8 -> 15 -> 16 -> 23 -> 42 Output:– 42 -> 23 -> 16 -> 15 -> 8 -> 4 Reversing a singly Linked List is based upon a very simple concept. We just need to swap the “NEXT” value of each of the node, as we iterate over the list. That means that after the complete process, the first node should point to NULL, and the second last node should point to the first node…and so…
-
Question: In C you can do something like:- char s[] = “Hello World”; and char *s = “Hello World”; What is the difference between the two of them? What happens to the memory during compile time and run-time? This declaration: Creates ONE object – a char array of size 6, called s, initialized with the values ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears. If the declaration is within a function, it will…
-
Question: C does not have the boolean data type by default. Is there a way by which we can implement it? In C language, we do not have the boolean datatype by default. That means we cannot do something like We can do this in C++ and JAVA. However, we have an alternative method to achieve the same functionality. Before moving on to the implementation it is important to know that in C, if a value is ‘0 (zero)’ it is treated as false, and any other value other than…
-
Question: Write a program to insert a node in a given sorted Linked List. Given List: 23 -> 32 -> 99 -> 101 -> 2222 Node to add:- 50 Output:- 23 -> 32 -> 50 -> 99 -> 101 -> 2222 To insert a node in a sorted Linked List, we need to perform a basic Linked List operation discussed in this post. Insert a node in a Linked List. In the given post, we knew the position at which we had to insert the node. But in this case…
-
Question: What is the difference between doing: ptr = (char **) malloc (MAXELEMS * sizeof(char *)); or: ptr = (char **) calloc (MAXELEMS, sizeof(char*)); When is it a good idea to use calloc and when to use malloc ? There are two differences:- Firstly it is in the number of arguments, malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. calloc() allocates a memory area, the length…
-
Question: In C, what is an unsigned char used for? How is this different from a regular char? In C, there are three distinct character types: char signed char unsigned char If you are using character types for text, use the unqualified char: it is the type of character literals like ‘a’ or ‘0’. it is the type that makes up C strings like “abcde” It also works out as a number value, but it is unspecified whether that value is treated as signed or unsigned. Beware character comparisons through…
-
Question: In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop ? i++ is known as Post Increment whereas ++i is called Pre Increment. ++i will increment the value of i, and then return the incremented value. i++ will increment the value of i, but return the original value that i held before being incremented. For a for loop, either works. ++i seems more common, perhaps because that is what is used in Dennis Ritchie. In…
-
Question: How can you divide a number by 3 without using *, /, +, – % operator? Input: 48 Output: 16 The method below implements the method using bit-wise operators. For example if your number is 10 then convert to binary 10 =>00001010 if 10 > 3 then shift the binary number 2 bits Now num will be 00000010 ie, 2 Now sum will be 2 num = (shift the number by 2 bits)+(number BITWISE AND 3) num = 2+2 Now the number will be 4 then, 4>3 => true…