Ex. No.
11 Stack Using C Program
Aim:
To write a C program using Stack with the operations of push and pop.
Algorithm:
1. Initialize a stack with a fixed size or dynamically allocate memory for the stack.
2. Define Operations like Push, Pop, Peek, and is Empty to manipulate the stack.
3. Ensure that the stack follows the Last In First Out (LIFO)principle.
4. Implement the push operation to add an element to the top of the stack.
5. Implement the Pop operation to remove the top element from the stack.
6. Implement the peek operation to get the value of the top element without removing it.
7. Implement the isEmpty operation to check if the stack is empty.
8. Handle Stack overflow and uderflow conditions appropriately.
9. Free Memory allocated to the stack when it’s no longer needed(if dynamically allocated).
Program:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// Structure representing a stack
typedef struct {
int items[MAX_SIZE];
int top;
} Stack;
// Function to initialize the stack
void initialize(Stack *stack) {
stack->top = -1;
}
// Function to check if the stack is empty
int isEmpty(Stack *stack) {
return stack->top == -1;
}
// Function to check if the stack is full
int isFull(Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
// Function to push an element onto the stack
void push(Stack *stack, int value) {
if (isFull(stack)) {
printf("Stack overflow!\n");
return;
}
stack->items[++stack->top] = value;
}
// Function to pop an element from the stack
int pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack underflow!\n");
return -1; // return an error value
}
return stack->items[stack->top--];
}
// Function to peek the top element of the stack
int peek(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty!\n");
return -1; // return an error value
}
return stack->items[stack->top];
}
int main() {
Stack stack;
initialize(&stack);
// Pushing elements onto the stack
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
// Peeking the top element
printf("Top element: %d\n", peek(&stack));
// Popping elements from the stack
printf("Popped element: %d\n", pop(&stack));
printf("Popped element: %d\n", pop(&stack));
// Checking if the stack is empty
printf("Is the stack empty? %s\n", isEmpty(&stack) ? "Yes" : "No");
return 0;
}
Output:
Top element : 30
Popped element : 30
Popped element : 20
Is the Stack empty? No
Result:
Thus a C program using Stack with the operations of push and pop executed successfully.
Ex. No.12 Sorting: Bubble Sort
Aim:
To write a C program using Bubble Sort.
Algorithm:
1. Start with the first element with the array.
2. Compare the current element with the next element.
3. If the current element is greater than the next element,swap them.
4. Move to the next element and repeat the steps 2-3 until the end of the array is reached.
5. Repeat the steps 1-4 until no swaps are needed, indicating the array is sorted.
Program:
#include <stdio.h>
// Function to perform Bubble Sort
void bubbleSort(int array[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap if the current element is greater than the next element
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
// Function to print the array
void printArray(int array[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int array[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(array) / sizeof(array[0]);
printf("Original array: ");
printArray(array, n);
bubbleSort(array, n);
printf("Sorted array: ");
printArray(array, n);
return 0;
}
Output:
Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
Result:
Thus a C program using Bubble Sort executed successfully.