Cranes Varsity Blog

A linked list is a way to store a collection of elements. Like an array these can be character or integers. Each element in a linked list is stored in the form of a node.

A node is a collection of two sub-elements or parts. A data part that stores the element and a next part that stores the link to the next node.

A linked list is formed when many such nodes are linked together to form a chain. Each node points to the next node present in the order. The first node is always used as a reference to traverse the list and is called HEAD. The last node points to NULL.

Declaring a Linked list :

In C language, a linked list can be implemented using structure and pointers .

struct LinkedList{

    int data;

    struct LinkedList *next;

 };

The above definition is used to create every node in the list. The data field stores the element and the next is a pointer to store the address of the next node.

Reverse a linked list

initialize three pointers prev as NULL, curr as head and next as NULL.

Iterate trough the linked list. In loop, do following.
// Before changing next of current,
// store next node
next = curr->next

// Now change next of current
// This is where actual reversing happens
curr->next = prev

// Move prev and curr one step forward
prev = curr
curr = next

#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node*link;

};

struct node*root=NULL;

void Append(void);

void reverse (void)

{

    struct node*temp = NULL,*temp1 = NULL;

    while(root) {

        temp1 = root->link;

        root->link = temp;

        temp = root;

        root = temp1;

    }

    struct node*p = temp;

    while(p) {

        printf("%d--->%p---->",p->data,&p->data);

       p = p->link;

    }

}

int main()

{

    while(1)

    {

        int ch;

        printf("1.Append:\n");

        printf("7.reverse:\n");

        printf("8.Quit:\n");

 

        printf("Enter the choice:\n");

        scanf("%d",&ch);

        switch(ch)

        {

            case 1 : Append();

                        break;

            case 2 : reverse();

                        break;

            case 3 : exit(1);

                        break;

            default : printf("Invalid choice\n");

        }

    }

}

void Append (void)

{

    struct node*temp;

    temp=(struct node*)malloc(sizeof(struct node));

    printf("enter the node value:\n");

    scanf("%d",&temp->data);

    temp->link=NULL;

    if(root==NULL)  {   //list is empty

        root=temp; }

    else{

        struct node*p;

        p=root;

        while(p->link!=NULL) {

            p=p->link;}

        p->link=temp;

}

}

The 5 technology trends to consider before the actual fully autonomous vehicles become a fact. These also show us that hows our future of automotive looks like.

Vehicles as IoT Devices

The internet of things provides a great potential for inter-connectivity, linking smart vehicles into a network. This provides the possibility of optimizing vehicle performance and driver experience.  This trend also leverages the ability of IC manufacturers to integrate enhanced functionality into more powerful microprocessor.

The wireless connectivity integration not only simplifies communication between vehicle’s subsystems but serves to link it with external networks, other vehicles or traffic control systems. The connected cars and the collected data offer potential for other features such as predictive maintenance, traffic management and of course the most important, enhanced safety and emergency response capabilities.

Our vehicles are no longer limited productivity spaces. What’s more, we can just continue with our other activities on the go, while still not being distracted from the road. This leads us to the next trend.

Advanced Driver Assistance Systems

The advanced driver assistance system (ADAS) is designed to reduce the driver’s work flow during the driving process itself. It’s based on embedded vision technology, which consists of vision systems that surround the vehicle, looking to place it within a protective bubble against driver error, road obstacles, other vehicles, and pedestrians.

These vision systems are capable of identification and real-time tracking. They usually require specialized knowledge of image acquisition and processing techniques. However, today the development of these complex systems have been simplified.

The dedicated vision processors and multicore CPUs have put the sophisticated vision capabilities in most engineers’ hands. There are even some development kits that lower the barriers to entry into embedded vision technologies. This means they are getting more and more widespread among new vehicle.

Advanced Motor Control

Modern vehicles are filled with dedicated motor-control systems, which drive fans, pumps, compressors, and servomechanisms of all types. The growing desire of maximum efficiency and control has motivated engineers to move beyond traditional scalar control systems to more sophisticated digital vector control algorithms capable of delivering full torque with acceleration and deceleration at rates that can be precisely managed.

