Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
Windows Tipps & SecurityGrafikkarte vor Überhitzung schützen: So geht’s(25.09.2026 um 08:00 Uhr)
••••••••••
Windows Tipps & SecurityGrafikkarte vor Überhitzung schützen: So geht’s(25.09.2026 um 08:00 Uhr)
••••••••••
Intelligence View
⚡ tsecurity.de Intelligence

Set Data Structure in C

In this article I will show how to implement a Set data structure in C using a hashtable, and discuss complexity, trade-offs, and possible improvements. Prerequisites: Basic knowledge of programming (logic, etc); C syntax,…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

  • In this article I will show how to implement a Set data structure in C using a hashtable, and discuss complexity, trade-offs, and possible improvements.






Prerequisites:




  • Basic knowledge of programming (logic, etc);

  • C syntax, allocating variables;

  • Memory management in C: pointers, malloc, free;

  • Basic understanding of hashing;






What is a Set?




  • A data structure that stores unique elements.

  • Same idea as in set theory in mathematics: no repeated values are allowed.






Why use a Hashtable?




  • A hashtable is a good choice for implementing a Set:


    • insert, find, remove -> O(1) average






  • Collisions can happen, so we need a strategy to handle them.







Collision Handling (Chaining)




  • In this implementation, collisions are handled using a linked list per bucket.

  • Multiple elements that hash to the same index are stored in the same list.






Complexity





  • Let:




    • m = number of buckets

    • k = elements in one bucket

    • n = total elements








  • Worst case:




    • insert O(k)

    • find O(k)

    • remove O(k)

    • iterate O(m + n)

    • isEmpty O(1)








  • Average case:




    • insert O(1)

    • find O(1)

    • remove O(1)

    • iterate O(m + n) -> O(n) if m is constant

    • isEmpty O(1)











Trade-offs and Improvements





  • Load factor not handled:




    • I haven't handled load factor by rehashing since the focus is the Set itself.

    • This can degrade performance as n grows.

    • Improvement: dynamic resizing (grow/shrink).








  • Hashtable vs Balanced Binary Search Tree (BST):




    • A hashtable is generally better for unordered sets, because it provides constant-time average performance and does not maintain order.

    • A balanced BST is preferable only when worst-case guarantees or sorted order are more important than average performance.

    • A set is an unordered data structure, so using a hashtable is preferable.








  • Time complexity comparison (insert, find, remove):




    • Hashtable:

    • Average case: O(1)

    • Worst case: O(n)

    • Balanced BST:

    • Average case: O(log n)

    • Worst case: O(log n)






  • To check load factor handling and hashtable growth using open addressing with double hashing:


    https://github.com/godinhojoao/dsa-studies/blob/main/dsa-in-c/hashtable.c








How to Develop a Set in C




  • In this code:


    • A hashtable with fixed size is used

    • Collisions are handled with linked lists (chaining)

    • FNV-1a is used as the hash function









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

#define SET_LIMIT_SIZE 1000

typedef struct SetNode {
struct SetNode* next;
int value;
} SetNode;

typedef struct Set {
SetNode** nodes;
int currLength;
} Set;

// 32-bit FNV-1a
unsigned int fnv1a_int(int value) {
unsigned int hash = 2166136261u;
unsigned char* p = (unsigned char*)&value;

for(int i = 0; i < (int)sizeof(int); i++) {
hash ^= (unsigned int)p[i];
hash *= 16777619u;
}

return hash;
}

int itemIndex(int value, int arrLimit) {
unsigned int hash = fnv1a_int(value);
return hash % arrLimit;
}

Set* initializeSet() {
Set* set = malloc(sizeof(Set));
set->currLength = 0;
set->nodes = malloc(sizeof(SetNode*) * SET_LIMIT_SIZE);

for(int i = 0; i < SET_LIMIT_SIZE; i++) {
set->nodes[i] = NULL;
}

return set;
}

SetNode* createNode(int value) {
SetNode* newNode = malloc(sizeof(SetNode));
newNode->next = NULL;
newNode->value = value;
return newNode;
}

void insert(Set* set, int value) {
int index = itemIndex(value, SET_LIMIT_SIZE);
SetNode* currentNode = set->nodes[index];

SetNode* prevNode = NULL;
while(currentNode != NULL) {
if(currentNode->value == value) {
printf("set do not allow repeated values: %d\n", value);
return;
}
prevNode = currentNode;
currentNode = currentNode->next;
}

SetNode* newNode = createNode(value);
set->currLength += 1;

if(prevNode == NULL) {
set->nodes[index] = newNode;
} else {
prevNode->next = newNode;
}
}

