0% found this document useful (0 votes)
3 views

Laboratory Manual Report

Uploaded by

c19-1633-217
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Laboratory Manual Report

Uploaded by

c19-1633-217
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 149

University of Perpetual Help System -

Laguna
College of Computer Studies

Data Structure

Laboratory Manual Report

By
Flores, Jason Lawrence M.
19-1633-217
BSCS-J2S
Table of Contents

Machine Problem / Hands-on / Project

Array…………………………………………………………………………………… 3 – 17

Single Linked List…………………………………………………………………. 18 - 54

Double Linked List……………………………………………………………….. 55 – 100

Circular Linked List………………………………………………………………..101 – 122

Stack……………………………………………………………………………………..123 – 129

Queue…………………………………………………………………………………..130 – 140

Tree……………………………………………………………………………………..142 – 147

Tree Traversals……………………………………………………………………..148 - 149


ARRAY

Hands – on #1

Hands – on #2
Hands – on #3
Hands – on #5 – Array Initialization

Hands – on #6 – Storing Values to Array


Hands – on #7 – Reading the values stored to array

Hands-on #8 – Computing value using Array


Hands – on #9 – Character String in Array
Hands – on #1

Hands – on #2
Hands – on #3
Hands – on #5 – Array Initialization

Hands – on #6 – Storing Values to Array


Hands – on #7 – Reading the values stored to array

Hands-on #8 – Computing value using Array


Hands – on #9 – Character String in Array
Hands-on #10 – string manipulation

a. strlen()

b. strcmp()
Machine Problem #1
(Write an array program to input a costumer lastname, First name, mi, extension name
(if applicable) and display it on one single line.

#include<stdio.h>
#include<conio.h> int main (void)
{
char last[20], first[20], middle[20]; puts("Your last name:");
gets(last);
puts("\nYour first name:"); gets(first);
puts("\nYour middle name:"); gets(middle);
printf("\n %s %s %s", last, first, middle); return 0;
}

Machine Problem#1 – Array Implementation


A. Write a program that fills an array A of size 5 filled with random integers in the range 1 to
100 and prints it out (for testing purposes). Write programs that then after creating such a
random array, do each of the following:
(a) Calculate the sum of the elements of A and print it out
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
const int size = 5; int array[size];
srand((unsigned)time(0));
for(int i=0; i<size; i++){ array[i] = (rand()%50)+1;
cout << array[i] << endl;
}
int sum;
{
for (int b = 0; b<size; b++)
sum = sum + array[b];
}
sum = sum -1; cout<<”sum is:”<<sum;
return 0;
}

(a) Count the number of elements in A whose contents are less than 10.
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std; int
main()
{
const int size = 20; int
array[size];
int carrier; srand((unsigned)time(0));
for(int i=0; i<size; i++){
array[i] = (rand()%20)+1; //needs to work with srand((unsigned)time(0)); cout
<< array[i] << endl;
if(array[i]<10)
{
carrier = carrier + 1;
}
}
cout<<”The total elements that are below 10 is “<<carrier<<”\n”; int
sum;
{
for (int a = 0; a<size; a++)
sum = sum + array[a];
}
cout<<”sum is: “<<sum<<”\n”;
{
}
return 0;
}
SINGLE LINKED LIST
DOUBLE LINKED LIST

Hands – on 11. Write a program in C to create and display a doubly linked


list. Test Data :
Input the number of
nodes : 3 Input data for
node 1 : 2 Input data for
node 2 : 5 Input data for
node 3 : 8 Expected
Output :
Data entered on the list are :
node 1 : 2
node 2 : 5
node 3 : 8

#include <stdio.h>
#include <stdlib.h>

struct node {
int num;
struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;

void
DlListcreation(int n);
void displayDlList();

int main()
{
int n;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Create and display a doubly linked list :\
n”); printf(“-------------------------------------------------------------------\n”);

printf(“ Input the number of nodes : “);


scanf(“%d”, &n);

DlListcreation(n);
displayDlList();
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);

stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
// putting data for rest of the nodes
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous node
fnNode->nextptr = NULL;

ennode->nextptr = fnNode; // previous node is linking with the new node


ennode = fnNode; // assign new node as last node
}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{
printf(“ Memory can not be allocated.”);
}
}
}
void displayDlList()
{
struct node *
tmp; int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
printf(“\n\n Data entered on the list are :\n”);

while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp-
>num); n++;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}
}
Hands – on 12. Write a program in C to create a doubly linked list and display in
reverse order.

Test Data :

Input the number of nodes : 3


Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Expected Output :

Data in reverse order are :


Data in node 1 : 8
Data in node 2 : 5
Data in node 3 : 2
#include <stdio.h>

#include <stdlib.h>

struct node {
int num;

struct node * preptr;


struct node * nextptr;

}*stnode, *ennode;

void DlListcreation(int n);


void displayDlListRev();

int main()

int n;

stnode = NULL;
ennode = NULL;

printf(“\n\n Doubly Linked List : Create and display a doubly linked list in reverse order :\n”);
printf(“------------------------------------------------------------------------------------\n”);

printf(“ Input the number of nodes : “);


scanf(“%d”, &n);

DlListcreation(n);
displayDlListRev();
return 0;

void DlListcreation(int n)

int i, num;

struct node *fnNode;

if(n >= 1)

{
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);

stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
// putting data for rest of the nodes
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous node
fnNode->nextptr = NULL;

ennode->nextptr = fnNode; // previous node is linking with the new node


ennode = fnNode; // assign new node as last node
}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{
printf(“ Memory can not be allocated.”);
}
}
}

void displayDlListRev()
{
struct node * tmp;
int n = 0;

if(ennode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = ennode;
printf(“\n Data in reverse order are :\n”);
while(tmp != NULL)
{
printf(“ Data in node %d : %d\n”, n+1, tmp->num);
n++;
tmp = tmp->preptr; // current pointer set with previous node
}
}
}
Hands – on 13. Write a program in C to insert a new node at the beginning in a
doubly linked list.
Test Data and Expected Output :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Data entered in the list are :
node 1 : 2
node 2 : 5
node 3 : 8
Input data for the first node : 1
After insertion the new list are :
node 1 : 1
node 2 : 2
node 3 : 5
node 4 : 8
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;
void DlListcreation(int n);
void DlLinsertNodeAtBeginning(int num);
void displayDlList(int a);

int main()
{
int n,num1,a;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Insert new node at the beginning in a doubly linked list
:\n”);
printf(“------------------------------------------------------------------------------------\n”);
printf(“ Input the number of nodes
: “); scanf(“%d”, &n);
DlListcreation(n);
a=1;
displayDlList(a);
printf(“ Input data for the first node : “);
scanf(“%d”, &num1);
DlLinsertNodeAtBeginning(num1);
a=2;
displayDlList(a);
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);

stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous node
fnNode->nextptr = NULL;

