You are on page 1of 51

JNTUK-R20

DATA STRUCTURES
LAB MANUAL
DATA STRUCTURES LAB MANUAL-R20

1. WRITE C PROGRAM THAT USE BOTH RECURSIVE AND NON-RECURSIVE


FUNCTIONS TO PERFORM LINEAR SEARCH FOR A KEY VALUE IN A GIVEN LIST
#include<stdio.h>
void main ()
{
int a[10] = {10, 23, 40, 1, 2, 0, 14, 13, 50, 9};
int item, i,flag;
printf("\nEnter Item which is to be searched\n");
scanf("%d",&item);
for (i = 0; i< 10; i++)
{
if(a[i] == item)
{
flag = i+1;
break;
}
else
flag = 0;
}
if(flag != 0)
{
printf("\nItem found at location %d\n",flag);
}
else
{
printf("\nItem not found\n");
}
}

Ouput:

Enter Item which is to be searched


20
Item not found
Enter Item which is to be searched
23
Item found at location 2

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

2.WRITE C PROGRAM THAT USE BOTH RECURSIVE AND NON-RECURSIVE


FUNCTIONS TO PERFORM BINARY SEARCH FOR A KEY VALUE IN A GIVEN LIST
#include<stdio.h>
int binarySearch(int[], int, int, int);
void main ()
{
int arr[10] = {16, 19, 20, 23, 45, 56, 78, 90, 96, 100};
int item, location=-1;
printf("Enter the item which you want to search ");
scanf("%d",&item);
location = binarySearch(arr, 0, 9, item);
if(location != -1)
{
printf("Item found at location %d",location);
}
else
{
printf("Item not found");
} }
int binarySearch(int a[], int beg, int end, int item)
{
int mid;
if(end >= beg)
{
mid = (beg + end)/2;
if(a[mid] == item)
{
return mid+1;
}
else if(a[mid] < item)
{
return binarySearch(a,mid+1,end,item);
}
else
{
return binarySearch(a,beg,mid-1,item);
} }
return -1;
}
Output:
Enter the item which you want to search
19

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

Item found at location 2


3.WRITE C PROGRAM THAT IMPLEMENTS BUBBLE SORT ,TO SORT A GIVEN
LIST OF INTEGERS IN ASCENDING ORDER

#include<stdio.h>
void print(int a[], int n) //function to print array elements
{
int i;
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
}
void bubble(int a[], int n) // function to implement bubble sort
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
} } } }
void main ()
{
int i, j,temp;
int a[5] = { 10, 35, 32, 13, 26};
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
print(a, n);
bubble(a, n);
printf("\nAfter sorting array elements are - \n");
print(a, n);
}
Output

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

4.WRITE C PROGRAM THAT IMPLEMENTS QUICK SORT ,TO SORT A GIVEN


LIST OF INTEGERS IN ASCENDING ORDER

#include <stdio.h>
/* function that consider last element as pivot,
place the pivot at its exact position, and place
smaller elements to left of pivot and greater
elements to right of pivot. */
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);

for (int j = start; j <= end - 1; j++)


{
// If current element is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}

/* function to implement quick sort */


void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting index, end = Endi
ng index */
{
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
DATA STRUCTURES LAB MANUAL-R20

/* function to print an array */


void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 24, 9, 29, 14, 19, 27 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);

return 0;
}
Output:

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

5.WRITE C PROGRAM THAT IMPLEMENTS INSERTION SORT ,TO SORT A GIVEN


LIST OF INTEGERS IN ASCENDING ORDER

#include <stdio.h>

void insert(int a[], int n) /* function to sort an aay with insertion sort */
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;

while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one position ahea
d from their urrent position*/
{
a[j+1] = a[j];
j = j-1;
}
[j+1] = temp;
}}

void printArr(int a[], int n) /* function to print the array */


