0% found this document useful (0 votes)
27 views15 pages

Ds Lab Manual Part B 2025

The document is a lab manual for a Data Structure course using C, detailing practical programs for implementing various data structures and algorithms. It includes tasks such as creating linked lists, stacks, queues, and binary trees, as well as converting and evaluating expressions. Each program is accompanied by code snippets and descriptions of their functionalities.

Uploaded by

kappekariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views15 pages

Ds Lab Manual Part B 2025

The document is a lab manual for a Data Structure course using C, detailing practical programs for implementing various data structures and algorithms. It includes tasks such as creating linked lists, stacks, queues, and binary trees, as well as converting and evaluating expressions. Each program is accompanied by code snippets and descriptions of their functionalities.

Uploaded by

kappekariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structure Lab BCA-SEM-2

Lab Manual
DATA STRUCTURE LAB USING C
Part B

LIST OF PRACTICAL PROGRAMS

PART B:

1. Program to implement linear linked list to perform insert and delete operations on it.

2. Write a C Program to implement Stack and its different operations.

3. Write a C Program to convert an infix expression to postfix.

4. Write a C Program to evaluate a postfix expression.

5. Write a C Program to implement simple queue and its different operations.

6. Write a program to implement circular queue using array.

7. Write a program to implement BFS.

8. Write a program to implement DFS.

9. Program to create and display different traversal of a binary tree.

10. Write a program to implement AVL Tree.

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru Page |1


Data Structure Lab BCA-SEM-2

Program 1: Program to implement linear linked list to perform insert and delete
operations on it.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
};
struct node *start=NULL;
int element;
void create();
void iae();
void dae();
void display();
void main()
{
int choice;
clrscr();
printf("Enter the elements to create list : ");
create();
while (1)
{
printf("1 : Insert at end\n");
printf("2 : Delete at end\n");
printf("3 : Display\n");
printf("4 : Exit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1: iae(); break;
case 2: dae(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Invalid choice\n");;
}
}
getch();
}
void create()
{
struct node *new,*temp=start;
printf("\n Enter the element : ");
scanf("%d",&element);
do
{
new=(struct node*)malloc(sizeof(struct node));
new->data=element;
new->link=NULL;
if(start==NULL)
{
start=new;
temp=new;
}
else

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru Page |2


Data Structure Lab BCA-SEM-2

{
temp->link=new;
temp=new;
}
printf("\n Enter the next element (0 to terminate): ");
scanf("%d",&element);
}
while(element!=0);
}
void display()
{
struct node *temp=start;
if(start==NULL)
printf("\nLinked list is empty\n");
else
{
printf(“List elements are : ”);
while(temp!=NULL)
{
printf(" %d ",temp->data);
temp=temp->link;
}
}
printf(“\n”);
}
void iae()
{
int ele;
struct node *temp=start;
struct node *newn;
printf("\n Enter the element to insert: ");
scanf("%d",&ele);
newn=(struct node*)malloc(sizeof(struct node));
newn->data=ele;
newn->link=NULL;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=newn;
}
void dae()
{
struct node *temp1=start,*temp2=start->link;
if(start==NULL)
printf("\n EMPTY\n\n");
else
{
while(temp2->link!=NULL)
{
temp1=temp1->link;
temp2=temp2->link;
}
temp1->link=NULL;
printf("\n Deleted element is %d\n\n",temp2->data);
free(temp2);
}
}

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru Page |3


Data Structure Lab BCA-SEM-2

Output:

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru Page |4


Data Structure Lab BCA-SEM-2

Program 2 . Write a C Program to implement Stack and its different operations


#include<stdio.h>
#include<conio.h>
#define MAX 5
int stack[MAX],choice,n,i,item;
static int top;
void push(void);
void pop(void);
void display(void);
void main()
{
clrscr();
top=-1;
while(1)
{
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT");
printf("\nEnter the Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push(); break;
case 2: pop(); break;
case 3: display();break;
case 4: exit(0); break;
default:printf ("\nPlease Enter a Valid choice(1/2/3/4)");
}
}
getch();
}
void push()
{
if(top==MAX-1)
{
printf("\n!!! STACK is over flow");
}
else
{
printf("Enter a value to be pushed : ");
scanf("%d",&item);
top++;
stack[top]=item;
}
}
void pop()
{
if(top==-1)
printf("!!! STACK is under flow\n");
else
{
printf("The popped elements is %d\n",stack[top]);
top--;
}
}

void display()
{
if(top==-1)
printf("!!! The STACK is empty\n");
Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru Page |5
Data Structure Lab BCA-SEM-2

else
{
printf("\nThe elements in STACK");
for(i=0; i<=top; i++)
printf("\n%d",stack[i]);
printf("\n");
}
}
Output:

Program 3. Write a C Program to convert an infix expression to postfix


#include<stdio.h>
#include<ctype.h>
#define MAX 20
char in[MAX],post[MAX];
void main()
{
clrscr();
printf("\n enter the infix ecxpression:\n");
gets(in);
convert();
printf("\n postfix expresion is %s",post);
getch();
}
convert()
{
char s[MAX],ch;
int top=-1,i,j=0,p1,p2;
s[++top]='#';
for(i=0;in[i]!=NULL;i++)
{
ch=in[i];
if(isalnum(ch))
post[j++]=ch;

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru Page |6


Data Structure Lab BCA-SEM-2

else if(ch=='(')
s[++top]=ch;
else if(ch==')')
{
while(s[top]!='(')
post[j++]=s[top--];
top--;
}
else
{
while(priority(s[top]) >= priority(ch))
post[j++]=s[top--];
s[++top]=ch;
}
}
while(s[top]!='#')
post[j++]=s[top--];
post[j]=NULL;
}
priority(char k)
{
switch(k)
{
case '#':
case '(': return (0);
case '+':
case '-': return(1);
case '*':
case '/': return (2);
case '^': return (3);
}
}
Output:

Program 4: Write a C Program to evaluate a postfix expression


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#define MAX 20
int s[MAX],top=-1;
void main()
{
char postfix[MAX],ch;
int i,op1,op2,res;
clrscr();

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru Page |7


Data Structure Lab BCA-SEM-2

printf("\n program to evaluate the postfix expression.\n");


printf("enter the postfix expression :\n");
scanf("%s",&postfix);
for (i=0;i<strlen(postfix);i++)
{
ch=postfix[i];
if(isdigit(ch)) /*check whether the digit*/
push(ch -'0');
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+': res=op1+op2;
break;
case '-': res=op1-op2;
break;
case '*': res=op1*op2;
break;
case '/': res=op1/op2;
break;
case '^': res=pow(op1,op2);
break;
default: printf("invalid character\n",ch);
}
push(res);
}
}
printf("result of above expression is:%d\n", pop());
getch();
}
push(int ele)
{
if(top==MAX-1)
printf("stack overflow");
else
++top;
s[top]=ele;
}
int pop()
{
int ele;
if(top==-1)
printf("stack underflow");
else
ele=s[top];
--top;
return(ele);
}

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru Page |8


