Dynamic Data Structures how to use

If you need to take data from the user that could be any length, you could define a really big array like so:
int array[10000];
There are several problems with this. No matter how big the array is, the user could still have more input. or If the user doesn’t have that much input, you have wasted memory.

When you are using malloc(), realloc() and free() you need the following header file:

#include <stdlib.h>

The process of allocating memory at runtime is known as

dynamic memory allocation

the malloc() allocates requested size of bytes and returns a void pointer pointing to the first byte of the allocated space
calloc() allocates space for an array of elements, initialize them to zero and then return a void pointer to the memory
free releases previously allocated memory
realloc modify the size of previously allocated space

now we can see this for Array in C
int a1[100]; // declare a static array, a1, of 100 ints
char c1[50]; // declare a static array, c1, of 50 chars
that is a static declaration

int *p_array;
double *d_array;

// call malloc to allocate that appropriate number of bytes for the array

p_array = (int *)malloc(sizeof(int)*50);      // allocate 50 ints
d_array = (int *)malloc(sizeof(double)*100);  // allocate 100 doubles

// use [] notation to access array buckets 
// (THIS IS THE PREFERED WAY TO DO IT)
for(i=0; i < 50; i++) {
  p_array[i] = 0;
}

// you can use pointer arithmetic (but in general don't)
double *dptr = d_array;    // the value of d_array is equivalent to &(d_array[0])
for(i=0; i < 50; i++) {
  *dptr = 0;
  dptr++;
}

that is the first example of

Dynamic Data Structures how to use

In C++ we use new

int main()
{
  int size;

  std::cin >> size;

  int *array = new int[size];

  delete [] array;

  return 0;
}

Don’t forget to delete every array you allocate with new.
because of its memory issue always.

You might want to consider using the Standard Template Library. It’s simple and easy to use, plus you don’t have to worry about memory allocations.

int size = 5; // declare the size of the vector
vector myvector(size, 0); // create a vector to hold “size” int’s
// all initialized to zero
myvector[0] = 1234;

Linked List

Let’s define a linked list node:

typedef struct node {
    int val;
    struct node * next;
} node_link;

Notice that we are defining the struct in a recursive manner, which is possible in C. Let’s name our node type node_link.

Now we can use the nodes. Let’s create a local variable which points to the first item of the list (called head).

node_link * head = NULL;
head = malloc(sizeof(node_link));
if (head == NULL) {
    return 1;
}

head->val = 1;
head->next = NULL;

We’ve just created the first variable in the list. We must set the value, and the next item to be empty if we want to finish populating the list. Notice that we should always check if malloc returned a NULL value or not.

To add a variable to the end of the list, we can just continue enhancing to the next pointer:

node_link * head = NULL;
head = malloc(sizeof(node_link));
head->val = 1;
head->next = malloc(sizeof(node_link));
head->next->val = 2;
head->next->next = NULL;

In C++

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

int main()
{
  node *root;       // This won't change, or we would lose the list in memory
  node *cc;  // This will point to each node as it traverses the list

  root = new node;  // Sets it to actually point to something
  root->next = 0;   //  Otherwise it would not work well
  root->x = 12;
  cc= root; // The conductor points to the first node
  if ( cc != 0 ) {
    while ( cc->next != 0)
      cc= cc->next;
  }
  cc->next = new node;  // Creates a node at the end of the list
  cc= cc->next; // Points to that node
  cc->next = 0;         // Prevents it from going any further
  cc->x = 42;
}

Dynamic Data Structures how to useDynamic Data Structures how to use
All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

One comment: On Dynamic Data Structures how to use

Leave a reply:

Your email address will not be published.