Data Structures C Labmanual BEC456D
Data Structures C Labmanual BEC456D
LABORATORY
MYSORE COLLEGE OF ENGINEERING AND MANAGEMENT
Contact Total
Course Core/Electiv Hours
Course Title Prerequisite Hrs/
Code e
L T P Sessions
DATA Programming with C
STRUCTURE 13
21EC584 S CORE 3
LABORATOR
Y
This laboratory course enable students to get practical experience in design, develop,
implement,
analyze and evaluation/testing of
Objectives • Asymptotic performance of algorithms.
• Linear data structures and their applications such as Stacks, Queues and Lists
• Non-Linear Data Structures and their Applications such as Trees and Graphs
• Sorting and Searching Algorithms
Laboratory Experiments Covered as per Syllabus
1. Design, Develop and Implement a menu driven Program in C for the following
Array operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Inserting an Element (ELEM) at a given valid Position (POS)
d. Deleting an Element at a given valid Position(POS)
e. Exit.
Support the program with functions for each of the above operations.
2. Design, Develop and Implement 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.
3. Design, Develop and Implement 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
DIVYASHREE.G Assistant Professor dept of ECE, MyCEM Page 1
DATA STRUCTURES WITH C
LABORATORY
Support the program with appropriate functions for each of the above operations
4. Design, Develop and Implement 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.
5. Design, Develop and Implement 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
6. Design, Develop and Implement 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
7. Design, Develop and Implement a menu driven Program in C for the following operations on
Singly Linked List (SLL) of Student Data with the fields: USN, Name, Branch, 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
8. Design, Develop and Implement 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
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit
9. Design, Develop and Implement a Program in C for the following operations on 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
10. Design, Develop and Implement 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
e. Exit
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. Design
and 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.
On the completion of this laboratory course, the students will be able to:
• Analyze and Compare various linear and non-linear data structures
Course • Code, debug and demonstrate the working nature of different types of data structures and
Outcomes their applications
• Implement, analyze and evaluate the searching and sorting algorithms
• Choose the appropriate data structure for solving real world problems
DATA STRUCTURES
Data Structures:
The logical or mathematical model of a particular organization of data is called data
structures. Data structures is the study of logical relationship existing between individualdata
elements, the way the data is organized in the memory and the efficient way of storing, accessing
and manipulating the data elements.
Choice of a particular data model depends on two considerations: it must be rich enough in
structure to mirror the actual relationships of the data in the real world. On the other hand, the
structure should be simple enough that one can effectively process the data when necessary.
Primitive data structures are the basic data structures that can be directly
manipulated/operated by machine instructions. Some of these are character, integer, real, pointers
etc.
Non-primitive data structures are derived from primitive data structures, they cannot be
directly manipulated/operated by machine instructions, and these are group of homogeneous or
heterogeneous data items. Some of these are Arrays, stacks, queues, trees,graphs etc.
In the Linear data structures processing of data items is possible in linear fashion, i.e., datacan
be processed one by one sequentially.
Example of such data structures are:
Array
Linked list
Stacks
Queues
A data structure in which insertion and deletion is not possible in a linear fashion is called
as non linear data structure. i.e., which does not show the relationship of logical adjacency between
the elements is called as non-linear data structure. Such as trees, graphsand files.
1. Design, Develop and Implement a menu driven program in c for the followingArray operations,
a. Creating an Array of N integer elements.
b. Display of Array elements with suitable headings.
c. Inserting an element (ele) at a given valid position(pos).
d. Deleting an element at a given valid position (pos).
e. Exit.
Support the program with functions for each of the above operations.
#include<stdio.h>
#include<conio.h>
int n,a[50];
void create()
{
int i;
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter %d array elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display()
{
int i;
printf("entered elements are\n");
for(i=0;i<n;i++) printf("%d\n",a[i]);
}
void insertion()
{
int i,POS,ELEM;
printf("enter the position and its value\n");
scanf("%d%d",&POS,&ELEM);
for(i=n;i>=POS;i--)
a[i]=a[i-1];
a[POS]=ELEM;
n=n+1;
display();
}
void deletion()
{
int i,POS,ELEM;
printf("enter the position to be deleted\n");
scanf("%d",&POS);
ELEM=a[POS];
for(i=POS;i<=n-1;i++)
a[i]=a[i+1];
printf("the deleated element is %d\n",ELEM);
n=n-1;
display();
}
2. Design, Develop and Implement a program in c for the following operations onstrings
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 ofpat in str with rep if
pat exits in str. Report with suitable messages in case pat does not exists in str.
Support the program with functions for each of the above operations. Don’t usebuilt in functions.
#include<stdio.h>
char STR[100],PAT[100],REP[100],ANS[100];
int i,j,c,m,k,flag=0;
void read()
{
printf("\n Enter MAIN string:\n");
gets(STR);
printf("\n Entre PATTERN string:\n");
gets(PAT);
printf("\n Enter REPLACE string\n");
gets(REP);
}
void replace()
{
i=m=c=j=0;
while(STR[c]!='\0')
{
if(STR[m]==PAT[i])
{
i++;m++;
if(PAT[i]=='\0')
{
for(k=0;REP[k]!='\0';k++,j++)
ANS[j]=REP[k];
i=0;
c=m;flag=1;
}
}
else
{
ANS[j]=STR[c];
j++;c++;m=c;i=0;
}
}
if(flag==0)
printf("pattern doesen't found!!!\n");
else
{
ANS[j]='\0';
printf("\n the RESULTANT string\n is %s \n",ANS);
}
}
void main()
{
clrscr();
read();
replace();
getch();
3. Design, Develop and Implement a menu driven program in c for the following operations on STACK of
integers(Array implementation of stack with maximumsize MAX)
a. Push an element onto the stack.
b. Pop an element from the stack.
c. Demonstrate how stack can be used to check palindrome.
d. Demonstrate overflow and underflow situations on stack.
e. Display the status of the stack.
f. Exit
Support the program with appropriate functions for each of the aboveoperations.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 5
int s[max],stop;
int ele,st[max],sp,ch;
void main()
{
stop= -1;
sp= -1;
while(1)
{
printf("enter tne choice\n");
printf("enter 1 to insert an element into the STACK\n");
printf("enter 2 to delete an element from the STACK\n");
printf("enter 3 to check an element is palindrome or not\n");
printf("enter 4 to check the status of the STACK\n");
printf("enter 5 to exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter the elements to de inserted to STACK\n");
scanf("%d",&ele);
push(ele,s,&stop);
break;
case 2:ele=pop(s,&stop);
if(ele!=0)
printf("element poped is %d\n",ele);break;
case 3:printf("enter the elements to chech weather it is a palindrome\n");
scanf("%d",&ele);
palindrome(ele,st);
break;
case 4:printf("the status of the STACK \n");
display(s,&stop);
break;
case5:exit(0
);
}
}
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
int F(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;case
'*':
case '%':
case '/':return 4;case
'^':
case '$':return 5;
case '(':return 0;
case '#':return -1;
default :return 8;
}
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;case
'*':
case '%':
case '/':return 3;case
'^':
case '$':return 6;
case '(':return 9;
case ')':return 0;
default :return 7;
}
}
void infix_postfix(char infix[],char postfix[])
{
int top,i,j; char
s[30]; char
symbol; top=-1;
s[++top]='#';j=0;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol))
postfix[j++]=s[top--];
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]; char
postfix[20];
printf("enter a valid infix expression\n");
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("the postfix expression is\n");
printf("%s\n",postfix);
#include<stdio.h>
#include<string.h>
#include<math.h>
int count=0, top=-1;
int operate(char symb, int op1, int op2)
{
switch(symb)
{
case '+':return op1+op2; case '-
':return op1-op2; case '/':return
op1/op2; case '*':return op1*op2;
case '%':return op1%op2;
case '^':return pow(op1,op2);
}
}
void push(int stack[],int d)
{
stack[++top]=d;
}
int pop(int stack[])
{
return(stack[top--]);
}
void tower( int n,char src, char intr, char des)
{
if(n)
{
tower(n-1,src,des,intr);
printf("disk %d moved from %c to %c\n",n,src,des);
count++;
tower(n-1,intr,src,des);
}
}
void main()
{
int n, choice,i,op1,op2,ans,stack[50];char
expr[20],symb;
while(1)
{
printf("\nprogram to perform evaluation of suffix expression and tower ofhanoi problem\n");
printf("\n1.evaluate suffix expression\n2.Tower of hanoi\n3.Exit\n ");
printf("\nenter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter the suffix expression : ");
scanf("%s",expr);
for(i=0;expr[i]!= '\0';i++)
ans=operate(symb,op1,op2); push(stack,ans);
}
}
ans=pop(stack);
printf("The result of the suffix expression is %d",ans);
break;
#include<stdio.h>
#include<stdlib.h>
#include<math.h> int
count=0,top=-1;
int operate(char symb,int r)
{
switch (symb)
{
case '+':return op1+op2; case '-
':return op1-op2; case '*':return
op1*op2; case '%':return
op1%op2;case '/':return op1/op2;
case '^':return pow(op1^op2);
}
}
void push(int stack[],int d)
stack[++top]=d;
int pop(int stack[]); return
(stack[top--]);
void tower(int n,char src,char intr,char des)
{
if(n)
case 3:exit(0);
}
}
}
6. Design, Develop and Implement a menu driven Program in C for the followingoperations on Circular
QUEUE of Characters (Array Implementation of Queue withmaximum 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.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 5
int front=0,rear=-1,count=0,it1;
char cqueue[MAX],element;
void insert();
void delete(); void
display();
void main()
{
int choice;
while(1)
{
printf("\n\nprogram to ililstrate operations on CIRCULAR QUEUE of characters\n");
printf("\n\t1=>insert an element on to CIRCULAR QUEUE\n\t2=>delete an element fromCIRCULAR
QUEUE\n\t3=>display the status of CIRCULAR QUEUE\n\t4=>exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:return;
}
}
}
void insert()
{
if(count==MAX)
{
printf("CIRCULAR QUEUE is full,elements can not be inserted\n");
return;
}
rear=(rear+1)%MAX;
printf("\n enter the element to be inserted into the CIRCULAR QUEUE\n");
element=getche();
cqueue[rear]=element;
void delete()
{
if(count==0)
{
printf("CIRCULAR QHEUE is empty,no element to delete\n");
return;
}
it1=cqueue[front];
front=(front+1)%MAX;
printf("the element deleted is %c\n",it1);count-=1;
}
void display()
{
int i;
if(count==0)
{
printf("CIRCULAR QUEUE is empty , no element to display\n");
return;
}
printf("CIRCULAR QUEUE contants are\n");
for(i=front;i<=count;i++)
printf("%c",cqueue[i]);
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
int usn;
char name[20];
char branch[20];
int semester;
char phone[20];
}
STUDENT;
struct node
{
int usn;
char name[20];
char branch[20];
int semester;
char phone[20];
struct node *link;
};
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int ssn;
char name[20],department[20],designation[20],phone[20];
float salary;
struct node *llink;
struct node *rlink;
}
typedef struct node *NODE;
struct node *item;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
void read()
{
item=getnode();
printf("ssn:");
scanf("%d",&item->ssn);
printf("name:");
scanf("%s",item->name);
printf("department:");
scanf("%s",item->department);
printf("designation:");
scanf("%s",item->designation);
printf("salary:");
scanf("%f",&item->salary);
printf("phone:");
scanf("%s",item->phone);
item->llink=item->rlink=NULL;
}
NODE insert_front(NODE first)
{
read();
if(first==NULL) return item;
item->rlink=first;
first->llink=item;
return item;
}
NODE delete_front(NODE first)
{
NODE second;
if(first==NULL)
{
printf("employee list is empty\n");
return NULL;
}
if(first->rlink==NULL)
{
printf("employee details deleted:ssn:=%d\n",first->ssn);
free(first);
return NULL;
}
second=first->rlink;
second->llink=NULL;
printf("employee details deleted:ssn:=%d\n",first->ssn);
free(first);
return second;
}
NODE delete_rear(NODE first)
{
NODE cur,prev;
if(first==NULL)
{
printf("list is empty cannot delete\n");
return first;
}
if(first->rlink==NULL)
{
printf("employee details deleted:ssn:=%d\n",first->ssn);
free(first);
return NULL;
}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{
prev=cur; cur=cur-
>rlink;
}
printf("employee details deleted:ssn:=%d\n",cur->ssn);free(cur);
prev->rlink=NULL;return
first;
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<math.h>
typedef struct node
display(node *head);
node *p,*q;
if(expo1>head->expo)
if(expo1==head->expo)
q=head;
while(q->next!=head&&expo1>=q->next->expo) q=q->next;
if(p->expo==q->expo) q-
>coef=q->coef+coef1;else
p->next=q->next; q->next=p;
return(head);
node *create()
for(i=0;i<n;i++)
return(head);
node *p;
node *head=NULL;
head=insert(head,p->expo,p->coef); p=p->next;
}while(p!=p1->next); p=p2->next;do
head=insert(head,p->expo,p->coef); p=p->next;
}while(p!=p2->next); return(head);
>next; do
n++; q=q->next;
if(n-1)
else
n--;
} while(p!=head->next);
void main()
int a,x,ch;
printf("\n\n\n\tEnter your
choice==>"); scanf("%d",&ch);
switch(ch)
a=eval(p1);
#include<stdio.h>
#include<stdlib.h>struct
node
{
int info;
struct node*llink;
struct node*rlink;
};
typedef struct node*NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
void preorder(NODE root)
{
if(root==NULL)return;
printf("%d ",root->info);
preorder(root->llink);
preorder(root->rlink);
}
void postorder(NODE root)
{
if(root==NULL)
return;
postorder(root->llink);
postorder(root->rlink);
printf("%d ",root->info);
}
void inorder(NODE root)
{
if(root==NULL)
return;
inorder(root->llink); printf("%d
",root->info);inorder(root-
>rlink);
}
void display(NODE root,int level)
{
int i;
if(root==NULL)
return;
11. Design, Develop and Implement a Program in C for the following operations onGraph(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 BFSmethod
c. Check whether a given graph is connected or not using DFS method.
#include<stdio.h>
#include<conio.h> int
a[10][10],s[10],n;
void bfs(int u)
{
int f,r,q[10],v;
printf("the nodes visited from %d ",u);f=0,r=-1;
q[++r]=u;
s[u]=1;
printf("%d ",u);
while(f<=r)
{
u=q[f++]; for(v=0;v<n;v++)
{
if(a[u][v]==1)
{
if(s[v]==0)
{
printf(" %d ",v);
s[v]=1;
q[++r]=v;
}
}
}
}
printf("\n");
}
void dfs(int u)
{
int v;
s[u]=1;
printf(" %d ",u); for(v=0;v<n;v++)
{
if(a[u][v]==1&&s[v]==0) dfs(v);
}
}
void main()
{
int i,j,choice,source,s1;
clrscr();
printf("enter the no of nodes\n");
scanf("%d",&n);
printf("enter the adjacency matrix\n");
}
break;
case 3:exit(0);
}
}
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h> #define
HASH_SIZE 5 typedef struct
empolyee
{
int id;
char name[20];char
des[20];
}EMPLOYEE;
void intialize_hash_table(EMPLOYEE a[])
{
int i; for(i=0;i<HASH_SIZE;i++)
{
a[i].id=0;
// a[i].name={""};
// a[i].des=NULL;
}
}
void insert_hash_table(int id,char des[],char name[],EMPLOYEE a[])
{
int i,index,h_value;
h_value=id%HASH_SIZE;
for(i=0;i<HASH_SIZE;i++)
{
index=(h_value+i)%HASH_SIZE;
if(a[index].id==0)
{
a[index].id=id;
strcpy(a[index].name,name);
strcpy(a[index].des,des); break;
}
}
if(i==HASH_SIZE)
printf("table full");
}
int search_hash_table(int key,EMPLOYEE a[])
{
int i,index,h_value;
h_value=key%HASH_SIZE;
for(i=0;i<HASH_SIZE;i++)
{
Viva questions
What is data-structure?
Source Code
/**
* A menu-driven program for elementary database management
* @author: Bibek Subedi
* @language: C
* This program uses file handling in Binary mode
*/
// Copied from
// https://stackoverflow.com/questions/35103745/read-a-string-as-an-input-using-scanf
void flush()
{
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main(){
FILE *fp, *ft; /// file pointerschar another,
choice;
char empname[40]; /// string to store name of the employeelong int recsize; /// size of each
record of employee
/// sizeo of each record i.e. size of structure variable erecsize = sizeof(e);
case '3': /// if user press 3 then do editing existing recordanother = 'y';
while(another == 'y'){
printf("Enter the employee name to modify: ");scanf("%s",
empname);
rewind(fp);
while(fread(&e,recsize,1,fp)==1){ /// fetch all record if(strcmp(e.name,empname) == 0){ ///if
entered name
matches with that in file
printf("\nEnter new name,age and bs: ");
scanf("%s%d%f",e.name,&e.age,&e.bs);
fseek(fp,-recsize,SEEK_CUR); /// move the cursor 1step back from current
position
fwrite(&e,recsize,1,fp); /// override the recordbreak;
break;
case '5':
fclose(fp); /// close the file exit(0); /// exit from the
program
}
}
return 0;
}