c++ node struct code example

Example 1: linkedlist implementation in c++

#include <iostream>

using namespace std;

struct node
{
    int data;
    node *next;
};

class linked_list
{
private:
    node *head,*tail;
public:
    linked_list()
    {
        head = NULL;
        tail = NULL;
    }

    void add_node(int n)
    {
        node *tmp = new node;
        tmp->data = n;
        tmp->next = NULL;

        if(head == NULL)
        {
            head = tmp;
            tail = tmp;
        }
        else
        {
            tail->next = tmp;
            tail = tail->next;
        }
    }
};

int main()
{
    linked_list a;
    a.add_node(1);
    a.add_node(2);
    return 0;
}

Example 2: what is a struct in c++

struct Person
{
    char name[50];
    int age;
    float salary;
};

Example 3: struct c++

struct product {
  int weight;
  double price;
} apple, banana, melon;

Tags:

Cpp Example