int isEmpty(Set* set) {
return set->currLength == 0;
}

SetNode* find(Set* set, int value) {
if(isEmpty(set))
return NULL;

int index = itemIndex(value, SET_LIMIT_SIZE);
SetNode* currNode = set->nodes[index];

while(currNode != NULL && currNode->value != value) {
currNode = currNode->next;
}

return currNode;
}

int removeItem(Set* set, int value) {
if(isEmpty(set))
return -1;

int index = itemIndex(value, SET_LIMIT_SIZE);
SetNode* currentNodeToDelete = set->nodes[index];

SetNode* prevNode = NULL;
while(currentNodeToDelete != NULL && currentNodeToDelete->value != value) {
prevNode = currentNodeToDelete;
currentNodeToDelete = currentNodeToDelete->next;
}

if(currentNodeToDelete == NULL)
return -1;

if(prevNode != NULL) {
prevNode->next = currentNodeToDelete->next;
} else {
set->nodes[index] = currentNodeToDelete->next;
}

free(currentNodeToDelete);
set->currLength -= 1;

return 1;
}

int main() {
Set* set = initializeSet();

printf("== insert 10, 20, 30, 10 ==\n");
insert(set, 10);
insert(set, 20);
insert(set, 30);
insert(set, 10); // duplicate test

printf("== find ==\n");
printf("find 10: %s\n", find(set, 10) ? "found" : "not found");
printf("find 99: %s\n", find(set, 99) ? "found" : "not found");

printf("== remove ==\n");
printf("remove 99: %d\n", removeItem(set, 99));
printf("remove 20: %d\n", removeItem(set, 20));
printf("remove 10: %d\n", removeItem(set, 10));
printf("remove 10 again: %d\n", removeItem(set, 10));

printf("== final finds ==\n");
printf("find 10: %s\n", find(set, 10) ? "found" : "not found");
printf("find 20: %s\n", find(set, 20) ? "found" : "not found");
printf("find 30: %s\n", find(set, 30) ? "found" : "not found");

return 0;
}









References



1. Sofort-Triage & Abwehrmaßnahmen

SOC Incident Playbook: Vulnerability Remediation & Verification
Syntax validiert (0 Fehler)
title: Detect Exploitation - Set Data Structure in C
id: fb176296-2eee-45be-b6c2-22b36f93ffc3
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-25
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
Syntax validiert (0 Fehler)
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-25"
        description = "YARA Signature for "
    strings:
        $str = "Set Data Structure in C" ascii wide
    condition:
        any of them
}
Syntax validiert (0 Fehler)
index=security sourcetype IN ("cisco:asa", "pan:traffic", "zeek_conn", "suricata", "WinEventLog:Security")
("Set Data Structure in C")
| stats count earliest(_time) as first_seen latest(_time) as last_seen by src_ip, dest_ip, dest_host, signature
| eval first_seen=strftime(first_seen, "%Y-%m-%d %H:%M:%S"), last_seen=strftime(last_seen, "%Y-%m-%d %H:%M:%S")
| sort - count
Syntax validiert (0 Fehler)
message: "*Set Data Structure in C*"
Syntax validiert (0 Fehler)
CommonSecurityLog
| where Message has "Set Data Structure in C"
| summarize EventCount = count(), FirstSeen = min(TimeGenerated), LastSeen = max(TimeGenerated) by SourceIP, DestinationIP, DestinationPort, Activity
| extend DetectionRule = "iShareStuff-CTI-Compiled"
| sort by EventCount desc

2. Cyber Threat Intelligence & Forensik

🎯
MITRE ATT&CK Matrix Navigator 14 Taktiken
Reconnaissance
-
Resource Development
-
Initial Access
Execution
Persistence
-
Privilege Escalation
Defense Evasion
Credential Access
-
Discovery
-
Lateral Movement
-
Collection
-
Command and Control
Exfiltration
-
Impact
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich Set Data Structure in C.... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

⚡ Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Set Data Structure in C

Thematisch verwandte Begriffe: Data, Structure · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-97818 | phpIPAM through 1.8.3 has incorrect authorization for id=="admins" and i…
Advisory →
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag