Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Linked list in c++

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

/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;

/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));

/* Assign data values */
one->data = 1;
two->data = 2;
three->data=3;

/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;

/* Save address of first node in head */
head = one;
 
PREVIOUS NEXT
Tagged: #Linked #list
ADD COMMENT
Topic
Name
1+2 =