אלגוריתמים ומבני נתונים (Algorithms and Data Structures) בשפת C


אלגוריתמים ומבני נתונים הם לב ליבה של כל תכנות. הם מספקים את הבסיס לפתרון בעיות בצורה יעילה ומסודרת. במדריך זה נסקור מספר אלגוריתמים ומבני נתונים בסיסיים בשפת C, ונראה כיצד ליישם אותם.


1. מערכים (Arrays)


מערכים הם מבנה נתונים פשוט המאגד מספר ערכים באותו סוג נתונים בתיבה אחת. הגישה למערכים היא באמצעות אינדקסים.


קוד:
c
קוד:
#include <stdio.h> int main() { int arr[5] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; i++) { printf("Element at index %d: %d\n", i, arr[i]); } return 0; }


2. רשימות מקושרות (Linked Lists)


רשימות מקושרות הן מבנה נתונים דינמי שבו כל צומת מכיל מידע ומצביע לצומת הבא.


קוד:
c
קוד:
#include <stdio.h> #include <stdlib.h> // הגדרת מבנה לצומת struct Node { int data; struct Node *next; }; // פונקציה להדפסת רשימה מקושרת void printList(struct Node *head) { struct Node *current = head; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("NULL\n"); } int main() { struct Node *head = NULL; // יצירת צמתים struct Node *node1 = (struct Node *)malloc(sizeof(struct Node)); struct Node *node2 = (struct Node *)malloc(sizeof(struct Node)); struct Node *node3 = (struct Node *)malloc(sizeof(struct Node)); node1->data = 10; node1->next = node2; node2->data = 20; node2->next = node3; node3->data = 30; node3->next = NULL; head = node1; // הדפסת הרשימה printList(head); // שחרור זיכרון free(node1); free(node2); free(node3); return 0; }


3. ערימות (Stacks)


ערימה היא מבנה נתונים שבו התוספות וההסרות מתבצעות לפי עקרון LIFO (Last In, First Out).


קוד:
c
קוד:
#include <stdio.h> #include <stdlib.h> #define MAX 100 // הגדרת מבנה לערימה struct Stack { int top; int items[MAX]; }; // פונקציה להוספת פריט לערימה void push(struct Stack *s, int item) { if (s->top == MAX - 1) { printf("Stack Overflow\n"); return; } s->items[++(s->top)] = item; } // פונקציה להסרת פריט מהערימה int pop(struct Stack *s) { if (s->top == -1) { printf("Stack Underflow\n"); return -1; } return s->items[(s->top)--]; } // פונקציה לבדוק אם הערימה ריקה int isEmpty(struct Stack *s) { return s->top == -1; } int main() { struct Stack s; s.top = -1; push(&s, 10); push(&s, 20); push(&s, 30); while (!isEmpty(&s)) { printf("%d\n", pop(&s)); } return 0; }


4. תורים (Queues)


תור הוא מבנה נתונים שבו התוספות מתבצעות בצד אחד וההסרות בצד השני, לפי עקרון FIFO (First In, First Out).


קוד:
c
קוד:
#include <stdio.h> #include <stdlib.h> #define MAX 100 // הגדרת מבנה לתור struct Queue { int front, rear, size; int items[MAX]; }; // פונקציה להוספת פריט לתור void enqueue(struct Queue *q, int item) { if (q->size == MAX) { printf("Queue Overflow\n"); return; } q->items[q->rear] = item; q->rear = (q->rear + 1) % MAX; q->size++; } // פונקציה להסרת פריט מהתור int dequeue(struct Queue *q) { if (q->size == 0) { printf("Queue Underflow\n"); return -1; } int item = q->items[q->front]; q->front = (q->front + 1) % MAX; q->size--; return item; } // פונקציה לבדוק אם התור ריק int isEmpty(struct Queue *q) { return q->size == 0; } int main() { struct Queue q; q.front = 0; q.rear = 0; q.size = 0; enqueue(&q, 10); enqueue(&q, 20); enqueue(&q, 30); while (!isEmpty(&q)) { printf("%d\n", dequeue(&q)); } return 0; }