Ds Lab External Questions
Ds Lab External Questions
Pop Operation
Program:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
// Structure to represent a node in the stack
struct Node {
int data;
struct Node* next;
};
// Structure to represent the stack
struct Stack {
struct Node* top;
};
// Function to initialise the stack
void initializeStack(struct Stack* stack) {
stack->top = NULL; // Initialize top to NULL to indicate an empty stack
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == NULL;
}
// Function to push an element onto the stack
void push(struct Stack* stack, int element) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed! Cannot push %d onto the stack.\n", element);
return;
}
newNode->data = element;
newNode->next = stack->top;
stack->top = newNode;
printf("Pushed %d onto the stack.\n", element);
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
struct Node* poppedNode;
int poppedElement;
if (isEmpty(stack)) {
printf("Stack underflow! Cannot pop from an empty stack.\n");
return -1; // Return an error value
}
poppedNode = stack->top;
poppedElement = poppedNode->data;
stack->top = poppedNode->next;
free(poppedNode);
return poppedElement;
}
// Function to display elements in the stack
void display(struct Stack* stack) {
struct Node* current = stack->top;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct Stack stack;
initializeStack(&stack);
clrscr();
// Push elements onto the stack
push(&stack, 5);
push(&stack, 10);
push(&stack, 10);
push(&stack, 15);
push(&stack, 15);
// Display elements in the stack
printf("Stack elements: ");
display(&stack);
// Pop elements from the stack and display
printf("Popped element: %d\n", pop(&stack));
printf("Popped element: %d\n", pop(&stack));
// Display remaining elements in the stack
printf("Stack elements after popping: ");
display(&stack);
getche();
return 0;
}
Output:
Pushed 5 onto the stack
Pushed 10 onto the stack
Pushed 15 onto the stack
Stack elements: 15 10 5
Popped element: 15
Popped element: 10
Stack elements after popping: 5
Result:
Hence, a stack using linked lists is implemented successfully
(b) C Programs to implement insertion Sort
Techniques.
Insertion Sort
AIM: To write a C Programs to implement the Insertion Sort Technique
Description:
Insertion sort is one of the best sorting techniques. It is twice as fast as Bubble sort.
In Insertion sort the element comparisons are as less as compared to bubble sort. In
this comparison the value until all prior elements are less than the compared values
is not found. This means that all the previous values are lesser than compared
values. Insertion sort is a good choice for small values and for nearly sorted values.
INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
Step 3: SET J = K - 1
SET J = J - 1
[END OF LOOP]
Step 6: EXIT
Program:
#include <stdio.h>
void insertionSort(int arr[], int size) {
int i, key, j;
for (i = 1; i < size; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int arr[50],n,i;
clrscr();
printf("Enter the no.of elements\n");
scanf("%d",&n);
printf("Enter the list of elements\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Original array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
insertionSort(arr, n);
printf("Sorted array (Insertion Sort): ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
getch();
return 0;
}
Output:
Enter the no.of elements
5
Enter the list of elements
20 28 45 12 3
Original array: 20 28 45 12 3
Sorted array(Insertion Sort): 3 12 20 28 45
Result: Thus, a C program to implement insertion sort technique is executed
successfully.