DS Lab
DS Lab
INTRODUCTION
A data structure is a specialized format for organizing, processing, retrieving and storing data. There
are several basic and advanced types of data structures, all designed to arrange data to suit a specific
purpose. Data structures make it easy for users to access and work with the data they need in
appropriate ways. Most importantly, data structures frame the organization of information so that
machines and humans can better understand it.
In computer science and computer programming, a data structure may be selected or designed to store
data for the purpose of using it with various algorithms. In some cases, the algorithm's basic operations
are tightly coupled to the data structure's design. Each data structure contains information about the
data values, relationships between the data and -- in some cases -- functions that can be applied to the
data.
Typical base data types, such as integers or floating-point values, that are available in most computer
programming languages are generally insufficient to capture the logical intent for data processing and
use. Yet applications that ingest, manipulate and produce information must understand how data should
be organized to simplify processing. Data structures bring together the data elements in a logical way
and facilitate the effective use, persistence and sharing of data. They provide a formal model that
describes the way the data elements are organized.
Data structures are the building blocks for more sophisticated applications. They are designed by
composing data elements into a logical unit representing an abstract data type that has relevance to the
algorithm or application. An example of an abstract data type is a "customer name" that is composed
of the character strings for "first name," "middle name" and "last name." Data Structures include linked
lists, stacks, queues, trees, and dictionaries. They could also be a theoretical entity, like the name and
address of a person.
It is not only important to use data structures, but it is also important to choose the proper data structure
for each task. Choosing an ill-suited data structure could result in slow runtimes or unresponsive code.
SYLLABUS
CO2: Demonstrate the working nature of different types of data structures and their applications
CO3: Use appropriate searching and sorting algorithms for the give scenario.
CO4: Apply the appropriate data structure for solving real world problems
Passing standard:
• The minimum marks to be secured in CIE to appear for SEE shall be 12 (40% of maximum
marks-30) in the theory component and 08 (40% of maximum marks -20) in the practical
component.
• SEE will be conducted for 100 marks and students shall secure 35% of the maximum marks
to qualify for the SEE. Marks secured will be scaled down to 50.
Lab Assignments
Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent 7
days of a week. Each Element of the array is a structure having three fields. The first field is
the name of the Day (A dynamically allocated String), The second field is the date of the Day
1 (A integer), the third field is the description of the activity for a particular day (A dynamically
allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data
from the keyboard and to print weeks activity details report on screen.
Develop a Program in C for the following operations on Strings.
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR
2 with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in
functions.
Develop a menu driven Program in C for the following operations on STACK of Integers
(Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
3 c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations
Develop a Program in C for converting an Infix Expression to Postfix Expression. Program
should support for both parenthesized and free parenthesized
4
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric
operands.
Develop a Program in C for the following Stack Applications
5 a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,^
b. Solving Tower of Hanoi problem with n disks
Develop a menu driven Program in C for the following operations on Circular QUEUE of
6 Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
12 Table (HT) of m memory locations with L as the set of memory addresses (2-digit) of
locations in HT. Let the keys in K and addresses in L are Integers. Develop a Program in
C that uses Hash function H: K →L as H(K)=K mod m (remainder method), and implement
hashing technique to map a given key K to the address space L. Resolve the collision (if
any) using linear probing.
General Instructions for the Laboratory
Do’s
• It is mandatory for all the students to attend all practical classes & complete the experiments
as per syllabus.
• Students should strictly follow the lab timings, dress code with Apron & ID cards.
• Submit the completed lab records of executed programs and update the index book in every
lab session.
Don’ts
• Should not take Bags and Mobile phones into the Laboratory.
• Systems & Components should be handled carefully failing to which penalty will be imposed.
PROGRAM-1
1. Develop a Program in C for the following:
b) Write functions create (), read () and display (); to create the calendar, to read the data
from the keyboard and to print weeks activity details report on screen.
ALGORITHM:
Step1: Initialize the calendar array with 7 elements.
Step2: For each element in the calendar array:
a. Create a structure to represent a day.
b. Dynamically allocate memory for the “name” field (a string) and set it to the name of the day.
c. Set the “date” field to the date of the day (an integer).
d. Dynamically allocate memory for the “description” field (a string) and set it to the description
of the day’s activities.
Step3: Store each day structure in the corresponding element of the calendar array.
PROGRAM CODE: 1A
#include <stdio.h>
#include <stdio.h>
#include <string.h>
struct Day {
char *name;
int date;
char *activity;
};
int main()
{
return 0;
}
OUTPUT:
PROGRAM CODE: 1B
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
struct Day week[7];
create(week);
return 0;
}
OUTPUT:
PROGRAM-2
2. Develop a Program in C for the following operations on Strings.
a) Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b) Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR
with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in
functions.
If you follow the rule of array initialization then you can write the above statement as follows:
C language supports a wide range of built-in functions that manipulate null-terminated strings as
follows:
strcpy(s1, s2); Copies string s2 into string s1.
strcat(s1, s2); Concatenates string s2 onto the end of string s1.
strlen(s1); Returns the length of string s1.
strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.
ALGORITHM:
Step 1: Start.
Step 2: Read main string STR, pattern string PAT and replace string REP.
Step 3: Search / find the pattern string PAT in the main string STR.
Step 4: if PAT is found then replace all occurrences of PAT in main string STR with REP string.
Step 5: if PAT is not found give a suitable error message.
Step 6: Stop.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
//Declarations
char str[100], pat[50], rep[50],
ans[100]; int i, j, c, m, k, flag=0;
void stringmatch()
{
i = m = c = j = 0;
while(str[c] ! = '\0')
{
if(str[m] = = pat[i]) // ...... matching
{
i++; m++;
if(pat[i] = = '\0') //.....found occurrences.
{
flag = 1;
//.... copy replace string in ans string.
for(k = 0; rep[k] != '\0'; k++, j++)
ans[j] = rep[k];
i = 0;
c = m;
}
}// if ends.
else //... mismatch
{
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}//else ends
} //end of while
ans[j] = '\0';
} //end stringmatch()
void main()
{
clrscr();
printf("\nEnter a main string \n");
gets(str);
printf("\nEnter a pattern string \n");
gets(pat);
printf("\nEnter a replace string \n");
gets(rep);
stringmatch();
if(flag = = 1)
printf("\nThe resultant string is\n %s" , ans);
else
printf("\nPattern string NOT found\n");
getch();
}// end of main
SAMPLE OUTPUT:
RUN 1:
Enter a main string
Test
Enter a pattern string
Te
Enter a replace string
Re
RUN 2:
Enter a main string
This is Data Structure lab
Enter a pattern string
Data Structure
Enter a replace string
Data structure with C
The resultant string is
This is Data structure with C lab
RUN 3:
Enter a main string
This is Data Structure lab
Enter a pattern string
Date
Enter a replace string
DATA
Pattern string NOT found
PROGRAM-3
Develop a menu driven Program in C for the following operations on STACK of Integers
(Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations
ABOUT THE EXPERIMENT:
A stack is an abstract data type (ADT), commonly used in most programming languages.
It is named stack as it behaves like a real-world stack, for example − deck of cards or pile of plates
etc.
A real-world stack allows operations at one end only. For example, we can place or remove
a card or plate from top of the stack only. LikewCSE, Stack ADT allows all data operations at
one end only. At any given time, we can only access the top element of a stack.
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the
element which is placed (inserted or added) last is accessed first. In stack terminology, insertion
operation is called PUSH operation and removal operation is called POP operation.
Below given diagram tries to depict a stack and its operations −
A stack can be implemented by means of Array, Structure, Pointer and Linked-List. Stack
can either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to
implement stack using arrays which makes it a fixed size stack implementation.
Basic Operations:
• push() - pushing (storing) an element on the stack.
• pop() - removing (accessing) an element from the stack.
To use a stack efficiently we need to check status of stack as well. For the same purpose, the
following functionality is added to stacks;
• peek() − get the top data element of the stack, without removing it.
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.
ALGORITHM:
Step 1: Start.
Step 2: Initialize stack size MAX and top of stack -1.
Step 3: Push integer element on to stack and display the contents of the stack.
if stack is full give a message as ‘Stack is Overflow’.
Step 3: Pop element from stack along with display the stack contents.
if stack is empty give a message as ‘Stack is Underflow’.
Step 4: Check whether the stack contents are Palindrome or not.
Step 5: Stop.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 4
int stack[MAX], item;
/*PUSH FUNCTION*/
void push(int item)
{
if (top == (MAX-1))
printf("\n\nStack is Overflow");
else
{
stack[++top] = item;
status++;
}
}
/*POP FUNCTION*/
int pop()
{
int del;
if(top == -1)
printf("\n\nStack is Underflow");
else
{
del = stack[top--];
status--;
printf("\nPopped element is %d", del);
}
return del;
}
/* FUNCTION TO CHECK STACK IS PALINDROME OR NOT */
void palindrome()
{
int i, temp;
temp = status;
for(i=0; i<temp; i++)
{
if(stack[i] == pop(stack))
count++;
}
if(temp==count)
printf("\nStack contents are Palindrome");
else
printf("\nStack contents are not palindrome");
}
/*MAIN
PROGRAM*/ void
main()
{
clrscr();
do
{
printf("\n\n----MAIN MENU----\n");
printf("\n1. PUSH (Insert) in the Stack");
printf("\n2. POP (Delete) from the Stack");
printf("\n3. PALINDROME check using Stack");
printf("\n4. Exit (End the Execution)");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter a element to be pushed: ");
scanf("%d", &item);
push(item);
display();
break;
case 2: item=pop( );
display();
break;
case 3:
palindrome( );
break;
case 4:
exit(0);
break;
default:printf("\nEND OF EXECUTION");
}//end switch
}while (ch != 4);
getch();
}
SAMPLE OUTPUT:
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 1
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 2
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 2
Popped element is 1
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 1
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 2
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 3
Stack contents are not Palindrome
PROGRAM-4
Develop a Program in C for converting an Infix Expression to Postfix Expression. Program
should support for both parenthesized and free parenthesized expressions with the
operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric operands.
ABOUT THE EXPERIMENT:
Infix: Operators are written in-between their operands. Ex:
X + Y Prefix: Operators are written before their operands.
Ex: +X Y postfix: Operators are written after their operands.
Ex: XY+
Examples of Infix, Prefix, and Postfix
ALGORITHM:
Step 1: Start.
Step 2: Read an infix expression with parenthesis and without parenthesis.
Step 3: convert the infix expression to postfix expression.
Step 4: Stop
PROGRAM CODE:
#include<stdio.h>
#include<string.h>
case '*':
case '/': return 4;
case '^':
case '$': return 5;
default: return 8;
}
}
case '*':
case '/': return 3;
case '^':
case '$': return 6;
default: return 7;
}
}
s[++top] = '#'; j = 0;
for(i=0; i < strlen(infix); i++)
{
symbol = infix[i];
while(F(s[top]) > G(symbol))
{
postfix[j] = s[top--];
j++;
}
if(F(s[top]) != G(symbol))
s[++top] = symbol;
else
top--;
}
while(s[top] != '#')
{
postfix[j++] = s[top--];
}
postfix[j] = '\0';
}
void main()
{
char infix[20],
postfix[20]; clrscr();
printf("\nEnter a valid infix expression\n");
gets(infix);
infix_postfix(infix,postfix);
printf("\nThe infix expression is:\n");
printf ("%s",infix);
printf("\nThe postfix expression is:\n");
printf ("%s",postfix);
getch();
}
SAMPLE OUTPUT:
Enter a valid infix expression (a+(b-c)*d)
The infix expression is: (a+(b-c)*d)
The postfix expression is: abc-d*+
PROGRAM-5
Develop a Program in C for the following Stack Applications
a) Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,^
b) Solving Tower of Hanoi problem with n disks
ABOUT THE EXPERIMENT:
Stack Expression
Next character scanned is "*", which is an operator. Thus, we pop the top two elements from the stack
and perform the "*" operation with the two operands. The second operand will be the first element that is
popped.
Stack Expression
The value of the expression(2*3) that has been evaluated(6) is pushed into the stack.
Stack Expression
Next character scanned is "+", which is an operator. Thus, we pop the top two elements from the stack
and perform the "+" operation with the two operands. The second operand will be the first element that
is popped.
Stack Expression
The value of the expression(1+6) that has been evaluated(7) is pushed into the stack.
Stack Expression
Next character scanned is "4", which is added to the stack.
Stack Expression
Next character scanned is "-", which is an operator. Thus, we pop the top two elements from the stack
and perform the "-" operation with the two operands. The second operand will be the first element that is
popped.
Stack Expression
The value of the expression(7-4) that has been evaluated(3) is pushed into the stack.
Stack Expression
Now, since all the characters are scanned, the remaining element in the stack (there will be only one
element in the stack) will be returned.
End result:
Postfix String: 123*+4-
Result: 3
ALGORITHM:
Step 1: Start.
Step 2: Read the postfix/suffix expression.
Step 3: Evaluate the postfix expression based on the precedence of the operator.
Step 4: Stop.
PROGRAM CODE:
#include<stdio.h>
#include<math.h>
#include<string.h>
double compute(char symbol, double op1, double op2)
{
switch(symbol)
{
case '+': return op1 + op2;
case' -': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
case '$':
case '^': return pow(op1,op2);
default: return 0;
}
}
void main()
{
double s[20], res, op1, op2;
int top, i;
char postfix[20], symbol;
printf("\nEnter the postfix expression:\n");
gets(postfix);
top=-1;
for(i=0; <strlen(postfix); i++)
{
symbol = postfix[i];
if(isdigit(symbol))
s[++top] = symbol - '0';
else
{
op2 = s[top--];
op1 = s[top--];
res = compute(symbol, op1, op2);
s[++top] = res;
}
}
res = s[top--];
printf("\nThe result is : %f\n", res);
}
SAMPLE OUTPUT:
RUN1:
Enter the postfix expression: 23+
The result is: 5.000000
RUN2:
Enter the postfix expression: 23+7*
ALGORITHM:
Step 1: Start.
Step 2: Read N number of discs.
Step 3: Move all the discs from source to destination by using temp rod.
Step 4: Stop.
PROGRAM 5B:
#include<stdio.h>
void main()
{
int n;
printf("\nEnter the number of discs: \n");
scanf("%d", &n);
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d", (int)pow(2,n)-1);
}
SAMPLE OUTPUT:
PROGRAM-6
Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
ALGORITHM:
Step 1: Start.
Step 2: Initialize queue size to MAX.
Step3: Insert the elements into circular queue. If queue is full give a message as ‘queue is
overflow”
Step 4: Delete an element from the circular queue. If queue is empty give a message as‘queue is
underflow’.
Step 5: Display the contents of the queue.
Step 6: Stop.
PROGRAM CODE:
#include<stdio.h>
#define MAX 4
void insert()
{
if(count == MAX)
printf("\nQueue is Full");
else
{
rear = (rear + 1) % MAX;
q[rear]=item;
count++;
}
}
void del()
{
if(count == 0)
printf("\nQueue is Empty");
else
{
item=q[front];
printf("\nDeleted item is: %c",item);
if(front ==rear)
{
front=0;
rear=-1;
count=0;
}
else
{
front = (front + 1) % MAX;
count--;
}
}
}
void display()
{
int i, f=front, r=rear;
if(count == 0)
printf("\nQueue is Empty");
else
{
printf("\n Contents of Queue\n");
for(i=f; i<=r; i++)
{
printf("%c\t", q[i]);
f = (f + 1) % MAX;
}
}
}
void main()
{
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
SAMPLE OUTPUT:
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 1
Enter the character / item to be inserted: A
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 1
Enter the character / item to be inserted: B
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 1
Enter the character / item to be inserted: C
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 1
Enter the character / item to be inserted: D
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 3
Contents of Queue
A B C D
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 1
Enter the character / item to be inserted: F
Queue is Full
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 2
Deleted item is: A
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 2
Deleted item is: B
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 3
Contents of Queue
C D
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 1
Enter the character / item to be inserted: K
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 3
Contents of Queue
C D K
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 4
PROGRAM-7
Develop a menu driven Program in C for the following operations on Singly Linked List
(SLL) of Student Data with the fields: USN, Name, Programme, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
ABOUT THE EXPERIMENT:
Linked List is a linear data structure and it is very common data structure which consists of group
of nodes in a sequence which is divided in two parts. Each node consists of its own data and the
address of the next node and forms a chain. Linked Lists are used to create trees and graphs.
• They are a dynamic in nature which allocates the memory when required.
• Insertion and deletion operations can be easily implemented.
• Stacks and queues can be easily executed.
• Linked List reduces the access time.
• Linked lists are used to implement stacks, queues, graphs, etc.
• Linked lists let you insert elements at the beginning and end of the list.
• In Linked Lists we don’t need to know the size in advance.
Doubly Linked List: In a doubly linked list, each node contains two links the first link points to
the previous node and the next link points to the next node in the sequence.
Circular Linked List: In the circular linked list the last node of the list contains the address of
the first node and forms a circular chain.
ALGORITHM:
Step 1: Start.
Step 2: Read the value of N. (N student’s information)
Step 2: Create a singly linked list. (SLL)
Step 3: Display the status of SLL.
Step 4: Count the number of nodes.
Step 5: Perform insertion at front of list.
Step 6: Perform deletion at the front of the list.
Step 7: Perform insertion at end of the list.
Step 8: Perform deletion at the end of the list.
Step 9: Demonstrate how singly linked list can be used as stack.
Step 10: Demonstrate how singly linked list can be used as queue.
Step 11: Stop.
PROGRAM CODE:
#include<stdio.h>
struct student
{
char usn[10], name[30], branch[5];
int sem;
char phno[10];
struct student *next; //Self referential pointer.
};
typedef struct student NODE;
NODE *first=NULL,*temp=NULL,*newnode=NULL,*cur,*prev;
int count;
NODE* getnode( )
{
newnode=(NODE*)malloc(sizeof(NODE)); //Create first NODE
printf("\nEnter USN, Name, Branch, Sem, Ph.No\n");
scanf(“%s”,newnode->usn);
scanf(“%s”,newnode->name);
scanf(“%s”,newnode->branch);
scanf("%d",&newnode->sem);
scanf(“%s”,newnode->phno);
newnode->next=NULL; //Set next to NULL...
return newnode;
}
void insert_front()
{
newnode=getnode();
newnode->next=first;
first=newnode;
}
void delete_front()
{
if(first==NULL)
{
printf(“Linked list is empty\n”);
return;
}
if(first->next==NULL)
{
free(first);
first=NULL;
return;
}
temp=first;
first=first->next;
free(temp);
}
}
void insert_rear()
{
newnode=getnode();
if(first==NULL)
first=newnode;
else
{
temp=first;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void delete_rear()
{
if(first==NULL)
{
printf(“Linked list is empty\n”);
return;
}
if(first->next==NULL)
{
free(first);
first=NULL;
return;
}
cur=first;
while(cur->next!=NULL)
{
prev=cur;
cur=cur->next;
}
prev->next=NULL;
free(cur);
}
void display()
{
count=0;
if(first == NULL)
{
printf("List empty to display \n");
return;
}
temp=first;
printf("\n");
while (temp!= NULL)
{
printf("%s %s %s %d %s\n", temp->usn, temp->name, temp->branch, temp-
>sem,temp->phno );
temp = temp->next;
count++;
}
printf(" No of students = %d ", count);
void main()
{
int ch,n,i;
printf("-----------------MENU----------------------\n");
printf("\n 1 – Create \n 2 – Display \n 3 - Insert at the rear \n 4 – delete from rear");
printf("\n 5 - Insert at front \n 6 - delete from front \n 7 - exit\n");
printf("------------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("\n Enter no of students : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insert_front();
break;
case 2: display();break;
case 3: insert_rear(); break;
case 4: delete_rear(); break;
case 5: insert_front();break;
case 6: delete_front(); break;
case 7: exit(0);
default: printf("Invalid choice\n");
}
}
}
SAMPLE OUTPUT:
–---------------MENU----------------------
1– Create
2 - Display
3 - Insert at the rear
4 - deletefrom rear
5 - Insert at front
6 - delete from front
7 -exit
------------------------------------------------
Enter choice : 1
Enter no of students : 2
Enter USN, Name, Branch, Sem, Ph.No : 007 vijay CSE 3 121
Enter USN, Name, Branch, Sem, Ph.No : 100 yashas CSE 3 911
Enter choice : 2
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2
Enter choice : 3
Enter USN, Name, Branch, Sem, Ph.No : 001 raj CSE 3 111
Enter choice : 2
100 yashas CSE 3 911
007 vijay CSE 3 121
001 raj CSE 3 111
No of students = 3
Enter choice : 4
Enter choice : 2
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2
Enter choice : 5
Enter USN, Name, Branch, Sem, Ph.No : 003 harsh cse 3 111
Enter choice : 2
003 harsh cse 3 111
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 3
Enter choice : 6
Enter choice : 2
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2
Enter choice : 7
PROGRAM-8
Develop a menu driven Program in C for the following operations on Doubly Linked List
(DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo
f. Exit
ABOUT THE EXPERIMENT:
Doubly Linked List: In a doubly linked list, each node contains two links the first link points
to theprevious node and the next link points to the next node in the sequence.
In computer science, a doubly linked list is a linked data structure that consists of a set of
sequentially linked records called nodes. Each node contains two fields, called links, that
are references to the previous and to the next node in the sequence of nodes. The beginning and
ending nodes' previous and next links, respectively, point to some kind of terminator, typically a
sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the
list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists
formed from the same data items, but in opposite sequential orders.
A doubly linked list whose nodes contain three fields: an integer value, the link to the next node,
and the link to the previous node. The two node links allow traversal of the list in either direction.
While adding or removing a node in a doubly linked list requires changing more links than the
same operations on a singly linked list, the operations are simpler and potentially more efficient
(for nodes other than first nodes) because there is no need to keep track of the previous node during
traversal or no need to traverse the list to find the previous node, so that its link can be modified.
ALGORITHM:
Step 1: Start.
Step 2: Read the value of N. (N student’s information)
Step 3: Create a doubly linked list. DLL)
Step 4: Display the status of DLL.
Step 5: Count the number of nodes.
Step 6: Perform insertion at front of list.
Step 7: Perform deletion at the front of the list.
Step 8: Perform insertion at end of the list.
Step 9: Perform deletion at the end of the list.
Step 10: Demonstrate how doubly linked list can be used as double ended queue.
Step 11: Stop.
PROGRAM CODE:
#include<stdio.h>
struct emp
{
int ssn;
char name[20], dept[10], desig[15], phno[10];
int sal;
struct emp *left;
struct emp *right;
};
typedef struct emp NODE;
NODE * first=NULL,*temp=NULL,*newnode=NULL;
NODE* getnode()
{
newnode=(NODE*)malloc(sizeof(NODE)); //Create first NODE
printf("\nEnter SSN, Name, Dept. , Desig, Ph.No and salary\n");
scanf("%d",&(newnode->ssn));
scanf("%s",newnode->name);
scanf("%s",newnode->dept);
scanf("%s",newnode->desig);
scanf("%s",newnode->phno);
scanf("%d",&(newnode->sal));
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
void insert_front()
{
NODE * newnode=getnode();
newnode->right=first;
if(first!=NULL)
first->left=newnode;
first=newnode;
}
void delete_front()
{
if(first==NULL)
{
printf("Linked list is empty\n");
return;
}
temp=first;
if(first!=NULL)
first=first->right;
free(temp);
first->left=NULL;
}
void insert_rear()
{
newnode=getnode();
if(first==NULL)
first=newnode;
else
{
temp=first;
while(temp->right!=NULL)
{
temp=temp->right;
}
temp->right=newnode;
newnode->left=temp;
}
}
void delete_rear()
{
if(first==NULL)
{
printf("Linked list is empty\n");
return;
}
if(first->right==NULL)
{
free(first);
first=NULL;
return;
}
temp=first;
while(temp->right!=NULL)
{
temp=temp->right;
}
temp->left->right=NULL;
free(temp);
}
void display()
{
int count=0;
if(first == NULL)
{
printf("List is empty..! \n");
return;
}
temp=first;
printf("\n----EMPLOYEE DATA----\n");
printf("\nSSN\tNAME\t\Dept\tDesig\tPh.NO.\tSalary\n");
while (temp!= NULL)
{
printf("%d\t%s\t%s\t%s\t%s\t%d\t%s\n", temp->ssn, temp->name, temp->dept,
temp->desig,temp->phno, temp->sal );
temp = temp->right;
count++;
}
printf(" No of employees = %d ", count);
}
void main()
{
int ch,n,i;
while (1)
{
printf("-----------------MENU----------------------\n");
printf("\n 1–Create \n 2–Display \n 3–Insert at the rear \n 4–delete from rear");
printf("\n 5–Insert at front \n 6–delete from front \n 7–exit\n");
printf("--------------------------------------------\n");
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("\n Enter no of employees : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insert_rear();
break;
case 2: display(); break;
case 3: insert_rear(); break;
case 4: delete_rear(); break;
case 5: insert_front(); break;
case 6: delete_front(); break;
case 7: exit(0);
default: printf("Invalid choice\n");
}
}
}
SAMPLE OUTPUT:
–---------------MENU-------- 2 - Display
-------------- 3 - Insert at the rear
1– Create 4 - deletefrom rear
PROGRAM-9
Develop a Program in C for the following operationson Singly Circular Linked List
(SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in
POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
ABOUT THE EXPERIMENT:
Circular Linked List:
In the circular linked list the last node of the list contains the address of the first node and
forms a circular chain.
Polynomial:
A polynomial equation is an equation that can be written in the form. axn + bxn-1 + . . .
+ rx + s = 0, where a, b, . . . , r and s are constants. We call the largest exponent of x appearing in
a non-zero term of a polynomial the degree of that polynomial.
As with polynomials with one variable, you must pay attention to the rules of exponents
and
the order of operations so that you correctly evaluate an expression with two or more
variables. Evaluate x2 + 3y3for x = 7 and y = −2. Substitute the given values for x and
2 2
y. Evaluate4x y – 2xy + x – 7 for x = 3 and y = −1.
When a term contains both a number and a variable part, the number part is called the
"coefficient". The coefficient on the leading term is called the "leading" coefficient.
In the above example, the coefficient of the leading term is 4; the coefficient of the second
term is 3; the constant term doesn't have a coefficient.
ALGORITHM:
Step 1: Start.
Step 2: Read a polynomial.
Step 3: Represent the polynomial using singly circular linked list.
Step 3: Evaluate the given polynomial
Step 4: Read two polynomials and find the sum of the polynomials.
Step 5: Stop
#include<stdio.h>
#include<math.h>
#include<alloc.h>
struct node
{
int co,ex,ey,ez;
int flag;
struct node *link;
};
typedef struct node* poly;
poly attach(int c, int x, int y, int z, poly head)
{
poly temp, cur;
temp = (struct node *)malloc(sizeof(struct node));
temp->co = c;
temp->ex = x;
temp->ey = y;
temp->ez = z;
cur = head;
while(cur->link!= head)
cur = cur->link;
cur->link = temp;
temp->link = head;
return head;
}
poly read(poly head)
{
int ch=1;
int cf,x,y,z;
while(ch!=0)
{
printf("Enter Coeff and 3 exponents: ");
scanf("%d%d%d%d",&cf,&x,&y,&z);
head=attach(cf,x,y,z,head);
printf("\nIf you wish to continue press 1 otherwise press 0: ");
scanf("%d", &ch);
}
return head;
}
void display(poly head)
{
poly temp;
if(head->link == head)
{
printf("polynomial does not exists\n");
return;
}
temp =head->link;
while(temp!=head)
{
if(temp->co < 0)
printf("%d x^%d y^%d z^%d",temp->co,temp->ex,temp->ey,temp->ez);
else
printf("+%d x^%d y^%d z^%d",temp->co,temp->ex,temp->ey,temp->ez);
temp = temp->link;
}
}
b=sec->link;
while(b!=sec)
{
if(b->flag==0)
res=attach(b->co,b->ex,b->ey,b->ez,res);
b=b->link;
}
return res;
}
void evaluate(poly head)
{
poly h1;
int x, y, z;
int result=0;
h1=head->link;
printf("\nEnter values of x, y and z to evaluate:\n");
scanf("%d%d%d", &x, &y, &z);
while(h1!= head)
{
result = result +h1->co*pow(x,h1->ex)*pow(y,h1->ey)*pow(z,h1->ez);
h1=h1->link;
}
printf("\nPolynomial result is: %d", result);
}
void main()
{
int ch;
poly eval,first,sec,res;
first = (struct node *)malloc(sizeof(struct node));
sec = (struct node *)malloc(sizeof(struct node));
res = (struct node *)malloc(sizeof(struct node));
first->link = first;
sec->link = sec;
res->link = res;
printf("\n1.Evaluate polynomial\n2.Add 2 polynomials\n3.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the polynomial\n");
first = read(eval);
evaluate(eval);
break;
case 2: printf("Enter the first polynomial\n");
first = read(first);
printf("Enter the second polynomial\n");
sec = read(sec);
res = add(first, sec, res);
printf("\nFirst Polynomial equation is\n");
display(first);
printf("\nSecond Polynomial equation is\n");
display(sec);
printf("\nResultant polynomial equation is \n");
display(res);
break;
case 3: exit(0);
default:printf("\ninvalid choice!!!");
}
}
SAMPLE OUTPUT:
1. Evaluate polynomial
2. Add 2 polynomials
3. Exit
Enter your choice:1
Enter the polynomial
Enter Coeff and 3 exponents: 6 2 2 1
If you wish to continue press 1 otherwise press 0:1
Enter Coeff and 3 exponents: -4 0 1 5
If you wish to continue press 1 otherwise press 0:1
Enter Coeff and 3 exponents: 3 3 1 1
If you wish to continue press 1 otherwise press 0:1
Enter Coeff and 3 exponents: 2 1 5 1
If you wish to continue press 1 otherwise press 0:1
Enter Coeff and 3 exponents: -2 1 1 3
If you wish to continue press 1 otherwise press 0:0
Enter values of x, y and z to evaluate: 1 1 1
Polynomial result is:5
PROGRAM-10
Develop a menu driven Program in C for the following operations on Binary Search Tree
(BST) of Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
ABOUT THE EXPERIMENT:
A binary search tree (BST) is a tree in which all nodes follows the below mentioned
properties
• The left sub-tree of a node has key less than or equal to its parent node's key.
• The right sub-tree of a node has key greater than or equal to its parent node's key.
Thus, a binary search tree (BST) divides all its sub-trees into two segments; left sub-tree
and right sub-tree and can be defined as
left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
ALGORITHM:
Step 1: Start.
Step 2: Create a Binary Search Tree for N elements.
Step 3: Traverse the tree in inorder.
Step 4: Traverse the tree in preorder
Step 6: Traverse the tree in postorder.
Step 7: Search the given key element in the BST.
Step 8: Delete an element from BST.
Step 9: Stop
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
NODE *root=NULL,*cur,*prev;
if(node == NULL)
printf("\nElement not found");
else if(key < node->data)
{
search(node->left,key);
}
else if(key > node->data)
{
search(node->right,key);
}
else
printf("\nElement found”);
}
void inorder(NODE *node)
{
if(node != NULL)
{
inorder(node->left);
printf("%d\t", node->data);
inorder(node->right);
}
}
void preorder(NODE *node)
{
if(node != NULL)
{
printf("%d\t", node->data);
preorder(node->left);
preorder(node->right);
}
}
void postorder(NODE *node)
{
if(node != NULL)
{
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
}
}
{
printf("\n Tree empty");
return;
}
cur=root;
while(cur!=NULL)
{
if(item==cur->data)
break;
parent=cur;
cur=(item<cur->data)?cur->left:cur->right;
}
if(cur==NULL)
{
printf(“Item not found\n”);
return;
}
if(cur->left==NULL)
q=cur->right;
else if(cur->right==NULL)
q=cur->left;
else
{
suc=cur->right;
while(suc->left!=NULL)
suc=suc->left;
suc->left=cur->left;
q=cur-right;
}
if(parent==NULL)
{
root=q;
return;
}
if(cur==parent->left)
parent->left=q;
else
parent->right=q;
free(cur);
}
void main()
{
int item, ch, i, n;
while (1)
{
printf("\n1.Create\n2.Search\n3.Delete\n4.Inorder\n5.Preorder\n6.Postorder\n
7.Exit");
SAMPLE OUTPUT:
1. Create
2. Search
3. Delete
4. Inorder
5. Preorder
6. Postorder
7. Exit
Enter your choice: 1
Inorder Traversal:
2 5 6 7 8 9 14 15 24
Preorder Traversal:
6 5 2 9 8 7 15 14 24
Postorder Traversal:
2 5 7 8 14 24 15 9 6
Inorder Traversal:
2 5 6 7 8 9 14 24
Preorder Traversal:
6 5 2 9 8 7 24 14
PROGRAM-11
Develop a Program in C for the following operations on Graph (G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method
ABOUT THE EXPERIMENT:
Adjacency Matrix
BFS
Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data
structures. It starts at the tree root and explores the neighbor nodes first, before moving to the next
level neighbors.
Breadth First Search algorithm(BFS) traverses a graph in a breadth wards motion and uses a
queue to remember to get the next vertex to start a search when a dead end occurs in any
iteration.
void create()
{
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
scanf("%d", &a[i][j]);
vis[i]=0;
visited[i]=0;
}
}
}
void bfs()
{
void main()
{
int ch;
while(1)
{
printf("\n1.Create\n2.BFS\n3.Check graph connected or not(DFS)\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create();
break;
case 2: bfs();
break;
case 3: count=1;
for(i=1;i<=n;i++)
if(vis[i]==0)
dfs(i);
if(count==n)
printf("\nGraph is Connected");
else
printf("\nGraph is not Connected");
break;
default: exit(0);
}
}
}
SAMPLE OUTPUT:
Graph is Connected
PROGRAM-12
Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m
memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the
keys in K and addresses in L are Integers. Develop a Program in C that uses Hash function
H: K →L as H(K)=K mod m (remainder method), and implement hashing technique to map
a given key K to the address space L. Resolve the collision (if any) using linear probing.
Hashing: Hashing is a technique to convert a range of key values into a range of indexes
of an array. We're going to use modulo operator to get a range of key values. Consider an example
of hashtable of size 20, and following items are to be stored. Item are in (key, value) format.
Linear Probing:
As we can see, it may happen that the hashing technique used create already used index of the
array. In such case, we can search the next empty location in the array by looking into the next cell
Basic Operations
Following are basic primary operations of a hashtable which are following.
Search − search an element in a hashtable.
Insert − insert an element in a hashtable.
delete − delete an element from a hashtable.
ALGORITHM:
Step 1: Start.
Step 2: Given a File of N employee records with a set K of Keys (4-digit) which uniquely
determine the records in file F.
Step 3: Assume that file F is maintained in memory by a Hash Table(HT) of m memory locations
with L as the set of memory addresses (2-digit) of locations in HT.
Step 3: Let the keys in K and addresses in L are Integers
Step 4: Hash function H: K ®L as H(K)=K mod m (remainder method)
Step 5: Hashing as to map a given key K to the address space L, Resolve the collision (if any) is
using linear probing.
Step6: Stop.
PROGRAM CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 3
struct employee
{
int flag;
int eid;
char ename[15];
};
void insert()
{
int id,addr;
char name[15];
printf("\nEnter emp id: ");
scanf("%d",&id);
printf("\nEnter emp name: ");
scanf("%s",name);
addr=hash(id);
printf("addr=%d",addr);
if(emp[addr].flag==-1)
{
emp[addr].eid=id;
emp[addr].flag=1;
strcpy(emp[addr].ename,name);
}
else
{
printf("\nCollision detected..");
addr=linear_prob(addr);
if(addr!=-1)
{
emp[addr].eid=id;
emp[addr].flag=1;
strcpy(emp[addr].ename,name);
}
else
{
printf("\nHash Table is full.. Cannot insert");
return;
}
}
}
void display()
{
int i;
printf("\nThe hash table is:\n");
printf("\nHTKey\tEmpID\tEmpName");
for(i=0;i<MAX;i++)
{
if(emp[i].flag!=-1)
{
printf("\n%d\t%d\t%s",i,emp[i].eid,emp[i].ename);
continue;
}
}
}
void main()
{
int i,ch;
printf("\nCollision handling by linear probing");
for(i=0;i<MAX;i++)
{
emp[i].flag = -1;
}
for(;;)
{
printf("\n1.Insert\n2.Display\n");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: getdata();
break;
case 2: display();
break;
default: exit(0);
}
}
}
SAMPLE OUTPUT:
Collision handling by linear probing Enter emp name: pqr
1. Insert addr=2
2.Display
Enter your choice: 1
Enter your choice: 1 Enter emp id: 2
Enter emp id: 123 Enter emp name: abc
Enter emp name: xyz addr=2
addr=0 Collision detected..
Hash Table is full.. Cannot insert
Enter your choice: 1
Enter emp id: 3 Enter your choice: 2
Enter emp name: jkl The hash table is:
addr=0 HTKey EmpID EmpName
Collision detected.. 0 123 xyz
1 3 jkl
Enter your choice: 1 2 2 pqr
Enter emp id: 2
VIVA QUESTIONS
1. What is a Register?
Ans- A register is a small amount of memory within the CPU that is used to temporarily store instructions
and data.
2. An _________ data type is a keyword of a programming language that specifies the amount of
memory needed to store data and the kind of data that will be stored in that memory location?
Ans-abstract
Integer, Floating-Type, Character & Boolean are the four different data type groups
Explanation: You determine the amount of memory to reserve by determining the appropriate abstract data
type group to use and then deciding which abstract data type within the group is right for the data. The
different abstract data type groups are Integer, Floating-Type, Character & Boolean
4.Which of the following abstract data types are NOT used by Integer Abstract Data type group?
A) Short
B) Int
C) float
D) long
Explanation: The integer abstract data type group consists of four abstract data types used to reserve
memory to store whole numbers: byte, short, int , and long
The answer is the void pointer. The heterogeneous linked list contains different data types in it's nodes and
we need a link, pointer, to connect them. Since we can't use ordinary pointers for this, we use the void
pointer. Void pointer is a generic pointer type, and capable of storing pointer to any type.
6.What is the minimum number of queues needed to implement the priority queue?
Two. One queue is used for the actual storing of data, and the other one is used for storing the priorities.
The answer is Stack. Stack has the LIFO (Last In First Out) property; it remembers it's ‘caller’. Therefore,
it knows to whom it should return when the function has to return. On the other hand, recursion makes use
of the system stack for storing the return addresses of the function calls.
Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent
iterative procedures are written explicit, stack is to be used.
8. What are some of the applications for the tree data structure?
9. Which data structures algorithm used in solving the eight Queens problem?
Backtracking
If the "pivotal value", or the "height factor", is greater than one or less than minus one.
11. There are 8, 15, 13, and 14 nodes in four different trees. Which one of them can form a full
binary tree?
Dept. of ISE, DSATM 2023-2024 Page 62
Data Structures Laboratory BCSL305
The answer is the tree with 15 nodes. In general, there are 2^n-1 nodes in a full binary tree.
By the method of elimination:
Full binary trees contain odd number of nodes, so there cannot be full binary trees with 8 or 14 nodes.
Moreover, with 13 nodes you can form a complete binary tree but not a full binary tree. Thus, the correct
answer is 15.
13. List out the areas in which data structures are applied extensively?
Answer: The names of areas are:
a. Compiler Design,
b. Operating System,
c. Database Management System,
d. Statistical analysis package,
e. Numerical Analysis,
f. Graphics,
g. Artificial Intelligence,
h. Simulation
14. What are the major data structures used in the following areas: RDBMS, Network data model
& Hierarchical data model.
The major data structures used are as follows:
a. RDBMS - Array (i.e. Array of structures)
b. Network data model - Graph
c. Hierarchical data model - Trees
d.
15. If you are using C language to implement the heterogeneous linked list, what pointer type will
you use?
The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to
connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void
pointer is capable of storing pointer to any type as it is a generic pointer type.
18. What are the notations used in Evaluation of Arithmetic Expressions using prefix and postfix
forms?
Polish and Reverse Polish notations.
Dept. of ISE, DSATM 2023-2024 Page 63
Data Structures Laboratory BCSL305
19. Convert the expression ((A + B) * C - (D - E) ^ (F + G)) to equivalent Prefix and Postfix
notations.
Prefix Notation: ^ - * +ABC - DE + FG
Postfix Notation: AB + C * DE - - FG + ^
20. How many null branches are there in a binary tree with 20 nodes?
Answer: 21
Let us take a tree with 5 nodes (n=5)
i ii iii iv v
In general:
If there are n nodes, there exist 2n-n different trees.
24. List out few of the applications that make use of Multilinked Structures?
Answer: The applications are listed below:
Dept. of ISE, DSATM 2023-2024 Page 64
Data Structures Laboratory BCSL305
• Sparse matrix,
• Index generation.
26. What is the type of the algorithm used in solving the 8 Queens problem?
Answer: Backtracking
28. What is the bucket size, when the overlapping and collision occur at same time?
Answer: One. If there is only one entry possible in the bucket, when the collision occurs, there is no way to
accommodate the colliding value. This results in the overlapping of values.
29. Traverse the given tree using Inorder, Preorder and Postorder traversals.
Answer:
• Inorder : D H B E A F C I G J
• Preorder: A B D H E C F G I J
• Postorder: H D E B F I J G C A
30. There are 8, 15, 13, 14 nodes were there in 4 different trees. Which of them could have formed
a full binary tree?
Answer: 15.
In general:
There are 2n-1 nodes in a full binary tree.
By the method of elimination:
Full binary trees contain odd number of nodes. So there cannot be full binary trees with 8 or 14 nodes, so
rejected. With 13 nodes you can form a complete binary tree but not a full binary tree. So the correct answer
is 15.
31. In the given binary tree, using array you can store the node 4 at which location?
Answer: At location 6
1 2 3 - - 4 - - 5
Root LC1 RC1 LC2 RC2 LC3 RC3 LC4 RC4
where LCn means Left Child of node n and RCn means Right Child of node n
65 45 50 80L 85 60 55R 75 70
65 45 50 55 85L 60R 80 75 70
65 45 50 55 60R 85L 80 75 70
When the L and R pointers cross each other the pivot value is interchanged with the value at right pointer. If
the pivot is changed it means that the pivot has occupied its original position in the sorted order (shown in
bold italics) and hence two different arrays are formed, one from start of the original array to the pivot
position-1 and the other from pivot position+1 to end.
60L 45 50 55R 65 85L 80 75 70R