C Programming Data Structures: Arrays, Stacks, and Queues
C Program: Inserting an Element into an Array
This program demonstrates how to insert a new element into an existing array at a user-specified position.
#include <stdio.h>
void insert(int a[], int len, int pos, int num);
int main() {
int a[10];
int len, pos, num;
printf("Enter the number of elements you want in an array\n");
scanf("%d", &len);
printf("Enter %d integers\n", len);
for (int i = 0; i < len; i++)
scanf("%d", &a[i]);
printf("Enter the number you want to insert in an array\n");
scanf("%d", &num);
printf("Enter the position (1 to %d) for insertion\n", len + 1);
scanf("%d", &pos);
// Convert 1-based position to 0-based index
pos--;
insert(a, len, pos, num);
printf("Array after insertion\n");
// Loop runs up to <= len because the array size increases by 1
for (int i = 0; i <= len; i++)
printf("%d ", a[i]);
return 0;
}
void insert(int a[], int len, int pos, int num) {
// Shift elements to the right
for (int i = len; i > pos; i--) {
a[i] = a[i - 1];
}
// Insert the new element
a[pos] = num;
}
C Program: Deleting an Element from an Array
This program handles the deletion of an element from an array based on its position, including input validation.
#include <stdio.h>
int main()
{
int a[10], len, pos, i;
printf("Enter the length of the array (max 10)\n");
scanf("%d", &len);
if (len <= 0 || len > 10)
{
printf("Invalid Length! It should be between 1 to 10\n");
return 1;
}
printf("Enter the elements of the array:\n");
for (i = 0; i < len; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the position for deletion (1 to %d): ", len);
scanf("%d", &pos);
if (pos < 1 || pos > len)
{
printf("Position exceeds the length!\n");
return 1;
}
// Shift elements to the left (overwrite the element at pos-1)
for (i = pos - 1; i < len - 1; i++)
{
a[i] = a[i + 1];
}
// Decrease the effective length of the array
len--;
printf("Array after deletion:\n");
for (i = 0; i < len; i++)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
C Program: Array Sorting using Bubble Sort (Ascending)
This program inputs elements into an array and sorts them in ascending order using the Bubble Sort algorithm.
#include <stdio.h>
int main() {
int array[10];
int i, j, len, temp;
printf("Enter the length of the array (max 10)\n");
scanf("%d", &len);
if (len <= 0 || len > 10) {
printf("Invalid Length! It should be between 1 to 10\n");
return 1;
}
printf("Enter the elements in the array one by one:\n");
for (i = 0; i < len; i++) {
scanf("%d", &array[i]);
}
printf("Elements in the array are:\n");
for (i = 0; i < len; i++) {
printf("%d\n", array[i]);
}
// Bubble Sort implementation
for (i = 0; i < len - 1; i++) {
for (j = 0; j < len - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap elements
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted Array is:\n");
for (i = 0; i < len; i++) {
printf("%d\n", array[i]);
}
return 0;
}
C Program: Calculating the Sum of Two Arrays
This program calculates the element-wise sum of two arrays of equal length.
#include<stdio.h>
int main(){
int n, i;
printf("Enter the number of elements you want in your array\n");
scanf("%d",&n);
// Using Variable Length Arrays (VLA)
int a[n], b[n], sum[n];
printf("Enter the elements of 1st array\n");
for(i = 0; i < n; i++){
scanf("%d",&a[i]);
}
printf("Enter the elements of 2nd array\n");
for(i = 0; i < n; i++){
scanf("%d",&b[i]);
}
// Calculate sum
for(i = 0; i < n; i++){
sum[i] = a[i] + b[i];
}
printf("The sum of two arrays is:\n");
for(i = 0; i < n; i++){
printf("%d\n",sum[i]);
}
return 0;
}
Stack Implementation in C (Push, Pop, Traverse, Seek)
This program implements a basic stack using an array, demonstrating the core operations: Push (add element), Pop (remove element), Traverse (display elements), and Seek (find element by position).
#include <stdio.h>
int stack[10];
int top = -1;
void push();
int pop();
void traverse();
void seek();
int main() {
int choice;
char ch;
do {
printf("\n--- Stack Operations ---");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Traverse");
printf("\n4. Seek");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
push();
break;
case 2:
printf("\nDeleted element is %d\n", pop());
break;
case 3:
traverse();
break;
case 4:
seek();
break;
default:
printf("\nYou entered an invalid choice.\n");
}
printf("\nDo you want to continue (y/n)? ");
// Clear the input buffer before reading character input
while ((getchar()) != '\n');
scanf("%c", &ch);
} while (ch == 'y' || ch == 'Y');
return 0;
}
void push() {
int item;
if (top == 9) {
printf("\nThe stack is full. Stack Overflow.\n");
} else {
printf("\nEnter the element to be pushed: ");
scanf("%d", &item);
top++;
stack[top] = item;
printf("%d pushed to stack.\n", item);
}
}
int pop() {
if (top == -1) {
printf("\nThe stack is empty. Stack Underflow.\n");
return -1;
} else {
int item = stack[top];
top--;
return item;
}
}
void traverse() {
if (top == -1) {
printf("\nThe stack is empty.\n");
} else {
printf("\nStack elements are (Top to Bottom):\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
void seek() {
int position;
printf("\nEnter the position to seek (0 is Top, %d is Bottom): ", top);
scanf("%d", &position);
if (position < 0 || position > top) {
printf("\nInvalid position. Please enter a position between 0 and %d.\n", top);
} else {
// Position 0 is stack[top], Position 1 is stack[top-1], etc.
printf("Element at position %d is: %d\n", position, stack[top - position]);
}
}
Queue Implementation in C (Enqueue, Dequeue, Display, Seek)
This program implements a basic queue using an array, utilizing front
and rear
pointers for FIFO (First-In, First-Out) operations.
#include<stdio.h>
int queue[5];
int front = -1;
int rear = -1;
void enqueue(int element) {
if (rear == 4) {
printf("The Queue is FULL (Overflow)\n");
return;
}
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = element;
printf("%d is inserted into the Queue\n", element);
}
void dequeue() {
if (front == -1 || front > rear) {
printf("The Queue is EMPTY (Underflow)\n");
return;
}
printf("%d is removed from the Queue\n", queue[front]);
front++;
// Reset queue if it becomes empty
if (front > rear) {
front = rear = -1;
}
}
void display() {
if (front == -1) {
printf("The Queue is EMPTY\n");
return;
}
printf("The Queue elements are (Front to Rear):\n");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
int seek() {
if (front == -1) {
printf("The Queue is EMPTY\n");
return -1;
}
// Seek returns the front element (Peek operation)
return queue[front];
}
int main() {
int choice, element;
while (1) {
printf("\n--- Queue Operations ---\n");
printf("1) Press 1 to ENQUEUE\n");
printf("2) Press 2 to DEQUEUE\n");
printf("3) Press 3 to DISPLAY the Queue\n");
printf("4) Press 4 to SEEK the Front Queue\n");
printf("5) Press 5 to EXIT\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the Element to be inserted into the Queue\n");
scanf("%d", &element);
enqueue(element);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
element = seek();
if (element != -1) {
printf("Front element is %d\n", element);
}
break;
case 5:
return 0;
default:
printf("Invalid Choice\n");
}
}
}