0% found this document useful (0 votes)
8 views3 pages

Stack and Sorting in C Programming

The document contains two C programs: one for implementing a stack using a linked list and another for sorting an array using selection sort. The stack program includes functions for pushing, popping, and displaying elements, while the sorting program prompts the user for input and sorts the array in ascending order. Sample outputs for both programs demonstrate their functionality.

Uploaded by

sahatanupriya11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Stack and Sorting in C Programming

The document contains two C programs: one for implementing a stack using a linked list and another for sorting an array using selection sort. The stack program includes functions for pushing, popping, and displaying elements, while the sorting program prompts the user for input and sorts the array in ascending order. Sample outputs for both programs demonstrate their functionality.

Uploaded by

sahatanupriya11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programs: Stack Using Linked List and Sorting

Techniques

1. Stack Implementation Using Linked List


#include <stdio.h>
#include <stdlib.h>

// Structure to create a node


struct Node {
int data;
struct Node* next;
};

// Global pointer to keep track of the top of the stack


struct Node* top = NULL;

// Function to add an element to the stack


void push(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Stack Overflow (Heap Memory Full)\n");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d pushed to stack\n", value);
}

// Function to remove an element from the stack


void pop() {
if (top == NULL) {
printf("Stack Underflow (Stack is empty)\n");
return;
}
struct Node* temp = top;
printf("Popped element: %d\n", top->data);
top = top->next;
free(temp);
}

// Function to display the stack elements


void display() {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
struct Node* temp = top;
printf("Stack elements: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
int choice, value;
printf("--- Stack using Linked List ---\n");
while (1) {
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}

Sample Output
--- Stack using Linked List ---
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 10
10 pushed to stack

Enter your choice: 1


Enter value to push: 20
20 pushed to stack

Enter your choice: 3


Stack elements: 20 -> 10 -> NULL

Enter your choice: 2


Popped element: 20

Enter your choice: 3


Stack elements: 10 -> NULL

2. Sorting an Array Using Selection Sort


#include <stdio.h>

int main() {
int arr[100], n, i, j, min_idx, temp;

printf("Enter the number of elements: ");


scanf("%d", &n);

printf("Enter %d integers:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

for (i = 0; i < n - 1; i++) {


min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
if (min_idx != i) {
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}

printf("\nSorted array in ascending order:\n");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Sample Output
Enter the number of elements: 5
Enter 5 integers:
64 25 12 22 11

Sorted array in ascending order:


11 12 22 25 64

You might also like