Data C Programming
Data C Programming
corresponding header files for each data structure (arrays, linked lists, stacks, and
queues). Additionally, I'll include a main program that demonstrates the use of each
data structure. The code will be well-documented with comments to explain the
purpose of each function and significant code blocks.
### `arrays.h`
```c
#ifndef ARRAYS_H
#define ARRAYS_H
#endif // ARRAYS_H
```
### `arrays.c`
```c
#include <stdio.h>
#include "arrays.h"
int array[MAX_SIZE];
int size = 0;
void printArray() {
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
```
### `linked_list.h`
```c
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
struct Node {
int data;
struct Node* next;
};
#endif // LINKED_LIST_H
```
### `linked_list.c`
```c
#include <stdio.h>
#include <stdlib.h>
#include "linked_list.h"
void printList() {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
```
### `stack.h`
```c
#ifndef STACK_H
#define STACK_H
#endif // STACK_H
```
### `stack.c`
```c
#include <stdio.h>
#include "stack.h"
int stack[MAX_SIZE];
int top = -1;
int pop() {
if (top < 0) {
printf("Stack underflow\n");
return -1;
}
return stack[top--];
}
int peek() {
if (top < 0) {
printf("Stack is empty\n");
return -1;
}
return stack[top];
}
int isEmpty() {
return top == -1;
}
void printStack() {
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
```
### `queue.h`
```c
#ifndef QUEUE_H
#define QUEUE_H
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *front, *rear;
};
#endif // QUEUE_H
```
### `queue.c`
```c
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
### `main.c`
```c
#include <stdio.h>
#include "arrays.h"
#include "linked_list.h"
#include "stack.h"
#include "queue.h"
int main() {
// Array operations
printf("Array Operations:\n");
insert(10, 0);
insert(20, 1);
insert(30, 2);
insert(15, 1);
printArray(); // Output: 10 15 20 30
delete(1);
printArray(); // Output: 10 20 30
printf("Element 20 found at position: %d\n", search(20)); // Output: 1
// Stack operations
printf("\nStack Operations:\n");
push(10);
push(20);
push(30