DSA_FILE_SEM_3
DSA_FILE_SEM_3
#include <stdio.h>
#include <stdlib.h> //Kunal Sharma
struct node
{
int data;
struct node *next;
}*head;
}
}
void main(){
printf("Enter the number of nodes to create : ");
int n;
scanf("%d",&n);
head = link_nodes(head,n);
print_list(head);
}
OUTPUT
--------------Kunal Sharma---------------
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node
{
int data;
struct node *next;
}*head,*tail;
}
// if the node is to be inserted at the end
else if(tail->data == key){
struct node *newnode;
newnode = create_node();
tail->next = newnode;
newnode = tail;
}
// if the node is to be inserted in between
else{
struct node* newnode,*temp;
newnode = create_node();
temp = head;
while(temp->data != key){
temp = temp->next;
newnode->next = temp->next;
temp->next = newnode;
return head;
}
}
while(temp != NULL)
{
printf("Data of node : %d \n",temp->data);
temp = temp->next;
}
}
}
// Main Function
void main(){
printf("-------------------------------------\n");
printf("Enter the number of nodes to create : ");
int n;
scanf("%d",&n);
head = link_nodes(head,n);
printf("Enter the node data you want to insert at : ");
int key;
scanf("%d",&key);
insert_node(head,key);
print_list(head);
OUTPUT
--------Kunal Sharma-----------
Data of node : 1
Data of node : 2
Data of node : 34
Data of node : 3
Data of node : 4
Data of node : 5
Program to delete a node at any position in a Singly linked list
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node
{
int data;
struct node *next;
}*head,*tail;
}
}
while(temp != NULL)
{
printf("Data of node : %d \n",temp->data);
temp = temp->next;
}
}
}
// Main Function
void main(){
OUTPUT
------------Kunal Sharma--------------
Enter the number of nodes to create : 5
Enter Data for node : 1
Enter Data for node : 2
Enter Data for node : 3
Enter Data for node : 4
Enter Data for node : 5
Data of node : 1
Data of node : 2
Data of node : 3
Data of node : 5
Program to create a Circular singly linked list and print it
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node{
int data;
struct node *next;
}*head;
void main(){
printf("Enter the number of nodes to create : ");
int n;
scanf("%d",&n);
head = Link_Nodes(head,n);
print_list(head);
OUTPUT
-----------Kunal Sharma-----------------
Enter the number of nodes to create : 4
Enter data for node : 1
Enter data for node : 2
Enter data for node : 3
Enter data for node : 4
Data of Node: 1
Data of Node: 2
Data of Node: 3
Data of Node: 4
Program to insert a node in a circular singly linked List
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node{
int data;
struct node *next;
}*head;
}
temp = temp->next;
}
if (temp->data == key) {
newnode->next = temp->next;
temp->next = newnode;
}
return head;
}
// Function to print the list
void main(){
printf("Enter the number of nodes to create : ");
int n;
scanf("%d",&n);
head = Link_Nodes(head,n);
printf("Enter the data of the node you want to insert at :");
int key;
scanf("%d",&key);
insert_Node(head,key);
print_list(head);
}
OUTPUT
---------Kunal Sharma---------
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*head,*tail;
if(head == NULL){
printf("The list is empty");
}
struct node *p = head;
struct node *prev = tail;
if (head->data == key) {
if (head == tail) {
free(head);
head = NULL;
return head;
} else {
prev->next = head->next;
struct node *temp = head;
head = head->next;
free(temp);
return head;
}
}
p = head;
while (p->next != head) {
if (p->next->data == key) {
struct node *temp = p->next;
p->next = p->next->next;
if (temp == tail) {
tail = p;
}
free(temp);
return head;
}
p = p->next;
}
}
// Function to print the list
void main(){
printf("Enter the number of nodes to create : ");
int n;
scanf("%d",&n);
head = Link_Nodes(head,n);
printf("Enter the data of the node you want to Delete :");
int key;
scanf("%d",&key);
delete_Node(head,key);
print_list(head);
OUTPUT
----------Kunal Sharma----------
Program to create and print a Doubly linked list
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node{
int data;
struct node *next;
struct node *prev;
}*head,*tail;
return head;
}
void main(){
int n;
printf("Enter the number of node to create : ");
scanf("%d",&n);
head = link_nodes(head,n);
print_list(head);
OUTPUT
---------Kunal Sharma----------
Enter the number of node to create : 4
Enter data for node : 1
Enter data for node : 2
Enter data for node : 3
Enter data for node : 4
Data at node : 1
Data at node : 2
Data at node : 3
Data at node : 4
Program to insert a node in a Doubly a linked list
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
struct node *prev;
}*head,*tail;
return head;
}
}
newnode->prev = temp;
newnode->next = temp->next;
temp->next->prev = newnode;
temp->next = newnode;
return head;
}
void main(){
int n;
printf("Enter the number of node to create : ");
scanf("%d",&n);
head = link_nodes(head,n);
printf("Enter the node data you want to insert at : ");
int key;
scanf("%d",&key);
insert_node(head,key);
print_list(head);
OUTPUT
----------Kunal Sharma-----------
Enter the number of node to create : 4
Enter data for node : 1
Enter data for node : 2
Enter data for node : 3
Enter data for node : 4
Enter the node data you want to insert at : 3
Enter data for node : 23
Data at node : 1
Data at node : 2
Data at node : 3
Data at node : 23
Data at node : 4
Program to delete a node in a Doubly Linked list
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node{
int data;
struct node *next;
struct node *prev;
}*head,*tail;
return head;
}
free(temp);
return head;
}
temp = head;
while (temp != NULL) {
if (temp->data == key) {
// Case 2: Deleting the tail node
if (temp == tail) {
tail = tail->prev;
if (tail != NULL) {
tail->next = NULL;
}
}
// Case 3: Deleting a node in the middle
else {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
}
free(temp);
return head;
}
temp = temp->next; // Traverse to the next node
}
void main(){
int n;
printf("Enter the number of node to create : ");
scanf("%d",&n);
head = link_nodes(head,n);
printf("Enter the node data you want to delete : ");
int key;
scanf("%d",&key);
head = delete_node(head,key);
print_list(head);
OUTPUT
----------Kunal Sharma----------
Enter the number of node to create : 5
Enter data for node : 1
Enter data for node : 2
Enter data for node : 3
Enter data for node : 4
Enter data for node : 5
Enter the node data you want to delete : 3
Data at node : 1
Data at node : 2
Data at node : 4
Data at node : 5
Program to create and print a Circular Doubly Linked List
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node{
int data;
struct node *next;
struct node *prev;
}*head,*tail;
tail->next = newnode;
head->prev = newnode;
tail = newnode;
}
n--;
}
tail = newnode;
return head;
}
//Function to print list
void main(){
int n;
printf("Enter the number of node to create : ");
scanf("%d",&n);
head = link_nodes(head,n);
print_list(head);
OUTPUT
---------Kunal Sharma---------
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node{
int data;
struct node *next;
struct node *prev;
}*head,*tail;
tail->next = newnode;
head->prev = newnode;
tail = newnode;
}
n--;
}
tail = newnode;
return head;
}
}
else{
while(temp->data == key){
temp = temp->next;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next->prev = newnode;
temp->next = newnode;
}
return head;
}
//Function to print list
void main(){
int n;
printf("Enter the number of node to create : ");
scanf("%d",&n);
head = link_nodes(head,n);
printf("Enter the data of the node you want to insert at : ");
int key;
scanf("%d",&key);
head = insert_node(head,key);
print_list(head);
OUTPUT
-----------Kunal Sharma----------
Enter the number of node to create : 4
Enter data for node : 1
Enter data for node : 2
Enter data for node : 3
Enter data for node : 4
Enter the data of the node you want to insert at : 4
Enter data for node : 5
Data at node : 1
Data at node : 2
Data at node : 3
Data at node : 4
Data at node : 5
Program to delete a node from a Cicular Doubly Linked List
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node{
int data;
struct node *next;
struct node *prev;
}*head,*tail;
tail->next = newnode;
head->prev = newnode;
tail = newnode;
}
n--;
}
tail = newnode;
return head;
}
}
//Function to print list
void main(){
int n;
printf("Enter the number of node to create : ");
scanf("%d",&n);
head = link_nodes(head,n);
printf("Enter the data of the node you want to insert at : ");
int key;
scanf("%d",&key);
head = delete_node(head,key);
print_list(head);
OUTPUT
--------Kunal Sharma--------
Program to Implement the Towe Of Hanoi
#include <stdio.h>
if (n == 1) {
printf("Move disk 1 from %c to %c\n", A, C);
return;
}
hanoi(n - 1, A, C, B);
printf("Move disk %d from %c to %c\n", n, A, C);
hanoi(n - 1, B, A, C);
}
void main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
OUTPUT
Enter the number of disks: 3
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Program to rotate a circular singly linked list by n positions
#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node {
int data;
struct node *next;
} *head;
p->next = head;
return head;
}
current = head;
for (int i = 1; i < length - n; i++) {
current = current->next;
}
return new_head;
}
// Main function
int main() {
printf("Enter the number of nodes to create: ");
int n;
scanf("%d", &n);
head = Link_Nodes(head, n);
printf("Enter the number of positions you want to rotate the list by: ");
int x;
scanf("%d", &x);
return 0;
}
OUTPUT
--------Kunal Sharma--------