ennode->nextptr = fnNode; // previous node is linking with the new node


ennode = fnNode; // assign new node as last node
}
else

DlListcreation(n);
a=1;
displayDlList(a);
printf(“ Input data for the first node : “);
scanf(“%d”, &num1);
DlLinsertNodeAtBeginning(num1);
a=2;
displayDlList(a);
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);

stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous node
fnNode->nextptr = NULL;

ennode->nextptr = fnNode; // previous node is linking with the new node


ennode = fnNode; // assign new node as last node
}
else

printf(“\n Data entered in the list are :\n”);


}
else
{
printf(“\n After insertion the new list are :\n”);
}
while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp-
>num); n++;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}
}

Hands – on 14. Write a program in C to insert a new node at the end of a doubly
linked list.
Test Data and Expected Output :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Data entered in the list are :
node 1 : 2
node 2 : 5
node 3 : 8
Input data for the last node : 9
After insertion the new list are :
node 1 : 2
node 2 : 5
node 3 : 8
node 4 : 9

#include <stdio.h>
#include <stdlib.h>

struct node {
int num;
struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;

void DlListcreation(int n);


void DlLinsertNodeAtEnd(int num);
void displayDlList(int a);

int main()
{
int n,num1,a;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Insert new node at the end in a doubly linked list :\n”);
printf(“------------------------------------------------------------------------------\n”);
printf(“ Input the number of nodes : “);
scanf(“%d”, &n);
DlListcreation(n);
a=1;
displayDlList(a);
printf(“ Input data for the last node : “);
scanf(“%d”, &num1);
DlLinsertNodeAtEnd(num1);
a=2;
displayDlList(a);
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);

stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous node
fnNode->nextptr = NULL;
ennode->nextptr = fnNode; // previous node is linking with the new node
ennode = fnNode; // assign new node as last node
}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{
printf(“ Memory can not be allocated.”);
}
}
}
void DlLinsertNodeAtEnd(int num)
{
struct node * newnode;

if(ennode == NULL)
{
printf(“ No data found in the list!\n”);
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
nodenewnode->nextptr = NULL; // set next address field of new node is NULL
newnode->preptr = ennode; // previous address of new node is linking with ending

} ennode->nextptr = newnode; // next address of ending node is linking with new node
ennode = newnode; // set the new node as ending node

}
void displayDlList(int m)
{
struct node *
tmp; int n =
1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp =
stnode;
if
(m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After insertion the new list are :\n”);
}
while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp-
>num); n++;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}
}
Hands – on 15. Write a program in C to insert a new node at any position in a
doubly linked list.
Test Data and Expected Output :
Input the number of nodes (3 or more ): 3
Input data for node 1 : 2
Input data for node 2 : 4
Input data for node 3 : 5
Data entered in the list are :
node 1 : 2
node 2 : 4
node 3 : 5
Input the position ( 2 to 2 ) to insert a new node : 2
Input data for the position 2 : 3
After insertion the new list are :
node 1 : 2
node 2 : 3
node 3 : 4
node 4 : 5

#include <stdio.h>
#include <stdlib.h>

struct node {
int num;
struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;

void DlListcreation(int n);


void DlLinsertNodeAtBeginning(int num);
void DlLinsertNodeAtEnd(int num);
void DlLinsertNodeAtAny(int num, int pos);
void displayDlList(int a);

int main()
{
int n,num1,a,insPlc;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Insert new node at any position in a doubly linked list
:\n”);
printf(“-----------------------------------------------------------------------------------\n”);
printf(“ Input the number of nodes : “);
scanf(“%d”, &n);
DlListcreation(n);
a=1;
displayDlList(a);
printf(“ Input the position ( 1 to %d ) to insert a new node : “,n+1);
scanf(“%d”, &insPlc);
printf(“ Input data for the position %d : “, insPlc);
scanf(“%d”, &num1);
DlLinsertNodeAtAny(num1,insPlc);
a=2;
displayDlList(a);
return 0;
}
void DlListcreation(int n)
{
int i, num;
struct node *fnNode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);
stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous node
fnNode->nextptr = NULL; // set next address of fnnode is NULL
ennode->nextptr = fnNode; // previous node is linking with the new node

ennode = fnNode; // assign new node as last node


}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{
printf(“ Memory can not be allocated.”);
}
}
}

void DlLinsertNodeAtAny(int num, int pos)


{
int i;
struct node * newnode, *tmp;
if(ennode == NULL)
{
printf(“ No data found in the list!\n”);
}
else
{
tmp = stnode;
i=1;
while(i<pos-1 && tmp!=NULL)
{
tmp = tmp->nextptr;
i++;
}
if(pos == 1)
{
DlLinsertNodeAtBeginning(num);
}
else if(tmp == ennode)
{
DlLinsertNodeAtEnd(num);
}
else if(tmp!=NULL)
{
newnode = (struct node *)malloc(sizeof(struct node));

newnode->num = num;
//next address of new node is linking with the next address of temp
node newnode->nextptr = tmp->nextptr;
// previous address of new node is linking with the tmp
node newnode->preptr = tmp;
if(tmp->nextptr != NULL)
{
tmp->nextptr->preptr = newnode; // n+1th node is linking with new node
}
tmp->nextptr = newnode; // n-1th node is linking with new node
}
else
{
printf(“ The position you entered, is invalid.\n”);
}
}
}
void DlLinsertNodeAtBeginning(int num)
{
struct node * newnode;
if(stnode == NULL)
{
printf(“ No data found in the list!\n”);
}
else
{
newnode = (struct node *)malloc(sizeof(struct
node)); newnode->num = num;
newnode->nextptr = stnode; // next address of new node is linking with starting
node newnode->preptr = NULL; // set previous address field of new node is
NULL
stnode->preptr = newnode; // previous address of starting node is linking with new
node stnode = newnode; // set the new node as starting node
}
}

void DlLinsertNodeAtEnd(int num)


{
struct node * newnode;

if(ennode == NULL)
{
printf(“ No data found in the list!\n”);
}
else
{

node
}
newnode = (struct node
*)malloc(sizeof(struct node)); newnode-
>num = num;
newnode->nextptr = NULL; // set next
address field of new node is NULL
newnode->preptr = ennode; // previous
address of new node is linking with ending
ennode->nextptr = newnode; // next
address of ending node is linking with new
node ennode = newnode; // set the new
node as ending node
}

void displayDlList(int m)
{
struct node * tmp; int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode; if (m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After insertion the new list are :\n”);
}
while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp->num); n+
+;
tmp = tmp->nextptr; // current pointer moves
to the next node
}
}
}
Hands – on 16. Write a program in C to insert a new node at the middle in a
doubly linked list.
Test Data and Expected Output :
Doubly Linked List : Insert new node at the middle in a doubly linked list :
Input the number of nodes (3 or more ): 3
Input data for node 1 : 2
Input data for node 2 : 4
Input data for node 3 : 5
Data entered in the list
are : node 1 : 2
node 2 : 4
node 3 : 5
Input the position ( 2 to 2 ) to insert a new node : 2
Input data for the position 2 : 3
After insertion the new list are :
node 1 : 2
node 2 : 3
node 3 : 4
node 4 : 5
#include <stdio.h>
#include <stdlib.h>
struct
node {
int
num;
struct node *
preptr; struct
node * nextptr;
}*stnode, *ennode;
void DlListcreation(int n);
void DlLinsertNodeAtMiddle(int num, int pos);
void displayDlList(int a);
int main()
{
int n,num1,a,insPlc;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Insert new node at the middle in a doubly linked list :\
n”);

printf(“----------------------------------------------------------------------------------\n”);
printf(“ Input the number of nodes (3 or more ): “);
scanf(“%d”, &n);
DlListcreation(n);
a=1;
displayDlList(a);
printf(“ Input the position ( 2 to %d ) to insert a new node : “,n-1);
scanf(“%d”, &insPlc);

if(insPlc<=1 || insPlc>=n)
{
printf(“\n Invalid position. Try again.\n “);
}
if(insPlc>1 && insPlc<n)
{
printf(“ Input data for the position %d : “, insPlc);
scanf(“%d”, &num1);
DlLinsertNodeAtMiddle(num1,insPlc);
a=2;
displayDlList(a);
}
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);

stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous node
fnNode->nextptr = NULL; // set next address of fnnode is NULL
ennode->nextptr = fnNode; // previous node is linking with the new node
ennode = fnNode; // assign new node as last node
}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{
printf(“ Memory can not be allocated.”);
}
}
}

void DlLinsertNodeAtMiddle(int num, int pos)


{
int i;
struct node * newnode, *tmp;
if(ennode == NULL)
{
printf(“ No data found in the list!\n”);
}
else
{
tmp = stnode;
i=1;
while(i<pos-1 && tmp!=NULL)
{
tmp = tmp->nextptr;
i++;
}
if(tmp!=NULL)

{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
//next address of new node is linking with the next address of temp node
newnode->nextptr = tmp->nextptr;
// previous address of new node is linking with the tmp node
newnode->preptr = tmp;
if(tmp->nextptr != NULL)
{
tmp->nextptr->preptr = newnode; // n+1th node is linking with new node
}
tmp->nextptr = newnode; // n-1th node is linking with new node
}
else
{
printf(“ The position you entered, is invalid.\n”);
}
}
}

void displayDlList(int m)
{
struct node * tmp;
int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
if (m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After insertion the new list are :\n”);
}
while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp->num); n+
+;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}
}
Hands – on 17. Write a program in C to delete a node from the beginning of a
doubly linked list.
Test Data and Expected Output :
Input the number of nodes (3 or more ): 3
Input data for node 1 : 1
Input data for node 2 : 2
Input data for node 3 : 3
Data entered in the list are :
node 1 : 1
node 2 : 2
node 3 : 3
After deletion the new list are :
node 1 : 2
node 2 : 3
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;
void DlListcreation(int n);
void DlListDeleteFirstNode();
void displayDlList(int a);

int main()
{
int n,num1,a,insPlc;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Delete node from the beginning of a doubly linked list
:\n”);
printf(“----------------------------------------------------------------------------------\n”);

printf(“ Input the number of nodes (3 or more ):


“); scanf(“%d”, &n);
DlListcreatio
n(n); a=1;
displayDlList(a);
DlListDeleteFirstNode();
a=2;
displayDlList(a);
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);
stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr =
NULL; ennode =
stnode; for(i=2; i<=n; i+
+)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous
node fnNode->nextptr = NULL; // set next address of
fnnode is NULL ennode->nextptr = fnNode; // previous node is linking
with the new node ennode = fnNode; // assign new node as last node
}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{

printf(“ Memory can not be allocated.”);


}
}
}
void DlListDeleteFirstNode()
{
struct node * NodeToDel;
if(stnode == NULL)
{
printf(“ Delete is not possible. No data in the list.\n”);
}
else
{
NodeToDel = stnode;
stnode = stnode->nextptr; // move the next address of starting node to 2
node stnode->preptr = NULL; // set previous address of staring node
is NULL free(NodeToDel); // delete the first node from memory
}
}

void displayDlList(int m)
{
struct node * tmp;
int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp =
stnode; if
(m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After deletion the new list are :\n”);
}
while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp->num);
n++;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}
}
Hands – on 18. Write a program in C to delete a node from the last of a doubly
linked list
Test Data and Expected Output :
Input the number of nodes (3 or more ): 3
Input data for node 1 : 1
Input data for node 2 : 2
Input data for node 3 : 3
Data entered in the list are
: node 1 : 1
node 2 : 2
node 3 : 3
After deletion the new list are :
node 1 : 1
node 2 : 2

#include <stdio.h>
#include <stdlib.h>

struct node {
int num;
struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;

void DlListcreation(int n);


void
DlListDeleteLastNode();
void displayDlList(int a);

int main()
{
int n,num1,a,insPlc;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Delete node from the last of a doubly linked list :\
n”); printf(“-----------------------------------------------------------------------------\n”);
printf(“ Input the number of nodes (3 or more ): “);
scanf(“%d”, &n);
DlListcreation(n);
a=1;
displayDlList(a); DlListDeleteLastNode();
a=2;
displayDlList(a);
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);
stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr =
NULL; ennode =
stnode; for(i=2; i<=n; i+
+)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous
node fnNode->nextptr = NULL; // set next address of
fnnode is NULL ennode->nextptr = fnNode; // previous node is linking
with the new node ennode = fnNode; // assign new node as last node
}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{
printf(“ Memory can not be allocated.”);

}
}
}

void DlListDeleteLastNode()
{
struct node * NodeToDel;

if(ennode == NULL)
{
printf(“ Delete is not possible. No data in the list.\n”);
}
else
{
NodeToDel = ennode;
ennode = ennode->preptr; // move the previous address of the last node to 2nd last
node ennode->nextptr = NULL; // set the next address of last node to NULL
free(NodeToDel); // delete the last node
}
}
void displayDlList(int m)
{
struct node *
tmp; int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp =
stnode; if
(m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After deletion the new list are :\n”);
}
while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp-
>num); n++;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}
}
Hands – on 19. Write a program in C to delete a node from any position of a
doubly linked list.
Test Data and Expected Output :
Doubly Linked List : Delete node from any position of a doubly linked list :
Input the number of nodes (3 or more ): 3
Input data for node 1 : 1
Input data for node 2 : 2
Input data for node 3 : 3
Data entered in the list
are : node 1 : 1
node 2 : 2
node 3 : 3
Input the position ( 1 to 3 ) to delete a
node : 3 After deletion the new list are :
node 1 : 1
node 2 : 2
#include <stdio.h>
#include <stdlib.h>

