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

Chrisfred Assignment

Assignment

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Chrisfred Assignment

Assignment

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 PDF, TXT or read online on Scribd
You are on page 1/ 7

AI PROFESSIONAL COLLEGE

Name:Chrisfred Dambo

Course: Business Information Technology

Department: Information Technology

ID.Num: 2324092

Year: One

Shift: Morning

Module: Data Structure in C Programing


Math

Task: Write a c program using Array, linked list, stacks, queues

Lecturer: Mr Emmauel
Array :
#include <stdio.h>
#define MAX_SIZE 100

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

int main() {
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
return 0;
}

SINGLY LINKED LIST :


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

struct Node {
int data;
struct Node* next;
};

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

int main() {
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
return 0;
}
STACK USING ARRAY:
#include <stdio.h>

#define MAX_SIZE 100

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

int main() {
push(10);
push(20);
push(30);
printStack(); // Output: 30 20 10
pop();
printStack(); // Output: 20 10
printf("Top element is %d\n", peek()); // Output: 20
printf("Stack is empty: %d\n", isEmpty()); // Output: 0
return 0;
}
QUEUE USING LINKED LIST :
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Queue {
struct Node *front, *rear;
};

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

int main() {
struct Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
printQueue(q); // Output: 10 20 30
dequeue(q);
printQueue(q); // Output: 20 30
printf("Front element is %d\n", peek(q)); // Output: 20
printf("Queue is empty: %d\n", isEmpty(q)); // Output: 0
return 0;
}

You might also like