{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
insert(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
DATA STRUCTURES LAB MANUAL-R20

return 0;
}
Output:

6. WRITE C PROGRAM THAT IMPLEMENTS RADIX SORT ,TO SORT A GIVEN


LIST OF INTEGERS IN ASCENDING ORDER

#include <stdio.h>
int largest(int a[]);
void radix_sort(int a[]);
void main()
{
int i;
int a[10]={90,23,101,45,65,23,67,89,34,23};
radix_sort(a);
printf("\n The sorted array is: \n");
for(i=0;i<10;i++)
printf(" %d\t", a[i]);
}

int largest(int a[])


{
int larger=a[0], i;
for(i=1;i<10;i++)
{
if(a[i]>larger)
larger = a[i];
}
return larger;
}
void radix_sort(int a[])
{
int bucket[10][10], bucket_count[10];
int i, j, k, remainder, NOP=0, divisor=1, larger, pass;
larger = largest(a);
while(larger>0)
{
NOP++;
larger/=10;
DATA STRUCTURES LAB MANUAL-R20

}
for(pass=0;pass<NOP;pass++) // Initialize the buckets
{
for(i=0;i<10;i++)
bucket_count[i]=0;
for(i=0;i<10;i++)
{
// sort the numbers according to the digit at passth place
remainder = (a[i]/divisor)%10;
bucket[remainder][bucket_count[remainder]] = a[i];
bucket_count[remainder] += 1;
}
// collect the numbers after PASS pass
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bucket_count[k];j++)
{
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
}
}

Output:

The sorted array is:


23
23
23
34
45
65
67
89
90
101
DATA STRUCTURES LAB MANUAL-R20

7. WRITE C PROGRAM THAT IMPLEMENTS MERGE SORT ,TO SORT A GIVEN


LIST OF INTEGERS IN ASCENDING ORDER

#include<stdio.h>
void mergeSort(int[],int,int);
void merge(int[],int,int,int);
void main ()
{
int a[10]= {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i;
mergeSort(a,0,9);
printf("printing the sorted elements");
for(i=0;i<10;i++)
{
printf("\n%d\n",a[i]);
}

}
void mergeSort(int a[], int beg, int end)
{
int mid;
if(beg<end)
{
mid = (beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
merge(a,beg,mid,end);
}
}
void merge(int a[], int beg, int mid, int end)
DATA STRUCTURES LAB MANUAL-R20

{
int i=beg,j=mid+1,k,index = beg;
int temp[10];
while(i<=mid && j<=end)
{
if(a[i]<a[j])
{
temp[index] = a[i];
i = i+1;
}
else
{
temp[index] = a[j];
j = j+1;
}
index++;
}
if(i>mid)
{
while(j<=end)
{
temp[index] = a[j];
index++;
j++;
}
}
else
{
while(i<=mid)
{
temp[index] = a[i];
index++;
i++;
}
}
k = beg;
while(k<index)
{
a[k]=temp[k];
k++;
}
}
DATA STRUCTURES LAB MANUAL-R20

Output:
printing the sorted elements

7
9
10
12
23
23
34
44
78
101

8.

A.WRITE C PROGRAM THAT USES FUNCTIONS TO CREATE A SINGLY LINKED


LIST

B. WRITE C PROGRAM THAT USES FUNCTIONS TO PERFORM INSERTION


OPERATION ON A SINGLY LINKED LIST

C. WRITE C PROGRAM THAT USES FUNCTIONS TO PERFORM DELETION


OPERATION ON A SINGLY LINKED LIST

D. WRITE C PROGRAM TO REVERSE ELEMENTS OF A SINGLY LINKED LIST

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
DATA STRUCTURES LAB MANUAL-R20

void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete fro
m Beginning\
5.Delete from last\n6.Delete node after specified location\n7.Search for an element\n8.Sho
w\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
DATA STRUCTURES LAB MANUAL-R20

case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}

}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
DATA STRUCTURES LAB MANUAL-R20

if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");

}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
DATA STRUCTURES LAB MANUAL-R20

printf("\ncan't insert\n");
return;
}

}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}

else
{
ptr = head;
DATA STRUCTURES LAB MANUAL-R20

while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;

if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}

void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{ printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

Output:
*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

8.Show
9.Exit

Enter your choice?


1

Enter value
1

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


2

Enter value?
2

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

6.Delete node after specified location


7.Search for an element
8.Show
9.Exit

Enter your choice?


3

Enter element value1

Enter the location after which you want to insert 1

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


8

printing values . . . . .

1
2
1

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


2

Enter value?
123

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


1

Enter value
1234

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


4

Node deleted from the begining ...

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


5

Deleted Node from the last ...

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

4.Delete from Beginning


5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


6

Enter the location of the node after which you want to perform deletion
1

Deleted node 2

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


8

printing values . . . . .

1
1

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


7

Enter item which you want to search?


1
item found at location 1
item found at location 2

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit

Enter your choice?


9

9.Write c program that implements queue(its operations)using arrays

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n")
;
printf("\n==========================================================
======\n");
prntf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
pintf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
int item;

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

printf("\nEnter the element\n");


scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");

}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;

}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

Output:
*************Main Menu**************

==============================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter the element


123

Value inserted

*************Main Menu**************

==============================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter the element


90

