0% found this document useful (0 votes)
23 views41 pages

DS Lab Programss

Lab programs for bca students for their study

Uploaded by

b39252502
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)
23 views41 pages

DS Lab Programss

Lab programs for bca students for their study

Uploaded by

b39252502
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/ 41

UUCMS NO: Subject: DS LAB

PART A
1. PROGRAM TO FIND GCD USING RECURSIVE FUNCTION

#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}

int hcf(int n1, int n2) {


if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}

OUTPUT:

Enter two positive integers:


60
G.C.D of 366 and 60 is 6.

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 1


UUCMS NO: Subject: DS LAB

2. PROGRAM TO GENERATE BINOMIAL CO-EFFICIENT UING RECURSIVE FUNCTION


#include<stdio.h>
int BinCof(int n, int k)
{
if(k==0 || k==n)
return 1;
else
return BinCof(n-1,k-1)+BinCof(n-1,k);
}
int main()
{
int n=4, k=2;
printf("Value of C(%d,%d) is %d", n,k,BinCof(n,k));
return 0;

OUTPUT:

Value of C(4,2) is 6

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 2


UUCMS NO: Subject: DS LAB

3. PROGRAM TO GENERATE N FIBONACCI NUMBERS USING RECURSIVE FUNCTION

#include<stdio.h>
int fibonacci_series(int);
int main()
{
int count, c = 0, i;
printf("Enter number of terms:");
scanf("%d",&count);

printf("\nFibonacci series:\n");
for ( i = 1 ; i <= count ; i++ )
printf("%d\n", fibonacci_series(c));
c++;
}

return 0;
}
int fibonacci_series(int num)
{
if ( num == 0 )
return 0;
else if ( num == 1 )
return 1;
else
return ( fibonacci_series(num-1) + fibonacci_series(num-2) );
}

OUTPUT:

Enter number of terms: 6


Fibonacci series:
0
1
1
2
3
5

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 3


UUCMS NO: Subject: DS LAB

4. PROGRAM TO IMPLEMENT TOWER OF HONOI USING RECURSION

#include<stdio.h>

void tower(int,
char,char,char);

void main()

int n;

char a,b,c;