Many cost-effective solutions are developed, that still provide maximum control capability and enable engineers to harness the power of advanced vector control methods. These sophisticated methods provide great flexibility, but also present designers with some challenges.

That’s why for the achievement of stable operation through all corner cases of a vector control design can demand highly specialized knowledge and often leads to slips in tight development cycles.

However, advanced motor control is a tech trend that provides an improved driver experience.

Predictive Technology

Artificial intelligence and machine learning impact almost all technological aspects. They have an essential role in the future of the automotive industry as well. Predictive capabilities are now becoming prevalent in cars. They provide a highly personalized driving experience.

Manufacturers are applying AI and ML algorithms to automate the setting up process of a vehicle, including its infotainment system and its application preferences. This makes vehicles our new IoT devices that connect to our smart phones or wearable devices and actually understand us.

The predictive technologies could also be implemented as sensors within a car. They can inform the driver if the vehicle needs service and estimate its performance, depending on the mileage and condition as well.

Vehicle-to-Everything Communication

As we already noted above, connectivity is among the key trends in the automotive industry. You’ve seen the IoT trend above as well. However, this one goes beyond network connectivity.  It’s actually already changing the future of driving. Intelligent traffic systems will be created, in order to avoid collisions, send emergency vehicle notifications to provide car diagnostics, and manage to park.

The vehicle-to-everything technology will send real-time traffic notifications, accident alerts, and other warnings. It’s set to increase traffic safety, sent forward-collision warnings, detect obstacles and offer safety measures. This technology will take us a step closer to smart roadways and safer driving experience.

If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the function.

By using the keyword friend compiler knows the given function is a friend function.

For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend.

Declaration of friend function in C++

Class class_name

{

friend data_type function_name(argument/s); // syntax of friend function.

};

In the above declaration, the friend function is preceded by the keyword friend.

The function can be defined anywhere in the program like a normal C++ function.

The function definition does not use either the keyword friend or scope resolution operator.

Characteristics of a Friend function:

  • The function is not in the scope of the class to which it has been declared as a friend
  • It cannot be called using the object as it is not in the scope of that class
  • It can be invoked like a normal function without using the object
  • It cannot access the member names directly and has to use an object name and dot membership operator with the member name
  • It can be declared either in the private or the public part

C++ friend function Example

Let's see the simple example of C++ friend function used to print the length of a box.

            #include <iostream>

            using namespace std;

            class Box

            {

                        private:

                        int length;

            public:

            Box() : length(0) {}

            friend int printLength(Box); // friend function

            };

            int printLength(Box b)

            {

                        b.length + = 10;

                        return b.length;

            }

            int main()

            {

                        Box b;

                        cout  << “Length of box: “ << printLength(b) << endl;

                        return 0;

            }

Output:

Length of box : 10

C++ Friend class

A friend class can access both private and protected members of the class in which it has been declared as friend.

Let's see a simple example of a friend class.

            #include <iostream>

            using namespace std;

            class A

            {

                        int x =5;

                        friend class B; // friend class

            };

            class B

            {

                        public:

                        void display(A &a)

                        {

                                    cout << “value of x is : “ << a.x << endl;    

                        }

            };

            int main()

            {
                        A a;

                        B b;

                        b.display(a);

                        return 0;

            }

 

Output:

Value of x is : 5

In the above example, class B is declared as a friend inside the class A. Therefore, B is a friend of class A. Class B can access the private members of class A.

The most important function of C is main() function. It is mostly defined with a return type of int and without parameters :
int main() { /* ... */ }
We can also give command-line arguments in C . Command-line arguments are given after the name of the program in command-line shell of Operating Systems.
To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.
int main(int argc, char *argv[]) { /* ... */ }
or
int main(int argc, char **argv) { /* ... */ }
argc (ARGument Count) is int and stores number of command-line arguments passed by the user including the name of the program. So if we pass a value to a program, value of argc would be 2 (one for argument and one for program name)
The value of argc should be non negative.
argv(ARGument Vector) is array of character pointers listing all the arguments.
If argc is greater than zero,the array elements from argv[0] to argv[argc-1] will contain pointers to strings.
Argv[0] is the name of the program , After that till argv[argc-1] every element is command -line arguments.

