Data Stucture - Lab
Data Stucture - Lab
Lab Manual
Semester:3rd
(2023-24)
Submitted By:
Ms. Reshu Tyagi
2. WAP is C you will get an input from the user and check whether number is palindrome 3
or not
3. WAP to calculate the average of first n numbers in C 4
INDEX
Department of CSE-Data Science /IT
Q1. WAP to print factorial of a number using recursion.
#include<stdio.h>
#include<conio.h>
long int fact (int n);
void main()
{
int n,i;
long int f;
clrscr();
printf("Enter the number");
scanf("%d",&n);
f=fact(n);
printf("The factorial is %ld",f);
getch();
}
long int fact (int n)
{
long int f=1;
int i;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
Department of CSE-Data Science /IT
Q2. WAP is C you will get an input from the user and check whether number is
palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,x;
clrscr();
printf(“enter the number”);
scanf(“%d”,&n);
x=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(x==sum) {
printf(“palindrome number”); }
else{
printf(“not palindrome”); }
getch();
}
Department of CSE-Data Science /IT
scanf("%d", &choice);
switch(choice)
{
case 1:
Department of CSE-Data Science /IT
printf("Enter the total number of nodes in list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
printf("Enter data of first node : ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 3:
printf("Enter data of last node : ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 4:
printf("Enter the position where you want to insert new node: ");
scanf("%d", &n);
printf("Enter data of %d node : ", n);
scanf("%d", &data);
insertAtN(data, n);
break;
case 5:
displayList();
break;
case 0:
break;
default:
printf("Error! Invalid choice. Please choose between 0-5");
}
printf("\n\n\n\n\n");
}
return 0;
Department of CSE-Data Science /IT
}
void createList(int n)
{
int i, data;
struct node *newNode;
if(n >= 1)
{
head = (struct node *)malloc(sizeof(struct node));
printf("Enter data of 1 node: ");
scanf("%d", &data);
head->data = data;
head->prev = NULL;
head->next = NULL;
last = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->prev = last; // Link new node with the previous node
newNode->next = NULL;
last->next = newNode; // Link previous node with the new node
last = newNode; // Make new node as last/previous node
}
printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void displayList()
{
struct node * temp;
int n = 1;
Department of CSE-Data Science /IT
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
temp = head;
printf("DATA IN THE LIST:\n");
while(temp != NULL)
{
printf("DATA of %d node = %d\n", n, temp->data);
n++;
temp = temp->next;
}
}
}
void insertAtBeginning(int data)
{
struct node * newNode;
if(head == NULL)
{
printf("Error, List is Empty!\n");
}
else
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = head; // Point to next node which is currently head
newNode->prev = NULL; // Previous node of first node is NULL
head->prev = newNode;
head = newNode;
printf("NODE INSERTED SUCCESSFULLY AT THE BEGINNING OF THE LIST\n");
Department of CSE-Data Science /IT
}
}
void insertAtEnd(int data)
{
struct node * newNode;
if(last == NULL)
{
printf("Error, List is empty!\n");
}
else
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = last;
last->next = newNode;
last = newNode;
printf("NODE INSERTED SUCCESSFULLY AT THE END OF LIST\n");
}
}
void insertAtN(int data, int position)
{
int i;
struct node * newNode, *temp;
if(head == NULL)
{
printf("Error, List is empty!\n");
}
else
{
temp = head;
i=1;
Department of CSE-Data Science /IT
while(i<position-1 && temp!=NULL)
{
temp = temp->next;
i++;
}
if(position == 1)
{
insertAtBeginning(data);
}
else if(temp == last)
{
insertAtEnd(data);
}
else if(temp!=NULL)
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = temp->next; // Connect new node with n+1th node
newNode->prev = temp; // Connect new node with n-1th node
if(temp->next != NULL)
{
temp->next->prev = newNode;
}
temp->next = newNode;
printf("NODE INSERTED SUCCESSFULLY AT %d POSITION\n", position);
}
else
{
printf("Error, Invalid position\n");
}
}
}
Department of CSE-Data Science /IT
Output
============================================
DOUBLY LINKED LIST PROGRAM
============================================
1. Create List
2. Insert node - at beginning
3. Insert node - at end
4. Insert node - at N
5. Display list
0. Exit
--------------------------------------------
Enter your choice : 4
Enter the position where you want to insert new node: 3
Enter data of 3 node : 15
NODE INSERTED SUCCESSFULLY AT 3 POSITION
if(n >= 1)
{
Department of CSE-Data Science /IT
head = (struct node *)malloc(sizeof(struct node));
printf("Enter data of 1 node: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
prevNode = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
prevNode->next = newNode;
prevNode = newNode;
}
prevNode->next = head;
printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void displayList()
{
struct node *current;
int n = 1;
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
current = head;
printf("DATA IN THE LIST:\n");
Department of CSE-Data Science /IT
do {
printf("Data %d = %d\n", n, current->data);
current = current->next;
n++;
}while(current != head);
}
}
void insertAtBeginning(int data)
{
struct node *newNode, *current;
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = head;
current = head;
while(current->next != head)
{
current = current->next;
}
current->next = newNode;
head = newNode;
printf("NODE INSERTED SUCCESSFULLY\n");
}
}
============================================
CIRCULAR LINKED LIST PROGRAM
============================================
1. Create List
2. Display list
3. Insert at beginning
4. Insert at any position
0. Exit
--------------------------------------------
Enter your choice : 3
Department of CSE-Data Science /IT
Enter data to be inserted at beginning: 10
NODE INSERTED SUCCESSFULLY
============================================
CIRCULAR LINKED LIST PROGRAM
============================================
1. Create List
2. Display list
3. Insert at beginning
4. Insert at any position
0. Exit
--------------------------------------------
Enter your choice : 4
Enter node position: 3
Enter data you want to insert at 3 position: 30
NODE INSERTED SUCCESSFULLY.
if (last == NULL) {
Department of CSE-Data Science /IT
temp->info = data;
temp->next = temp;
last = temp;
}
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
void deleteAtIndex()
{
int pos, i = 1;
struct node *temp, *position;
temp = last->next;
if (last == NULL)
printf("\nList is empty.\n");
else {
printf("\nEnter index : ");
scanf("%d", &pos);
while (i <= pos - 1) {
temp = temp->next;
i++;
}
position = temp->next;
temp->next = position->next;
free(position);
}
}
void viewList()
Department of CSE-Data Science /IT
{
if (last == NULL)
printf("\nList is empty\n");
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
int main()
{
addatlast();
addatlast();
addatlast();
deleteAtIndex();
viewList();
return 0;
}