1. Implement the following:
a. Write a program to store the elements in 1-D array and perform the operations like searching, sorting and reversing the elements. [Menu Driven]
#include<stdio.h>
#include<stdlib.h>
int a[20],b[20],c[40];
int m,n,p,val,i,j,key,pos,temp;
/*Function Prototype*/
void create();
void display();
void insert();
void del();
void search();
void merge();
void sort();
int main()
{
int choice;
do{
printf("\n\n--------Menu-----------\n");
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert\n");
printf("4.Delete\n");
printf("5.Search\n");
printf("6.Sort\n");
printf("7.Merge\n");
printf("8.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
del();
break;
case 5:
search();
break;
case 6:
sort();
break;
case 7:
merge();
break;
case 8:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=8);
return 0;
}
void create() //creating an array
{
printf("\nEnter the size of the array elements:\t");
scanf("%d",&n);
printf("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}//end of create()
void display() //displaying an array elements
{
int i;
printf("\nThe array elements are:\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
}//end of display()
void insert() //inserting an element in to an array
{
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&val);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}//end of insert()
void del() //deleting an array element
{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nThe deleted element is =%d",val);
}//end of delete()
void search() //searching an array element
{
printf("\nEnter the element to be searched:\t");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("\nThe element is present at position %d",i);
break;
}
}
if(i==n)
{
printf("\nThe search is unsuccessful");
}
}//end of serach()
void sort() //sorting the array elements
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++) { if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nAfter sorting the array elements are:\n");
display();
}//end of sort
void merge() //merging two arrays
{
printf("\nEnter the size of the second array:\t");
scanf("%d",&m);
printf("\nEnter the elements for the second array:\n");
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
for(i=0,j=0;i<n;i++,j++)
{
c[j]=a[i];
}
for(i=0;i<m;i++,j++)
{
c[j]=b[i];
}
p=n+m;
printf("\nArray elements after merging:\n");
for(i=0;i<p;i++)
{
printf("%d\t",c[i]);
}
}//end of merge()
b. Read the two arrays from the user and merge them and display the elements in sorted order.
/*
* C Program to Merge the Elements of 2 Sorted Array
*/
#include <stdio.h>
void main()
{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
printf("\n Enter size of array Array 1: ");
scanf("%d", &m);
printf("\n Enter sorted elements of array 1: \n");
for (i = 0; i < m; i++)
{
scanf("%d", &array1[i]);
}
printf("\n Enter size of array 2: ");
scanf("%d", &n);
printf("\n Enter sorted elements of array 2: \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array2[i]);
}
i = 0;
j = 0;
while (i < m && j < n)
{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}
else
{
array3[k] = array2[j];
j++;
}
k++;
}
if (i >= m)
{
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
}
if (j >= n)
{
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
}
printf("\n After merging: \n");
for (i = 0; i < m + n; i++)
{
printf("\n%d", array3[i]);
}
}
c. Write a program to perform the Matrix addition, Multiplication and Transpose Operation.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,k,j,c1,c2,r1,r2;
int m1[10][10],m2[10][10],m3[10][10];
clrscr();
while(1)
{
printf("\n 1. Transpose of Matrix:-\n");
printf("\n 2. Addition of Matrix:-\n");
printf("\n 3. Multiplication of Matrix:-\n");
printf("\n 4. Exit\n");
printf("\n Enter your choice:-");
scanf("%d",&a);
switch(a)
{
case 1 :
printf("\n Enter the number of row and coloum:-");
scanf("%d%d",&r1,&c1);
printf("\n Enter the element :-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
m2[j][i]=m1[i][j];
}
}
/*Displaying transpose of matrix*/
printf("\n Transpose of Matrix is:-\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("\t%d",m2[i][j]);
printf("\n");
}
break;
case 2:
printf("\n how many row and coloum in Matrix one:-");
scanf("%d%d",&r1,&c1);
printf("\n How amny row and coloum in Matrix two:-");
scanf("%d%d",&r2,&c2);
if((r1==r2)&&(c1==c2))
{
printf("\n Addition is possible:-");
printf("\n Input Matrix one:-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("\n Input Matrix two:-");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
/* Addition of Matrix*/
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
m3[i][j]=m1[i][j]+ m2[i][j];
}
printf("\n The sum is:-\n");
for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
printf("%5d",m3[i][j]);
printf("\n");
}
}
else
printf("\n Addition is not possible:-");
break;
case 3:
printf("\n Enter number of row and coloum in matrix one:-");
scanf("%d%d",&r1,&c1);
printf("\n Enter number of row and coloum in matrix two:-");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("\n Multiplication is possible:-");
printf("\n Input value of Matrix one:-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("\n Input value of Matrix two:-");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{
m3[i][j]=0;
for(k=0;k<c1;k++)
m3[i][j]=m3[i][j]+m1[i][k]*m2[k][j];
}
/*Displaying final matrix*/
printf("\n Multiplication of Matrix:-\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("\t%d",m3[i][j]);
printf("\n");
}
}
else
printf("\n Multiplication is not possible");
break;
case 4:
exit(0);
break;
}
getch();
}
}
2. Implement the following for Linked List:
a. Write a program to create a single linked list and display the node elements in reverse order.
/*
* C Program to Display the Nodes of a Linked List in Reverse without
* using Recursion
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int visited;
int a;
struct node *next;
};
void generate(struct node **);
void display(struct node *);
void linear(struct node *);
void delete(struct node **);
int main()
{
struct node *head = NULL;
generate(&head);
printf("\nPrinting the list in linear order\n");
linear(head);
printf("\nPrinting the list in reverse order\n");
display(head);
delete(&head);
return 0;
}
void display(struct node *head)
{
struct node *temp = head, *prev = head;
while (temp->visited == 0)
{
while (temp->next != NULL && temp->next->visited == 0)
{
temp = temp->next;
}
printf("%d ", temp->a);
temp->visited = 1;
temp = head;
}
}
void linear(struct node *head)
{
while (head != NULL)
{
printf("%d ", head->a);
head = head->next;
}
printf("\n");
}
void generate(struct node **head)
{
int num, i;
struct node *temp;
printf("Enter length of list: ");
scanf("%d", &num);
for (i = num; i > 0; i--)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
temp->visited = 0;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}
void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
b. Write a program to search the elements in the linked list and display the same.
/*
* C Program to Search for an Element in the Linked List without
* using Recursion
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int a;
struct node *next;
};
void generate(struct node **, int);
void search(struct node *, int);
void delete(struct node **);
int main()
{
struct node *head = NULL;
int key, num;
printf("Enter the number of nodes: ");
scanf("%d", &num);
printf("\nDisplaying the list\n");
generate(&head, num);
printf("\nEnter key to search: ");
scanf("%d", &key);
search(head, key);
delete(&head);
return 0;
}
void generate(struct node **head, int num)
{
int i;
struct node *temp;
for (i = 0; i < num; i++)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = rand() % num;
if (*head == NULL)
{
*head = temp;
temp->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
printf("%d ", temp->a);
}
}
void search(struct node *head, int key)
{
while (head != NULL)
{
if (head->a == key)
{
printf("key found\n");
return;
}
head = head->next;
}
printf("Key not found\n");
}
void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
c. Write a program to create double linked list and sort the elements in the linked list.
#include<stdio.h>
#include<stdlib.h>
struct dllNode {
int data;
struct dllNode *previous, *next;
};
struct dllNode *head = NULL;
static int totNodes = 0;
struct dllNode *createNode(int);
void insertNode(int);
void deleteNode(int);
void insertionSort();
void traverseList();
/* creating a new node */
struct dllNode* createNode(int data) {
struct dllNode *nPtr = (struct dllNode *)malloc(sizeof (struct dllNode));
nPtr->data = data;
nPtr->previous = NULL;
nPtr->next = NULL;
return (nPtr);
}
/* Insert a newnode at the end of the list */
void insertNode(int data) {
struct dllNode *tmp, *nPtr = createNode(data);
if (!head) {
head = nPtr;
totNodes++;
return;
} else {
tmp = head;
while (tmp) {
if (tmp->next == NULL) {
tmp->next = nPtr;
nPtr->previous = tmp;
totNodes++;
return;
}
tmp=tmp->next;
}
}
}
/* delete the node with given data */
void deleteNode(int data) {
struct dllNode *nPtr, *tmp = head;
if (head == NULL) {
printf("Data unavailable\n");
return;
} else if (tmp->data == data) {
nPtr = tmp->next;
tmp->next = NULL;
free(tmp);
head = nPtr;
totNodes--;
} else {
while (tmp->next != NULL && tmp->data != data) {
nPtr = tmp;
tmp = tmp->next;
}
if (tmp->next == NULL && tmp->data != data) {
printf("Given data unvavailable in list\n");
return;
} else if (tmp->next != NULL && tmp->data == data) {
nPtr->next = tmp->next;
tmp->next->previous = tmp->previous;
tmp->next = NULL;
tmp->previous = NULL;
free(tmp);
printf("Data deleted successfully\n");
totNodes--;
} else if (tmp->next == NULL && tmp->data == data) {
nPtr->next = NULL;
tmp->next = tmp->previous = NULL;
free(tmp);
printf("Data deleted successfully\n");
totNodes--;
}
}
}
/* sort the given list - insertNode sort*/
void insertionSort() {
struct dllNode *nPtr1, *nPtr2;
int i, j, tmp;
nPtr1 = nPtr2 = head;
for (i = 0; i < totNodes; i++) {
tmp = nPtr1->data;
for (j = 0; j < i; j++)
nPtr2 = nPtr2->next;
for (j = i; j > 0 && nPtr2->previous->data < tmp; j--) {
nPtr2->data = nPtr2->previous->data;
nPtr2 = nPtr2->previous;
}
nPtr2->data = tmp;
nPtr2 = head;
nPtr1 = nPtr1->next;
}
}
/* traverse the doubly linked list and print data in each node */
void traverseList() {
struct dllNode *nPtr = head;
int i = 0;
while (nPtr) {
printf("%d ", nPtr->data);
nPtr = nPtr->next;
i++;
}
}
int main() {
int ch, data;
while (1) {
printf("1.Insertion\t2.Delete Operation\n");
printf("3.Sort List\t4.Display\t");
printf("5.Exit\nEnter ur choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data to insert:");
scanf("%d", &data);
insertNode(data);
break;
case 2:
printf("Enter data to delete:");
scanf("%d", &data);
deleteNode(data);
break;
case 3:
insertionSort();
break;
case 4:
traverseList();
printf("\n");
break;
case 5:
exit(0);
default:
printf("Wrong Option!!\n");
break;
}
}
}
a. Write a program to store the elements in 1-D array and perform the operations like searching, sorting and reversing the elements. [Menu Driven]
#include<stdio.h>
#include<stdlib.h>
int a[20],b[20],c[40];
int m,n,p,val,i,j,key,pos,temp;
/*Function Prototype*/
void create();
void display();
void insert();
void del();
void search();
void merge();
void sort();
int main()
{
int choice;
do{
printf("\n\n--------Menu-----------\n");
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert\n");
printf("4.Delete\n");
printf("5.Search\n");
printf("6.Sort\n");
printf("7.Merge\n");
printf("8.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
del();
break;
case 5:
search();
break;
case 6:
sort();
break;
case 7:
merge();
break;
case 8:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=8);
return 0;
}
void create() //creating an array
{
printf("\nEnter the size of the array elements:\t");
scanf("%d",&n);
printf("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}//end of create()
void display() //displaying an array elements
{
int i;
printf("\nThe array elements are:\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
}//end of display()
void insert() //inserting an element in to an array
{
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&val);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}//end of insert()
void del() //deleting an array element
{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nThe deleted element is =%d",val);
}//end of delete()
void search() //searching an array element
{
printf("\nEnter the element to be searched:\t");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("\nThe element is present at position %d",i);
break;
}
}
if(i==n)
{
printf("\nThe search is unsuccessful");
}
}//end of serach()
void sort() //sorting the array elements
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++) { if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nAfter sorting the array elements are:\n");
display();
}//end of sort
void merge() //merging two arrays
{
printf("\nEnter the size of the second array:\t");
scanf("%d",&m);
printf("\nEnter the elements for the second array:\n");
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
for(i=0,j=0;i<n;i++,j++)
{
c[j]=a[i];
}
for(i=0;i<m;i++,j++)
{
c[j]=b[i];
}
p=n+m;
printf("\nArray elements after merging:\n");
for(i=0;i<p;i++)
{
printf("%d\t",c[i]);
}
}//end of merge()
b. Read the two arrays from the user and merge them and display the elements in sorted order.
/*
* C Program to Merge the Elements of 2 Sorted Array
*/
#include <stdio.h>
void main()
{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
printf("\n Enter size of array Array 1: ");
scanf("%d", &m);
printf("\n Enter sorted elements of array 1: \n");
for (i = 0; i < m; i++)
{
scanf("%d", &array1[i]);
}
printf("\n Enter size of array 2: ");
scanf("%d", &n);
printf("\n Enter sorted elements of array 2: \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array2[i]);
}
i = 0;
j = 0;
while (i < m && j < n)
{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}
else
{
array3[k] = array2[j];
j++;
}
k++;
}
if (i >= m)
{
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
}
if (j >= n)
{
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
}
printf("\n After merging: \n");
for (i = 0; i < m + n; i++)
{
printf("\n%d", array3[i]);
}
}
c. Write a program to perform the Matrix addition, Multiplication and Transpose Operation.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,k,j,c1,c2,r1,r2;
int m1[10][10],m2[10][10],m3[10][10];
clrscr();
while(1)
{
printf("\n 1. Transpose of Matrix:-\n");
printf("\n 2. Addition of Matrix:-\n");
printf("\n 3. Multiplication of Matrix:-\n");
printf("\n 4. Exit\n");
printf("\n Enter your choice:-");
scanf("%d",&a);
switch(a)
{
case 1 :
printf("\n Enter the number of row and coloum:-");
scanf("%d%d",&r1,&c1);
printf("\n Enter the element :-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
m2[j][i]=m1[i][j];
}
}
/*Displaying transpose of matrix*/
printf("\n Transpose of Matrix is:-\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("\t%d",m2[i][j]);
printf("\n");
}
break;
case 2:
printf("\n how many row and coloum in Matrix one:-");
scanf("%d%d",&r1,&c1);
printf("\n How amny row and coloum in Matrix two:-");
scanf("%d%d",&r2,&c2);
if((r1==r2)&&(c1==c2))
{
printf("\n Addition is possible:-");
printf("\n Input Matrix one:-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("\n Input Matrix two:-");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
/* Addition of Matrix*/
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
m3[i][j]=m1[i][j]+ m2[i][j];
}
printf("\n The sum is:-\n");
for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
printf("%5d",m3[i][j]);
printf("\n");
}
}
else
printf("\n Addition is not possible:-");
break;
case 3:
printf("\n Enter number of row and coloum in matrix one:-");
scanf("%d%d",&r1,&c1);
printf("\n Enter number of row and coloum in matrix two:-");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("\n Multiplication is possible:-");
printf("\n Input value of Matrix one:-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("\n Input value of Matrix two:-");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{
m3[i][j]=0;
for(k=0;k<c1;k++)
m3[i][j]=m3[i][j]+m1[i][k]*m2[k][j];
}
/*Displaying final matrix*/
printf("\n Multiplication of Matrix:-\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("\t%d",m3[i][j]);
printf("\n");
}
}
else
printf("\n Multiplication is not possible");
break;
case 4:
exit(0);
break;
}
getch();
}
}
2. Implement the following for Linked List:
a. Write a program to create a single linked list and display the node elements in reverse order.
/*
* C Program to Display the Nodes of a Linked List in Reverse without
* using Recursion
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int visited;
int a;
struct node *next;
};
void generate(struct node **);
void display(struct node *);
void linear(struct node *);
void delete(struct node **);
int main()
{
struct node *head = NULL;
generate(&head);
printf("\nPrinting the list in linear order\n");
linear(head);
printf("\nPrinting the list in reverse order\n");
display(head);
delete(&head);
return 0;
}
void display(struct node *head)
{
struct node *temp = head, *prev = head;
while (temp->visited == 0)
{
while (temp->next != NULL && temp->next->visited == 0)
{
temp = temp->next;
}
printf("%d ", temp->a);
temp->visited = 1;
temp = head;
}
}
void linear(struct node *head)
{
while (head != NULL)
{
printf("%d ", head->a);
head = head->next;
}
printf("\n");
}
void generate(struct node **head)
{
int num, i;
struct node *temp;
printf("Enter length of list: ");
scanf("%d", &num);
for (i = num; i > 0; i--)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
temp->visited = 0;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}
void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
b. Write a program to search the elements in the linked list and display the same.
/*
* C Program to Search for an Element in the Linked List without
* using Recursion
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int a;
struct node *next;
};
void generate(struct node **, int);
void search(struct node *, int);
void delete(struct node **);
int main()
{
struct node *head = NULL;
int key, num;
printf("Enter the number of nodes: ");
scanf("%d", &num);
printf("\nDisplaying the list\n");
generate(&head, num);
printf("\nEnter key to search: ");
scanf("%d", &key);
search(head, key);
delete(&head);
return 0;
}
void generate(struct node **head, int num)
{
int i;
struct node *temp;
for (i = 0; i < num; i++)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = rand() % num;
if (*head == NULL)
{
*head = temp;
temp->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
printf("%d ", temp->a);
}
}
void search(struct node *head, int key)
{
while (head != NULL)
{
if (head->a == key)
{
printf("key found\n");
return;
}
head = head->next;
}
printf("Key not found\n");
}
void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
c. Write a program to create double linked list and sort the elements in the linked list.
#include<stdio.h>
#include<stdlib.h>
struct dllNode {
int data;
struct dllNode *previous, *next;
};
struct dllNode *head = NULL;
static int totNodes = 0;
struct dllNode *createNode(int);
void insertNode(int);
void deleteNode(int);
void insertionSort();
void traverseList();
/* creating a new node */
struct dllNode* createNode(int data) {
struct dllNode *nPtr = (struct dllNode *)malloc(sizeof (struct dllNode));
nPtr->data = data;
nPtr->previous = NULL;
nPtr->next = NULL;
return (nPtr);
}
/* Insert a newnode at the end of the list */
void insertNode(int data) {
struct dllNode *tmp, *nPtr = createNode(data);
if (!head) {
head = nPtr;
totNodes++;
return;
} else {
tmp = head;
while (tmp) {
if (tmp->next == NULL) {
tmp->next = nPtr;
nPtr->previous = tmp;
totNodes++;
return;
}
tmp=tmp->next;
}
}
}
/* delete the node with given data */
void deleteNode(int data) {
struct dllNode *nPtr, *tmp = head;
if (head == NULL) {
printf("Data unavailable\n");
return;
} else if (tmp->data == data) {
nPtr = tmp->next;
tmp->next = NULL;
free(tmp);
head = nPtr;
totNodes--;
} else {
while (tmp->next != NULL && tmp->data != data) {
nPtr = tmp;
tmp = tmp->next;
}
if (tmp->next == NULL && tmp->data != data) {
printf("Given data unvavailable in list\n");
return;
} else if (tmp->next != NULL && tmp->data == data) {
nPtr->next = tmp->next;
tmp->next->previous = tmp->previous;
tmp->next = NULL;
tmp->previous = NULL;
free(tmp);
printf("Data deleted successfully\n");
totNodes--;
} else if (tmp->next == NULL && tmp->data == data) {
nPtr->next = NULL;
tmp->next = tmp->previous = NULL;
free(tmp);
printf("Data deleted successfully\n");
totNodes--;
}
}
}
/* sort the given list - insertNode sort*/
void insertionSort() {
struct dllNode *nPtr1, *nPtr2;
int i, j, tmp;
nPtr1 = nPtr2 = head;
for (i = 0; i < totNodes; i++) {
tmp = nPtr1->data;
for (j = 0; j < i; j++)
nPtr2 = nPtr2->next;
for (j = i; j > 0 && nPtr2->previous->data < tmp; j--) {
nPtr2->data = nPtr2->previous->data;
nPtr2 = nPtr2->previous;
}
nPtr2->data = tmp;
nPtr2 = head;
nPtr1 = nPtr1->next;
}
}
/* traverse the doubly linked list and print data in each node */
void traverseList() {
struct dllNode *nPtr = head;
int i = 0;
while (nPtr) {
printf("%d ", nPtr->data);
nPtr = nPtr->next;
i++;
}
}
int main() {
int ch, data;
while (1) {
printf("1.Insertion\t2.Delete Operation\n");
printf("3.Sort List\t4.Display\t");
printf("5.Exit\nEnter ur choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data to insert:");
scanf("%d", &data);
insertNode(data);
break;
case 2:
printf("Enter data to delete:");
scanf("%d", &data);
deleteNode(data);
break;
case 3:
insertionSort();
break;
case 4:
traverseList();
printf("\n");
break;
case 5:
exit(0);
default:
printf("Wrong Option!!\n");
break;
}
}
}