0
Q:

data structures in java

#string
string = 'Hi my name is Dr.Hippo'
#integer(whole number)
integer = 12345
#floating point number
fl = 123.456
#list
li = [1,2,3,,'string',None]
#boolean
t = True
f = False
#none(equivalent to null or undefined in other languages)
n = None
#dictionary
dictionary = {'key':'value'}
6
Data Structures
in Java: Array, Collections Framework, Map.
Depending on the data that I am working with, I use Arrays , lists,sets, maps

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


List (implementing class ArrayList, Stack, LinkedList,
Vector)
List is the only Interface that has index.
- Size is dynamic.
- Ordered
- Allows Duplicates
- Can not store primitives

ArrayList (It’s array based collection. Internally uses
arrays. Dynamic array)
- Size is dynamic.
- Ordered
- Allows Duplicates
- Can not store primitives
- Implementing class of List interface in Collection
framework

  
  LinkedList
- Stores data in individual nodes that are
connected to each other
- Each node will have a pointer to the next
node/value
- Implementing class of List and Deque interfaces
  
  
  Set (implementing class HashSet, LinkedHashSet)
Collection of unique/distinct values. No duplicates.
- Size is dynamic.
- Not ordered
- Unique values
When we want the Set or Map to accept only unique
objects from our custom class, we need to override
equals() and hashCode() methods inside our custom
class.
0
class Test { 
      
// class contents 
void show()  
{ 
    System.out.println("Test::show() called"); 
} 
} 
  
public class Main { 
      
    // Driver Code 
    public static void main(String[] args) 
    { 
          
        // all objects are dynamically  
        // allocated 
        Test t = new Test();  
        t.show(); // No error  
    } 
} 
0
in javascript data structures includes arrays, maps, and sets
0
// 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);
}
0

New to Communities?

Join the community