DS PRAC
DS PRAC
DS PRAC
struct Node
{
int data;
struct Node *next;
};
void insertAtNthPosition(struct Node **head, int value, int n)//to insert at specific position
{
struct Node *newNode = createNode(value);
if (n == 0)//when pos=0 insert at begining
{
insertAtBeginning(head, value);
return;
}
struct Node *temp = *head;//temp pointer
for (int i = 0; i< n - 1 &&temp != NULL; i++)//until position
{
temp = temp->next;//set temp = nextnode
}
if (temp == NULL)//position is out of bound
{
printf("Position is out of bounds. Node not inserted.\n");
free(newNode);//deallocate the newnode
return;
}
//else
newNode->next = temp->next; //set next of new node = next of temp
temp->next = newNode;//set the next of temp = new node
}
void deleteAtNthPosition(struct Node **head, int n)//to delete from specific position
{ if (*head == NULL)//LL is empty
{
printf("List is empty! Cannot delete.\n");
return;
}
struct Node *temp = *head;//temp pointer= head
if (n == 0)//when pos==0 delete from begining
{
*head = temp->next;
free(temp);//deallocate the memory of temp pointing node
return;
}
for (int i = 0; i< n - 1 &&temp != NULL; i++)//traverse to position node
{
temp = temp->next;
}
if (temp == NULL || temp->next == NULL)//position is out of bound
{
printf("Position is out of bounds. Node not deleted.\n");
return;
}
struct Node *nodeToDelete = temp->next;//store the node to be deleted
temp->next = nodeToDelete->next;//link the n-1 node to n+1 node
free(nodeToDelete);//free the node
}
void display(struct Node *head)//to display
{
if (head == NULL)//empty
{
printf("List is empty!\n");
return;
}
struct Node *temp = head;//temp pointer
printf("Linked List: ");
while (temp != NULL)//until last
{
printf("%d -> ", temp->data);//print the node
temp = temp->next;//increment the node pointer
}
printf("NULL\n");//indicates the end of list
}
switch (choice)
{
case 1://calls insertatBegining
printf("Enter a value to insert at beginning: ");
scanf("%d", &value);//input value to be insertedd
insertAtBeginning(&head, value);//insert at begining
break;
case 2://insert at end
printf("Enter a value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3://delete the first node
deleteAtBeginning(&head);
break;
case 4:
deleteAtEnd(&head);//deleted the last element
break;
case 5://insert at specific position
printf("Enter a value to insert and the position: ");
scanf("%d %d", &value, &position);
insertAtNthPosition(&head, value, position);
break;
case 6:
printf("Enter the position to delete: ");
scanf("%d", &position);//input he position
deleteAtNthPosition(&head, position);
break;
case 7: display(head);
break;
case 8:
printf("\nEnter the value to search:");
scanf("%d",&value);
search(head,value);
break;
case 9:
printf("\nExiting...\n");//it will exit the execution
break;
default:
printf("Invalid choice! Please try again.\n");//when entered envalid choice
}
} while (choice != 9);//exit when ch==9
return 0;
}
Output:
PS C:\Users\adi\OneDrive\Desktop\CSE-3rdSem\DS\Practicals\pract7>gccSinglyLL.c
PS C:\Users\adi\OneDrive\Desktop\CSE-3rdSem\DS\Practicals\pract7> ./a.exe
Exiting...
PS C:\Users\adi\OneDrive\Desktop\CSE-3rdSem\DS\Practicals\pract7>