Insert at Position
Insert at Position
h>
#include <stdio.h>
struct Node
{
int info;
struct Node* next;
};
void insert_beginning()
{
struct Node* new_node;
int n;
new_node=getNode(n);
if (Start==NULL)
{
Start=new_node;
return;
}
else
{
new_node->next = Start;
Start = new_node;
}
}
void traverse()
{
struct Node* p = Start;
if (p == NULL)
{
printf("Empty list\n");
return;
}
int main()
{
insert_beginning();
insert_position(45,2);
traverse();
return 0;
}
void insert_sorted() {
int key;
printf("Enter a value to be inserted in sorted order: ");
scanf("%d", &key);
struct Node* new_node = getNode(key);
// Handle case when the list is empty or the new node should be at the
beginning
if (Start == NULL || Start->info > key) {
new_node->next = Start;
Start = new_node;
} else {
struct Node* p = Start;
// Traverse the list to find the correct insertion point
while (p->next != NULL && p->next->info < key) {
p = p->next;
}
new_node->next = p->next;
p->next = new_node;
}
}