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

Data Structures Lab Manual

The document outlines 10 programming assignments for Part A and 10 for Part B focusing on data structures and algorithms in C including string operations, searching and sorting algorithms, linked lists, stacks, queues, trees and recursion. Students are tasked with writing C programs to perform tasks like binary search, selection sort, linked list operations, towers of Hanoi, infix to postfix conversion and tree traversal.

Uploaded by

Suman Chinivar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Data Structures Lab Manual

The document outlines 10 programming assignments for Part A and 10 for Part B focusing on data structures and algorithms in C including string operations, searching and sorting algorithms, linked lists, stacks, queues, trees and recursion. Students are tasked with writing C programs to perform tasks like binary search, selection sort, linked list operations, towers of Hanoi, infix to postfix conversion and tree traversal.

Uploaded by

Suman Chinivar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

GLOBAL INSTITUTE OF TECHNOLOGY

R.R.NAGAR, BANGALORE -98

BCA203P: DATA STRUCTURES USING C LAB


PART – A
1. Write a menu driven C program to perform the following string operations without using string
functions: (i) String Length (ii) String Concatenation (ii) String Reverse
2. Write a C program to search for an element in an array using Binary search
3. Write a C program to sort a list of N elements using Selection Sort Algorithm.
4. Write a C program to construct a singly linked list and perform insertion, deletion and Display
operations.
5. Write a C program to demonstrate the working of stack using liked list.
6. Write a C program for Towers of Hanoi problem.
7. Write a C program to find GCD of two numbers using recursion
8. Write a C program to convert infix arithmetic expression to post fix expression.
9. Write a C program to simulate the working of Circular Queue using an array.
10.Write a C program to create and traverse a binary search tree.

PART – B
1. Write a C Program to search an element in an array using linear search.
2. Write a C program to sort a list of N elements using Bubble Sort Algorithm
3. Write a C program to sort a list of N elements using Insertion Sort Algorithm
4. Write a C Program to insert element at the required position in the Array
5. Write a C Program to delete an element at the required position in the Array
6. Write a C program to execute recursive factorial of a number using functions.
7. Write a C program to execute recursive Fibonacci series using functions.
8. Write a C Program to implement stacks using Arrays
9. Write a C Program to evaluate Postfix expression by using Stacks
10.Write a C Program to implement a linear queue by using Arrays.

PART – A
1. Write a menu driven C program to perform the following string operations without using string
functions: (i) String Length (ii) String Concatenation (ii) String Reverse

#include<stdio.h>
#include<conio.h>
int len(char *);
void concat(char *, char *);
void rev(char *);
void main()
{
int lens, n, p, f, lag, ch;
char str1[20], str2[20], *ptr;
clrscr();
while(1)
{
printf("\n string operation are");
printf("\n 1.length of string");
printf("\n 2.concatination of strings");
printf("\n 3.reverse the string");
printf("\n 4.exit");
printf("\n Enter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\n Enter a string");
scanf("%s", str1);
lens = len(str1);
printf("\n The length of string is %d", lens);
free(str1);
break;

case 2: printf("\n Enter a string");


scanf("%s", str1);
printf("\n Enter the second string");
scanf("%s", str2);
concat(str1, str2);
printf("\n after concatination\n");
puts(str1);
free(str1);
break;

case 3: printf("\n Enter a string");


scanf("%s", str1);
rev(str1);
printf("\n the reversed string");
puts(str1);
free(str1);
break;

case 4: exit(0);
default: printf("\n Invalid choise");

}
}
}

int len(char *s)


{
int i=0;
while(*s != '\0')
{
i++;
s++;
}
return i;
}

void rev(char *s)


{
char *begin, *end;
char temp;
int length, i, j;
begin=s;
end=s;
length = len(s);
for(i=0; i<length-1; i++)
end++;
for(i=0; i< length/2; i++)
{
temp = *begin;
*begin = *end;
*end = temp;
begin++;
end--;

}
}