Value inserted

*************Main Menu**************

===================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?2


VIKAS COLLEGE OF ENGINEERING AND
TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

value deleted

*************Main Menu**************
==============================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

printing values .....


90
*************Main Menu**************
==============================================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?4


**********Main Menu**********
==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter value?
123

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

Enter value?
90

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

printing values .....

123

90

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?2

***********Main Menu**********

==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

printing values .....

90

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

***********Main Menu**********
==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?4

10.Write c program that implements queue(its operations)using linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n")
;
printf("\n==========================================================
======\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
VIKAS COLLEGE OF ENGINEERING AND
TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

Output: 1-->3-->7-->5-->10
Size of Queue : 5
Front Element : 1
Rear Element : 10
Removed Element : 1
Removed Element : 3
Removed Element : 7

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

Removed Element : 5
Removed Element : 10

Queue is Empty

11.Write c program that implements stack(its operations)using arrays

#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
{

printf("Enter the number of elements in the stack ");


scanf("%d",&n);
printf("*********Stack operations using array*********");

printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

case 2:
{
pop();
break;
}
case 3:
{
show();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}

void push ()
{
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}

void pop ()
{
if(top == -1)
printf("Underflow");
else

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

top = top -1;


}
void show()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}

Enter the size of STACK[MAX=100]:10

STACK OPERATIONS USING ARRAY


--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12

Enter the Choice:1


Enter a value to be pushed:24

Enter the Choice:1


Enter a value to be pushed:98

Enter the Choice:3

The elements in STACK

98
24
12
Press Next Choice
Enter the Choice:2

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

The popped elements is 98


Enter the Choice:3

The elements in STACK

24
12
Press Next Choice
Enter the Choice:4

EXIT POINT

12.Write c program that implements stack(its operations)using linked list

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

printf("Enter the value");


scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;

}
printf("Item pushed");

}
}

void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");

}
}
void display()
{
int i;
struct node *ptr;

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}

Output:

Stack Size : 4

Top Element : 4
Stack as linked List
4-->3-->2-->1
Removed Element : 4
Removed Element : 3
Removed Element : 2
Removed Element : 1

Stack is Empty

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

13.write a c program that uses stack operations to evaluate postfix expression

#include<stdio.h> //standard input output functions


#include<conio.h> //console functions
#include<string.h> //string functions
#define MAX 50 //max size defined
int stack[MAX]; //a global stack
char post[MAX]; //a global postfix stack
int top=-1; //initializing top to -1
void pushstack(int tmp); //push function
void evaluate(char c); //calculate function
void main()
{
int i,l;
//clrscr();
printf("Insert a postfix notation :: ");
VIKAS COLLEGE OF ENGINEERING AND
TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

gets(post); //getting a postfix expression


l=strlen(post); //string length
for(i=0;i<l;i++)
{
if(post[i]>='0' && post[i]<='9')
{
pushstack(i); //if the element is a number push it
}
if(post[i]=='+' || post[i]=='-' || post[i]=='*' ||
post[i]=='/' || post[i]=='^') //if element is an operator
{
evaluate(post[i]); //pass it to the evaluate
}
} //print the result from the top
printf("\n\nResult :: %d",stack[top]);
getch();
}

void pushstack(int tmp) //definiton for push


{
top++; //incrementing top
stack[top]=(int)(post[tmp]-48); //type casting the string to its integer value
}

void evaluate(char c) //evaluate function


{
int a,b,ans; //variables used
a=stack[top]; //a takes the value stored in the top
stack[top]='\0'; //make the stack top NULL as its a string
top--; //decrement top's value
b=stack[top]; //put the value at new top to b
stack[top]='\0'; //make it NULL
top--; //decrement top
switch(c) //check operator been passed to evaluate
{
case '+': //addition
ans=b+a;
break;
case '-': //subtraction
ans=b-a;
break;
VIKAS COLLEGE OF ENGINEERING AND
TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

case '*': //multiplication


ans=b*a;
break;
case '/': //division
ans=b/a;
break;
case '^': //power
ans=b^a;
break;
default:
ans=0; //else 0
}
top++; //increment top
stack[top]=ans; //store the answer at top
}

Output:

14.write a recursive c program for traversing a binary tree in pre-order, in order ,post
order
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *left,*right;
};
struct node *root;

void ins(struct node *n,int val,int opt)


{

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

struct node *t;


t=(struct node *)malloc(sizeof(struct node));
t->data=val;
t->right=t->left=NULL;
if (opt==1)
n->left=t;
else
n->right=t;
printf("\n %d is inserted",val);
if (opt==1)
{
printf("\tat the left\n");
getch();
}
else
{
printf("\tat the right\n");
getch();
}
}

