DS Lab Manual
DS Lab Manual
Program 1
#include<stdio.h>
#include<conio.h>
struct Employee
{
int Empno;
char name[20];
int age;
unsigned long salary;
};
void printEmployeeDetails();
void main()
{
clrscr();
printEmployeeDetails();
getch();
}
void printEmployeeDetails()
{
int n,i;
struct Employee empArray[50];
printf("Enter the number of Employee Details\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d employee Empno\n",(i+1));
scanf("%d",&empArray[i].Empno);
printf("Enter the %d employee Name\n",(i+1));
scanf("%s",&empArray[i].name);
printf("Enter the %d employee Age\n",(i+1));
scanf("%d",&empArray[i].age);
printf("Enter the %d employee Salary\n",(i+1));
scanf("%lu",&empArray[i].salary);
}
printf("___________________________________\n");
printf("Empno\tName\tAge\tSalary\n");
printf("___________________________________\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\t%lu\n",empArray[i].Empno,empArray[i].name,
empArray[i].age,empArray[i].salary);
}
OUTPUT:
Program 2
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int *ptra,*ptrb;
//int* ptrb;
clrscr();
printf("Enter the value for a and b");
scanf("%d%d",&a,&b);
ptra = &a;
ptrb = &b;
printf("Sum is %d\n",(*ptra + *ptrb));
printf("Difference is %d\n",(*ptra - *ptrb));
printf("Product is %d\n",(*ptra * *ptrb));
printf("Quotient is %d\n",(*ptra / *ptrb));
getch();
}
OUTPUT:
Program 3
#include<stdio.h>
void main()
{
FILE *fp;
int i,n;
int rollNo, mark1,mark2,total,avg;
char name[10],filename[10];
clrscr();
printf("Input file name\n");
scanf("%s",filename);
fp= fopen(filename, "w");
printf("Enter the no student details\n\n");
scanf("%d",&n);
printf("Enter the %d details of student\n",n);
for(i=1;i<=n;i++)
{
printf("Enter %d student details(Rollno,Name,Marks1,Marks2)\n", i);
// Readinng userinput from keyboard
scanf("%d %s %d %d",&rollNo,name,&mark1,&mark2);
//fscanf(stdin,"%d %s %d %d",&rollNo,name,&mark1,&mark2);
// Writing to the file and data for writing comes from stdin.
fprintf(fp,"%d %s %d %d\n",rollNo,name,mark1,mark2);
}
fclose(fp);
printf("\n\n");
//fprintf(stdout,"\n\n");
fp = fopen(filename,"r");
printf("RollNo\t Name\t Mark1\t Mark2\t Total\t Average\n");
for(i=1;i<=n;i++)
{
// Readinng data from file
fscanf(fp,"%d %s %d %d",&rollNo,name,&mark1,&mark2);
total = mark1 + mark2;
avg = (mark1 + mark2) / 2;
// Printing data on console, the data was red from file through scanf.
//fprintf(stdout,"%d\t%s\t%d\t%d\t%d\t%d\n",rollNo,name,mark1,mark2,total,a
vg);
printf("%d\t%s\t%d\t%d\t%d\t%d\n",rollNo,name,mark1,mark2,total,avg);
}
fclose(fp);
getch();
}
OUTPUT:
Program 4
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int chars=0,words=1,spaces=0;
clrscr();
fp = fopen("demo.txt", "w");// Opening file in write mode.
printf("Enter text in to the file\n");
while( (ch = getchar()) != EOF)//Looping until reading EOF(End of file)
{
putc(ch,fp);
}
fclose(fp);// Closing the file.
fp = fopen("demo.txt", "r");//Opening file in read mode
while((ch = getc(fp))!= EOF)//Looping until EOF
{
if(ch == ' ') spaces++;
if(ch != ' ') chars++;
if(ch== ' ' || ch=='\n' || ch=='\t') words++;
}
printf("Space count %d",spaces);
printf("Character count %d",chars);
printf("Words count %d",words);
Note : Need to type ctrl + Z or F6 (windows) or ctrl + D(Unix) after giving the input
to specify the end of line.
OUTPUT:
Program 5
#define MAXSIZE 10
#include<stdio.h>
#include<conio.h>
struct Stack
{
int top;
int a[MAXSIZE];
}s;
void main()
{
void push(int);
void display();
int pop();
int item,choice;
s.top = -1;
clrscr();
while(1)
{
printf("\n******Stack Operations*****");
printf("\n 1.Push");
printf("\n 2.Pop");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the element to be push\n");
scanf("%d",&item);
push(item);
break;
case 2: item = pop();
if(item != (-1))
printf("Poped element is %d\n",item);
break;
case 3: printf("Elements of the Stack are\n");
display();
break;
case 4: exit();
default : printf("Error in choice\n");
}
}
}
OUTPUT 1:
For Push
OUTPUT 2:
For Pop:
OUTPUT 3:
For Display :
Program 6
# include <string.h>
char postfix[50];
char infix[50];
char opstack[50]; /* operator stack */
int i, j, top = 0;
int lesspriority(char op, char op_at_stack)
{
int k;
int pv1; /* priority value of op */
int pv2; /* priority value of op_at_stack */
char operators[] = {'+', '-', '*', '/', '%', '^', '(' };
//int priority_value[] = {0,0,1,1,2,3,4};
int priority_value[] = {1,1,2,2,3,4,5};
if( op_at_stack == '(' )
return 0;
for(k = 0; k < 6; k ++)
{
if(op == operators[k])
pv1 = priority_value[k];
}
for(k = 0; k < 6; k ++)
{
if(op_at_stack== operators[k])
pv2 = priority_value[k];
}
if(pv1 < pv2)
return 1;
else
return 0;
}
}
postfix[j] = '\0';
printf("\n Infix Expression : %s ", infix);
printf("\n Postfix Expression : %s ", postfix);
getch();
}
OUTPUT 1:
OUTPUT 2:
Program 7
#include<stdio.h>
#include<conio.h>
#define MAX 6
void insertQ();
void deleteQ();
void displayQ();
struct Queue
{
int Q[MAX];
int front,rear;
}s;
void main()
{
int choice;
s.front = -1;
s.rear = -1;
clrscr();
while(1)
{
}
}
}
void insertQ()
{
int data;
if(s.rear == MAX-1)
{
printf("\n Queue is full");
return;
}
else
{
if(s.front == -1) s.front++;
printf("\n Enter data: ");
scanf("%d", &data);
s.rear++;
s.Q[s.rear] = data;
}
}
void deleteQ()
{
if(s.front == -1)
{
printf("\nQueue is Underflow..");
return;
}
else
{
OUTPUT :
For Insertion
For Display
For Delete
Program 8
#include<stdio.h>
#include<conio.h>
struct CircularQueue
{
int cq[10],i,n,e,ch;
int f;
int r;
}s;
void cqInsert();
void cqDelete();
void cqDisplay();
void main()
{
s.f=-1;
s.r=-1;
clrscr();
printf("Enter the size of the queue:\n");
scanf("%d",&s.n);
while(1)
{
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter the choice:\n");
scanf("%d",&s.ch);
switch(s.ch)
{
case 1: cqInsert();
break;
case 2: cqDelete();
break;
case 3: cqDisplay();
break;
case 4: exit(0);break;
}
}
}
void cqInsert()
{
if(s.f==(s.r+1)%s.n)
{
printf("Queue is full,leads to overflow\n");
return;
}
printf("Enter the element to insert:\n");
scanf("%d",&s.e);
s.r=(s.r+1)%s.n;
s.cq[r] = s.e;
if(f==-1)
f++;
}
void cqDelete()
{
if(s.f==-1)
{
printf("Queue is empty,leads to underflow\n");
return;
}
printf("The deleted element is:%d\s.n",s.cq[f]);
if(s.f==s.r)
s.f=s.r=-1;
else
s.f=(s.f+1)%s.n;
}
void cqDisplay()
{
if(s.f==-1)
{
printf("Queue is empty,nothing to display\n");
return;
}
printf("Elements of queue:\n");
for(s.i=s.f;s.i<=s.r;s.i++)
printf("%d\t",s.cq[s.i]);
if(s.f>s.r)
{
for(s.i=s.f;s.i<s.n;s.i++)
printf("%d\t",s.cq[s.i]);
for(s.i=0;s.i<=r;s.i++)
printf("%d\t",s.cq[s.i]);
}
}
OUTPUT:
For Insert:
For Display:
For Delete:
Program 9
#include<stdio.h>
#include<stdlib.h>
#define MAX 7
struct DQueue
{
int deque_arr[MAX];
int f;
int r;
}dq;
main()
{
int choice,item;
dq.f=-1;
dq.r=-1;
clrscr();
while(1)
{
printf("1.Insert at the front end\n");
printf("2.Insert at the rear end\n");
printf("3.Delete from front end\n");
printf("4.Delete from rear end\n");
printf("5.Display\n");
printf("6.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input the element for adding in queue : ");
scanf("%d",&item);
insert_frontEnd(item);
break;
case 2:
printf("Input the element for adding in queue : ");
scanf("%d",&item);
insert_rearEnd(item);
break;
case 3:
}
else if(dq.r==MAX-1) /*rear is at last position of queue */
dq.r=0;
else
dq.r=dq.r+1;
dq.deque_arr[dq.r]=item ;
}/*End of insert_rearEnd()*/
int delete_frontEnd()
{
int item;
if( isEmpty() )
{
printf("Queue Underflow\n");
exit(1);
}
item=dq.deque_arr[dq.f];
if(dq.f==dq.r) /*Queue has only one element */
{
dq.f=-1;
dq.r=-1;
}
else
if(dq.f==MAX-1)
dq.f=0;
else
dq.f=dq.f+1;
return item;
}/*End of delete_frontEnd()*/
int delete_rearEnd()
{
int item;
if( isEmpty() )
{
printf("Queue Underflow\n");
exit(1);
}
item=dq.deque_arr[dq.r];
}/*End of delete_rearEnd() */
int isFull()
{
if ( (dq.f==0 && dq.r==MAX-1) || (dq.f==dq.r+1) )
return 1;
else
return 0;
}/*End of isFull()*/
int isEmpty()
{
if( dq.f == -1)
return 1;
else
return 0;
}/*End of isEmpty()*/
void display()
{
int i;
if( isEmpty() )
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
i=dq.f;
if( dq.f<=dq.r )
{
while(i<=dq.r)
printf("%d ",dq.deque_arr[i++]);
}
else
{
while(i<=MAX-1)
printf("%d ",dq.deque_arr[i++]);
i=0;
while(i<=dq.r)
printf("%d ",dq.deque_arr[i++]);
}
printf("\n");
}/*End of display() */
OUTPUT:
Program 10
void main()
{
int n,r,ncr;
clrscr();
printf("Enter values of n and r\n");
scanf("%d%d",&n,&r);
ncr=recbicoff(n,r);
printf("Binomial coefficient of c(%d,%d) is %d",n,r,ncr);
getch();
}
int recbicoff(int x,int y)
{
if(y>x)
{
printf("The value of n can't be less than r");
getch();
exit(1);
}
if(y==0||y==x)
{
return(1);
}
else
{
return(recbicoff(x-1,y-1)+recbicoff(x-1,y));
}
}
OUTPUT:
Program 11
#include<stdio.h>
int genFibSeries(int);
void main()
{
int n, i = 0, c;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", genFibSeries(i));
i++;
}
getch();
}
int genFibSeries(int n)
{
if (n==0)
return 0;
else if (n==1)
return 1;
else
return (genFibSeries(n-1) + genFibSeries(n-2));
}
OUTPUT:
Program 12
#include <stdio.h>
#include <conio.h>
void towers_of_hanoi (int n, char *a, char *b, char *c);
void main (void)
{
int n;
clrscr();
printf("Enter number of discs:\n");
scanf("%d",&n);
towers_of_hanoi (n, "Tower 1", "Tower 2", "Tower 3");
getch();
}
void towers_of_hanoi (int n, char *a, char *b, char *c)
{
if (n == 1)
{
OUTPUT:
Program 13
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int regno;
char name[30];
struct node *link;
}*start;
void insert(int regno,char name[30]);
void display();
void search(int regno);
void deleteL(int regno);
void main()
{
int choice,regno;
char name[30];
int searchStatus;
clrscr();
while(1)
{
printf("\nLinked List operations\n");
printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n");
printf("Enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the regno and name of the student\n");
scanf("%d",®no);
scanf("%s",name);
insert(regno,name);break;
case 2:printf("Enter the student regno to delete\n");
scanf("%d",®no);
deleteL(regno);
display();break;
case 3:printf("Enter the student regno to search\n");
scanf("%d",®no);
search(regno);
break;
case 4: display();break;
case 5: exit(1);break;
default : printf("Enter the correct choice\n");break;
}
}
}
{
struct node *newnode,*ptr;
newnode = (struct node *)malloc(sizeof(struct node));
if(start == NULL)
{
newnode->regno = regno;
strcpy(newnode->name,name);
newnode->link = NULL;
start = newnode;
}
else
{
ptr = start;
while(ptr->link != NULL)
{
ptr=ptr->link;
}
newnode->regno = regno;
strcpy(newnode->name,name);
newnode->link = NULL;
ptr->link = newnode;
}
printf("Node added to the List");
}
void display()
{
struct node *ptr;
ptr = start;
if(start == NULL)
{
printf("List is empty\n");
return;
}
printf("Linked list contents are\n");
while(ptr!=NULL)
{
printf("%d,%s->",ptr->regno,ptr->name);
ptr=ptr->link;
}
printf("End of List");
}
void search(int regno)
{
struct node *ptr;
ptr = start;
if(ptr != NULL)
{
while(ptr->link != NULL)
{
if(ptr->regno == regno)
{
printf("Student with regno %d is found",regno);
return;
}
else
ptr = ptr->link;
}
if(ptr->regno == regno)
printf("Student with regno %d is found",regno);
else
printf("Student with regno %d is not found",regno);
}
else
{
printf("Student List is empty,nothing to search");
}
}
void deleteL(int regno)
{
struct node *ptr,*prev;
ptr=start;
prev=NULL;
if(ptr==NULL)
{
printf("\n Student Linked list is empty,nothing to delete\n");
return;
}
else if(start->regno==regno)
{
start=start->link;
printf("Student with regno: %d is deleted\n",regno);
free(ptr);
return;
}
while((ptr->regno!=regno)&&(ptr!=NULL))
{
prev=ptr;
ptr=ptr->link;
}
if(ptr==NULL)
printf("Student with %d is not found in the linked list,delete is not
possible\n",regno);
else
{
prev->link=ptr->link;
printf("Student with regno: %d is deleted\n",regno);
free(ptr);
}
return;
}
OUTPUT:
For Inserting
For Display
For Search
For Display
Program 14
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *Llink;
struct node *Rlink;
}*start;
void insert(int data);
void display();
void deleteL(int data);
void main()
{
int choice,data;
int searchStatus;
clrscr();
while(1)
{
printf("\nDoubly Linked List operations\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the data to insert\n");
scanf("%d",&data);
insert(data);break;
case 2:printf("Enter the data to delete\n");
scanf("%d",&data);
deleteL(data);break;
case 3: display();break;
case 4: exit(1);break;
default : printf("Enter the correct choice\n");break;
}
}
}
start = newnode;
}
else
{
ptr = start;
while(ptr->Rlink != NULL)
{
ptr=ptr->Rlink;
}
newnode->info = data;
newnode->Rlink = NULL;
newnode->Llink = ptr;
ptr->Rlink = newnode;
}
printf("List created\n");
}
void display()
{
struct node *ptr;
ptr = start;
if(start == NULL)
{
printf("List is empty\n");
return;
}
printf("Linked list contents are\n");
while(ptr!=NULL)
{
printf("%d->",ptr->info);
ptr=ptr->Rlink;
}
printf("End of List");
}
void deleteL(int data)
{
struct node *ptr,*prev,*next;
ptr=start;
prev=NULL;
if(ptr==NULL)
{
printf("\n Linked list is empty,nothing to delete\n");
return;
}
else if(start->info==data)
{
start=start->Rlink;
start->Llink=NULL;
free(ptr);
printf("%d is deleted\n",data);
display();
return;
}
while((ptr->info!=data)&&(ptr!=NULL))
{
prev=ptr;
ptr=ptr->Rlink;
}
if(ptr==NULL)
printf("Data %d is not found in the linked list,delete is not possible\n",data);
else
{
//updating previous link
prev->Rlink=ptr->Rlink;
/*updating next link,
by storing next node addrees in next and updated next node Llink*/
next = ptr->Rlink;
next->Llink = ptr->Llink;
free(ptr);
printf("%d is deleted\n",data);
display();
}
return;
}
OUTPUT:
For Insertion:
For Display
For Delete
Program 15
while(element!=0)
{
tree=insert(tree,element);
scanf("%d",&element);
}
printf("\n Inorder traversal:");
inorder(tree);
printf("\n Preorder traversal:");
preorder(tree);
printf("\n Post order traversal:");
postorder(tree);
getch();
}
/*function to insert a node at front*/
btnode * insert(btnode *tree,int ele)
{
btnode *p,*q,*r;
p=(btnode*)malloc(sizeof(btnode));
p->data=ele;
p->lchild=p->rchild=NULL;
if(tree==NULL)
{
return(p);
}
else
{
q=NULL;
r=tree;
while(r!=NULL)
{
q=r;
if(ele==r->data)
{
printf("\n no duplicate elements");
free(p);
return(tree);
}
if(ele < r->data)
r=r->lchild;
else
r=r->rchild;
}
if(ele < q->data)
q->lchild=p;
else
q->rchild=p;
}
return(tree);
}
/*inorder traversal*/
inorder(btnode *tree)
{
if(tree!=NULL)
{
inorder(tree->lchild);
printf("\t%d",tree->data);
inorder(tree->rchild);
}
return;
}
/*preorder traversal*/
preorder(btnode *tree)
{
if(tree!=NULL)
{
printf("\t %d",tree->data);
preorder(tree->lchild);
preorder(tree->rchild);
}
return;
}
/*postorder traversal*/
postorder(btnode *tree)
{
if(tree!=NULL)
{
postorder(tree->lchild);
postorder(tree->rchild);
printf("\t %d",tree->data);
}
return;
}
OUTPUT 1:
OUTPUT 2: