0% found this document useful (0 votes)
13 views12 pages

DSFILEFORS

Uploaded by

adarsh23169049
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)
13 views12 pages

DSFILEFORS

Uploaded by

adarsh23169049
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/ 12

Program number:1

Objective: WAP in C to implement following operation in an array-


(i)Insertion (ii)Deletion (iii)Traversing
Code: int val;

#include <stdio.h> printf("Enter value to add at the end: ");

#include <stdlib.h> scanf("%d", &val);

#define MAX_SIZE 10 arr[current_size] = val;

int arr[MAX_SIZE]; current_size++;

int current_size = 0; }

void add_start() {

if (current_size >= MAX_SIZE) { void add_position() {

printf("Array is full, can't add at the start.\n"); if (current_size >= MAX_SIZE) {

return; printf("Array is full, can't add at this


position.\n");
}
return;
int val;
}
printf("Enter value to add at the start: ");
int val, pos;
scanf("%d", &val);
printf("Enter value to add: ");
for (int i = current_size; i > 0; i--) {
scanf("%d", &val);
arr[i] = arr[i - 1];
printf("Enter position (0 to %d): ",
}
current_size);
arr[0] = val;
scanf("%d", &pos);
current_size++;
if (pos < 0 || pos > current_size) {
}
printf("Invalid position.\n");
void add_end() {
return;
if (current_size >= MAX_SIZE) {
}
printf("Array is full, can't add at the end.\n");
for (int i = current_size; i > pos; i--) {
return;
arr[i] = arr[i - 1];
}
}
arr[pos] = val; printf("Array is empty, can't remove from this
position.\n");
current_size++;
return;
}
}

int pos;
void remove_start() {
printf("Enter position to remove (0 to %d): ",
if (current_size == 0) {
current_size - 1);
printf("Array is empty, can't remove from
scanf("%d", &pos);
start.\n");
if (pos < 0 || pos >= current_size) {
return;
printf("Invalid position.\n");
}
return;
printf("Removed element: %d\n", arr[0]);
}
for (int i = 0; i < current_size - 1; i++) {
printf("Removed element: %d\n", arr[pos]);
arr[i] = arr[i + 1];
for (int i = pos; i < current_size - 1; i++) {
}
arr[i] = arr[i + 1];
current_size--;
}
}
current_size--;

}
void remove_end() {

if (current_size == 0) {
void show_array() {
printf("Array is empty, can't remove from
end.\n"); if (current_size == 0) {

return; printf("Array is currently empty.\n");

} return;

printf("Removed element: %d\n", }


arr[current_size - 1]);
printf("Current array elements: ");
current_size--;
for (int i = 0; i < current_size; i++) {
}
printf("%d ", arr[i]);

}
void remove_position() {
printf("\n");
if (current_size == 0) {
}
case 1: add_start(); break;

int main() { case 2: add_end(); break;

int option; case 3: add_position();

printf("\nOptions:\n"); break;

printf("1. Add at start\n"); case 4: remove_start(); break;

printf("2. Add at end\n"); case 5: remove_end(); break;

printf("3. Add at position\n"); case 6: remove_position(); break;

printf("4. Remove from start\n"); case 7: show_array(); break;

printf("5. Remove from end\n"); case 8:

printf("6. Remove from position\n"); printf("Program has ended.\n");

printf("7. Show array\n"); exit(0);

printf("8. Quit\n"); default:

printf("Invalid selection! Try again.\n");

while (1) { }

printf("Select an option: "); }

scanf("%d", &option); return 0;

switch (option) { }
Output:
Program number:2
Objective: WAP in C to implement following operation in a linked list-
(i)Insertion (ii)Deletion (iii)Traversing
Code:

#include <stdio.h> }

#include <stdlib.h>

// Function to insert a new node at the end

struct Node { void addAtEnd() {

int value; int value;

struct Node* nextNode; printf("Enter value to add: ");

}; scanf("%d", &value);

struct Node* start = NULL; struct Node* node = createNode(value);

if (start == NULL) {

// Function to create a new node start = node;

struct Node* createNode(int value) { return;

struct Node* node = (struct }


Node*)malloc(sizeof(struct Node));
struct Node* current = start;
node->value = value;
while (current->nextNode != NULL) {
node->nextNode = NULL;
current = current->nextNode;
return node;
}
}
current->nextNode = node;

}
// Function to insert a new node at the start

void addAtStart() {
// Function to insert a new node at a specific
int value; position

printf("Enter value to add: "); void addAtPosition() {

scanf("%d", &value); int value, pos;

struct Node* node = createNode(value); printf("Enter value and position: ");

node->nextNode = start; scanf("%d %d", &value, &pos);

start = node; if (pos < 0) {


printf("Invalid position\n"); free(temp);

return; }

struct Node* node = createNode(value); // Function to delete a node from the end

struct Node* temp = start; void deleteFromEnd() {

if (pos == 0) { if (start == NULL) {

node->nextNode = start; printf("List is empty\n");

start = node; return;

return; }

} if (start->nextNode == NULL) {

for (int i = 0; i < pos - 1; i++) { free(start);

if (temp == NULL) { start = NULL;

printf("Position out of range\n"); return;

return; }

} struct Node* temp = start;

temp = temp->nextNode; while (temp->nextNode->nextNode != NULL) {

} temp = temp->nextNode;

node->nextNode = temp->nextNode; }

temp->nextNode = node; struct Node* last = temp->nextNode;

} temp->nextNode = NULL;

printf("Deleted value: %d\n", last->value);

// Function to delete a node from the start free(last);

void deleteFromStart() { }

if (start == NULL) {

printf("List is empty\n"); // Function to delete a node from a specific


position
return;
void deleteAtPosition() {
}
int pos;
struct Node* temp = start;
if (start == NULL) {
start = start->nextNode;
printf("List is empty\n");
printf("Deleted value: %d\n", temp->value);
return; void displayList() {

} struct Node* temp = start;

printf("Enter position to delete: "); if (temp == NULL) {

scanf("%d", &pos); printf("List is empty\n");

if (pos < 0) { return;

printf("Invalid position\n"); }

return; while (temp != NULL) {

} printf("%d ", temp->value);

struct Node* temp = start; temp = temp->nextNode;

if (pos == 0) { }

start = start->nextNode; printf("\n");

printf("Deleted value: %d\n", temp->value); }

free(temp);

return; int main() {

} int option;

for (int i = 0; i < pos - 1; i++) { printf("\nOptions:\n");

if (temp->nextNode == NULL) { printf("1. Add at Start\n");

printf("Position out of range\n"); printf("2. Add at End\n");

return; printf("3. Add at Position\n");

} printf("4. Delete from Start\n");

temp = temp->nextNode; printf("5. Delete from End\n");

} printf("6. Delete from Position\n");

struct Node* deleteNode = temp->nextNode; printf("7. Display List\n");

temp->nextNode = deleteNode->nextNode; printf("8. Exit\n");

printf("Deleted value: %d\n", deleteNode-


>value);
while (1) {
free(deleteNode);
printf("Choose an option: ");
}
scanf("%d", &option);

switch (option) {
// Function to display the list
case 1: addAtStart(); break;
case 2: addAtEnd(); break; default: printf("Invalid choice\n");

case 3: addAtPosition(); break; }

case 4: deleteFromStart(); break; }

case 5: deleteFromEnd(); break; return 0;

case 6: deleteAtPosition(); break; }

case 7: displayList(); break;

case 8: printf("Program terminated\n");


exit(0);

Output:
Program Number:3
Objective: Write a program in C to implement stack using an array.
Code:
#include <stdio.h> case 3:

#include <stdlib.h> displayStack();

break;

#define MAX_SIZE 5 case 4:

int stackTop = -1, stack[MAX_SIZE]; printf("\nProgram terminated. Thank


you!\n");

exit(0);
void pushElement();
default:
void popElement();
printf("Invalid choice! Try again.\n");
void displayStack();
}

}
int main() {
}
int userChoice;

printf("Choose an operation:\n");
void pushElement() {
printf("1. Push\n2. Pop\n3. Display\n4.
Exit\n"); if (stackTop >= MAX_SIZE - 1) {

printf("Stack Overflow! No space to push


more elements.\n");
while (1) {
} else {
printf("Enter your choice: ");
stackTop++;
scanf("%d", &userChoice);
printf("Enter value to push: ");

scanf("%d", &stack[stackTop]);
switch (userChoice) {
}
case 1:
}
pushElement();

break;
void displayStack() {
case 2:
if (stackTop == -1) {
popElement();
printf("Stack is empty.\n");
break;
} else { if (stackTop == -1) {

printf("Current stack elements:\n"); printf("Stack Underflow! No elements to


pop.\n");
for (int i = 0; i <= stackTop; i++) {
} else {
printf("%d\n", stack[i]);
printf("Popped element: %d\n",
}
stack[stackTop]);
}
stackTop--;
}
}

}
void popElement() {

Output:
Program Number:4
Objective: Write a program in C to implement queue using an array.
Code:

#include <stdio.h> printf("<<< Queue is empty. Nothing to


remove. >>>\n");
#include <stdlib.h>
} else if (frontIndex == rearIndex) {
#define QUEUE_SIZE 10
printf("Removed: %d\n",
myQueue[frontIndex]);
int myQueue[QUEUE_SIZE];
frontIndex = rearIndex = -1;
int frontIndex = -1;
} else {
int rearIndex = -1;
printf("Removed: %d\n",
myQueue[frontIndex]);

void addToQueue() { frontIndex++;

int data; }

if (rearIndex == QUEUE_SIZE - 1) { }

printf("<<< Queue is full. Cannot add more


elements. >>>\n");
void displayQueue() {
} else {
if (rearIndex == -1) {
printf("Enter element to add to the queue:
printf("<<< Queue is empty. >>>\n");
");
} else {
scanf("%d", &data);
printf("Current elements in the queue:\n");
rearIndex++;
for (int i = frontIndex; i <= rearIndex; i++) {
myQueue[rearIndex] = data;
printf("%d\n", myQueue[i]);
if (frontIndex == -1) {
}
frontIndex = 0;
}
}
}
}

}
int main() {

int option;
void removeFromQueue() {
printf("\nQueue Operations Menu:\n");
if (rearIndex == -1) {
printf("1. Add Element\n2. Remove
Element\n3. Display Queue\n4. Exit\n");

while (1) {

printf("\nSelect an option: ");

scanf("%d", &option);

switch (option) {

case 1:

addToQueue();

break;

case 2:

removeFromQueue();

break;

case 3:

displayQueue();

break;

case 4:

printf("Exiting program. Goodbye!\n");

exit(0);

default:

printf("Invalid option! Please try


again.\n");

return 0;

Output:

You might also like