printf("\n enter the


number of disc");

scanf("%d", &n);

printf("tower of
Hanoi of %d disc
\n",n);

tower(n,'a','b','c');

void tower(int n, char


beg, char aux, char
end)

if(n<=0)

printf("\n illegal
entry");

else if(n==1)

printf("\n move disc


%d from %c to %c", n,
beg,end);

else

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 4


UUCMS NO: Subject: DS LAB
tower(n-1, beg, end,
aux);

printf("\n move disc


%d from %c to %c", n,
beg,end);

tower(n-1, aux,beg,
end);

OUTPUT:

student@ubuntu:~$
gcc pgm4a.c

student@ubuntu:~$
./a.out

enter the number of disc3

tower of Hanoi of 3 disc

move disc 1 from a to c

move disc 2 from a to b

move disc 1 from c to b

move disc 3 from a to c

move disc 1 from b to a

move disc 2 from b to c

move disc 1 from a to c

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 5


UUCMS NO: Subject: DS LAB

5. PROGRAM TO IMPLEMENT DYNAMIC ARRAY, FIND SMALLEST AND LARGEST


ELEMENT OF ARRAY

#include<stdio.h>
#include<stdlib.h>

int main()
{
int i,n,small,large;
int *ptr;

printf("Enter the total number of elements: ");


scanf("%d", &n);

// Allocating memory for n elements


ptr= (int *)calloc(n, sizeof(int));
if (ptr == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}

// Storing numbers entered by the user.


printf("Enter %d Elements:\n", n);
for (i = 0; i < n; ++i)
{
scanf("%d",ptr + i);
}

// Finding the smallest number


small = *ptr;
for (i = 1; i < n; ++i)
{
if (small >*(ptr + i))
{
small = *(ptr + i);
}
}
printf("\nsmallest number = %d", small);

// Finding the largest number


large = *ptr;
for (i = 1; i < n; ++i)
{
if (large <*(ptr + i))
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 6
UUCMS NO: Subject: DS LAB

{
large = *(ptr + i);
}
}
printf("\nLargest number = %d", large);

free(ptr);

return 0;
}
OUTPUT:

[user@localhost dslab]$ gcc assign5A.c


[user@localhost dslab]$ ./a.out
Enter the total number of elements: 6
Enter 6 Elements:
2
4
3
5
6
8

smallest number = 2
Largest number = 8

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 7


UUCMS NO: Subject: DS LAB

6. PROGRAM TO READ THE NAMES OF CITIES AND ARRANGE THEM


ALPHABETICALLY

#include<stdio.h>
#include<string.h>
int main()
{
char name[20][20],temp[20];
int i,j,n;

printf("Enter how many cities :\n");


scanf("%d",&n);

printf("Enter %d names of the cities :\n",n);


for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}

printf("Cities in alphabetical order :\n");


for(i=0;i<n;i++)
{
printf("%5d. %s\n",i+1,name[i]);
}
return 0;
}

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 8


UUCMS NO: Subject: DS LAB

OUTPUT:

[user@localhost dslab]$ gcc assign6A.c


[user@localhost dslab]$ ./a.out
How many cities 5

Enter 5 City Names:

Hubballi

Belagavi

Gokak

Vijayapura

Haveri

City Names in ascending order:

Belagavi

Gokak

Haveri

Hubballi

Vijayapura

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 9


UUCMS NO: Subject: DS LAB

7. PROGRAM TO SORT THE GIVEN LIST USING SELECTION SORT TECHNIQUE

#include<stdio.h>
int main()
{
int a[10], n, i;
void selection(int[], int);

printf("\nEnter size: ");


scanf("%d", &n);

printf("\nEnter %d elements: ",n);


for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

selection(a, n);

printf("\nSorted Array\n");
for(i = 0; i < n; i++)
printf("\n%d", a[i]);

return 0;
}/* end of main */

/*function selection*/
void selection(int a[], int n)
{
int i, j, pos, temp, small;

for(i = 0; i < n; i++)


{
small = a[i];
pos = i;
for(j = i + 1; j < n; j++)
{
if(a[j] < small)
{
small = a[j];
pos = j;

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 10


UUCMS NO: Subject: DS LAB

}
}
temp = a[pos];
a[pos] = a[i];
a[i] = temp;
}
}
/*end of function selection*/

OUTPUT:
[user@localhost dslab]$ gcc assign7A.c
[user@localhost dslab]$ ./a.out

Enter size: 7

Enter 7 elements: 4 1 3 8 6 9 5

Sorted Array

1
3
4
5
6
8
9

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 11


UUCMS NO: Subject: DS LAB

8. PROGRAM TO SORT THE GIVEN LIST USING BUBBLE SORT TECHNIQUE


#include<stdio.h>
int main()
{
int i, n, a[20];
void bubble(int[], int);

printf("\nEnter size: ");


scanf("%d", &n);

printf("\nEnter %d elements: ",n);


for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

bubble(a,n);

printf("The sorted element are \n");


for(i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}

return 0;
}

void bubble(int a[], int n)


{
int i, j, temp;
for(j = 1; j < n; j++)
{
for(i = 0; i < n-j ; i++)
{
if( a[i] > a[i+1] )
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 12
UUCMS NO: Subject: DS LAB

}
}

OUTPUT:
[user@localhost dslab]$ gcc assign8A.c
[user@localhost dslab]$ ./a.out

Enter size: 6

Enter 6 elements: 8 3 7 2 6 1
The sorted element are
1 2 3 6 7 8

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 13


UUCMS NO: Subject: DS LAB

9. PROGRAM TO SORT THE GIVEN LIST USING INSERTION SORT TECHNIQUE


#include<stdio.h>
int main()
{
int a[10], n, i;
void insertion(int[],int);

printf("\nEnter size : ");


scanf("%d",&n);

printf("\nEnter %d elements : ",n);


for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

nsertion(a, n);

printf("\nSorted Array\n");
for(i = 0; i < n; i++)
printf("\n%d\n", a[i]);
return 0;
}/* end of main */

/*function insertion*/
void insertion(int a[], int n)
{
int i, j, temp;
for(i = 1; i < n; i++)
{
for(j = i; j >= 1; j--)
{
if( a[j] < a[j-1] )
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
}/*end of function insertion*/
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 14
UUCMS NO: Subject: DS LAB
OUTPUT:
[user@localhost dslab]$ gcc assign9A.c
[user@localhost dslab]$ ./a.out

Enter size : 7

Enter 7 elements : 2 8 1 6 5 9 3

Sorted Array

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 15


UUCMS NO: Subject: DS LAB

PART B
1. Program to sort the given list using quick sort technique.
#include<stdio.h>
int main()
{
int a[10], low,high, i,j, n;
printf("\nEnter number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements : ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
low=0;
high=n-1;
quicksort(a, low, high);
printf("\nSorted elements are :");
for(i=0;i<n;i++)
{
printf("\t %d", a[i]);
}
return 0;
}

int quicksort(int a[], int low, int high)


{
int keypos;
if(low>high)
return 0;
keypos=partition(a,low, high);
quicksort(a, low, keypos-1);
quicksort(a, keypos+1,high);
}

int partition(int a[], int low, int high)


{
int j, i, pivot, temp, flag=1; pivot=a[low];
i=low+1;
j =high;
flag=1;
while(flag==1)
{
while((a[i]<pivot)&&(i<j))
i=i+1;

while(a[j]>pivot)
j=j-1;

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 16


UUCMS NO: Subject: DS LAB

if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
flag=0;
}

temp=a[low];
a[low]=a[j];
a[j]=temp;
return(j);
}

OUTPUT:

Enter number of elements :5


Enter the elements :98 78 23 54 76
Sorted elements are : 23 54 76 78 98

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 17


UUCMS NO: Subject: DS LAB

2. Program to sort the given list using merge sort technique.


#include<stdio.h>

void mergesort(int a[],int i,int j);


void merge(int a[],int i1,int j1,int i2,int j2);

int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);

printf("Enter array elements:");


for(i=0;i<n;i++)
scanf("%d",&a[i]);

mergesort(a,0,n-1);

printf("\nSorted array is :");


for(i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}

void mergesort(int a[],int i,int j)


{
int mid;

if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);//left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}

void merge(int a[],int i1,int j1,int i2,int j2)


{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 18


UUCMS NO: Subject: DS LAB

j=i2; //beginning of the second list


k=0;

while(i<=j1 && j<=j2)//while elements in both lists


{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}

while(i<=j1) //copy remaining elements of the first list


temp[k++]=a[i++];

while(j<=j2) //copy remaining elements of the second list


temp[k++]=a[j++];

//Transfer elements from temp[] back to a[]


for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

OUTPUT:

Enter no of elements :5
Enter array elements :98 79 25 54 76
Sorted arrays are : 25 54 76 79 98

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 19


UUCMS NO: Subject: DS LAB

3. Program to search an element using linear search technique.

#include<stdio.h>
int main()
{
int i,key,flag=0,a[10],n;
printf("\n Enter how many no : ");
scanf("%d",&n);

printf("\n Enter the elements:");


for(i=1;i<=n;i++)
scanf("%d",&a[i]);

printf("\n Enter the element to be searched:");


scanf("%d",&key);

or(i=1;i<=n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
if(flag==1)
printf("\n Search successful!... Element found at position :%d \n", i);
else
printf("\n Search Unsuccessful!... \n");
return 0;
}

OUTPUT:

Enter how many no : 5


Enter the elements : 24 56 12 9 76
Enter the element to be searched : 12
Search successful !... Element found at position : 3

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 20


UUCMS NO: Subject: DS LAB

4. Program to search an element using binary search technique.


#include<stdio.h>
int main()
{
int a[10],n,i,key,low,high,mid;
printf("\n Enter no of elements : ");
scanf("%d",&n);
printf("\n Enter the elements : ");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the element to be searched");
scanf("%d",&key);
low = 1;
high = n;

while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
printf("\n Search Successful !... Element found at position :%d ",mid);
return 0;
break;
}
if(key<a[mid])
high=mid-1;
else
low = mid+1;
}

printf("\n Search Unsuccessful !... ");

return 0;
}

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 21


UUCMS NO: Subject: DS LAB

OUTPUT:

Enter how many no : 5


Enter the elements : 20 30 40 50 60

Enter the element to be searched : 50


Search successful !...
Element found at position : 4

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 22


UUCMS NO: Subject: DS LAB

5. Program to implement Stack.


#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 3

int stack[MAXSIZE];
int top = -1;

void push();
void pop();
void display();

int main()
{
int ch;

while(1)
{
printf("\n\n\t\t\t STACK OPERATIONS ");
printf("\n\n\t\t\t 1.Push");
printf("\n\t\t\t 2.Pop");
printf("\n\t\t\t 3.Display");
printf("\n\t\t\t 4.Exit");
printf("\n\n\t\t\t Enter your choice: ");
scanf("%d",&ch);

switch(ch)
{
case 1:push();
break;

case 2:pop();
break;

case 3:display();
break;

case 4: exit(0);

default : printf("Invalid choice\n");

}
}
}

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 23


UUCMS NO: Subject: DS LAB

/**************push function********************/
void push()
{
int element;

if(top == MAXSIZE-1)
{
printf("\n\tStack Full \n");
}
else
{
top = top + 1;
printf("\nEnter element to be inserted \n");
scanf("%d", &element);
stack[top] = element;
}
}

/**************pop function***********************/
void pop()
{
if(top == -1)
{
printf("\n \t\tStack Empty........deletion not possible");
}
else
{
printf("\nDeleted elements is = %d\n",stack[top]);
top = top -1;
}
}

/**************display function*********************/
void display()
{
int i;

if(top==-1)
{
printf("Stack Empty --------- no more elements to display\n");
}
else
{
printf("\nStack elements are\n");
for(i = top; i >= 0; i--)
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 24
UUCMS NO: Subject: DS LAB
{
printf("\t%d\n", stack[i]);
}
}
}
OUTPUT:

[user@localhost dslab]$ gcc assign5B.c


[user@localhost dslab]$ ./a.out

1 STACK OPERATIONS 4 STACK OPERATIONS

1.Push 1.Push
2.Pop 2.Pop
3.Display 3.Display
4.Exit 4.Exit

Enter your choice: Enter your choice: 1


1
Stack Full
Enter element to be inserted
10

2 STACK OPERATIONS 5 STACK OPERATIONS

1.Push 1.Push
2.Pop 2.Pop
3.Display 3.Display
4.Exit 4.Exit

Enter your choice: Enter your choice: 3


1
Stack elements are
Enter element to be inserted 30
20 20
10

3 STACK OPERATIONS 6 STACK OPERATIONS

1.Push 1.Push
2.Pop 2.Pop
3.Display

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 25


UUCMS NO: Subject: DS LAB

4.Exit 3.Display
4.Exit
Enter your choice:
1 Enter your choice: 2

Enter element to be inserted Deleted elements is = 30


30

7 STACK OPERATIONS 10 STACK OPERATIONS

1.Push 1.Push
2.Pop 2.Pop
3.Display 3.Display
4.Exit 4.Exit

Enter your choice: Enter your choice: 6


2 Invalid choice

Deleted elements is = 20

8 STACK OPERATIONS 11 STACK OPERATIONS

1.Push 1.Push
2.Pop 2.Pop
3.Display 3.Display
4.Exit 4.Exit

Enter your choice: Enter your choice: 4


2

Deleted elements is = 10

9 STACK OPERATIONS

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:


2
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 26
Stack Empty ....... deletion not possible
UUCMS NO: Subject: DS LAB

6. Program to convert an infix expression to postfix.

#include<stdio.h>
#include<string.h>

int in = 0, pos = 0, top = -1, length;


char temp, symbol;
char stack[20], infix[20], postfix[20];

void push(char);
char pop();
void infix_postfix(char[ ], char[ ]);
int preced(char);

int main()
{
printf("\nEnter the infix expression:");
scanf("%s", infix);

printf("\ninfix expression : %s", infix);

infix_postfix(infix , postfix);

printf("\npostfix expression : %s\n", postfix);

return(0);
}

void infix_postfix(char infix[ ], char postfix[ ])


{
length = strlen(infix);
push( ' # ' );
while( in < length )
{
symbol = infix[in];

switch(symbol)
{
case ' ( ' : push(symbol);
break;
case ' ) ' : temp = pop();
while( temp != ' ( ' )
{
postfix[ pos++ ] = temp;
temp = pop();
}
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 27
UUCMS NO: Subject: DS LAB

break;
case ' + ' :
case ' - ' :
case ' * ' :
case ' / ' : while(preced(stack[top]) >= preced(symbol))
{
temp = pop();
postfix[pos++] = temp;
}
push(symbol);
break;
default :postfix[pos++] = symbol;
break;
}
in++;
}
while(top>0)
{
temp = pop();
postfix[pos++] = temp;
}
}

void push(char symb)


{
top = top + 1;
stack[top] = symb;
}

char pop()
{
char symb;
symb = stack[top];
top = top - 1;
return(symb);
}

int preced(char symb)


{
int p;
switch(symb)
{
case ' * ' :
case ' / ' : p=2;
break;
case ' + ' :
case ' - ' : p = 1;
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 28
UUCMS NO: Subject: DS LAB

break;
case ' ( ':
case ' ) ' : p = 0;
break;

case ' # ': p = -1;


break;
}
return(p);
}
OUTPUT:

[user@localhost dslab]$ gcc assign6B.c


[user@localhost dslab]$ ./a.out

Enter the infix expression:a+b*c

infix expression : a+b*c


postfix expression : abc*+

[user@localhost dslab]$ ./a.out

Enter the infix expression:(a+b)*c

infix expression:(a+b)*c
postfix expression:ab+c*

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 29


UUCMS NO: Subject: DS LAB

7. Program to implement simple queue.


#include<stdio.h>
#include<stdlib.h>
#define MAX 3

void insert();
void delete();
void display();

int queue[MAX];
int rear = -1;
int front = -1;

int main()
{
int choice;

while(1)
{
printf("\t\t\t1.Insert element to queue \n");
printf("\t\t\t2.Delete element from queue \n");
printf("\t\t\t3.Display all elements of queue \n");
printf("\t\t\t4.Quit \n");

printf("Enter your choice : ");


scanf("%d", &choice);

switch(choice)
{
case 1 : insert();
break;

case 2 : delete();
break;

case 3 : display();
break;

case 4 : exit(0);

default : printf("Invalid choice \n");


}
}
}

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 30


UUCMS NO: Subject: DS LAB

/**************insert function********************/
void insert()
{
int element;

if(rear == MAX-1)
{
printf("Queue Overflow \n");
}
else
{
if(front == -1)
front = 0;

printf("\tInsret the element in queue :");


scanf("%d",&element);

rear = rear+1;
queue[rear] = element;
}
}

/**************delete function***********************/
void delete()
{
if(front == -1 || front > rear)
{
printf("Queue Underflow \n");
return;
}
else
{
printf("\tElement deleted from queue is : %d \n", queue[front]);
front = front + 1;
}
}

/**************display function*********************/
void display()
{
int i;

if(front == -1 || front > rear)


printf("Queue is empty \n");
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 31
UUCMS NO: Subject: DS LAB

else
{
printf("Queue is :");
for(i = front; i<= rear; i++)
printf("\t%d", queue[i]);
printf("\n");
}
}

OUTPUT:

[user@localhost dslab]$ gcc assign7B.c


[user@localhost dslab]$ ./a.out

1.Insert element to queue


2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Insret the element in queue :10

1.Insert element to queue


2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Insret the element in queue :20

1.Insert element to queue


2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Insret the element in queue :30

1.Insert element to queue


2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Queue Overflow

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 32


UUCMS NO: Subject: DS LAB
Enter your choice : 3

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 33


UUCMS NO: Subject: DS LAB

1.Insert element to queue 2.Delete element from queue


3.Display all elements of queue4.Quit

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 34


UUCMS NO: Subject: DS LAB

Queue is : 10 20 30

1.Insert element to queue


2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 10

1. Insert element to queue


2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 20
1. Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 30

1.Insert element to queue


2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Queue Underflow

1.Insert element to queue


2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 7
Invalid choice

1.Insert element to queue


2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 4

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 35


UUCMS NO: Subject: DS LAB

8. Program to implement linear linked list.


#include<stdio.h>
#include<stdlib.h>
struct student
{
int rno;
struct student *next;
};

int main()
{
struct student *head,*temp,*newnode;
int choice;
head=NULL;

do
{
printf("\n MENU");
printf("\n 1. INSERT BEGINNING\n 2. DELETE BEGINNING \n 3. DISPlAY
\n 4. EXIT");
printf("\n Enter your Choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
newnode=(struct student *) malloc(sizeof(struct student));
printf("Enter Roll No:");
scanf("%d",&newnode->rno);

if(head == NULL)
{
head=newnode;
newnode->next=NULL;
}
else
{
newnode->next=head;
head=newnode;
}
printf("%d is inserted at beginning\n",newnode->rno);
break;
case 2:
if(head==NULL)
printf("Linked List is Empty\n");
else
{
temp=head;
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 36
UUCMS NO: Subject: DS LAB

head=head->next;
printf("%d is deleted at beginning\n",temp->rno);
free(temp);
}
break;

case 3:
if(head==NULL)
printf("Linked List is empty/n");
else
{
temp=head;
printf("Contents of linked list\n");
while(temp!=NULL)
{
printf("\t%d",temp->rno);
temp=temp->next;
}
}
break;
case 4:
printf("Exiting the linked list");
break;
default: printf("Invalid Option\n");
}
}
while(choice!=4);
return 0;
}

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 37


UUCMS NO: Subject: DS LAB
OUTPUT:

abcd@ubuntu:~/programs/PARTB$ gcc 8B.c


abcd@ubuntu:~/programs/PARTB$ ./a.out

MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice1
Enter Roll No:2001
2001 is inserted at beginning

MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice1
Enter Roll No:2003
2003 is inserted at beginning

MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice3
Contents of linked list
2003 2001
MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice2
2003 is deleted at beginning

MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice3
Contents of linked list
2001

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 38


UUCMS NO: Subject: DS LAB

MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice4
Exiting the linked list

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 39


UUCMS NO: Subject: DS LAB
9. Program to implement linear linked list.

#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}

// Create a new Node


struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
// Insert on the right of the node
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}
int main() {
struct node* root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);
printf("Inorder traversal \n");
inorderTraversal(root);

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 40


UUCMS NO: Subject: DS LAB
OUTPUT:
student@ubuntu:~$ gcc pgm9b.c
student@ubuntu:~$ ./a.out
Inorder traversal

4 ->2 ->1 ->3 ->student@ubuntu:~$

JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 41

You might also like