struct
node {
int
num;
struct node *
preptr; struct
node * nextptr;
}*stnode, *ennode;
void DlListcreation(int
n); void
DlListDeleteFirstNode();
void
DlListDeleteLastNode();
void DlListDeleteAnyNode(int
pos); void displayDlList(int a);

int main()
{
int
n,num1,a,insPlc;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Delete node from any position of a doubly linked list
:\n”); printf(“----------------------------------------------------------------------------------\n”);
printf(“ Input the number of nodes (3 or more ): “);

scanf(“%d”, &n);
DlListcreation(n);
a=1;
displayDlList(a);
printf(“ Input the position ( 1 to %d ) to delete a node :
“,n); scanf(“%d”, &insPlc);

if(insPlc<1 ||
insPlc>n)
{
printf(“\n Invalid position. Try again.\n “);
}
if(insPlc>=1 && insPlc<=n)
{

DlListDeleteAnyNode(insPlc)
; a=2;
displayDlList(a);
}
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);
stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr =
NULL; ennode =
stnode; for(i=2; i<=n; i+
+)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous
node fnNode->nextptr = NULL; // set next address of
fnnode is NULL ennode->nextptr = fnNode; // previous node is linking
with the new node ennode = fnNode; // assign new node as last node
}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{
printf(“ Memory can not be allocated.”);
}
}
}

void DlListDeleteAnyNode(int pos)


{
struct node
*curNode; int i;

curNode = stnode;
for(i=1; i<pos && curNode!=NULL; i++)
{
curNode = curNode->nextptr;
}

if(pos == 1)
{
DlListDeleteFirstNode();
}
else if(curNode == ennode)
{
DlListDeleteLastNode();
}
else if(curNode != NULL)
{

curNode->preptr->nextptr = curNode-
>nextptr; curNode->nextptr->preptr =
curNode->preptr;
free(curNode); //Delete the n node
}
else
{
printf(“ The given position is invalid!\n”);
}
}

void DlListDeleteFirstNode()
{
struct node *
NodeToDel; if(stnode
== NULL)
{
printf(“ Delete is not possible. No data in the list.\n”);
}
else
{
NodeToDel = stnode;
stnode = stnode->nextptr; // move the next address of starting node to 2
node stnode->preptr = NULL; // set previous address of staring
node is NULL free(NodeToDel); // delete the first node from memory
}
}
void DlListDeleteLastNode()
{
struct node * NodeToDel;

if(ennode == NULL)
{
printf(“ Delete is not possible. No data in the list.\n”);
}
else
{
NodeToDel = ennode;
ennode = ennode->preptr; // move the previous address of the last node to 2nd last
node ennode->nextptr = NULL; // set the next address of last node to NULL
free(NodeToDel); // delete the last node
}
}
void displayDlList(int m)
{
struct node * tmp;
int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp =
stnode; if
(m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After deletion the new list are :\n”);
}
while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp->num);
n++;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}
}

Hands – on 20. Write a program in C to delete a node from the middle of a


doubly linked list.
Test Data and Expected Output :
Input the number of nodes (3 or more ): 3
Input data for node 1 : 1
Input data for node 2 : 2
Input data for node 3 : 3
Data entered in the list are
: node 1 : 1
node 2 : 2
node 3 : 3
Input the position ( 1 to 3 ) to delete a node :
2 After deletion the new list are :
node 1 : 1
node 2 : 3

#include <stdio.h>
#include <stdlib.h>

struct node {
int num;
struct node * preptr;
struct node *
nextptr;
}*stnode, *ennode;

void DlListcreation(int n);


void DlListDeleteMiddleNode(int pos);
void displayDlList(int a);

int main()
{
int n,num1,a,insPlc;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Delete node from middle of a doubly linked list :\
n”); printf(“---------------------------------------------------------------------------\n”);
printf(“ Input the number of nodes (3 or more ): “);
scanf(“%d”, &n);
DlListcreation(n);
a=1;
displayDlList(a);

printf(“ Input the position ( 2 to %d ) to delete a node : “,n-1);


scanf(“%d”, &insPlc);

if(insPlc<=1 || insPlc>=n)
{
printf(“\n Invalid position. Try again.\n “);
}
if(insPlc>1 &&
insPlc<n)
{
DlListDeleteMiddleNode(insPlc);
a=2;
displayDlList(a);
}
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);

stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr =
NULL; ennode =
stnode; for(i=2; i<=n; i+
+)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous node
fnNode->nextptr = NULL; // set next address of fnnode is NULL
ennode->nextptr = fnNode; // previous node is linking with the new
node ennode = fnNode; // assign new node as last node
}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{
printf(“ Memory can not be allocated.”);
}
}
}

void DlListDeleteMiddleNode(int pos)


{
struct node
*curNode; int i;

curNode = stnode;
for(i=1; i<pos && curNode!=NULL; i++)
{
curNode = curNode->nextptr;
}
if(curNode != NULL)
{
curNode->preptr->nextptr = curNode-
>nextptr; curNode->nextptr->preptr =
curNode->preptr;

free(curNode); //Delete the n node


}
else
{
printf(“ The given position is invalid!\n”);
}
}

void displayDlList(int m)
{
struct node * tmp;

int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp =
stnode; if
(m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After deletion the new list are :\n”);
}
while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp->num);
n++;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}
}

Hands – on 21. Write a program in C to find the maximum value from a


doubly linked list.
Test Data :
Input the number of nodes :
3 Input data for node 1 : 5
Input data for node 2 : 9
Input data for node 3 : 1
Expected Output :
Data entered in the list are :
node 1 : 5
node 2 : 9
node 3 : 1
The Maximum Value in the Linked List : 9

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

struct node {
int num;
struct node * preptr;
struct node *
nextptr;
}*stnode, *ennode;

void DlListcreation(int n);


int getMaxNode(struct node *stnode);
void displayDlList();

int main()
{
int n;
stnode = NULL;
ennode = NULL;
printf(“\n\n Doubly Linked List : Find maximum value from a doubly linked list :\
n”); printf(“--------------------------------------------------------------------------\n”);
printf(“ Input the number of nodes : “);
scanf(“%d”, &n);
DlListcreation(n);
displayDlList();
printf(“\n The Maximum Value in the Linked List : %d\n\n”, getMaxNode(stnode));
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)
{
printf(“ Input data for node 1 : “); // assigning data in the first node
scanf(“%d”, &num);

stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr =
NULL; ennode =
stnode;
// putting data for rest of the nodes
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
fnNode->num = num;
fnNode->preptr = ennode; // new node is linking with the previous
node fnNode->nextptr = NULL;

ennode->nextptr = fnNode; // previous node is linking with the new


node ennode = fnNode; // assign new node as last node
}
else
{
printf(“ Memory can not be allocated.”);
break;
}
}
}
else
{

printf(“ Memory can not be allocated.”);


}
}
}