void concat(char *s1, char *s2)


{

while(*s1 != '\0')
s1++;
while(*s2 != '\0')
{
*s1 = *s2;
s1++;
s2++;
}
*s1 = '\0';
}

OUTPUT
2. Write a C program to search for an element in an array using Binary search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,low,high,mid,item,temp;
clrscr();
printf("\n Enter the number of elements");
scanf("%d",&n);
printf("\n Enter the %d elements of the array",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n The entered array elements are");
for(i=0;i<n;i++)
printf("%d ",a[i]);

/* bubble sorting */
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

}
}
printf("\n After sorting the array elements are");
for(i=0;i<n;i++)
printf("\n %d",a[i]);
/*start binary search*/
printf("\n Enter the item to be found");
scanf("%d",&item);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
{
printf("\n Item found successfully at the
position %d",mid+1);
break;
}
else if(item<a[mid])
high=mid-1;
else
low=mid+1;
}
if(low>high)
printf("\n Item not found");
getch();
}

OUTPUT
3. Write a C program to sort a list of N elements using Selection Sort Algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100], i, j, n, temp, min;
clrscr();
printf("Enter the size of the array");
scanf("%d", &n);
printf("Enter the elements");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\n Elements before sorting");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
for(i=0; i<n-1; i++)
{
min = i;
for(j=i+1; j<n; j++)
{
if(a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
printf("\n Elements after sorting");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}

OUTPUT

4. Write a C program to construct a singly linked list and perform insertion, deletion and Display
operations.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
void create();
void insertbeg();
void deleteitem();
void display();
struct node
{
int rollno;
char name[20];
struct node *next;
};
typedef struct node NODE;
NODE *start=NULL;

void main()
{
int ch;
clrscr();
while(1)
{
printf("\n singly linked list operation:");
printf("\n1:create a linked list:");
printf("\n2:insert a node at the begining:");
printf("\n3:delete a node based on rollno:");
printf("\n4:display all nodes:");
printf("\n5:exit:");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:start=NULL;
create();
break;
case 2:insertbeg();
break;
case 3:deleteitem();
break;
case 4:display();
break;
case 5:exit(0);
default:printf("\n invalid choice:");
}
}
}

void create()
{
char ch;
int i=0;
NODE *curptr,*newnode;
curptr=(NODE *)malloc(sizeof(NODE));
start=curptr;
while(1)
{
printf("\n enter the node %d details:",i+1);
printf("\n enter the rollno:");
scanf("%d",&curptr->rollno);
printf("\n enter the name:");
scanf("%s",curptr->name);
printf("\n do you wish to add one more node (y/n)");
ch=getche();
if(ch=='y'||ch=='y')
{
newnode=(NODE *)malloc(sizeof(NODE));
curptr->next=newnode;
curptr=newnode;
}
else
{
curptr->next=NULL;
break;
}
i++;
}
}

void display()
{
NODE *curptr=start;
if(start==NULL)
printf("\n linked list is empty");
else
{
printf("\n linked list is:");
while(curptr!=NULL)
{
printf("%d",curptr->rollno);
printf("->");
printf("%s",curptr->name);
printf("->");
curptr=curptr->next;
}
printf("NULL");
}
}

void insertbeg()
{
NODE *newnode;
newnode=(NODE *)malloc(sizeof(NODE));
printf("\n enter the rollno of new node:");
scanf("%d",&newnode->rollno);
printf("\n enter the name:");
scanf("%s",newnode->name);
newnode->next=start;
start=newnode;
display();
}

void deleteitem()
{
int item;
NODE *curptr=start,*prevptr=NULL;
printf("\n enter the rollno to be deleted:");
scanf("%d",&item);
if(start==NULL)
{
printf("\n linked list is empty:");
return;
}
else if(start->rollno==item)
{
start=start->next;
free(curptr);
return;
}
else
{
while(curptr->rollno!=item && (curptr!=NULL))
{
prevptr=curptr;
curptr=curptr->next;
}
if(curptr==NULL)
printf("\n item is ot found in linked list:");
else
prevptr->next=curptr->next;
}
display();
}
OUTPUT
5. Write a C program to demonstrate the working of stack using liked list.
6. Write a C program for Towers of Hanoi problem.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void hanoi(int,char,char,char);
int count=0;
void main()
{
int n;
char source='s',temp='t',dest='d';
clrscr();
printf("\n Enter the number of disk:");
scanf("%d",&n);
hanoi(n,source,temp,dest);
printf("\n Number of moves : %d",count);
getch();
}
void hanoi(int n,char source,char temp, char dest)
{
if(n>0)
{
hanoi(n-1,source,dest,temp);
printf("\n Move %c--> %c\n",source,dest);
count=count+1;
hanoi(n-1,temp,source,dest);
}
}

OUTPUT
7. Write a C program to find GCD of two numbers using recursion

#include<stdio.h>
#include<conio.h>
int gcd(int, int);
void main()
{
int m, n, m1, n1;
clrscr();
printf("\n Enter two numbers");
scanf("%d %d",&n,&m);
m1 = m;
n1 = n;
printf("\n GCD(%d, %d) = %d", m1,n1,gcd(m,n));
getch();
}

int gcd(int x, int y)


{
if(y == 0)
return x;
else if(y>x)
return gcd(y,x);
else
return gcd(y, x%y);

OUTPUT
8. Write a C program to convert infix arithmetic expression to post fix expression.
#include<stdio.h>
#include<conio.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}

char 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 == '/')
return 2;
if(x == '^')
return 3;
}
void main()
{
char exp[20];
char *e, x;
clrscr();
printf("Enter the expression :: ");
scanf("%s",exp);
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());
}
getch();
}
OUTPUT
9. Write a C program to simulate the working of Circular Queue using an array
10. Write a C program to create and traverse a binary search tree

