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

C_Programs_and_Algorithms

The document provides C programs and algorithms for various operations on arrays and linked lists, including inserting and deleting elements, implementing stacks and queues, and reversing strings. Each section includes an algorithm description, C code implementation, and sample output. Key operations covered include insertion and deletion in arrays, stack operations, queue implementations, and linked list manipulations.

Uploaded by

scarlettflare479
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

C_Programs_and_Algorithms

The document provides C programs and algorithms for various operations on arrays and linked lists, including inserting and deleting elements, implementing stacks and queues, and reversing strings. Each section includes an algorithm description, C code implementation, and sample output. Key operations covered include insertion and deletion in arrays, stack operations, queue implementations, and linked list manipulations.

Uploaded by

scarlettflare479
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C Programs and Algorithms

1. Insert an element in a specific position in an Array

Algorithm:
1. Start
2. Input array elements and size.
3. Input element to insert and position.
4. Shift elements from the position to right.
5. Insert new element.
6. Display updated array.
7. End

Code:
#include <stdio.h>
void insert(int arr[], int *n, int elem, int pos) {
for (int i = *n; i >= pos; i--) arr[i] = arr[i - 1];
arr[pos - 1] = elem;
(*n)++;
}
int main() {
int arr[100], n, elem, pos;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements: ");
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
printf("Enter element and position to insert: ");
scanf("%d %d", &elem, &pos);
insert(arr, &n, elem, pos);
printf("Updated array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

Output:
Enter number of elements: 5
Enter elements: 1 2 3 4 5
Enter element and position to insert: 99 3
Updated array: 1 2 99 3 4 5
C Programs and Algorithms

2. Delete an element from a specific position in an Array

Algorithm:
1. Start
2. Input array elements and size.
3. Input position to delete.
4. Shift elements from the position to left.
5. Decrease array size.
6. Display updated array.
7. End

Code:
#include <stdio.h>
void delete(int arr[], int *n, int pos) {
for (int i = pos - 1; i < *n - 1; i++) arr[i] = arr[i + 1];
(*n)--;
}
int main() {
int arr[100], n, pos;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements: ");
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
printf("Enter position to delete: ");
scanf("%d", &pos);
delete(arr, &n, pos);
printf("Updated array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

Output:
Enter number of elements: 5
Enter elements: 1 2 3 4 5
Enter position to delete: 3
Updated array: 1 2 4 5
C Programs and Algorithms

3. Implement push() and pop() in a stack using array

Algorithm:
1. Start
2. Use array to implement stack.
3. Implement push() to add element.
4. Implement pop() to remove top element.
5. Handle overflow/underflow.
6. End

Code:
#include <stdio.h>
#define MAX 100
int stack[MAX], top = -1;
void push(int x) {
if (top == MAX - 1) printf("Stack Overflow\n");
else stack[++top] = x;
}
void pop() {
if (top == -1) printf("Stack Underflow\n");
else printf("Popped: %d\n", stack[top--]);
}
int main() {
push(10); push(20); push(30);
pop(); pop(); pop(); pop();
return 0;
}

Output:
Popped: 30
Popped: 20
Popped: 10
Stack Underflow
C Programs and Algorithms

4. Reverse a string using stack

Algorithm:
1. Start
2. Read string from user.
3. Push all characters to stack.
4. Pop and print to reverse.
5. End

Code:
#include <stdio.h>
#include <string.h>
#define MAX 100
int top = -1;
char stack[MAX];
void push(char ch) {
if (top < MAX - 1) stack[++top] = ch;
}
char pop() {
if (top >= 0) return stack[top--];
return '\0';
}
int main() {
char str[MAX];
printf("Enter string: ");
gets(str);
for (int i = 0; i < strlen(str); i++) push(str[i]);
printf("Reversed string: ");
while (top != -1) printf("%c", pop());
return 0;
}

Output:
Enter string: hello
Reversed string: olleh
C Programs and Algorithms

5. Implement enqueue() and dequeue() in a linear queue using array

Algorithm:
1. Start
2. Initialize front and rear.
3. Implement enqueue() to insert.
4. Implement dequeue() to remove.
5. Check overflow/underflow.
6. End

Code:
#include <stdio.h>
#define MAX 100
int queue[MAX], front = -1, rear = -1;
void enqueue(int x) {
if (rear == MAX - 1) printf("Queue Overflow\n");
else {
if (front == -1) front = 0;
queue[++rear] = x;
}
}
void dequeue() {
if (front == -1 || front > rear) printf("Queue Underflow\n");
else printf("Dequeued: %d\n", queue[front++]);
}
int main() {
enqueue(1); enqueue(2); enqueue(3);
dequeue(); dequeue(); dequeue(); dequeue();
return 0;
}

Output:
Dequeued: 1
Dequeued: 2
Dequeued: 3
Queue Underflow
C Programs and Algorithms

6. Implement circular queue using array

Algorithm:
1. Start
2. Initialize front and rear.
3. Use modulo for circular increment.
4. Implement enqueue and dequeue.
5. Check overflow/underflow.
6. End

Code:
#include <stdio.h>
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;
void enqueue(int value) {
if ((rear + 1) % SIZE == front) printf("Queue Overflow\n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
queue[rear] = value;
}
}
void dequeue() {
if (front == -1) printf("Queue Underflow\n");
else {
printf("Dequeued: %d\n", queue[front]);
if (front == rear) front = rear = -1;
else front = (front + 1) % SIZE;
}
}
int main() {
enqueue(1); enqueue(2); enqueue(3); enqueue(4); enqueue(5);
dequeue(); dequeue();
enqueue(6); enqueue(7);
return 0;
}

Output:
Dequeued: 1
Dequeued: 2
C Programs and Algorithms

7. Insert node at beginning of singly linked list

Algorithm:
1. Start
2. Create new node.
3. Make its next point to head.
4. Update head to new node.
5. End

Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void insertBeginning(struct Node **head, int data) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void display(struct Node *node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main() {
struct Node *head = NULL;
insertBeginning(&head, 10);
insertBeginning(&head, 20);
display(head);
return 0;
}

Output:
20 -> 10 -> NULL
C Programs and Algorithms

8. Insert node at end of singly linked list

Algorithm:
1. Start
2. Create new node.
3. Traverse to end.
4. Set last node's next to new node.
5. End

Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void insertEnd(struct Node **head, 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 display(struct Node *node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main() {
struct Node *head = NULL;
insertEnd(&head, 10);
insertEnd(&head, 20);
display(head);
return 0;
}

Output:
10 -> 20 -> NULL
C Programs and Algorithms

9. Delete first node of singly linked list

Algorithm:
1. Start
2. Check if list is empty.
3. Store head in temp.
4. Move head to next.
5. Free temp.
6. End

Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void deleteFirst(struct Node **head) {
if (*head == NULL) return;
struct Node *temp = *head;
*head = (*head)->next;
free(temp);
}
void display(struct Node *node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main() {
struct Node *head = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = (struct Node*)malloc(sizeof(struct Node));
head->next->data = 20;
head->next->next = NULL;
deleteFirst(&head);
display(head);
return 0;
}

Output:
20 -> NULL
C Programs and Algorithms

10. Delete last node of singly linked list

Algorithm:
1. Start
2. Traverse to second last node.
3. Free last node.
4. Set second last's next to NULL.
5. End

Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void deleteLast(struct Node **head) {
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node *temp = *head;
while (temp->next->next != NULL) temp = temp->next;
free(temp->next);
temp->next = NULL;
}
void display(struct Node *node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main() {
struct Node *head = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = (struct Node*)malloc(sizeof(struct Node));
head->next->data = 20;
head->next->next = NULL;
deleteLast(&head);
display(head);
return 0;
}

Output:
10 -> NULL

You might also like