int getMaxNode(struct node *stnode)


{
if(stnode == NULL){
printf(“ User message : Invalid Input !!!!\n”);
return INT_MIN;
}
int max = stnode->num;
while(stnode != NULL){
if(stnode->num > max){
max = stnode->num;
}
stnode = stnode->nextptr;
}
return max;
}

void displayDlList()
{
struct node * tmp;
int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
printf(“\n\n Data entered in the list are :\n”);
while(tmp != NULL)
{
printf(“ node %d : %d\n”, n, tmp->num);
n++;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}
}
CIRCULAR LINKED LIST

#include <stdio.h> Hands on 22: Circular Linked List


#include <stdlib.h>

struct node {
int num;
struct node * nextptr;
}*stnode;

void ClListcreation(int n);


void displayClList();

int main()
{

int n;
stnode = NULL;
printf("\n\n Circular Linked List : Create and display a circular linked list
:\n"); printf(" \n");

printf(" Input the number of nodes : ");


scanf("%d", &n);

ClListcreation(
n);
displayClList();
return 0;

void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

printf(“ Input data for node 1 : “);


scanf(“%d”, &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}

void displayClList()
{
struct node *tmp;
int n = 1;

if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
printf(“\n\n Data entered in the list are :\n”);

do {
printf(“ Data %d = %d\n”, n, tmp->num);

tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
#include <stdio.h> Hands on 23: Circular Linked List
#include <stdlib.h>

struct node {
int num;
struct node * nextptr;
}*stnode;

void ClListcreation(int n);


void ClLinsertNodeAtBeginning(int num);
void displayClList(int a);

int main()
{

int n,num1,a;
stnode = NULL;
printf("\n\n Circular Linked List : Insert a node at the beginning of a circular linked list
:\n");
printf(" \n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
ClListcreation
(n); a=1;
displayClList(a);
printf(“ Input data to be inserted at the beginning : “);
scanf(“%d”, &num1);
ClLinsertNodeAtBeginning(num1);
a=2;
displayClList(a);
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

printf(“ Input data for node 1 : “);


scanf(“%d”, &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}

void ClLinsertNodeAtBeginning(int num)


{
struct node *newnode, *curNode;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
newnode->nextptr = stnode;

curNode = stnode;
while(curNode->nextptr != stnode)
{
curNode = curNode->nextptr;
}
curNode->nextptr = newnode;
stnode = newnode;
}
}
void displayClList(int m)
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
if (m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After insertion the new list are :\n”);
}
do {
printf(“ Data %d = %d\n”, n, tmp->num);
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
#include <stdio.h> Hands on 24: Circular Linked List
#include <stdlib.h>

struct node {
int num;
struct node * nextptr;
}*stnode;

struct node *tail,*p,*q,*store;

void ClListcreation(int n);


void ClLinsertNodeAtEnd(int num);
void displayClList(int a);

int main()
{

int n,num1,a,insPlc;
stnode = NULL;
printf("\n\n Circular Linked List : Insert a node at the end of a circular linked list
:\n"); printf(" \n");
printf(" Input the number of nodes : ");
scanf(“%d”, &n);
ClListcreation(n);
a=1;
displayClList(a);
printf(“ Input the data to be inserted : “);
scanf(“%d”, &num1);
ClLinsertNodeAtEnd(num1);
a=2;
displayClList(a);
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
printf(“ Input data for node 1 : “);
scanf(“%d”, &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}

void ClLinsertNodeAtEnd(int num1)


{
int a;
a=num1;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->num=a;
p=stnode;
while(p->nextptr!=stnode)
{
p=p->nextptr;
}

p->nextptr=temp;
temp->nextptr=stnode;
}

void displayClList(int m)
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
if (m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After insertion the new list are :\n”);
}
do {
printf(“ Data %d = %d\n”, n, tmp->num);

tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
#include <stdio.h> Hands on 25: Circular Linked List
#include <stdlib.h>

struct node {
int num;
struct node * nextptr;
}*stnode;

void ClListcreation(int n);


void ClLinsertNodeAtBeginning(int num);
void ClLinsertNodeAtAny(int num, int pos);
void displayClList(int a);

int main()
{

int n,num1,a,insPlc;
stnode = NULL;
printf("\n\n Circular Linked List : Insert a node at any position in a circular linked
list
:\n");

printf(“ \n”);

printf(“ Input the number of nodes : “);


scanf(“%d”, &n);
ClListcreation(n);
a=1;
displayClList(a);
printf(“ Input the position to insert the new node : “);
scanf(“%d”, &insPlc);
printf(“ Input data for the position %d : “, insPlc);
scanf(“%d”, &num1);
ClLinsertNodeAtAny(num1,insPlc);
a=2;
displayClList(a);
return 0;
}

void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

printf(“ Input data for node 1 : “);


scanf(“%d”, &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}

void ClLinsertNodeAtBeginning(int num)


{
struct node *newnode, *curNode;

if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
newnode->nextptr = stnode;
curNode = stnode;
while(curNode->nextptr != stnode)
{
curNode = curNode->nextptr;
}
curNode->nextptr = newnode;
stnode = newnode;
}
}

void ClLinsertNodeAtAny(int num, int pos)


{
struct node *newnode, *curNode;
int i;

if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else if(pos == 1)
{
ClLinsertNodeAtBeginning(num);
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
curNode = stnode;
for(i=2; i<=pos-1; i++)
{
curNode = curNode->nextptr;
}
newnode->nextptr = curNode->nextptr;
curNode->nextptr = newnode;
}
}

void displayClList(int m)
{
struct node *tmp;
int n = 1;

if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
if (m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After insertion the new list are :\n”);
}
do {
printf(“ Data %d = %d\n”, n, tmp->num);
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
#include <stdio.h> Hands on 26: Circular Linked List
#include <stdlib.h>

struct node {
int num;
struct node * nextptr;
}*stnode;

struct node *tail,*p,*q,*store;

void ClListcreation(int n);


void ClListDeleteFirstNode();
void displayClList(int a);

int main()
{

int
n,num1,a,insPl
c; stnode =
NULL;
printf("\n\n Circular Linked List : Delete node from the beginning of a circular
linked list :\n");
printf(" \n");

printf(" Input the number of nodes : ");

scanf(“%d”, &n);
ClListcreation(n);
a=1;
displayClList(a);
ClListDeleteFirstNode();
a=2;
displayClList(a);
return 0;
}

void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

printf(“ Input data for node 1 : “);


scanf(“%d”, &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}

void ClListDeleteFirstNode()
{
p=stnode;
while(p->nextptr!=stnode)
{
p=p->nextptr;
}
store=stnode;
stnode=stnode->nextptr;
printf(“\n The deleted node is -> %d”,store->num);
p->nextptr=stnode;

free (store);
}

void displayClList(int m)
{
struct node *tmp;
int n = 1;

if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
if (m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After deletion the new list are :\n”);
}
do {
printf(“ Data %d = %d\n”, n, tmp->num);

tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
#include <stdio.h> Hands on 27: Circular Linked List
#include <stdlib.h>

struct node {
int num;
struct node * nextptr;
}*stnode;

struct node *tail,*p,*q,*store;

void ClListcreation(int n);


void ClListDeleteMiddle(int pos);
void displayClList(int a);

int main()
{

int n,num1,a,pos;
stnode = NULL;
printf("\n\n Circular Linked List : Delete node from the middle of a circular linked list
:\n");
printf(" \n");

printf(" Input the number of nodes : ");

scanf(“%d”, &n);
ClListcreation(n);
a=1;
displayClList(a);
printf(“\n Input the position to delete the node : “);
scanf(“%d”,&pos);
ClListDeleteMiddle(pos);
a=2;
displayClList(a);
return 0;
}

void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

printf(“ Input data for node 1 : “);


scanf(“%d”, &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}

void ClListDeleteMiddle(int pos)


{
int delNode,k=1;
delNode=pos;
p=stnode; while(k!
=delNode)
{
q=p;
p=p->nextptr;
k++;
}
q->nextptr=p->nextptr;
printf(“\n The deleted node is : %d”,p->num);
free(p);
}

void displayClList(int m)
{
struct node *tmp;
int n = 1;

if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
if (m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After deletion the new list are :\n”);
}
do {
printf(“ Data %d = %d\n”, n, tmp->num);

tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
#include <stdio.h> Hands on 28: Circular Linked List
#include <stdlib.h>

struct node {
int num;
struct node * nextptr;
}*stnode;

struct node *tail,*p,*q,*store;

void ClListcreation(int n);


void ClListDeleteLastNode();
void displayClList(int a);

int main()
{

int
n,num1,a,pos;
stnode =
NULL;
printf("\n\n Circular Linked List : Delete node at the end of a circular linked list
:\n");
printf(“ \n”);

printf(“ Input the number of nodes : “);


scanf(“%d”, &n);
ClListcreation(n);
a=1;
displayClList(a);
ClListDeleteLastNode();
a=2;
displayClList(a);
return 0;
}

void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

printf(“ Input data for node 1 : “);


scanf(“%d”, &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(“ Input data for node %d : “, i);
scanf(“%d”, &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}

void ClListDeleteLastNode()
{
p=stnode;
while(p->nextptr!=stnode)
{
q=p;
p=p->nextptr;
}
q->nextptr=stnode;
printf(“\n The deleted node is : %d”,p->num);
free(p);
}

void displayClList(int m)
{
struct node *tmp;
int n = 1;

if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
if (m==1)
{
printf(“\n Data entered in the list are :\n”);
}
else
{
printf(“\n After deletion the new list are :\n”);
}
do {
printf(“ Data %d = %d\n”, n, tmp->num);

tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
#include <stdio.h> Hands on 29: Circular Linked List
#include <stdlib.h>

struct node {
int num;
struct node * nextptr;
}*stnode,*ennode;

void ClListcreation(int n);


int FindElement(int FindElem, int n);
void displayClList();

int main()
{

int n,m;
int
i,FindElem,FindPl
c; stnode =
NULL; ennode =
NULL;
printf("\n\n Circular Linked List : Search an element in a circular linked list :\n");
printf(“ \n”);

printf(“ Input the number of nodes : “);


scanf(“%d”, &n);
m=n;

ClListcreation(n);
displayClList();
printf(“ Input the element you want to find : “);
scanf(“%d”, &FindElem);

FindPlc=FindElement(FindElem,m);
if(FindPlc<n)
printf(“ Element found at node %d \n\n”,FindPlc);
else
printf(“ This element does not exists in linked list.\n\n”)
return 0;
}

void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

printf(" Input data for node 1 : ");


scanf("%d", &num);
stnode->num = num; stnode-
>nextptr = NULL; preptr =
stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node preptr
= newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}

int FindElement(int FindElem, int a)


{
int ctr=1;
ennode=stnode;
while(ennode->nextptr!=NULL)
{
if(ennode->num==FindElem)
break;
else
ctr++;
ennode=ennode->nextptr;
if (ctr==a+1)
break;
}

return ctr;
}

void displayClList()
{
struct node *tmp;
int n = 1;

if(stnode == NULL)
{
printf(“ No data found in the List yet.”);
}
else
{
tmp = stnode;
printf(“\n\n Data entered in the list are :\n”);

do {
printf(“ Data %d = %d\n”, n, tmp->num);

tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}

STACK
Stack – Hands on #1F
#include <bits/stdc++.h>

using namespace std;

#define MAX 1000

class Stack {
int top;

public:
int a[MAX]; // Maximum size of Stack

Stack() { top = -1;


} bool push(int x);
int pop();
int peek();
bool isEmpty();
};

bool Stack::push(int x)
{
if (top >= (MAX – 1)) {
cout << “Stack Overflow”;
return false;
}
else { a[+
+top] = x;
cout << x << “ pushed into stack\n”;
return true;
}
}

int Stack::pop()
{
if (top < 0) {
cout << “Stack Underflow”;
return 0;
}
else {
int x = a[top--];
return x;

}
}
int Stack::peek()
{
if (top < 0) {
cout << “Stack is Empty”;
return 0;
}
else {
int x = a[top];
return x;
}
}

bool Stack::isEmpty()
{
return (top < 0);
}

// Driver program to test above


functions int main()
{
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << “ Popped from stack\n”;

return 0;
}

Hands on #2F – Stack


#include <bits/stdc++.h>
using namespace std;

// A structure to represent a stack


class StackNode {
public:
int
data;
StackNode* next;
};

StackNode* newNode(int data)


{
StackNode* stackNode = new
StackNode(); stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}

int isEmpty(StackNode* root)


{
return !root;
}

void push(StackNode** root, int data)


{
StackNode* stackNode =
newNode(data); stackNode->next =
*root;
*root = stackNode;
cout << data << “ pushed to stack\n”;
}

int pop(StackNode** root)


{
if (isEmpty(*root))
return INT_MIN;
StackNode* temp = *root;
*root = (*root)->next;
int popped = temp->data;
free(temp);

return popped;

int peek(StackNode* root)


{
if (isEmpty(root))
return INT_MIN;
return root-
>data;
}

int main()
{
StackNode* root = NULL;

push(&root, 10);
push(&root, 20);
push(&root, 30);

cout << pop(&root) << “ popped from stack\n”;

cout << “Top element is “ << peek(root) << endl;

return 0;
}

// This is code is contributed by rathbhupendra

Hands on #3 – Stack
#include <stdio.h>

int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//
clrscr();
top=-1;
printf(“\n Enter the size of STACK[MAX=100]: “);
scanf(“%d”,&n);
printf(“\n\t STACK OPERATIONS USING ARRAY”);
printf(“\n\t “);
printf(“\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT”);
do
{
printf(“\n Enter the Choice:”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop()
;
brea
k;
}
case 3:
{
display();
break;
}
case 4:
{
printf(“\n\t EXIT POINT “);
break;
}

default:
{
printf(“\n\t Please Enter a Valid choice(1/2/3/4)”);
}
}
}
while(choice!
=4); return 0;
}
void push()
{
if(top>=n-1)
{
printf(“\n\Tstack is over flow”);
}
else
{
printf(“Enter a value to be pushed: “);
scanf(“%d”,&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf(“\n\t Stack is under flow”);
}
else
{
printf(“\n\t The poppes elements is
%d”,stack[top]); top--;
}
}
void display()
{
if (top>=0)
{
printf(“\n The elements in STACK \n”);
for(i=top; i>=0; i--)
printf(“\n%d”, stack[i]);
printf(“\n Press Next Choice”);
}

else
{
printf(“\n The STACK is empty”);
}
}
QUEUE
HANDS ON 4F – QUEUE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int
intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
int peek() {
return intArray[front];
}
bool isEmpty() {
return itemCount == 0;
}
bool isFull() {
return itemCount == MAX;
}
int size() {
return itemCount;
}
void insert(int data) { if(!
isFull()) {

if(rear == MAX-1)
{ rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int removeData() {
int data = intArray[front++];

if(front == MAX) {
front = 0;
}

itemCount--;
return data;
}
int main() {
/* insert 5 items
*/ insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
// front : 0
// rear : 4
//
// index : 0 1 2 3 4
//
// queue : 3 5 9 1 12
insert(15);
// front : 0
// rear : 5
//
// index : 0 1 2 3 4 5
//
// queue : 3 5 9 1 12 15

if(isFull()) {
printf(“Queue is full!\
n”);
}
// remove one item
int num = removeData();

printf(“Element removed: %d\n”,num);


// front : 1
// rear : 5
//
// index : 1 2 3 4 5
//
// queue : 5 9 1 12 15
// insert more items
insert(16);
// front : 1

// rear : -1
//
// index : 0 1 2 3 4 5
//
// queue : 16 5 9 1 12 15
// As queue is full, elements will not be inserted.
Insert(17);
insert(18);
//
// index : 0 1 2 3 4 5
//
// queue : 16 5 9 1 12 15
printf(“Element at front: %d\
n”,peek()); printf(“----------------------\
n”);
printf(“index : 5 4 3 2 1 0\
n”);
printf(“----------------------\n”);
printf(“Queue: “);

while(!isEmpty()) {
int n = removeData();
printf(“%d “,n);
}
}

HANDS ON 5F – QUEUE

/* Simple Queue Program in C*/


/* Data Structure Programs,C Array Examples */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE
100 int main() {
int item, choice, i;
int
arr_queue[MAX_SIZE];
int rear = 0;
int front = 0;
int exit = 1;
printf(“\nSimple Queue Example – Array”);
do {
printf(“\n\n Queue Main Menu”);
printf(“\n1.Insert \n2.Remove \n3.Display \nOthers to exit”);
printf(“\nEnter Your Choice : “);
scanf(“%d”,
&choice); switch
(choice) { case 1:
if (rear == MAX_SIZE)
printf(“\n## Queue Reached Max!!”);
else {
printf(“\nEnter The Value to be Insert : “);
scanf(“%d”, &item);
printf(“\n## Position : %d , Insert Value : %d “, rear + 1,
item);
arr_queue[rear++] = item;
}
break;
case 2:
if (front == rear)
printf(“\n## Queue is Empty!”);
else {
printf(“\n## Position : %d , Remove Value : %d “, front,
arr_queue[front]);
front++;
}

break;
case 3:
printf(“\n## Queue Size : %d “, rear);
for (i = front; i < rear; i++)
printf(“\n## Position : %d , Value : %d “, i,
arr_queue[i])
; break;
default:
exit = 0;
break;
}
} while (exit);
return 0;
}
HANDS ON 6F – QUEUE

/*
* C Program to Implement a Queue using an Array
*/
#include <stdio.h>

#define MAX 50

void insert();
void Delete();
void
display();
void exit();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf(“1.Insert element to queue \n”);
printf(“2.Delete element from queue \n”);
printf(“3.Display all elements of queue \n”);
printf(“4.Quit \n”);
printf(“Enter your choice :
“); scanf(“%d”, &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
Delete();
break;
case 3:
display();
break;
case 4:
return 0;

default:
printf(“Wrong choice \n”);
} /* End of switch */
} /* End of while */
} /* End of main() */

void insert()
{
int add_item;
if (rear == MAX – 1)
printf(“Queue Overflow \n”);
else
{
if (front == - 1)
/*If queue is initially empty
*/ front = 0;
printf(“Inset the element in queue : “);
scanf(“%d”, &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */

void Delete()
{
if (front == - 1 || front > rear)
{
printf(“Queue Underflow \n”);
return ;
}
else
{
printf(“Element deleted from queue is : %d\n”, queue_array[front]);
front = front + 1;
}
} /* End of delete() */

void display()
{
int i;

if (front == - 1)
printf(“Queue is empty \
n”); else
{
printf(“Queue is : \n”);
for (i = front; i <= rear; i++)
printf(“%d “,
queue_array[i]); printf(“\n”);
}
} /* End of display() */
HANDS ON 7F – QUEUE

#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear =-1;
int isFull()
{
if( (front == rear + 1) || (front == 0 && rear == SIZE-1)) return 1;
return 0;
}
int isEmpty()
{
if(front == -1) return 1;
return 0;
}
void enQueue(int element)
{
if(isFull()) printf(“\n Queue is full!! \
n”); else
{
if(front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf(“\n Inserted -> %d”, element);
}
}
int deQueue()
{
int element;
if(isEmpty())
{
printf(“\n Queue is empty !! \
n”); return(-1);
} else {
element =
items[front]; if (front
== rear){
front = -1;
rear = -1;
} /* Q has only one element, so we reset the queue after dequeing it. ? */
else {

front = (front + 1) % SIZE;

}
printf(“\n Deleted element -> %d \n”, element);
return(element);
}
}
void display()
{
int i;
if(isEmpty()) printf(“ \n Empty Queue\n”);
else
{
printf(“\n Front -> %d “,front);
printf(“\n Items -> “);
for( i = front; i!=rear; i=(i+1)%SIZE) {
printf(“%d “,items[i]);
}
printf(“%d “,items[i]);
printf(“\n Rear -> %d \n”,rear);
}
}
int main()
{
// Fails because front = -1
deQueue();

enQueue(1
);
enQueue(2
);
enQueue(3
);
enQueue(4
);
enQueue(5
);
// Fails to enqueue because front == 0 && rear == SIZE –
1 enQueue(6);
display();
deQueue();

display();
enQueue(7);
display();

// Fails to enqueue because front == rear + 1


enQueue(8);

return 0;
}
TREE
1. What is tree?
Other data structures such as arrays, linked list, stack, and queue are linear data
structures that store data sequentially. In order to perform any operation in a linear
data structure, the time complexity increases with the increase in the data size. But, it
is not acceptable in today's computational world.
2. What are the elements of Tree?
Root, Parent Root, Siblings, Child Node, Sub tree, Degree, Internal Node, and Leaf
Node
3. What are the different representation of tree?
Sequential Representation and Linked Representation
4. Identify and Discuss the different terminologies of Tree?
-
1. Root
The first node from where the tree originates is called a root node.
In any tree, there must be only one root node.
We can never have multiple root nodes in a tree data structure.
2. Edge
The connecting link between any two nodes is called an edge.
In a tree with n number of nodes, there is exactly (n-1) number of edges.
3. Parent
The node which has a branch from it to any other node is called a parent node.
In other words, the node which has one or more children is called a parent node.
In a tree, a parent node can have any number of child nodes.
4. Child
The node which is a descendant of some node is called a child node.
All the nodes except the root node are child nodes.
5. Siblings
Nodes that belong to the same parent are called siblings.
In other words, nodes with the same parent are sibling nodes.
6. Degree
The degree of a node is the total number of children of that node.
The degree of a tree is the highest degree of a node among all the nodes in the tree.

7. Internal Node
The node which has at least one child is called an internal node.
Internal nodes are also called non-terminal nodes.
Every non-leaf node is an internal node.
8. Leaf Node
The node which does not have any child is called a leaf node.
Leaf nodes are also called external nodes or terminal nodes.
5. What is the difference and similarities of full binary tree and complete binary tree
A full binary tree (sometimes a proper binary tree or 2-tree) is a tree in which every
node other than the leaves has two children.
A complete binary tree is a binary tree in which every level, except possibly the last,
is completely filled, and all nodes are as far left as possible.
6. What is AVL and Non AVL Tree?
An AVL tree is a self-balancing binary search tree.
An AVL tree is opposite it is not balancing binary search tree.

* * C Program for Depth First Binary Tree Search using Recursion */ #include <stdio.h>
#include <stdlib.h>
struct node {
int a;
struct node *left;#include <stdio.h> #include <stdlib.h>
struct node {
int a;
struct node *left; struct node *right;
};
void generate(struct node **, int); void DFS(struct node *);
void delete(struct node **);
int main() {
struct node *head = NULL;
int choice = 0, num, flag = 0, key;
do {
printf("\nEnter your choice:\n1. Insert\n2. Perform DFS Traversal\n3. Exit\nChoice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter element to insert: "); scanf("%d", &num); generate(&head, num);
break;
case 2: DFS(head); break;
case 3:
delete(&head);
printf("Memory Cleared\nPROGRAM TERMINATED\n"); break;
default:
printf("Not a valid input, try again\n");
}
} while (choice != 3); return 0;
}
void generate(struct node **head, int num) {
struct node *temp = *head, *prev = *head;
if (*head == NULL) {
*head = (struct node *)malloc(sizeof(struct node)); (*head)->a = num;
(*head)->left = (*head)->right = NULL;
} else {
while (temp != NULL) {
if (num > temp->a) {
prev = temp;
temp = temp->right; }
else {
prev = temp;
temp = temp->left; }
}
temp = (struct node *)malloc(sizeof(struct node)); temp->a = num;
if (num >= prev->a)
{
prev->right = temp; }
else {
prev->left = temp; }
}}
void DFS(struct node *head) {
if (head) {
if (head->left) {
DFS(head->left); }
if (head->right) {
DFS(head->right); }
printf("%d ", head->a); }
}
void delete(struct node **head) {
if (*head != NULL) {
if ((*head)->left) {
delete(&(*head)->left); }
if ((*head)->right) {
delete(&(*head)->right); }
free(*head); }
}
struct node *right; };
void generate(struct node **, int); void DFS(struct node *);
void delete(struct node **);
int main() {
struct node *head = NULL;
int choice = 0, num, flag = 0, key;
do {
printf("\nEnter your choice:\n1. Insert\n2. Perform DFS Traversal\n3. Exit\nChoice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter element to insert: "); scanf("%d", &num); generate(&head, num);
break;
case 2: DFS(head); break;

case 3:
delete(&head);
printf("Memory Cleared\nPROGRAM TERMINATED\n"); break;
default:
printf("Not a valid input, try again\n");
}
} while (choice != 3); return 0;
}
void generate(struct node **head, int num) {
struct node *temp = *head, *prev = *head;
if (*head == NULL) {
*head = (struct node *)malloc(sizeof(struct node)); (*head)->a = num;
(*head)->left = (*head)->right = NULL;
} else {
while (temp != NULL) {
if (num > temp->a) {
prev = temp;
temp = temp->right;
} else {
prev = temp;
temp = temp->left; }
}
temp = (struct node *)malloc(sizeof(struct node)); temp->a = num;
if (num >= prev->a)
{
prev->right = temp; }
else {
prev->left = temp; }
}}
void DFS(struct node *head) {
if (head) {
if (head->left) {
DFS(head->left); }
if (head->right) {
DFS(head->right); }
printf("%d ", head->a); }
}
void delete(struct node **head) {
if (*head != NULL) {
if ((*head)->left) {
delete(&(*head)->left); }
if ((*head)->right) {
delete(&(*head)->right); }
free(*head); }
}
TREE TRAVERSALS
1. What is Tree Traversals?
In computer science, tree traversal (also known as tree search and walking the tree)
is a form of graph traversal and refers to the process of visiting (checking and/or
updating) each node in a tree data structure, exactly once. Such traversals are
classified by the order in which the nodes are visited.
2. How tree traversals works on Binary Search Tree (BST)?
BST is a collection of nodes arranged in a way where they maintain BST properties.
Each node has a key and an associated value. While searching, the desired key is
compared to the keys in BST and if found, the associated value is retrieved.
3. What are the Basic Operations of a BST? List down the steps for each operation.
Search − Searches an element in a tree.
Insert − Inserts an element in a tree.
Pre-order Traversal − Traverses a tree in a pre-order manner.
In-order Traversal − Traverses a tree in an in-order manner.
Post-order Traversal − Traverses a tree in a post-order manner.
4. What are the techniques of tree traversal? List down the steps for each
techniques.
There are 3 techniques for every technique there are 3 steps and that is:
In-order Traversal
* Step 1 − Recursively traverse left subtree.
* Step 2 − Visit root node.
* Step 3 − Recursively traverse right subtree.
Pre-order Traversal
* Step 1 − Visit root node.
* Step 2 − Recursively traverse left subtree.
* Step 3 − Recursively traverse right subtree.
Post-order Traversal
* Step 1 − Recursively traverse left subtree.
* Step 2 − Recursively traverse right subtree.
* Step 3 − Visit root node.

You might also like