PART – B
1. Write a C Program to search an element in an array using linear search.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,item,flag=0;
clrscr();
printf("\n Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter the elements of the array : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n The array elements entered are : ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n Enter the key to be found : ");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(item==a[i])
{
flag=1;
printf("key found at %d",i+1);
break;
}
}
if(flag==0)
printf("Key not found");
getch();
}

OUTPUT

2. Write a C program to sort a list of N elements using Bubble Sort Algorithm


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("\n Enter the Array size : ");
scanf("%d",&n);
printf("\n Enter the Array elements : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Before Sorting Array element are: ");
for(i=0;i<n;i++)
printf("\n %d", a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n After Sorting array elements are: ");
for(i=0;i<n;i++)
printf("\n %d", a[i]);
getch();
}
3. Write a C program to sort a list of N elements using Insertion Sort Algorithm

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,item,a[20];
clrscr();
printf("\n\tEnter total elements: ");
scanf("%d",&n);
printf("\n\n\tEnter %d elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
item=a[i];
j=i-1;
while(j>=0 &&item<a[j])
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=item;
}
printf("\n\n\tAfter sorting: ");
for(i=0;i<n;i++)
printf("\n\t %d",a[i]);
getch();
}

OUTPUT
4. Write a C Program to insert element at the required position in the Array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,val,pos;
clrscr();
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nElements are:");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
printf("\nEnter the new value:");
scanf("%d",&val);
printf("\nEnter the pos to be inserted:\n");
scanf("%d",&pos);
for(i=n-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=val;
n=n+1;
for(i=0;i<n;i++)
printf("\n %d",a[i]);
getch();
}

OUTPUT

5. Write a C Program to delete an element at the required position in the Array


6. Write a C program to execute recursive factorial of a number using functions
7. Write a C program to execute recursive Fibonacci series using functions
8. Write a C Program to implement stacks using Arrays
9. Write a C Program to evaluate Postfix expression by using Stacks
10. Write a C Program to implement a linear queue by using Arrays.

You might also like