For better understanding run this code on your linux machine.
// Name of program mainreturn.cpp
#include <stdio.h>
int main(int argc, char** argv)
{
      printf("You have entered argc arguments:\n");
      for (int i = 0; i < argc; ++i)
      printf(“%s”, argv[i]);
      return 0;
}


Input:
$ gcc mainreturn.cpp -o main
$ ./main cranes varsity

Output:
You have entered 4 arguments:
./main
cranes
varsity


Properties of Command Line Arguments:
1. They are passed to main() function.
2. They are parameters/arguments supplied to the program when it is invoked.
3. They are used to control program from outside instead of hard coding those values inside the code.
4. argv[argc] is a NULL pointer.
5. argv[0] holds the name of the program.
6. argv[1] points to the first command line argument and argv[n] points last argument.
Note : You pass all the command line arguments separated by a space, but if argument itself has a space then you can pass such arguments by putting them inside double quotes “” or single quotes ”.
// C program to illustrate
// command line arguments
#include<stdio.h>
int main(int argc,char* argv[])
{
      int counter;
      printf("Program Name Is: %s",argv[0]);
      if(argc==1)
      printf("\nNo Extra Command Line Argument Passed Other Than Program Name");
      if(argc>=2)
      {
      printf("\nNumber Of Arguments Passed: %d",argc);
      printf("\n----Following Are The Command Line Arguments Passed----");
      for(counter=0;counter<argc;counter++)
      {
      printf("\nargv[%d]: %s",counter,argv[counter]);
      }
      }
      return 0;
}


Output in different scenarios:
1. Without argument: When the above code is compiled and executed without passing any argument, it produces following output.
$ ./a.out
Program Name Is: ./a.out
No Extra Command Line Argument Passed Other Than Program Name
2. Three arguments : When the above code is compiled and executed with a three arguments, it produces the following output.
$ ./a.out First Second Third
Program Name Is: ./a.out
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First
argv[2]: Second
argv[3]: Third
3. Single Argument : When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following output.
$ ./a.out "First Second Third"
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third
4. Single argument in quotes separated by space : When the above code is compiled and executed with a single argument separated by space but inside single quotes, it produces the following output.
$ ./a.out 'First Second Third'
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third

 

Macros are preprocessed, meaning that all the macros would be executed before compilation stage. However, functions are not preprocessed but compiled.

Example of Macro:

#include<stdio.h>

#define  A 10

int main()

{

     printf("%d",A);

     return 0;

}

 OUTPUT=10;

Example of Function:

#include<stdio.h>

int A()

{

    return 10;

}

int main()

{

    printf("%d", A());

    return 0;

}

OUTPUT=10;

Now compile them using the command:

gcc –E file_name.c

This will give you the executable code as shown below:

 

  • #include<stdio.h>

#define  A 10

int main()

{

     printf("%d",A);

     return 0;

}

 

  • #include<stdio.h>

int A()

{

    return 10;

}

int main()

{

    printf("%d", A());

    return 0;

}

The first program shows that the macros are preprocessed while functions are not.

  • In macros, no type checking (incompatible operand, etc.) is done and thus use of macros can lead to errors/side-effects in some cases. This is not the case with functions. Macros do not check for compilation error.
  • Macros are usually one liner. However, they can consist of more than one line there are no such constraints in functions.
  • The speed at which macros and functions differs. Macros are typically faster than functions as they don’t involve actual function call overhead.

MACRO

FUNCTION

Macro is Preprocessed

 

Function is Compiled

No Type Checking is done in Macro

Type Checking is Done in Function

Using Macro increases the code length

Using Function keeps the code length unaffected

Use of macro can lead to side effect at later stages

Functions do not lead to any side effect in any case

Speed of Execution using Macro is Faster

Speed of Execution using Function is Slower

Before Compilation, macro name is replaced by macro value

During function call, transfer of control takes place

Macros are useful when small code is repeated many times

Functions are useful when large code is to be written

Macro does not check any Compile-Time Errors

Function checks Compile-Time Errors