DS Lab Manual
DS Lab Manual
DS Lab Manual
Bengaluru–560 107.
LAB MANUAL
Prepared By
Dr. Nataraju A B
Mr. Shadakshari
Reviewed By
Dr. Nataraju A B
Asst. Prof., Dept. of ECE, AIT
Approved By
Dr. Rajeswari
Prof and Head, Department of ECE, AIT
DEPARTMENTOFELECTRONICSANDCOMMUNICATIONENGINEERING
VISION STATEMENT
“To be a premier engineering department with excellence in teaching, research and innovation, to meet
the global industrial standards and to have significant impact on the wellbeing of the society”.
MISSIONSTATEMENT
Program Outcomes(POs)
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge
to assess societal, health, safety, legal and cultural issues and the consequent responsible relevant
to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
Program Specific Outcomes(PSOs)
PSO1: Analog / Digital Circuit Design: Apply the conceptual knowledge in the analysis and/or
design ,evaluate analog/digital circuits and systems.
PSO2: VLSI , Signal Processing and Embedded Systems : Demonstrate technical competency
in the analysis, design , and validation of components in VLSI , Signal Processing, and
Embedded Systems
PSO3: Communication and Networking: Apply the domain knowledge in the implementation
and performance analysis of Communication Systems and Computer Networks.
PEO1: Students shall have a successful professional career in industry, academia, R & D organization
or entrepreneur in specialized field of Electronics & Communication engineering and allied disciplines.
PEO2:Students shall be competent, creative and valued professional in the chosen field.
1. Write a C Program to create a Student record structure to store, N records, each record having the
structure shown below: USN, Student Name and Semester. Write necessary functions a. To display all
the records in the file. b. To search for a specific record based on the USN. In case the record is not
found, suitable message should be displayed. Both the options in this case must be demonstrated. (Use
pointer to structure for dynamic memory allocation)
2 Write a C Program to construct a stack of integers and to perform the following operations on it: a. Push
b. Pop c. Display The program should print appropriate messages for stack overflow, stack underflow,
and stack empty.
3 Write a C Program to convert and print a given valid parenthesized infix arithmetic expression to
postfix expression. The expression consists of single character operands and the binary operators +
(plus), - (minus), * (multiply) and / (divide).
4 Write a C Program to simulate the working of a queue of integers using an array. Provide the following
operations: a. Insert b. Delete c. Display
5 Write a C Program using dynamic variables and pointers to construct a stack of integers using singly
linked list and to perform the following operations: a. Push b. Pop c. Display The program should print
appropriate messages for stack overflow and stack empty.
6 Write a C Program to support the following operations on a doubly linked list where each node consists
of integers: a. Create a doubly linked list by adding each node at the front. b. Insert a new node to the
left of the node whose key value is read as an input c. Delete the node of a given data, if it is found,
otherwise display appropriate message. d. Display the contents of the list. (Note: Only either (a,b and d)
or (a, c and d) may be asked in the examination)
7 Write a C Program a. To construct a binary search tree of integers. b. To traverse the tree using all the
methods i.e., inorder, preorder and postorder. c. To display the elements in the tree.
8 Write recursive C Programs for a. Searching an element on a given list of integers using the Binary
Search method. b. Solving the Towers of Hanoi problem.
10 Design and develop a program in C that uses Hash Function H:K->L as H(K)=K mod m(reminder
method) and implement hashing technique to map a given key K to the address space L. Resolve the
collision (if any) using linear probing
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
Write a C Program to create a Student record structure to store, N records, each record having the structure
shown below: USN, Student Name and Semester. Write necessary functions a. To display all the records in
the file. b. To search for a specific record based on the USN. In case the record is not found, suitable message
should be displayed. Both the options in this case must be demonstrated. (Use pointer to structure for dynamic
memory allocation)
The student data structure contain basic details about a student such as name, usn, result, address, …..
for example:
struct student {
char firstName[50]; // Student’s Name
int roll; // Student’s Roll number
float marks; // Student’s marks
}
// Driver code
int main()
{
int i = 0, n = 5, stFound=0;
char usn[12];
{
printf("Enter the student %d details : \n", i+1);
printf("Name : ");
scanf("%s", student[i].name);
printf("Semester : ");
scanf("%d", & student[i].Semester);
}
if(stFound == 0)
printf("\tThere are No Student found with USN = %s\n", usn);
return 0;
}
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
Example O/P – 1
USN = 1111
Name = aaaa
Semester = 3
Example O/P – 2
USN = 11
Name = aa
Semester = 3
USN = 23
Name = bb
Semester = 4
USN = 11
Name = cc
Semester = 1
Semester = 3
Example O/P – 3
USN = 11
Name = aa
Semester = 4
USN = 22
Name = bb
Semester = 2
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> // For INT_MIN
int main()
{
int choice, data;
while(1)
{
/* Menu */
printf("------------------------------------\n");
printf(" STACK IMPLEMENTATION PROGRAM \n");
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
printf("------------------------------------\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Size\n");
printf("4. Exit\n");
printf("------------------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter data to push into stack: ");
scanf("%d", &data);
case 2:
data = pop();
case 3:
printf("Stack size: %d\n", top + 1);
break;
case 4:
printf("Exiting from app.\n");
exit(0);
break;
default:
printf("Invalid choice, please try again.\n");
}
printf("\n");
}
return 0;
}
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
return INT_MIN;
}
Example O/P – 1
------------------------------------
STACK IMPLEMENTATION PROGRAM
------------------------------------
1. Push
2. Pop
3. Size
4. Exit
------------------------------------
Enter your choice: 1
Enter data to push into stack: 11
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
------------------------------------
STACK IMPLEMENTATION PROGRAM
------------------------------------
1. Push
2. Pop
3. Size
4. Exit
------------------------------------
Enter your choice: 1
Enter data to push into stack: 22
Data pushed to stack.
------------------------------------
STACK IMPLEMENTATION PROGRAM
------------------------------------
1. Push
2. Pop
3. Size
4. Exit
------------------------------------
Enter your choice: 3
Stack size: 2
------------------------------------
STACK IMPLEMENTATION PROGRAM
------------------------------------
1. Push
2. Pop
3. Size
4. Exit
------------------------------------
STACK IMPLEMENTATION PROGRAM
------------------------------------
1. Push
2. Pop
3. Size
4. Exit
------------------------------------
Enter your choice: 2
Data => 22
------------------------------------
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
------------------------------------
STACK IMPLEMENTATION PROGRAM
------------------------------------
1. Push
2. Pop
3. Size
4. Exit
------------------------------------
Enter your choice: 2
Stack is empty.
Hence return INT_MIN as an indication of EMPTY stack.
------------------------------------
Enter your choice: 4
Exiting from app.
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
Infix expression can be represented with A+B, the operator is in the middle of the expression.
In postfix expression, the operator will be at end of the expression, such as AB+
We can easily solve problems using Infix notation, but it is not possible for the computer to solve the given
expression, so system must convert infix to postfix, to evaluate that expression.
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
// Pre increment push
stack[++top] = x;
}
char pop()
{
// Post decrement POP
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}
return 0;
}
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
a b c * +
a b + c * d a - +
Experiment – 4 : Queue
Write a C Program to simulate the working of a queue of integers using an array. Provide the following
operations: a. Insert b. Delete c. Display
#include<stdio.h>
#include <stdlib.h>
int queue[MAX_SIZE];
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = element;
}
int dequeue()
{
if (front == -1 || front > rear) {
printf("Queue is empty");
return -1;
}
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
return element;
}
void display()
{
int i;
int main()
{
int choice, num, size;
enqueue(num);
break;
}
case 2:
{
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
num = dequeue();
printf("\n Dequeued element is : %d ", num);
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
Example O/P – 1
Dequeued element is : 11
Enter the Choice:3
Dequeued element is : 22
Enter the Choice:3
Dequeued element is : 33
Enter the Choice:3
In linked list implementation of stack, the nodes are maintained non-contiguously in the memory. Each node
contains a pointer to its immediate successor node in the stack. Stack is said to be overflown if the space left
in the memory heap is not enough to create a node.
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\nChoose one from the below options...");
printf("\n\t1.Push\n\t2.Pop\n\t3.Show\n\t4.Exit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push(); break;
}
case 2:
{
pop(); break;
}
case 3:
{
display(); break;
}
case 4:
{
printf("Exiting...."); break;
}
default:
{
printf("Please Enter valid choice ");
}
}
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow / Stack Empty");
}
else
{
item = head->val;
ptr = head;
head = head->next;
printf("Item popped is %d \n", ptr->val);
free(ptr);
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
----------------------------------------------
Choose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice : 1
Enter the value 11
Item pushed is 11
Choose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice : 1
Enter the value 22
Item pushed is 22
Choose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice : 3
Printing Stack elements
22
11
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
struct node {
struct node *next;
int data; // This element may be replaced with any
// application specific structure / class / union / ….
struct node *prev;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any rando
m location\n4.Delete from Beginning\n
5.Delete from last\n6.Delete the node after the given data\n7.Search\n8
.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insertion_beginning(); break;
case 2: insertion_last(); break;
case 3: insertion_specified(); break;
case 4: deletion_beginning(); break;
case 5: deletion_last(); break;
case 6: deletion_specified(); break;
case 7: search(); break;
case 8: display(); break;
case 9: exit(0); break;
default: printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
Data Structures using C Lab (BECL456D) 4th Sem B.E ECE
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
}
Experiment – 7 : Binary Search Tree and its traversal
Write a C Program a. To construct a binary search tree(BST) of integers. b. To traverse the tree using all the
methods i.e., inorder, preorder and postorder. c. To display the elements in the tree.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left;
struct node *right;
};
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
return root;
}
}
int main()
{
struct node *root = NULL;
int data;
char ch;
/* Do while loop to display various options to select from to
decide the input */
do
{
printf("\nSelect one of the operations::");
printf("\n1. To insert a new node in the Binary Tree");
printf("\n2. To display the nodes (via Inorder Traversal).");
printf("\n3. To display the nodes (via Preorder Traversal).");
printf("\n4. To display the nodes (via Postorder Traversal).\n");
int choice;
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf("\nEnter the value to be inserted : ");
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
printf("\nInorder Traversal of the Binary Tree::\n");
inorder(root);
break;
case 3 :
printf("\nPreorder Traversal of the Binary Tree::\n");
preorder(root);
break;
case 4 :
printf("\nPostorder Traversal of the Binary Tree::\n");
postorder(root);
break;
default :
printf("Wrong Entry\n");
break;
}
return 0;
}
// driver code
int main(void)
{
// taking a sorted array
int arr[] = { 2, 3, 4, 10, 40 };
int size = sizeof(arr) / sizeof(arr[0]);
// element to be searched
int x = 10;
// calling binary search
int index = binarySearch(arr, 0, size - 1, x);
if (index == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", index);
}
return 0;
}
if (result == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", result);
}
return 0;
.h>