data structure code example

Example 1: purpose of data structure

In computer science, a data structure is a data organization, management,
and storage format that enables efficient access and modification. 
More precisely, a data structure is a collection of data values, 
the relationships among them, and the functions or operations that can be
applied to the data.

Example 2: data structures

Data Structures
in Java: Array, Collections Framework, Map.

1-ARRAY:
- It is fixed size.
- Ordered
-Allows Duplicates
-Can store Primitives and objects
-Can be multi-dimensional
-Build in data structure

2-COLLECTIONS: (LIST, SET, QUQUE)
List: Can store duplicate values,
      Keeps the insertion order. 
      It allows multiple null values, 
      Also we can read a certain value by index.
- ArrayList not syncronized, array based class 
- LinkedList not synchronized, doubly linked
- Vector is synchronized, thread safe

Set: Can only store unique values, 
     And does not maintain order
- HashSet can have null, order is not guaranteed
- LinkedHashSet can have null and keeps the order 
- TreeSet sorts the order and don't accept null 

Quque : Accepts duplicates, 
        Doesn't have index num,
        First in first our order.

3-MAP:
is a (key-value format) 
      and keys are always unique, 
      and value can be duplicated. 
- HashTable don't have null key, sychronized(thread-safe)
- LinkedHashMap can have null key, keeps order
- HasHMap can have null key, order is not guaranteed
- TreeMap doesn't have null key and keys are sorted

Example 3: data structures in c

// Stack implementation in C

#include <stdio.h>
#include <stdlib.h>

#define MAX 10

int count = 0;

// Creating a stack
struct stack {
  int items[MAX];
  int top;
};
typedef struct stack st;

void createEmptyStack(st *s) {
  s->top = -1;
}

// Check if the stack is full
int isfull(st *s) {
  if (s->top == MAX - 1)
    return 1;
  else
    return 0;
}

// Check if the stack is empty
int isempty(st *s) {
  if (s->top == -1)
    return 1;
  else
    return 0;
}

// Add elements into stack
void push(st *s, int newitem) {
  if (isfull(s)) {
    printf("STACK FULL");
  } else {
    s->top++;
    s->items[s->top] = newitem;
  }
  count++;
}

// Remove element from stack
void pop(st *s) {
  if (isempty(s)) {
    printf("\n STACK EMPTY \n");
  } else {
    printf("Item popped= %d", s->items[s->top]);
    s->top--;
  }
  count--;
  printf("\n");
}

// Print elements of stack
void printStack(st *s) {
  printf("Stack: ");
  for (int i = 0; i < count; i++) {
    printf("%d ", s->items[i]);
  }
  printf("\n");
}

// Driver code
int main() {
  int ch;
  st *s = (st *)malloc(sizeof(st));

  createEmptyStack(s);

  push(s, 1);
  push(s, 2);
  push(s, 3);
  push(s, 4);

  printStack(s);

  pop(s);

  printf("\nAfter popping out\n");
  printStack(s);
}

Example 4: data structures used

Depending on the data that I am working with, I use
Arrays, Lists, Sets, Maps.

Example 5: data structures

Implementation of all basic Data Structures in Python at this link:

https://github.com/shreyasvedpathak/Data-Structure-Python

Tags:

C Example