Data Structure Lab BCA-SEM-2

Output:

Program 5: Write a C Program to implement simple queue and its different operations
#include <stdio.h>
#include <conio.h>
#define N 5
void insert();
void delete();
void display();
int Q[N];
int rear = - 1;
int front = - 1;
void main()
{
int ch;
clrscr();
printf(“Simple Queue operations\n”);
while (1)
{
printf("1.Insert\n 2.Delete\n 3.Display\n 4.Quit\n”);
printf("Enter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1: insert();break;
case 2: delete();break;
case 3: display();break;
case 4: exit(1);
default: printf("Invalid choice \n");
}
}
getch();
}
void insert()
{
int item;
if (rear == N - 1)
printf("Queue Overflow \n");
else
{
printf("Enter the element to insert in to queue : ");
scanf("%d", &item);
if (front == - 1)/*If queue is initially empty */
{
front = 0;
rear=0;
Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru Page |9
Data Structure Lab BCA-SEM-2

}
else
rear++;
Q[rear] = item;
}
} /* End of insert() */

void delete()
{
if (front == - 1)
{
front=rear=-1;
printf("Queue Underflow \n");
}
else
{
printf("Element deleted from queue is : %d\n", Q[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--> ", Q[i]);
printf("\n");
}
}

Output:

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru P a g e | 10


Data Structure Lab BCA-SEM-2

Program 6: Write a program to implement circular queue using array.


#include<stdio.h>
#include<conio.h>
#define SIZE 5
int q[SIZE],front = 0, rear = -1,count=0;
void enqueue();
void dequeue();
void display();
void main()
{
int ch;
clrscr();
printf("Simple Queue operations\n");
while (1)
{
printf("1.Insert\n 2.Delete\n 3.Display\n 4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1: enqueue();
break;
case 2: dequeue();break;
case 3: display();break;
case 4: exit(1); break;
default:printf("Invalid choice \n");
}
}
getch();
}
void enqueue()
{
if(count==SIZE)
{
printf("\n Q is overflow");
getch();
return;
}
rear++;

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru P a g e | 11


Data Structure Lab BCA-SEM-2

if(rear==SIZE)
rear=0;
printf("\n enter an item:");
scanf("%d",&q[rear]);
count++;
}
void dequeue()
{
if (count==0)
{
printf("Queue is Empty (Underflow)\n");
getch();
return;
}
else
{
printf("Deleted item=%d", q[front]);
front++;
count--;
if(front==SIZE)
front=0;
}
}
void display()
{
int i,j;
if (count==0)
{
printf("Queue is Empty\n");
getch();
return;
}
j=front;
for(i=1;i<=count;i++)
{
printf("%d\n",q[j]);
j++;
if(j==SIZE)
j=0;
}
}
Output:

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru P a g e | 12


Data Structure Lab BCA-SEM-2

Program 7: Write a program to implement BFS.


#include<stdio.h>
#include<conio.h>
int a[10][10],q[10],visited[10],n,i,j,f=-1,r=-1;
void bfs(int v)
{
for(i=1;i<=n;i++)
{
if(a[v][i]&&!visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
}
int main()
{
int v;
clrscr();
printf("enter the number of vertices:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru P a g e | 13
Data Structure Lab BCA-SEM-2

else
printf("BFS is not possible. Not all nodes are reachable");
getch();
}

Output:

Program 8: Write a program to implement DFS


#include<stdio.h>
int adj[100][100],n,reach[10];
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(adj[v][i]&&!reach[i])
{
printf("\n %d->%d",v,i);
dfs(i);
}
}
void main()
{
int i,j,count=0;
clrscr();
printf("enter the number of vertices\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru P a g e | 14


Data Structure Lab BCA-SEM-2

for(j=1;j<=n;j++)
adj[i][j]=0;
}
printf("\n enter the adjascency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++ )
scanf("%d",&adj[i][j]);
dfs(1);
printf("\n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\n graph is connected");
else
printf("\n graph is not connected");
getch();
}
Output:

Manjula N, Dept. of Computer Science, Seshadripuram College, Tumakuru P a g e | 15

You might also like