CBS For Lab Programming in C - CS3271
CBS For Lab Programming in C - CS3271
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);
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:
Algorithm:
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.