Stack using array
#include<stdio.h>
#include<stdlib.h>
#define Size 4
int Top = -1, inp_array[Size];
void Push();
void Pop();
void Show(); // Note that the function name is now "Show" with an uppercase "S"
int main()
{
int choice;
while(1)
{
printf("\nOperations performed by Stack");
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
printf("\n\nEnter the choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: Show(); // Updated to "Show" to match the function declaration
break;
case 4: exit(0);
default: printf("\nInvalid choice!!");
}
}
return 0;
}
void Push()
{
int x;
if (Top == Size - 1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter element to be inserted to the stack: ");
scanf("%d", &x);
Top = Top + 1;
inp_array[Top] = x;
}
}
void Pop()
{
if (Top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d", inp_array[Top]);
Top = Top - 1;
}
}
void Show() // Updated to "Show" to match the call in the main function
{
if (Top == -1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nElements present in the stack: \n");
for (int i = Top; i >= 0; --i)
printf("%d\n", inp_array[i]);
}
}
#include<stdio.h>
#include<stdlib.h>
#define Size 4
int Top = -1, inp_array[Size];
void Push();
void Pop();
void Show(); // Note that the function name is now "Show" with an uppercase "S"
int main()
{
int choice;
while(1)
{
printf("\nOperations performed by Stack");
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
printf("\n\nEnter the choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: Show(); // Updated to "Show" to match the function declaration
break;
case 4: exit(0);
default: printf("\nInvalid choice!!");
}
}
return 0;
}
void Push()
{
int x;
if (Top == Size - 1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter element to be inserted to the stack: ");
scanf("%d", &x);
Top = Top + 1;
inp_array[Top] = x;
}
}
void Pop()
{
if (Top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d", inp_array[Top]);
Top = Top - 1;
}
}
void Show() // Updated to "Show" to match the call in the main function
{
if (Top == -1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nElements present in the stack: \n");
for (int i = Top; i >= 0; --i)
printf("%d\n", inp_array[i]);
}
}
LL creation
#include <iostream>
using namespace std;
// Define a structure for a node in the linked list
struct Node {
int data; // Data to store (integer)
Node* next; // Pointer to the next node
};
// Function to create a new node
Node* createNode(int data) {
Node* newNode = new Node(); // Allocate memory for a new node
newNode->data = data; // Set the data
newNode->next = nullptr; // Initialize next pointer to null
return newNode;
}
// Function to insert a node at the end of the linked list
void insertNode(Node*& head, int data) {
Node* newNode = createNode(data); // Create a new node
if (head == nullptr) { // If the list is empty
head = newNode; // Set head to the new node
} else {
Node* temp = head; // Temporary node to traverse the list
while (temp->next != nullptr) // Traverse to the last node
temp = temp->next;
temp->next = newNode; // Link the last node to the new node
}
}
// Function to display the linked list
void displayList(Node* head) {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
Node* temp = head; // Temporary node to traverse the list
while (temp != nullptr) {
cout << temp->data << " -> "; // Print the current node's data
temp = temp->next; // Move to the next node
}
cout << "NULL" << endl; // Mark the end of the list
}
int main() {
Node* head = nullptr; // Initialize the head of the list as nullptr
int choice, data;
do {
cout << "Enter a number to insert into the linked list: ";
cin >> data;
insertNode(head, data); // Insert user input into the list
cout << "Do you want to insert another number? (1 for yes, 0 for no): ";
cin >> choice;
} while (choice != 0); // Continue until the user chooses to stop
// Display the linked list
cout << "Linked list elements: ";
displayList(head);
return 0;
}
Node insertion-Beginning
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node
struct Node {
int data; // Data part of the node
struct Node* next; // Pointer to the next node
};
// Initialize the head of the linked list as NULL (empty list)
struct Node* head = NULL;
// Function to insert a node at the beginning of the linked list
void Insert_begin() {
// Allocate memory for the new node
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
// Check if memory allocation was successful
if (p == NULL) {
printf("Memory allocation failed!\n");
return;
}
// Get data from the user
printf("Enter the data: ");
scanf("%d", &p->data);
// Set the next pointer of the new node to NULL initially
p->next = NULL;
// If the list is empty, make the new node the head
if (head == NULL) {
head = p;
} else {
// Otherwise, link the new node to the current head and update head
p->next = head;
head = p;
}
}
// Function to display the linked list
void Display() {
// If the list is empty
if (head == NULL) {
printf("List is empty.\n");
return;
}
// Temporary pointer to traverse the list
struct Node* temp = head;
printf("Elements of the linked list: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next; // Move to the next node
}
printf("NULL\n"); // Mark the end of the list
}
int main() {
int choice;
while (1) {
// Menu to insert node at the beginning or display the list
printf("\n1. Insert at Beginning\n");
printf("2. Display Linked List\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
Insert_begin(); // Insert node at the beginning
break;
case 2:
Display(); // Display the list
break;
case 3:
exit(0); // Exit the program
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Node insertion- Middle
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node
struct Node {
int data;
struct Node* next;
};
// Initialize the head of the linked list as NULL
struct Node* head = NULL;
// Function to insert a node at the beginning (for testing purpose)
void insert_begin(int data) {
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
p->data = data;
p->next = head;
head = p;
}
// Function to insert a node after a specific value (key) in the linked list
void insert_between() {
struct Node* p;
struct Node* temp;
struct Node* prev;
int key, r;
// Allocate memory for a new node
p = (struct Node*)malloc(sizeof(struct Node));
// Check if memory allocation was successful
if (p == NULL) {
printf("Memory allocation failed!\n");
return;
}
// Get the data to insert
printf("Enter the data: ");
scanf("%d", &p->data);
// Initialize the next pointer of the new node to NULL
p->next = NULL;
// Get the key (position) after which the new node should be inserted
printf("Enter the value after which data is to be inserted: ");
scanf("%d", &key);
// Start searching for the key in the list
temp = head;
while (temp != NULL) {
if (temp->data == key) {
// Insert the node after the key
p->next = temp->next; // Link the new node to the next of the key node
temp->next = p; // Link the key node to the new node
printf("Node inserted after %d.\n", key);
return;
}
temp = temp->next; // Move to the next node
}
// If the key is not found
printf("Key %d not found in the list.\n", key);
}
// Function to display the linked list
void display() {
struct Node* temp = head;
if (head == NULL) {
printf("List is empty.\n");
return;
}
printf("Linked list elements: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main function to test the program
int main() {
int choice, data;
while (1) {
printf("\n1. Insert at Beginning\n");
printf("2. Insert After Specific Value\n");
printf("3. Display Linked List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
insert_begin(data);
break;
case 2:
insert_between();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Node insertion-End
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node
typedef struct node {
int data;
struct node *next;
} node;
node *head = NULL;
// Function to insert a node at the end of the linked list
void insert_end() {
node *p, *temp;
// Allocate memory for the new node
p = (node*)malloc(sizeof(node));
// Input data for the new node
printf("Enter data: ");
scanf("%d", &p->data);
// Set the next pointer of the new node to NULL
p->next = NULL;
// If the list is empty, make the new node the head
if (head == NULL) {
head = p;
} else {
// Traverse the list to find the last node
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
// Set the next of the last node to the new node
temp->next = p;
}
}
// Function to display the linked list
void display() {
node *temp = head;
// If the list is empty
if (head == NULL) {
printf("List is empty.\n");
return;
}
// Traverse and print each node's data
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
int choice;
while (1) {
printf("\n1. Insert at end\n");
printf("2. Display list\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert_end();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
Delete node-Begin
#include <stdio.h>
#include <stdlib.h>
// Define the node structure
typedef struct node {
int data;
struct node* next;
} node;
// Function to delete the head node
node* delete_b(node* head) {
node* p;
p = head;
// If there's only one node in the list
if (head->next == NULL) {
head = NULL;
free(p); // Free the memory of the old head node
} else {
// Move the head to the next node
head = head->next;
free(p); // Free the old head node
}
return head; // Return the new head of the list
}
// Utility function to add nodes at the front
node* push(node* head, int data) {
node* new_node = (node*)malloc(sizeof(node));
new_node->data = data;
new_node->next = head;
return new_node;
}
// Utility function to print the list
void printList(node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
node* head = NULL;
int choice, data, n, i;
while (1) {
printf("\nMenu:\n");
printf("1. Add nodes to the list\n");
printf("2. Delete the head node\n");
printf("3. Print the list\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("How many nodes do you want to add? ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the value for node %d: ", i + 1);
scanf("%d", &data);
head = push(head, data);
}
break;
case 2:
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
} else {
head = delete_b(head);
printf("Head node deleted.\n");
}
break;
case 3:
if (head == NULL) {
printf("List is empty.\n");
} else {
printList(head);
}
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Delete node-Middle
#include <stdio.h>
#include <stdlib.h>
// Define the node structure
typedef struct node {
int data;
struct node* next;
} node;
// Function to delete a node by key in a singly linked list
node* delete_in_between(node* head, int key) {
node *temp, *prev;
// If the list is empty
if (head == NULL) {
printf("List is empty.\n");
return head;
}
// If the head node itself holds the key to be deleted
if (head->data == key) {
temp = head; // Store head node temporarily
head = head->next; // Move the head to the next node
free(temp); // Free the old head
return head;
}
// Search for the key and keep track of the previous node
temp = head;
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
// If key was not present in the list
if (temp == NULL) {
printf("Key not found in the list.\n");
return head;
}
// Unlink the node from the list
prev->next = temp->next;
free(temp); // Free the memory of the node to be deleted
return head;
}
// Function to add nodes at the end of the list
node* insert_end(node* head, int data) {
node* new_node = (node*)malloc(sizeof(node));
new_node->data = data;
new_node->next = NULL;
if (head == NULL) {
head = new_node; // First node in the list
} else {
node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
}
return head;
}
// Function to print the list
void printList(node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
node* head = NULL;
int choice, data, key, n, i;
while (1) {
printf("\nMenu:\n");
printf("1. Add nodes to the list\n");
printf("2. Delete a node by key\n");
printf("3. Print the list\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("How many nodes do you want to add? ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the value for node %d: ", i + 1);
scanf("%d", &data);
head = insert_end(head, data);
}
break;
case 2:
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
} else {
printf("Enter the key to delete: ");
scanf("%d", &key);
head = delete_in_between(head, key);
}
break;
case 3:
printList(head);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Delete node-End
#include <stdio.h>
#include <stdlib.h>
// Define the node structure
typedef struct node {
int data;
struct node* next;
} node;
// Function to delete the last node
node* delete_end(node* head) {
node *p, *q;
p = head;
// If the list is empty
if (head == NULL) {
printf("List is empty. No nodes to delete.\n");
return head;
}
// If there's only one node in the list
if (head->next == NULL) {
free(p); // Free the memory of the last node
head = NULL;
} else {
// Traverse to the second last node
q = head;
while (q->next->next != NULL) {
q = q->next;
}
// Now q points to the second last node, p to the last node
p = q->next;
q->next = NULL; // Remove the link to the last node
free(p); // Free the memory of the last node
}
return head; // Return the new head of the list
}
// Utility function to add nodes at the front
node* push(node* head, int data) {
node* new_node = (node*)malloc(sizeof(node));
new_node->data = data;
new_node->next = head;
return new_node;
}
// Utility function to print the list
void printList(node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
node* head = NULL;
int choice, data, n, i;
while (1) {
printf("\nMenu:\n");
printf("1. Add nodes to the list\n");
printf("2. Delete the last node\n");
printf("3. Print the list\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("How many nodes do you want to add? ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the value for node %d: ", i + 1);
scanf("%d", &data);
head = push(head, data);
}
break;
case 2:
head = delete_end(head);
break;
case 3:
printList(head);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Infix-Postfix
#include <stdio.h>
#include <ctype.h>
#define SIZE 50
char s[SIZE];
int top = -1;
// Push function to push an element onto the stack
void push(char elem) {
s[++top] = elem;
}
// Pop function to pop an element from the stack
char pop() {
return s[top--];
}
// Function to return precedence of operators
int pr(char elem) {
switch (elem) {
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
default: return -1;
}
}
int main() {
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
// Read infix expression from the user
printf("Enter the Infix Expression: ");
scanf("%s", infx);
push('#'); // Add a sentinel to the stack
// Convert infix to postfix
while ((ch = infx[i++]) != '\0') {
if (ch == '(') {
push(ch); // Push opening parentheses
} else if (isalnum(ch)) {
pofx[k++] = ch; // Add operand to postfix expression
} else if (ch == ')') {
while (s[top] != '(') {
pofx[k++] = pop(); // Pop and add operators to postfix until '('
}
elem = pop(); // Pop '(' from the stack
} else {
// Operator encountered, pop from stack based on precedence
while (pr(s[top]) >= pr(ch)) {
pofx[k++] = pop();
}
push(ch); // Push the current operator
}
}
// Pop the remaining operators from the stack
while (s[top] != '#') {
pofx[k++] = pop();
}
pofx[k] = '\0'; // Terminate the postfix expression
// Print the postfix expression
printf("Given Infix Expression: %s\nPostfix Expression: %s\n", infx, pofx);
return 0;
}
String Reverse
#include <stdio.h>
#include <string.h>
#define MAX 20
int top = -1;
char stack[MAX];
// Push function to push a character onto the stack
void push(char item) {
if (top == (MAX - 1)) {
printf("Stack Overflow\n");
} else {
stack[++top] = item;
}
}
// Pop function to pop a character from the stack
char pop() {
if (top == -1) {
printf("Stack Underflow\n");
return '\0'; // Return null character in case of underflow
} else {
return stack[top--];
}
}
// Main function
int main() {
char str[20], str1[20];
int i;
// Input the string
printf("Enter the string: ");
fgets(str, sizeof(str), stdin);
// Remove the newline character added by fgets
str[strcspn(str, "\n")] = '\0';
// Push each character of the string onto the stack
for (i = 0; i < strlen(str); i++) {
push(str[i]);
}
// Pop characters from the stack to reverse the string
for (i = 0; i < strlen(str); i++) {
str1[i] = pop();
}
str1[i] = '\0'; // Null-terminate the reversed string
// Output the reversed string
printf("Reversed string is: ");
puts(str1);
return 0;
}
Selection Sort
#include <stdio.h>
int main() {
/* Here i & j for loop counters, temp for swapping,
* count for total number of elements, number[] to
* store the input numbers in array. You can increase
* or decrease the size of number array as per requirement.
*/
int i, j, count, temp, number[25];
printf("Enter the number of elements: ");
scanf("%d", &count);
printf("Enter %d numbers: ", count);
// Loop to get the elements stored in the array
for (i = 0; i < count; i++) {
scanf("%d", &number[i]);
}
// Logic of selection sort algorithm
for (i = 0; i < count - 1; i++) { // Iterate over each element except the last
for (j = i + 1; j < count; j++) {
if (number[i] > number[j]) {
// Swap the elements if they are in the wrong order
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
printf("Sorted Array: ");
for (i = 0; i < count; i++) {
printf("%d ", number[i]);
}
printf("\n"); // Add a new line at the end for better output formatting
return 0;
}
Bubble Sort
#include <stdio.h>
int main() {
int array[100], n, c, d, swap;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
// Bubble sort algorithm
for (c = 0; c < n - 1; c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d + 1]) { // For decreasing order, use '<' instead of '>'
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++) {
printf("%d\n", array[c]);
}
return 0;
}
Quick Sort
#include <stdio.h>
// Function to perform Quick Sort
void quicksort(int list[], int low, int high);
int main() {
int list[50]; // Array to hold the numbers
int size, i;
// Get the size of the list
printf("Enter the number of elements: ");
scanf("%d", &size);
// Get the elements to be sorted
printf("Enter the elements to be sorted:\n");
for (i = 0; i < size; i++) {
scanf("%d", &list[i]);
}
// Apply quicksort on the entire array
quicksort(list, 0, size - 1);
// Print the sorted list
printf("After applying quick sort:\n");
for (i = 0; i < size; i++) {
printf("%d ", list[i]);
}
printf("\n");
return 0;
}
// Function to perform Quick Sort recursively
void quicksort(int list[], int low, int high) {
int pivot, i, j, temp;
if (low < high) {
pivot = low; // Select the pivot element
i = low;
j = high;
// Partitioning the array
while (i < j) {
// Find an element larger than the pivot
while (list[i] <= list[pivot] && i <= high) {
i++;
}
// Find an element smaller than or equal to the pivot
while (list[j] > list[pivot] && j >= low) {
j--;
}
// Swap if needed
if (i < j) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
// Swap the pivot element with the element at j
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
// Recursively sort the sub-arrays
quicksort(list, low, j - 1); // Left side of pivot
quicksort(list, j + 1, high); // Right side of pivot
}
}