0% found this document useful (0 votes)
4 views37 pages

DSA_FILE_SEM_3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views37 pages

DSA_FILE_SEM_3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Program to create a singly linked list and print it

#include <stdio.h>
#include <stdlib.h> //Kunal Sharma
struct node
{
int data;
struct node *next;
}*head;

// Fucntion to Create Nodes

struct node *create_node(){


struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter Data for node : ");
scanf("%d",&p->data);
return p;

// Function to link nodes

struct node *link_nodes(struct node *head,int n){

struct node *p,*newnode;


while(n>0)
{
if(head == NULL) // if head is null this means no node has been created
{
p = create_node(); // so we create a node and assign it to the head
head = p;
}
else
{
newnode = create_node(); // else there is already a node present so we create a new node
p->next = newnode;
p = newnode;
}
n--;
}
return head;
}

// Print the created list

struct node *print_list(struct node *head){


struct node *temp = head;
if(temp == NULL)
{
printf("----------Kunal Sharma-------\n");
printf("The list is empty");
exit(0);
}
else
{
printf("---------Kunal Sharma---------\n");
while(temp != NULL)
{
printf("Data of node : %d \n",temp->data);
temp = temp->next;
}

}
}

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 : 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 : 4
Data of node : 5
Program to insert 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;

// Fucntion to Create Nodes

struct node *create_node(){


struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter Data for node : ");
scanf("%d",&p->data);
return p;

// Function to link nodes

struct node *link_nodes(struct node *head,int n){

struct node *p,*newnode;


while(n>0)
{
if(head == NULL) // if head is null this means no node has been created
{
p = create_node(); // so we create a node and assign it to the head
head = p;
}
else
{
newnode = create_node(); // else there is already a node present so we create a new node and
link it to list
p->next = newnode;
p = newnode;
tail = p;
}
n--;
}
return head;
}
// Function to insert a node at any position
struct node *insert_node(struct node *head,int key){
// if the node is to be inserted at the start
if(head->data == key){
struct node *newnode;
newnode = create_node();
newnode->next = head;
head = newnode;
return head;

}
// 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;

}
}

// print the list


struct node *print_list(struct node *head){
struct node *temp = head;
if(temp == NULL)
{

printf("The list is empty");


exit(0);
}
else
{

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-----------

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

Enter the node data you want to insert : 3


Enter Data for node : 34

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;

// Fucntion to Create Nodes

struct node *create_node(){


struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter Data for node : ");
scanf("%d",&p->data);
return p;

// Function to link nodes

struct node *link_nodes(struct node *head,int n){

struct node *p,*newnode;


while(n>0)
{
if(head == NULL) // if head is null this means no node has been created
{
p = create_node(); // so we create a node and assign it to the head
head = p;
}
else
{
newnode = create_node(); // else there is already a node present so we create a new node and
link it to list
p->next = newnode;
p = newnode;
tail = p;
}
n--;
}
return head;
}
// Function to Delete a node at any position
struct node *insert_node(struct node *head,int key){

if(head != NULL && head->data == key){

struct node *temp = head;


head = head->next;
free(temp);
return head;
}
else if(tail->data == key){
struct node *p,*q;
q = head;
p = head->next;
while(p->next !=NULL){
q = p;
p = p->next;
}
free(p);
q->next = NULL;
}
else{
struct node *p,*q;
p = head;
q = head->next;
while(q->data !=key){
p = p->next;
q = q->next;
}
p->next = q->next;
free(q);
return head;

}
}

// print the list


struct node *print_list(struct node *head){
struct node *temp = head;
if(temp == NULL)
{

printf("The list is empty");


exit(0);
}
else
{

while(temp != NULL)
{
printf("Data of node : %d \n",temp->data);
temp = temp->next;
}

}
}
// Main Function
void main(){

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 delete : ");
int key;
scanf("%d",&key);
insert_node(head,key);
print_list(head);

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

Enter the node data you want to delete : 4

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;

// Function to create Nodes

struct node *create_Node(){


struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node : ");
scanf("%d",&p->data);
return p;
}

// Function to link nodes

struct node *Link_Nodes(struct node *head,int n){


struct node *p,*newnode;
while(n>0){
if(head == NULL){
p = create_Node();
head = p;
}
else{
newnode = create_Node();
p->next = newnode;
p = newnode;
}
n--;
}
p->next = head;
return head;
}

// Function to print the list

void print_list(struct node *head){


struct node *temp;
temp = head;
if(temp == NULL){
printf("The list is empty");
exit(0);
}
else{
do {
printf("Data of Node: %d\n", temp->data);
temp = temp->next;
} while (temp != head);
}
}
// Main function

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;

// Function to create Nodes

struct node *create_Node(){


struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node : ");
scanf("%d",&p->data);
return p;
}

// Function to link nodes

struct node *Link_Nodes(struct node *head,int n){


struct node *p,*newnode;
while(n>0){
if(head == NULL){
p = create_Node();
head = p;
}
else{
newnode = create_Node();
p->next = newnode;
p = newnode;
}
n--;
}
p->next = head;
return head;
}

// Function to insert a node

struct node *insert_Node(struct node *head,int key){


struct node *newnode,*temp;
temp = head;
newnode = create_Node();
if(head == NULL){
printf("The list is empty\n");
}
while (temp->next != head) {
if (temp->data == key) {
newnode->next = temp->next;
temp->next = newnode;
return head;

}
temp = temp->next;
}

if (temp->data == key) {
newnode->next = temp->next;
temp->next = newnode;

}
return head;
}
// Function to print the list

void print_list(struct node *head){


struct node *temp;
temp = head;
if(temp == NULL){
printf("The list is empty");
exit(0);
}
else{
do {
printf("Data of Node: %d\n", temp->data);
temp = temp->next;
} while (temp != head);
}
}
// Main function

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---------

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
Enter the data of the node you want to insert at :4
Enter data for node : 34
Data of Node: 1
Data of Node: 2
Data of Node: 3
Data of Node: 4
Data of Node: 34
Program to delete a node from a Circular Singly Linked List

#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*head,*tail;

// Function to create Nodes

struct node *create_Node(){


struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node : ");
scanf("%d",&p->data);
return p;
}

// Function to link nodes

struct node *Link_Nodes(struct node *head,int n){


struct node *p,*newnode;
while(n>0){
if(head == NULL){
p = create_Node();
head = p;
}
else{
newnode = create_Node();
p->next = newnode;
p = newnode;
}
n--;
}
p->next = head;
tail = p;
return head;
}

// Function to Delete a node

struct node *delete_Node(struct node *head,int key){

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 print_list(struct node *head){


struct node *temp;
temp = head;
if(temp == NULL){
printf("The list is empty");
exit(0);
}
else{
do {
printf("Data of Node: %d\n", temp->data);
temp = temp->next;
} while (temp != head);
}
}
// Main function

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;

// Function to create a node


struct node *create_Node(){
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node : ");
scanf("%d",&p->data);
p->next = NULL;
p->prev = NULL;
return p;

// Function to link nodes

struct node *link_nodes(struct node *head,int n){


struct node *newnode,*temp;
while(n>0){
if(head == NULL){
head = create_Node();
}
else{
temp = head;
while(temp->next != NULL){
temp = temp->next;
}
newnode = create_Node();
temp->next = newnode;
newnode->prev = temp;
}
n--;
}
tail = newnode;

return head;
}

//Function to print list

void print_list(struct node *head){


struct node *temp;
temp = head;
if(temp == NULL){
printf("The list is empty");
}
else{
while(temp != NULL){
printf("Data at node : %d\n",temp->data);
temp = temp->next;
}
}
}
// Main Function

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;

// Function to create a node


struct node *create_Node(){
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node : ");
scanf("%d",&p->data);
p->next = NULL;
p->prev = NULL;
return p;

// Function to link nodes

struct node *link_nodes(struct node *head,int n){


struct node *newnode,*temp;
while(n>0){
if(head == NULL){
head = create_Node();
}
else{
temp = head;
while(temp->next != NULL){
temp = temp->next;
}
newnode = create_Node();
temp->next = newnode;
newnode->prev = temp;
}
n--;
}
tail = newnode;

return head;
}

// Function to insert a node

struct node *insert_node(struct node *head,int key){


struct node *newnode;
struct node *temp;
temp = head;
newnode = create_Node();
if(head->data == key){
newnode->next = head;
head->prev = newnode;
head = newnode;
return head;
}
else if(tail->data == key){
tail->next = newnode;
newnode->prev = tail;
}
else{
while(temp->data !=key){
temp = temp->next;

}
newnode->prev = temp;
newnode->next = temp->next;
temp->next->prev = newnode;
temp->next = newnode;
return head;
}

//Function to print list

void print_list(struct node *head){


struct node *temp;
temp = head;
if(temp == NULL){
printf("The list is empty");
}
else{
while(temp != NULL){
printf("Data at node : %d\n",temp->data);
temp = temp->next;
}
}
}
// Main Function

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;

// Function to create a node


struct node *create_Node(){
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node : ");
scanf("%d",&p->data);
p->next = NULL;
p->prev = NULL;
return p;

// Function to link nodes

struct node *link_nodes(struct node *head,int n){


struct node *newnode,*temp;
while(n>0){
if(head == NULL){
head = create_Node();
}
else{
temp = head;
while(temp->next != NULL){
temp = temp->next;
}
newnode = create_Node();
temp->next = newnode;
newnode->prev = temp;
}
n--;
}
tail = newnode;

return head;
}

// Function to Delete a node

struct node *delete_node(struct node *head,int key){


struct node *temp;
if (head != NULL && head->data == key) {
temp = head;
head = head->next;
if(head != NULL){
head->prev = NULL;
}

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
}

printf("Key not found in the list.\n");


return head;
}

//Function to print list

void print_list(struct node *head){


struct node *temp;
temp = head;
if(temp == NULL){
printf("The list is empty");
}
else{
while(temp != NULL){
printf("Data at node : %d\n",temp->data);
temp = temp->next;
}
}
}
// Main Function

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;

// Function to create a node


struct node *create_Node(){
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node : ");
scanf("%d",&p->data);
p->next = NULL;
p->prev = NULL;
return p;

// Function to link nodes

struct node *link_nodes(struct node *head,int n){


struct node *newnode,*temp;
while(n>0){
if(head == NULL){
head = create_Node();
head->next = head;
head->prev = head;
tail = head;
}
else {
newnode = create_Node();
newnode->next = head;
newnode->prev = tail;

tail->next = newnode;
head->prev = newnode;
tail = newnode;
}
n--;
}
tail = newnode;

return head;
}
//Function to print list

void print_list(struct node *head){


struct node *temp;
temp = head;
if(temp == NULL){
printf("The list is empty");
}
else{
do {
printf("Data at node : %d\n", temp->data);
temp = temp->next;
} while (temp != head);
}
}
// Main Function

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 : 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 at node : 1
Data at node : 2
Data at node : 3
Data at node : 4
Data at node : 5
Program to insert a node in a Circular Doubley Linked List

#include <stdio.h>
#include <stdlib.h> // Kunal Sharma
struct node{
int data;
struct node *next;
struct node *prev;

}*head,*tail;

// Function to create a node


struct node *create_Node(){
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node : ");
scanf("%d",&p->data);
p->next = NULL;
p->prev = NULL;
return p;

// Function to link nodes

struct node *link_nodes(struct node *head,int n){


struct node *newnode,*temp;
while(n>0){
if(head == NULL){
head = create_Node();
head->next = head;
head->prev = head;
tail = head;
}
else {
newnode = create_Node();
newnode->next = head;
newnode->prev = tail;

tail->next = newnode;
head->prev = newnode;
tail = newnode;
}
n--;
}
tail = newnode;

return head;
}

// Function to insert a node in the list


struct node *insert_node(struct node *head,int key){
struct node *newnode = create_Node();
struct node *temp;
temp = head;
if(head->data == key){
newnode->next = head;
newnode->prev = head->prev;
head->prev->next = newnode;
head->prev = newnode;
head = newnode;
}
else if(tail->data ==key){
newnode->next = head;
newnode->prev = tail;
tail->next = newnode;
head->prev = newnode;
tail = newnode;

}
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 print_list(struct node *head){


struct node *temp;
temp = head;
if(temp == NULL){
printf("The list is empty");
}
else{
do {
printf("Data at node : %d\n", temp->data);
temp = temp->next;
} while (temp != head);
}
}
// Main Function

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;

// Function to create a node


struct node *create_Node(){
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node : ");
scanf("%d",&p->data);
p->next = NULL;
p->prev = NULL;
return p;

// Function to link nodes

struct node *link_nodes(struct node *head,int n){


struct node *newnode,*temp;
while(n>0){
if(head == NULL){
head = create_Node();
head->next = head;
head->prev = head;
tail = head;
}
else {
newnode = create_Node();
newnode->next = head;
newnode->prev = tail;

tail->next = newnode;
head->prev = newnode;
tail = newnode;
}
n--;
}
tail = newnode;

return head;
}

// Function to Delete a node in the list


struct node *delete_node(struct node *head,int key){
if (head == NULL) {
printf("The list is empty.\n");
return NULL;
}

struct node *temp = head;

// Deleting the head node


if (head->data == key) {
if (head == tail) {
free(head);
head = NULL;
tail = NULL;
return head;
} else {
head = head->next;
head->prev = tail;
tail->next = head;
free(temp);
return head;
}
}

// Deleting a node in the middle or tail


do {
if (temp->data == key) {
if (temp == tail) {
tail = tail->prev;
tail->next = head;
head->prev = tail;
} else {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
}
free(temp);
return head;
}
temp = temp->next;
} while (temp != head);

printf("Node with data %d not found.\n", key);


return head;

}
//Function to print list

void print_list(struct node *head){


struct node *temp;
temp = head;
if(temp == NULL){
printf("The list is empty");
}
else{
do {
printf("Data at node : %d\n", temp->data);
temp = temp->next;
} while (temp != head);
}
}
// Main Function

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>

void hanoi(int n, char A, char B, char C) {

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;

// Function to create Nodes


struct node *create_Node() {
struct node *p = (struct node *)malloc(sizeof(struct node));
if (p == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
printf("Enter data for node: ");
scanf("%d", &p->data);
p->next = NULL;
return p;
}

// Function to link nodes


struct node *Link_Nodes(struct node *head, int n) {
if (n <= 0) return NULL;

struct node *p = create_Node();


head = p;

for (int i = 1; i < n; i++) {


struct node *newnode = create_Node();
p->next = newnode;
p = newnode;
}

p->next = head;
return head;
}

// Function to rotate the list by n positions


struct node *rotate_list(struct node *head, int n) {
if (head == NULL || head->next == head || n <= 0) {
return head; // No rotation needed for empty or single-node list or non-positive n
}

struct node *current = head;

// Calculate the length of the list


int length = 1;
while (current->next != head) {
current = current->next;
length++;
}

current = head;
for (int i = 1; i < length - n; i++) {
current = current->next;
}

struct node *new_head = current->next;


current->next = new_head;

return new_head;
}

// Function to print the list


void print_list(struct node *head) {
if (head == NULL) {
printf("The list is empty\n");
return;
}

struct node *temp = head;


do {
printf("Data of Node: %d\n", temp->data);
temp = temp->next;
} while (temp != 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);

head = rotate_list(head, x);


print_list(head);

return 0;
}
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
Enter the number of positions you want to rotate the list by: 2
Data of Node: 4
Data of Node: 5
Data of Node: 1
Data of Node: 2
Data of Node: 3

You might also like