0% found this document useful (0 votes)
16 views

Data C Programming

Students of computer science

Uploaded by

Chrisfred Dambo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Data C Programming

Students of computer science

Uploaded by

Chrisfred Dambo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

To meet the specified requirements, I'll create separate C source files and

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.

Here is the directory structure for the project:


```
data_structures/
├── arrays.c
├── arrays.h
├── linked_list.c
├── linked_list.h
├── stack.c
├── stack.h
├── queue.c
├── queue.h
├── main.c
```

### `arrays.h`
```c
#ifndef ARRAYS_H
#define ARRAYS_H

#define MAX_SIZE 100

void insert(int element, int position);


void delete(int position);
int search(int element);
void printArray();

#endif // ARRAYS_H
```

### `arrays.c`
```c
#include <stdio.h>
#include "arrays.h"

int array[MAX_SIZE];
int size = 0;

void insert(int element, int position) {


if (size >= MAX_SIZE) {
printf("Array is full\n");
return;
}
if (position < 0 || position > size) {
printf("Invalid position\n");
return;
}
for (int i = size; i > position; i--) {
array[i] = array[i - 1];
}
array[position] = element;
size++;
}

void delete(int position) {


if (position < 0 || position >= size) {
printf("Invalid position\n");
return;
}
for (int i = position; i < size - 1; i++) {
array[i] = array[i + 1];
}
size--;
}

int search(int element) {


for (int i = 0; i < size; i++) {
if (array[i] == element) {
return i;
}
}
return -1;
}

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;
};

void insertBeginning(int data);


void insertEnd(int data);
void insertMiddle(int data, int position);
void deleteValue(int value);
int search(int value);
void printList();

#endif // LINKED_LIST_H
```

### `linked_list.c`
```c
#include <stdio.h>
#include <stdlib.h>
#include "linked_list.h"

struct Node* head = NULL;

void insertBeginning(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = head;
head = newNode;
}

void insertEnd(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

void insertMiddle(int data, int position) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (position == 0) {
newNode->next = head;
head = newNode;
return;
}
struct Node* temp = head;
for (int i = 0; i < position - 1; i++) {
if (temp == NULL) {
printf("Position out of range\n");
return;
}
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}

void deleteValue(int value) {


struct Node* temp = head;
struct Node* prev = NULL;
if (temp != NULL && temp->data == value) {
head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Value not found\n");
return;
}
prev->next = temp->next;
free(temp);
}

int search(int value) {


struct Node* temp = head;
int position = 0;
while (temp != NULL) {
if (temp->data == value) {
return position;
}
temp = temp->next;
position++;
}
return -1;
}

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

#define MAX_SIZE 100

void push(int data);


int pop();
int peek();
int isEmpty();
void printStack();

#endif // STACK_H
```

### `stack.c`
```c
#include <stdio.h>
#include "stack.h"

int stack[MAX_SIZE];
int top = -1;

void push(int data) {


if (top >= MAX_SIZE - 1) {
printf("Stack overflow\n");
return;
}
stack[++top] = data;
}

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;
};

struct Queue* createQueue();


void enqueue(struct Queue* q, int data);
int dequeue(struct Queue* q);
int peek(struct Queue* q);
int isEmpty(struct Queue* q);
void printQueue(struct Queue* q);

#endif // QUEUE_H
```

### `queue.c`
```c
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

struct Queue* createQueue() {


struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}

void enqueue(struct Queue* q, int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
q->rear->next = newNode;
q->rear = newNode;
}

int dequeue(struct Queue* q) {


if (q->front == NULL) {
printf("Queue is empty\n");
return -1;
}
struct Node* temp = q->front;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
int data = temp->data;
free(temp);
return data;
}

int peek(struct Queue* q) {


if (q->front == NULL) {
printf("Queue is empty\n");
return -1;
}
return q->front->data;
}

int isEmpty(struct Queue* q) {


return q->front == NULL;
}

void printQueue(struct Queue* q) {


struct Node* temp = q->front;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
```

### `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

// Linked List operations


printf("\nLinked List Operations:\n");
insertBeginning(10);
insertEnd(20);
insertEnd(30);
insertMiddle(15, 1);
printList(); // Output: 10 15 20 30
deleteValue(15);
printList(); // 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

You might also like