Data Structures Lab Manual 2
Data Structures Lab Manual 2
Data Structures Lab Manual 2
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
1
Data structures Marri Laxman Reddy of Institute of Technology and Management
CERTIFICATE
This is to certify that this manual is a bonafide record of practical work in the data structures in First
Semester of II year B.Tech (CSE) programme during the academic year 2019-20. This book is
prepared by Mr.Sivaramakrishna (Asst.Professor) Department of Computer Science and
Engineering.
2
Data structures Marri Laxman Reddy of Institute of Technology and Management
INDEX
S.No Content Page No:
Preface 5
Acknowledgement 6
General Instructions 7
Institute Vision and Mission 8
Department Vision, Mission, Programme Educational Objectives and Specific 9
Outcomes
Programme Outcomes 10-11
Course Structure, Objectives & Outcomes 11
Experiments Learning Outcomes 12
1 Write c program to implement single linked list 13
PREFACE
3
Data structures Marri Laxman Reddy of Institute of Technology and Management
This book “data structures” lab manual is intended to teach the design and analysis of basic data
structures and their implementation in an object-oriented language. Readers of this book need only be
familiar with the basic syntax of Java and similar languages. The “data structures Concepts” is
increasingly becoming the default choice of the IT industry especially industries involved in software
development at system level. Therefore, for proper development of “Object Oriented Programming ”
skills among the students this practical manual has been prepared. The manual contains the exercise
programs and their solution for easy & quick understanding of the students. We hope that this practical
manual will be helpful for students of Computer Science & Engineering for understanding the subject
from the point of view of applied aspects. There is always scope for improvement in the manual. We
would appreciate to receive valuable suggestions from readers and users for future use.
By
Sivaramakrishna
K.Shalini,
sravani.
4
Data structures Marri Laxman Reddy of Institute of Technology and Management
ACKNOWLEDGEMENT
It was really a good experience, working with “data structures lab”. First we would like to thank
Mr.K.Abdul Basith, Assoc.Professor, HOD of Department of Computer Science and Engineering,
Marri Laxman Reddy Institute of technology & Management for his concern and giving the technical
support in preparing the document.
We are deeply indebted and gratefully acknowledge the constant support and valuable patronage of
Dr.R.Kotaih, Director, Marri Laxman Reddy Institute of technology & Management for giving us this
wonderful opportunity for preparing the data structures laboratory manual.
We express our hearty thanks to Dr.K.Venkateswara Reddy, Principal, Marri Laxman Reddy Institute
of technology & Management, for timely corrections and scholarly guidance.
At last, but not the least I would like to thanks the entire CSE Department faculties those who had
inspired and helped us to achieve our goal.
By
Sivaramakrishna
5
Data structures Marri Laxman Reddy of Institute of Technology and Management
GENERAL INSTRUCTIONS
1. Students are instructed to come to Datasrutures laboratory on time. Late comers arenot entertained in
the lab.
2. Students should be punctual to the lab. If not, the conducted experiments will not be repeated.
3. Students are expected to come prepared at home with the experiments which are going to be performed.
4. Students are instructed to display their identity cards before entering into the lab.
6. Any damage/loss of system parts like keyboard, mouse during the lab session, it is student’s
responsibility and penalty or fine will be collected from the student.
7. Students should update the records and lab observation books session wise. Before leaving the lab the
student should get his lab observation book signed by the faculty.
8. Students should submit the lab records by the next lab to the concerned faculty members in the staffroom
for their correction and return.
9. Students should not move around the lab during the lab session.
10. If any emergency arises, the student should take the permission from faculty member concernedin
written format.
11. The faculty members may suspend any student from the lab session on disciplinary grounds.
12. Never copy the output from other students. Write down your own outputs.
6
Data structures Marri Laxman Reddy of Institute of Technology and Management
VISION
To establish as an ideal academic institutions in the service of the nation, the world and the humanity
by graduating talented engineers to be ethically strong, globally competent by conducting high quality
research, developing breakthrough technologies, and disseminating and preserving technical
knowledge.
MISSION
To fulfill the promised vision through the following strategic characteristics and aspirations:
Contemporary and rigorous educational experiences that develop the engineers and managers.
An atmosphere that facilitates personal commitment to the educational success of students in an
environment that values diversity and community.
Undergraduate programs that integrate global awareness, communication skills and team building.
Education and Training that prepares students for interdisciplinary engineering research and
advanced problem solving abilities.
7
Data structures Marri Laxman Reddy of Institute of Technology and Management
VISION
The Computer science Engineering department strives to impart quality education by extracting the
innovative skills of students and to face the challenges in latest technological advancements and to serve the
society.
MISSION
Provide quality education and to motivate students towards professionalism
Address the advanced technologies in research and industrial issues
PEO-I solving computer science engineering problems in different circumstances PEO-II Pursue higher
education and research for professional development.
PSO1. UNDERSTANDING: Graduates will have an ability to describe, analyze, and solve problems using
mathematics and systematic problem-solving techniques.
PSO2. ANALYTICAL SKILLS: Graduates will have an ability to design a system, component, or process
to meet desired needs within realistic constraints such as economic, environmental, social, political, ethical,
health and safety, manufacturability, and sustainability.
PSO3. BROADNESS: Graduates will have a broad education necessary to understand the impact of
engineering solutions in a global, economic, and societal context.
8
Data structures Marri Laxman Reddy of Institute of Technology and Management
9
Data structures Marri Laxman Reddy of Institute of Technology and Management
i) Recognition of the need for, and an ability to engage in continuing professional development and
life-long learning
Graduates should show that they appreciate the need for further education and self improvement, understand
the value of professional licensure the necessity of continuing professional developments, and the value of
membership in appropriate professional organizations.
k) An ability to use the techniques, skills, and modern engineering tools necessary for engineering
practice
Graduates should have ability to use practical methods readily and effectively in the performance of
engineering analysis and design. Graduates should be able to select and use modern engineering tools used
by practicing engineers, including computer software such as computer aided drawing (CAD)
l) An ability to apply design and development principles in the construction of software and hardware
systems of varying complexity
Computer science Graduates should have ability to design and develop principles involved in
construction of different structures like buildings, shopping complexes, roads, water structures and to
analyse the stability of structures using different softwares like stadpro. Studs etc.
10
Data structures Marri Laxman Reddy of Institute of Technology and Management
COURSE STRUCTURE
Data structures lab will have a continuous evaluation during 7th semester for 25 sessional marks and 50
end semester examination marks.
Out of the 25 marks for internal evaluation, day-to-day work in the laboratory shall be evaluated for 15
marks and internal practical examination shall be evaluated for 10 marks conducted by the laboratory
teacher concerned.
The end semester examination shall be conducted with an external examiner and internal examiner. The
external examiner shall be appointed by the principal / Chief Controller of examinations
COURSE OBJECTIVE
To write programs in java to solve problems using divide and conquer strategy.
To write programs in java to solve problems using backtracking strategy.
To write programs in java to solve problems using greedy and dynamic programming techniques.
COURSE OUTCOME
Ability to write programs in java to solve problems using algorithm design techniques
such as Divide and Conquer, Greedy, Dynamic programming, and Backtracking.
HODs and faculty members are directed to submit the soft copies of the course files in the director’s
office/L.M.S at the earliest, other wise it will be treated as not submitted
11
Data structures Marri Laxman Reddy of Institute of Technology and Management
EXPERIMENT-1
Aim: Write a program that uses functions to perform the following operations on single linked list.
i)Creation ii) Insertion iii)Deletion iv)Traversal
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
int data;
int main() {
int option = 0;
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("4 : Count Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option:");
scanf("%d", &option);
switch (option) {
case 1:
insert();
break;
case 2:
12
Data structures Marri Laxman Reddy of Institute of Technology and Management
delete();
break;
case 3:
display();
break;
case 4:
count();
break;
default:
break;
}
}
return 0;
}
void insert() {
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node->value = data;
if (first_node == 0) {
first_node = temp_node;
} else {
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete() {
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
13
Data structures Marri Laxman Reddy of Institute of Technology and Management
void display() {
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
14
Data structures Marri Laxman Reddy of Institute of Technology and Management
int count() {
int count = 0;
temp_node = first_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
Sample Output:
Singly Linked List Example - All Operations
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
100
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
200
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Options
1 : Insert into Linked List
2 : Delete from Linked List
16
Data structures Marri Laxman Reddy of Institute of Technology and Management
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:4
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:2
Deleted Successfully
Options
17
Data structures Marri Laxman Reddy of Institute of Technology and Management
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:2
Invalid Position
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:
5
18
Data structures Marri Laxman Reddy of Institute of Technology and Management
viva-questions
2. Write a program that uses functions to perform the following operations on doubly linked list:
struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void insert1();
void insert2();
void insert3();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
void delete();
int count = 0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
20
Data structures Marri Laxman Reddy of Institute of Technology and Management
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
insert3();
break;
case 4:
delete();
break;
case 5:
traversebeg();
break;
case 6:
temp2 = h;
if (temp2 == NULL)
printf("\n Error : List empty to display ");
else
{
printf("\n Reverse order of linked list is : ");
traverseend(temp2->n);
}
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
exit(0);
default:
printf("\n Wrong choice menu");
}
}
}
21
Data structures Marri Laxman Reddy of Institute of Technology and Management
temp->n = data;
count++;
}
/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
/* To insert at end */
void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
22
Data structures Marri Laxman Reddy of Institute of Technology and Management
}
if ((h == NULL) && (pos == 1))
{
create();
h = temp;
temp1 = h;
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}
}
/* To delete an element */
void delete()
{
int i = 1, pos;
23
Data structures Marri Laxman Reddy of Institute of Technology and Management
}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */
if (i == 1)
h = temp2->next;
printf("\n Node deleted");
free(temp2);
}
count--;
}
if (temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");
if (temp2 == NULL)
24
Data structures Marri Laxman Reddy of Institute of Technology and Management
{
printf("\n Error : List empty to search for data");
return;
}
printf("\n Enter value to search : ");
scanf("%d", &data);
while (temp2 != NULL)
{
if (temp2->n == data)
{
printf("\n Data found in %d position",count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf("\n Error : %d not found in list", data);
}
temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;
}
temp2 = h;
temp4 = h;
25
Data structures Marri Laxman Reddy of Institute of Technology and Management
if (temp2 == NULL)
{
printf("\n List empty to sort");
return;
}
1 - Insert at beginning
2 - Insert at end
3 - Insert at position i
4 - Delete at i
5 - Display from beginning
6 - Display from end
7 - Search for element
8 - Sort the list
9 - Update an element
10 - Exit
Enter choice : 1
Enter choice : 2
Enter choice : 4
Node deleted
Enter choice : 1
Enter choice : 3
Enter choice : 4
26
Data structures Marri Laxman Reddy of Institute of Technology and Management
Enter choice : 1
Enter choice : 3
Enter choice : 4
Node deleted
Enter choice : 7
27
Data structures Marri Laxman Reddy of Institute of Technology and Management
3. Write a program that uses functions to perform the following operations on circular linked list:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
void create();
void ins_at_beg();
void ins_at_pos();
void del_at_beg();
void del_at_pos();
void traverse();
void search();
void sort();
void update();
void rev_traverse(struct node *p);
void main()
{
int ch;
void ins_at_beg()
{
x = head;
y = (struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d", &y->data);
while (x->link != head)
{
x = x->link;
}
x->link = y;
y->link = head;
head = y;
}
void ins_at_pos()
{
struct node *ptr;
int c = 1, pos, count = 1;
}
while (c < pos)
{
z = x;
x = x->link;
c++;
}
y->link = x;
z->link = y;
}
void del_at_beg()
{
if (head == NULL)
printf("\n List is empty");
else
{
x = head;
y = head;
while (x->link != head)
{
x = x->link;
}
head = y->link;
x->link = head;
free(y);
}
}
void del_at_pos()
{
if (head == NULL)
printf("\n List is empty");
else
{
int c = 1, pos;
printf("\n Enter the position to be deleted:");
scanf("%d", &pos);
x = head;
while (c < pos)
{
y = x;
x = x->link;
c++;
}
y->link = x->link;
free(x);
}
}
void traverse()
{
if (head == NULL)
printf("\n List is empty");
else
{
x = head;
while (x->link != head)
{
printf("%d->", x->data);
x = x->link;
}
printf("%d", x->data);
}
}
void search()
{
int search_val, count = 0, flag = 0;
printf("\nenter the element to search\n");
scanf("%d", &search_val);
if (head == NULL)
printf("\nList is empty nothing to search");
else
{
x = head;
while (x->link != head)
{
if (x->data == search_val)
{
printf("\nthe element is found at %d", count);
flag = 1;
break;
}
count++;
x = x->link;
}
if (x->data == search_val)
{
printf("element found at postion %d", count);
}
if (flag == 0)
{
printf("\nelement not found");
}
}
}
void sort()
33
Data structures Marri Laxman Reddy of Institute of Technology and Management
{
struct node *ptr, *nxt;
int temp;
if (head == NULL)
{
printf("empty linkedlist");
}
else
{
ptr = head;
while (ptr->link != head)
{
nxt = ptr->link;
while (nxt != head)
{
if (nxt != head)
{
if (ptr->data > nxt->data)
{
temp = ptr->data;
ptr->data = nxt->data;
nxt->data = temp;
}
}
else
{
break;
}
nxt = nxt->link;
}
ptr = ptr->link;
}
}
}
if (head == NULL)
{
printf("\n empty list");
}
else
{
printf("enter the value to be edited\n");
scanf("%d", &search_val);
fflush(stdin);
printf("enter the value to be replace\n");
scanf("%d", &replace_val);
34
Data structures Marri Laxman Reddy of Institute of Technology and Management
ptr = head;
while (ptr->link != head)
{
if (ptr->data == search_val)
{
ptr->data = replace_val;
flag = 1;
break;
}
ptr = ptr->link;
}
if (ptr->data == search_val)
{
ptr->data = replace_val;
flag = 1;
}
if (flag == 1)
{
printf("\nUPdate sucessful");
}
else
{
printf("\n update not successful");
}
}
}
if (head == NULL)
{
printf("empty linked list");
}
else
{
if (p->link != head)
{
i = p->data;
rev_traverse(p->link);
printf(" %d", i);
}
if (p->link == head)
{
printf(" %d", p->data);
}
}
}
$ cc circular_singly_ll.c
$ a.out
1.Creation
35
Data structures Marri Laxman Reddy of Institute of Technology and Management
2.Insertion at beginning
3.Insertion at remaining
4.Deletion at beginning
5.Deletion at remaining
6.traverse
7.Search
8.sort
9.update
10.Exit
Enter your choice:6
List is empty
Enter your choice:5
List is empty
Enter your choice:9
empty list
Enter your choice:7
enter the element to search
12
List is empty nothing to search
Enter your choice:1
Enter the data:10
If you wish to continue press 1 otherwise 0:0
Enter your choice:3
Enter the data:20
Enter the position to be inserted:5
OUT OF BOUND
Enter your choice:2
Enter the data:12
Enter your choice:6
12->10
Enter your choice:3
Enter the data:13
Enter the position to be inserted:3
Enter your choice:3
Enter the data:14
Enter the position to be inserted:4
Enter your choice:6
12->10->13->14
Enter your choice:3
Enter the data:24
Enter the position to be inserted:4
Enter your choice:6
12->10->13->24->14
Enter your choice:3
Enter the data:10
Enter the position to be inserted:100
OUT OF BOUND
Enter your choice:4
Enter your choice:6
10->13->24->14
Enter your choice:5
Enter the position to be deleted:4
Enter your choice:6
10->13->24
Enter your choice:5
Enter the position to be deleted:2
Enter your choice:6
10->24
36
Data structures Marri Laxman Reddy of Institute of Technology and Management
38
Data structures Marri Laxman Reddy of Institute of Technology and Management
/*Begin of push*/
void push()
{
int pushed_item;
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
printf("Enter the item to be pushed in stack : ");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top] = pushed_item;
}
}
/*End of push*/
/*Begin of pop*/
void pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
{
printf("Popped element is : %d\n",stack_arr[top]);
top=top-1;
}
}
/*End of pop*/
/*Begin of display*/
void display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack_arr[i] );
}
}
/*End of display*/
/*Begin of main*/
main()
{
int choice;
do{
39
Data structures Marri Laxman Reddy of Institute of Technology and Management
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}}while(choice!=4);
}
/*End of main*/
Sample Output:
STACK IMPLEMENTATION PROGRAM
1. Push
2. Pop
3. Size
4. Exit
1. Push
2. Pop
3. Size
4. Exit
1. Push
2. Pop
3. Size
4. Exit
40
Data structures Marri Laxman Reddy of Institute of Technology and Management
1. Push
2. Pop
3. Size
4. Exit
1. Push
2. Pop
3. Size
4. Exit
1. Push
2. Pop
3. Size
4. Exit
1. Push
2. Pop
3. Size
4. Exit
1. Push
2. Pop
3. Size
41
Data structures Marri Laxman Reddy of Institute of Technology and Management
4. Exit
1. Push
2. Pop
3. Size
4. Exit
1. Push
2. Pop
3. Size
4. Exit
ii) Pointers:
42
Data structures Marri Laxman Reddy of Institute of Technology and Management
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
43
Data structures Marri Laxman Reddy of Institute of Technology and Management
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
44
Data structures Marri Laxman Reddy of Institute of Technology and Management
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
45
Data structures Marri Laxman Reddy of Institute of Technology and Management
}
free(top1);
top = NULL;
Sample Output:
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
90 78 56
Enter choice : 7
Stack is empty
Enter choice : 5
viva-questions
47
Data structures Marri Laxman Reddy of Institute of Technology and Management
i) Arrays
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
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:
exit(1);
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 */
48
Data structures Marri Laxman Reddy of Institute of Technology and Management
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() */
Sample Output:
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 15
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 20
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
49
Data structures Marri Laxman Reddy of Institute of Technology and Management
ii) Pointers:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
50
Data structures Marri Laxman Reddy of Institute of Technology and Management
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
51
Data structures Marri Laxman Reddy of Institute of Technology and Management
temp->ptr = NULL;
rear = temp;
}
count++;
}
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
52
Data structures Marri Laxman Reddy of Institute of Technology and Management
Sample output:
1 - Enque
2 - Deque
3 - Front element
4 - Empty
5 - Exit
6 - Display
7 - Queue size
Enter choice : 1
Enter data : 14
Enter choice : 1
Enter data : 85
Enter choice : 1
Enter data : 38
Enter choice : 3
Front element : 14
Enter choice : 6
14 85 38
Enter choice : 7
Queue size : 3
Enter choice : 2
Dequed value : 14
Enter choice : 6
85 38
Enter choice : 7
Queue size : 2
Enter choice : 4
Queue not empty
Enter choice : 5
viva-questions
54
Data structures Marri Laxman Reddy of Institute of Technology and Management
6. Write a program that implements the following sorting methods to sort a given list of
integers in ascending order
i) Bubble sort ii) Selection sort iii) Insertion sort
i) Bubble sort :
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
swap = array[d];
55
Data structures Marri Laxman Reddy of Institute of Technology and Management
array[d] = array[d+1];
array[d+1] = swap;
printf("%d\n", array[c]);
return 0;
Sample Output:
56
Data structures Marri Laxman Reddy of Institute of Technology and Management
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
position = c;
57
Data structures Marri Laxman Reddy of Institute of Technology and Management
position = d;
if (position != c)
swap = array[c];
array[c] = array[position];
array[position] = swap;
printf("%d\n", array[c]);
return 0;
Sample Output:
58
Data structures Marri Laxman Reddy of Institute of Technology and Management
#include <stdio.h>
int main()
int n, array[1000], c, d, t;
scanf("%d", &n);
scanf("%d", &array[c]);
d = c;
59
Data structures Marri Laxman Reddy of Institute of Technology and Management
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
printf("%d\n", array[c]);
return 0;
Sample Output:
sorted elements:
1 2 3 4 6 8 9
viva-questions
29. When the value is passed in the function is called call by value?
30 .Explain call by reference?
31 .When the address of the value is passed is called call by reference.
32 .Why we used keyword „break‟?
33 .When break is encountered inside any loop, control automatically passes to the first statement after the
loop.
34 .What is stack?
35 .What are the operations performed on stack? 36
What is push operation?
61
Data structures Marri Laxman Reddy of Institute of Technology and Management
7. Write a program that use both recursive and non recursive functions to perform the following searching operations for a
Key value in a given list of integers:
i) Linear search
#include <stdio.h>
#define MAX_LEN 10
void l_search_recursive(int l[],int num,int ele);
void l_search_nonrecursive(int l[],int num,int ele);
void read_list(int l[],int num);
void print_list(int l[],int num);
int main()
{
int l[MAX_LEN], num, ele;
int ch;
printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Linear Search using Recursion method");
printf("\n[2] Linear Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);
if(ch<=2 & ch>0)
62
Data structures Marri Laxman Reddy of Institute of Technology and Management
{
printf("Enter the number of elements :");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nElement you want to search:\n\n");
scanf("%d",&ele);
switch(ch)
{
case 1:
printf("\n**Recursion method**\n");
l_search_recursive(l,num,ele);
break;
case 2:
printf("\n**Non-Recursion method**\n");
l_search_nonrecursive(l,num,ele);
break;
}
}
}
//end main
//Non-Recursive method
//Recursive method
}
}
getch();
}
Sample Output:
======================================================
MENU
======================================================
**Recursion method**
64
Data structures Marri Laxman Reddy of Institute of Technology and Management
#include <stdio.h>
#define MAX_LEN 10
/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else
if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}
/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}
/*main function*/
main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;
printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
{
printf("Element is not found");
}
else
{
printf("Element is found at %d position",pos);
}
//getch();
break;
Sample Output:
======================================================
MENU
=====================================================
66
Data structures Marri Laxman Reddy of Institute of Technology and Management
12 22 32 42 52
42
Recursive method:
Element is found at 3 position
viva-questions
1. Where is linear searching used?
2. What is the best case for linear search?
3. What is the worst case for linear search?
4. What is the best case and worst case complexity of ordered linear search?
5. What is the disadvantage of linear search?
6. Is there any difference in the speed of execution between linear serach(recursive) vs linear
search(lterative)?
7. Is the space consumed by the linear search(recursive) and linear search(iterative) same?
8. What is the worst case runtime of linear search(recursive) algorithm?
9. Linear search(recursive) algorithm used in ?
10. The array is as follows: 1,2,3,6,8,10. At what time the element 6 is found? (By using linear
search(recursive) algorithm)
11. The array is as follows: 1,2,3,6,8,10. Given that the number 17 is to be searched. At which call it tells that
there’s no such element? (By using linear search(recursive) algorithm)
12. What is the best case runtime of linear search(recursive) algorithm on an ordered set of elements?
13. Can linear search recursive algorithm and binary search recursive algorithm be performed on an unordered
list?
14. What is the recurrence relation for the linear search recursive algorithm?
15. What is the advantage of recursive approach than an iterative approach?
16. Given an input arr = {2,5,7,99,899}; key = 899; What is the level of recursion?
17. Given an array arr = {45,77,89,90,94,99,100} and key = 99; what are the mid values(corresponding array
elements) in the first and second levels of recursion?
18. What is the worst case complexity of binary search using recursion?
19. What is the average case time complexity of binary search using recursion?
20. What are the applications of binary search?
67
Data structures Marri Laxman Reddy of Institute of Technology and Management
68
Data structures Marri Laxman Reddy of Institute of Technology and Management
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
69
Data structures Marri Laxman Reddy of Institute of Technology and Management
newNode->right = NULL;
return newNode;
}
int main(){
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
Sample Output:
Inorder traversal
70
Data structures Marri Laxman Reddy of Institute of Technology and Management
Preorder traversal
Postorder traversal
viva-questions
1.What is the space complexity of the in-order traversal in the recursive fashion? (d is the tree depth and n is the
number of nodes
2.In postorder traversal of binary tree right subtree is traversed before visiting .
3. What is the possible number of binary trees that can be created with 3 nodes, giving the sequence N, M, L when
traversed in post-order?
4. The post-order traversal of a binary tree is O P Q R S T. Then possible pre-order traversal will be?
5. A binary search tree contains values 7, 8, 13, 26, 35, 40, 70, 75. Which one of the following is a valid post-order
sequence of the tree provided the pre-order sequence as 35, 13, 7, 8, 26, 70, 40 and 75?
6. Which of the following pair’s traversals on a binary tree can build the tree uniquely?
7.A full binary tree can be generated using _
8.The maximum number of nodes in a tree for which post-order and pre-order traversals may be equal is
9. The steps for finding post-order traversal are .
10. The pre-order and in-order are traversals of a binary tree are T M L N P O Q and L M N T O P Q. Which of
following is post-order traversal of the tree?
11 For a binary tree the first node visited in in-order and post-order traversal is same. Find the postorder traversal
of the binary tree shown below.
12 What is the space complexity of the post-order traversal in the recursive fashion? (d is the tree depth and n is
the number of nodes)
13 To obtain a prefix expression, which of the tree traversals is used?
14 What is the space complexity of the in-order traversal in the recursive fashion? (d is the tree depth and n is the
number of nodes)
15 What is the time complexity of level order traversal?
16 Which of the following graph traversals closely imitates level order traversal of a binary tree?
17 In a binary search tree, which of the following traversals would print the numbers in the ascending order?
18. What is common in three different types of traversals (Inorder, Preorder and Postorder)?
19. The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The postorder
traversal of the binary tree is:
20. Which traversal of tree resembles the breadth first search of the graph?
21. Which of the following tree traversal uses a queue data structure?
71
Data structures Marri Laxman Reddy of Institute of Technology and Management
22. Which of the following cannot generate the full binary tree?
23. The value returned by the function DoSomething when a pointer to the root of a non-empty tree is passed as
argument is (GATE CS 2004)
24. The array representation of a complete binary tree contains the data in sorted order. Which traversal of the tree
will produce the data in sorted form?
25. Which of the following sequences denotes the post order traversal sequence of the given tree?
a
/
b e
/ /
c d f
/
g
26. The post-order traversal of a binary search tree is given by 2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12. Then the pre-
order traversal of this tree is:
27. he in-order and pre-order traversal of a binary tree are d b e a f c g and a b d e c f g respectively. The post order
traversal of a binary tree is
28. The number of rotations required to insert a sequence of elements 9,6,5,8,7,10 into an empty AVL tree is?
29. The inorder and preorder Traversal of binary Tree are dbeafcg and abdecfg respectively. The post-order
Traversal is .
30. Level order Traversal of a rooted Tree can be done by starting from root and performing:
31. The in-order traversal of a tree resulted in FBGADCE. Then the pre-order traversal of that tree would result in
32. Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search
tree. The binary search tree uses the usual ordering on natural numbers. What is the inorder traversal sequence of
the resultant tree?
33. What is tree traversal?
34 .What is binary tree?
35 .Whats is pre- order?
36 .What is post order?
37 .What is root?
38 .What is degree?
39 .How many binary trees are there?
40 .What is thread?
41 .What is strictly binary tree?
42 .What is skewed binary tree?
43 .What is complete binary tree?
44 .What is full binary tree?
45 .What is difference between complete full binary trees?
46 .Define forest.
47 .Write any three proprieties of binary tree?
48 .What is ADT?
49 .What is the use of bintree(bt)
50 . In how many ways a binary tree can be represented?
72
Data structures Marri Laxman Reddy of Institute of Technology and Management
#include<stdio.h>
#include<stdlib.h>
void dfs(); // For Deapth First Search(DFS) Traversal
void bfs(); // For Breadth First Search(BFS) Traversal
struct link // Structure for adjacency list
{
struct node *next;
struct link *adj;
};
if(flag==0)
{
p->adj=k;
l=k;
flag++;
}
else
{
l->adj=k;
l=k;
}
q=start;
while(q!=NULL)
{
if(q->data==dat)
k->next=q;
q=q->next;
}
}
p=p->next;
}
}
j++;
}
getche();
int main()
{
int ch;
create(); //Invoking the create function
while(1)
{
printf("1: DFS\n2: BSF\n0: Exit\nEnter your choice: ");
scanf("%d",&ch); //User enters his choice
switch(ch)
{
case 1:
dfs(); //For Depth First Traversal
break;
case 2:
bfs(); //For Breadth First Traversal
break;
case 0:
exit(0); //If Zero then exit the program (Omit this line if you don't want that)
75
Data structures Marri Laxman Reddy of Institute of Technology and Management
break;
default:
printf("Incorrect choice!");
}
}
return 0;
} //End of program.
viva-questions
1What is Graph?
2 .What is undirected Graph?
3 .Define directed graph?
4 .What is BFS?
5 .What is spaning tree?
6 .The cost of the spaning tree
is? 7 .What is krukskals
algorithm? 8 .Explain prims
algorithm?
9 .Breadth First Search is equivalent to which of the traversal in the Binary Trees?
10. The Breadth First Search traversal of a graph will result into?
11 . In BFS, how many times a node is visited?
12 .What can be the applications of Breadth First Search?
13. When the Breadth First Search of a graph is unique?
14 .Depth First Search is equivalent to which of the traversal in the Binary Trees?
15 .The Depth First Search traversal of a graph will result into?
16 .What can be the applications of Depth First Search?
17 .When the Depth First Search of a graph is unique?
18 .In Depth First Search, how many times a node is visited?
19 .What is DFS?
20 .The aim of DFS algorithm is?
21 .The aim of BFS algorithm is?
76
Data structures Marri Laxman Reddy of Institute of Technology and Management
22 .Traversal of a graph is different from tree because?
77
Data structures Marri Laxman Reddy of Institute of Technology and Management
78