void inser(struct node *t,int x)


{
if (t->data >x)
if (t->left==NULL)
ins(t,x,1);
else
inser(t->left,x);
else if (t->data < x)
if (t->right==NULL)
ins(t,x,2);
else
inser(t->right,x);
else
printf("\n Element is already present in the list\n");
}

void inorder(struct node *p)


{
if (p!=NULL)
{
inorder(p->left);
printf("\n %5d",p->data);
inorder (p->right);
}
}

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

void preorder(struct node *p)


{
if (p!=NULL)
{
printf("\n %5d",p->data);
preorder(p->left);
preorder (p->right);
}
}
void postorder(struct node *p)
{
if (p!=NULL)
{
preorder(p->left);
preorder (p->right);
printf("\n %5d",p->data);
}
}
void main()
{
int op,n;
root=(struct node *)malloc(sizeof(struct node));
root->data=30;
root->right=root->left=NULL;
clrscr();
do
{
printf("\n 1.Insertion");
printf("\n 2.Preorder");
printf("\n 3.Inorder");
printf("\n 4.Postorder");
printf("\n 5.Quit");
printf("\n Enter your choice\n");
scanf("%d",&op);

switch (op)
{
case 1: printf("\n Enter the element to insert\n");
scanf("%d",&n);
inser(root,n);
break;
case 2: printf("\n The preorder elements are\n");
preorder(root);
getch();
break;

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

case 3: printf("\n The inorder elements are\n");


inorder(root);
getch();
break;

case 4: printf("\n The postorder elements are\n");


postorder(root);
getch();
break;
default: exit(0);
}
}while(op<5);
getch();

OUTPUT:

Inorder: 19 20 23 25 26 31 36
Preorder: 25 20 19 23 31 26 36
Postorder: 19 23 20 26 36 31 25

15.
a. Write c program to create a BST
b. Write c program to insert a node into a BST
c. . Write c program to delete a node from a BST
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node {
int data;
Node *left;
Node *right;
};
Node* create(int item)
VIKAS COLLEGE OF ENGINEERING AND
TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

{
Node* node = new Node;
node->data = item;
node->left = node->right = NULL;
return node;
}

void inorder(Node *root)


{
if (root == NULL)
return;

inorder(root->left);
cout<< root->data << " ";
inorder(root->right);
}
Node* findMinimum(Node* cur)
{
while(cur->left != NULL) {
cur = cur->left;
}
return cur;
}
Node* insertion(Node* root, int item)
{
if (root == NULL)
return create(item);
if (item < root->data)
root->left = insertion(root->left, item);
else
root->right = insertion(root->right, item);

return root;
}

void search(Node* &cur, int item, Node* &parent)


{
while (cur != NULL && cur->data != item)
{
parent = cur;

if (item < cur->data)

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

cur = cur->left;
else
cur = cur->right;
}
}

void deletion(Node*& root, int item)


{
Node* parent = NULL;
Node* cur = root;

search(cur, item, parent);


if (cur == NULL)
return;

if (cur->left == NULL && cur->right == NULL)


{
if (cur != root)
{
if (parent->left == cur)
parent->left = NULL;
else
parent->right = NULL;
}
else
root = NULL;

free(cur);
}
else if (cur->left && cur->right)
{
Node* succ = findMinimum(cur- >right);

int val = succ->data;

deletion(root, succ->data);

cur->data = val;
}
else
{
Node* child = (cur->left)? Cur- >left: cur->right;

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

if (cur != root)
{
if (cur == parent->left)
parent->left = child;
else
parent->right = child;
}

else
root = child;
free(cur);
}
}

int main()
{
Node* root = NULL;
int keys[8];
for(int i=0;i<8;i++)
{
cout << "Enter value to be inserted";
cin>>keys[i];
root = insertion(root, keys[i]);
}

inorder(root);
cout<<"\n";
deletion(root, 10);
inorder(root);
return 0;
}
Output:
Enter value to be inserted? 10
Enter value to be inserted? 20
Enter value to be inserted? 30
Enter value to be inserted? 40
Enter value to be inserted? 5
Enter value to be inserted? 25
Enter value to be inserted? 15
Enter value to be inserted? 5

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY
DATA STRUCTURES LAB MANUAL-R20

5 5 10 15 20 25 30 40
5 5 15 20 25 30 40

VIKAS COLLEGE OF ENGINEERING AND


TECHNOLOGY

You might also like