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

Ds Lab External Questions

hv

Uploaded by

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

Ds Lab External Questions

hv

Uploaded by

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

6. (a) Implement a stack using a linked list.

Stack using Linked list:


Aim: To Implement a stack using linked lists.

Description: In a linked list-based implementation, the push operation is


implemented by creating a new node with the new element and setting the next
pointer of the current top node to the new node. The pop operation is implemented
by setting the next pointer of the current top node to the next node and returning
the value of the current top node.
Algorithm:
Push Operation

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.

Algorithm for insertion sort

INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1

Step 2: SET TEMP = ARR[K]

Step 3: SET J = K - 1

Step 4: Repeat while TEMP <= ARR[J]

SET ARR[J + 1] = ARR[J]

SET J = J - 1

[END OF INNER LOOP]

Step 5: SET ARR[J + 1] = TEMP

[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.

You might also like