Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Complex Data Structure | Advanced Structs Usage
C Structs
course content

Conteúdo do Curso

C Structs

C Structs

1. Introduction to Structs
2. Pointers and Structs
3. Structs and Memory
4. Advanced Structs Usage
5. Implementing Data Structures

book
Complex Data Structure

Data structures enable efficient storage, organization and management of data.

For example, the use of arrays, lists, and trees allows programmers to manage data more efficiently than simply sequential storage.

In the C language, structures allow you to implement a wide variety of data structures. An example of the implementation of the most famous data structures using structures is discussed below.

Arrays: Used when you need to store a fixed number of elements and have quick access to them.

Linked Lists: Useful for cases where the number of elements changes dynamically.

struct Node {
    int data;           // data in node
    struct Node* next;  // pointer to next node
};

Hash tables: Used to look up data by key.

The hash function is needed to convert the key into an index of the array in which the data is stored.

For example, the key is your client's name and the required value is their debt.

You already have an array with all the names and debts.

A hash function using an ASCII table converts your client's name into some digital equivalent and as a result you get an index under which all information about the client is stored.

In this example, the hash function converts a key string into an array index using the sum of the ASCII character values and modulo division of the table size.

struct Node {
    char* key;              // key
    int value;                // value
    struct Node* next;   // pointer to next node
};

struct HashTable {
    struct Node** table; // array of pointers to nodes
    int size;                   // table size
};

unsigned int hashFunction(char* key, int size) {
    unsigned int hash = 0;
    while (*key) {
        hash += *key++;
    }
    return hash % size;
}

Trees: Useful for building hierarchies and performing quick searches, additions, and deletions.

A binary tree is a type of tree in which each node has at most two child nodes. These child nodes are usually called left and right children.

struct node
{
	int data;
	struct node* left; 
	struct node* right; 
};

When a node has its own child nodes, it acts as a parent node to those nodes. At the same time, this node itself can be a child node of its immediate parent node.

Stack: Used to model networks and relationships.

A stack is a data structure in which elements are added (push) and removed (pop) according to the LIFO principle - (Last In, First Out).

// Stack structure
typedef struct {
    int data[MAX_SIZE]; 
    int top;
} Stack;

MAX_SIZE - the maximum number of elements a stack can contain.

// Push an element onto the stack
void push(Stack *stack, int value) {
    stack->data[++stack->top] = value;  // Increment top and add the element
    printf("Element %d pushed onto the stack\n", value);
}
// Pop an element from the stack
int pop(Stack *stack) {
    int value = stack->data[stack->top--];  // Retrieve the element and decrement top
    printf("Element %d popped from the stack\n", value);
    return value;
}

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

course content

Conteúdo do Curso

C Structs

C Structs

1. Introduction to Structs
2. Pointers and Structs
3. Structs and Memory
4. Advanced Structs Usage
5. Implementing Data Structures

book
Complex Data Structure

Data structures enable efficient storage, organization and management of data.

For example, the use of arrays, lists, and trees allows programmers to manage data more efficiently than simply sequential storage.

In the C language, structures allow you to implement a wide variety of data structures. An example of the implementation of the most famous data structures using structures is discussed below.

Arrays: Used when you need to store a fixed number of elements and have quick access to them.

Linked Lists: Useful for cases where the number of elements changes dynamically.

struct Node {
    int data;           // data in node
    struct Node* next;  // pointer to next node
};

Hash tables: Used to look up data by key.

The hash function is needed to convert the key into an index of the array in which the data is stored.

For example, the key is your client's name and the required value is their debt.

You already have an array with all the names and debts.

A hash function using an ASCII table converts your client's name into some digital equivalent and as a result you get an index under which all information about the client is stored.

In this example, the hash function converts a key string into an array index using the sum of the ASCII character values and modulo division of the table size.

struct Node {
    char* key;              // key
    int value;                // value
    struct Node* next;   // pointer to next node
};

struct HashTable {
    struct Node** table; // array of pointers to nodes
    int size;                   // table size
};

unsigned int hashFunction(char* key, int size) {
    unsigned int hash = 0;
    while (*key) {
        hash += *key++;
    }
    return hash % size;
}

Trees: Useful for building hierarchies and performing quick searches, additions, and deletions.

A binary tree is a type of tree in which each node has at most two child nodes. These child nodes are usually called left and right children.

struct node
{
	int data;
	struct node* left; 
	struct node* right; 
};

When a node has its own child nodes, it acts as a parent node to those nodes. At the same time, this node itself can be a child node of its immediate parent node.

Stack: Used to model networks and relationships.

A stack is a data structure in which elements are added (push) and removed (pop) according to the LIFO principle - (Last In, First Out).

// Stack structure
typedef struct {
    int data[MAX_SIZE]; 
    int top;
} Stack;

MAX_SIZE - the maximum number of elements a stack can contain.

// Push an element onto the stack
void push(Stack *stack, int value) {
    stack->data[++stack->top] = value;  // Increment top and add the element
    printf("Element %d pushed onto the stack\n", value);
}
// Pop an element from the stack
int pop(Stack *stack) {
    int value = stack->data[stack->top--];  // Retrieve the element and decrement top
    printf("Element %d popped from the stack\n", value);
    return value;
}

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 4
some-alt