17.
/* Program to insert in a sorted list */
#include <stdio.h>
#include <stdlib.h>
/* Global head pointer for the linked list */
struct Node {
int data;
struct Node* next;
};
/* Global pointer for the head of the list */
struct Node* head = NULL;
void sortedInsert(int) ;
void printList() ;
/* Menu-driven program */
int main() {
int choice, data;
do {
printf("\nMenu:\n");
printf("1. Insert an element\n");
printf("2. Print the list\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &data);
sortedInsert(data);
printf("Value inserted successfully.\n");
break;
case 2:
printf("Current list: ");
printList();
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 3);
return 0;
}
/* function to insert a new_node in a sorted list */
void sortedInsert(int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
struct Node* current;
/* Special case for the head end */
if (head == NULL || head->data >= new_node->data) {
new_node->next = head;
head = new_node;
} else {
/* Locate the node before the point of insertion */
current = head;
while (current->next != NULL && current->next->data < new_node->data) {
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
/* Function to print linked list */
void printList() {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
18.doubly linked list
#include <stdio.h>
#include <stdlib.h>
// Define the structure of Node
typedef struct Node {
int data;
struct Node *next;
struct Node *prev;
} Node;
// Global pointer for the head of the list
Node *head = NULL;
void createNode(int);
void printList();
/* Menu-driven program */
int main() {
int choice, data;
do {
printf("\nMenu:\n");
printf("1. Insert an element\n");
printf("2. Print the list\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &data);
createNode(data);
printf("Node inserted successfully.\n");
break;
case 2:
printList();
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 3);
return 0;
}
/* Function to create a new node */
void createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
// If the list is empty, set the new node as head
if (head == NULL) {
head = newNode;
} else {
// Traverse to the last node
Node *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
// Link the new node at the end of the list
temp->next = newNode;
newNode->prev = temp;
}
}
/* Function to print the doubly linked list */
void printList() {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
Node *temp = head;
printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}