0% found this document useful (0 votes)
15 views65 pages

Final DS

The document outlines a lab course on Data Structures and Algorithms for MSc I Semester students, detailing various programming tasks including recursion, array operations, search algorithms, linked lists, and queue implementations. Each task is accompanied by sample code and expected outputs, demonstrating practical applications of data structures. The document serves as a comprehensive guide for students to develop their programming skills in C.

Uploaded by

Sowmya Santosh
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)
15 views65 pages

Final DS

The document outlines a lab course on Data Structures and Algorithms for MSc I Semester students, detailing various programming tasks including recursion, array operations, search algorithms, linked lists, and queue implementations. Each task is accompanied by sample code and expected outputs, demonstrating practical applications of data structures. The document serves as a comprehensive guide for students to develop their programming skills in C.

Uploaded by

Sowmya Santosh
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/ 65

DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

SI.No PRAGRAM Page No.

1 Program to find factorial of a given number using recursion function.

2 Design, develop and implement a menu driver program for various array
operations.
3 Program to demonstrate use of sequential search.

4 Program to demonstrate use of binary search.


5 Program to search for a student information using rollno as a key

Program to implement singly linked list perform Search, Insert and Delete
6
operation using dynamic memory allocation.

Program to implement doubly linked list perform Search, Insert and Delete
7
operation using dynamic memory allocation.

8 Program to implement stack using array implementation.


9 Program to implement stack using linked list, using dynamic memory allocation.

10 Reverse a string using dynamic memory allocation.


Program to convert infix to postfix expression using stack, using dynamic
11
memory allocation.

Program to the evaluation of postfix expression using stack dynamic memory


12
allocation.

13 Program to implement queue to perform enqueue and dequeue operations


using dynamic memory allocation.
Program to implement queue to perform enqueue and dequeue expression using
14
array operation.

15 Program to implement Double Ended Queue.

16 Program to implement priority queue.


17 Program to implement Tower of Hanoi using recursion.

18 Program to implement Fibonacci series using recursion.

19 Program to implement binary tree traversal.


20 Program to implement Hash Table with open addressing.

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

1. Program to find factorial of a given number using recursion function.


CODE:
#include<stdio.h>
#include<conio.h>
long int MultiplyNumbers(int n);
void main()
{
int n;
printf("Enter a number");
scanf("%d",&n);
printf("Factorial of %d is%ld",n,MultiplyNumbers(n));
return 0;
getch();
}
long int MultiplyNumbers(int n)
{
if(n>=1)
return n*MultiplyNumbers(n-1);
else
return(1);
}

OUTPUT:
Enter a number5
Factorial of 5 is120

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

2. Design, develop and implement a menu driver program in c for the


following array operations.
a) creating array of N integers elements,
b) display of array element with suitable headings,
c) inserting an element at a given valid position,
d) deleting a element at a given valid position,
e) exit

CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[10];
int n,i,pos,elem;
void create();
void display();
void insert();
void Delete();
void main()
{
int ch;

while(1)
{
Printf(“\n Array operation\n”);
printf("\n1.create\n 2.display\n 3.insert\n 4.delete\n 5.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:display();
break;
case 3:insert();
break;
case 4:Delete();
break;
case 5:exit(0);
break;
default:printf("I\nnvalid choice\n");

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

break;
}
}
}
void create()
{
printf("\nEnter the number of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void display()
{
printf("\nArray elements are\n");
for(i=0;i<n;i++)
{
printf("a[%d]=%d\t",i,a[i]);
}
}
void insert()
{
printf("\nEnter the position in which you want to insert\n");
scanf("%d",&pos);
if(pos>=n-1)
{
printf("\n Insertion not possible");
}
else
{
printf("\nEnter the value which you want to insert\n");
scanf("%d",&elem);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
a[pos]=elem;
n++;
}
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

void Delete()
{
printf("\nEnter the position which u want to delete\n");
scanf("%d",&pos);
if(pos==n-1)
{
printf("\n Deletion not possible\n");
}
else
{ elem=a[pos];
for(i=pos;i>=n;i++)
a[i]=a[i+1];
n--;
}
}

OUTPUT:
Array operation:
1.create
2.display
3.insert
4.delete
5.exit
1
Enter the number of elements
4
10
20
30
40
1.create
2.display
3.insert
4.delete
5.exit
2

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

Array elements are


a[0]=10 a[1]=20 a[2]=30 a[3]=40
1.create
2.display
3.insert
4.delete
5.exit
3
Enter the position in which you want to insert
2
Enter the value which you want to insert
80
1.create
2.display
3.insert
4.delete
5.exit
2
Array elements are
a[0]=10 a[1]=20 a[2]=80 a[3]=80 a[4]=40 a[5]=0
1.create
2.display
3.insert
4.delete
5.exit
4Enter the position which u want to delete
3
1.create
2.display
3.insert
4.delete
5.exit
2
Array elements are
a[0]=10 a[1]=20 a[2]=80 a[3]=80 a[4]=40
1.create
2.display
3.insert
4.delete
5.exit

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

3. Program to demonstrate use of sequential search.


CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,a[10],flag=0,key;
printf("Enter the size of an array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element to find\n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
flag=1;
}
if(flag==1)
printf("search element is found\n");
else
printf("search element is not found\n");
return 0;
}
OUTPUT:
Enter the size of an array
5
Enter the array elements
10
4
3
55
9
Enter the key element to find
4
search element is found

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

4. Program to demonstrate use of binary search.

CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[100],n,key,i,first,last,mid;
clrscr();
printf("Enter the size of array\n");
scanf("%d",&n);
printf("Enter %d integers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element\n");
scanf("%d",&key);
first=0;
last=n-1;
mid=(first+last)/2;
while(first<=last)
{
if(a[mid]<key)
first=mid+1;
else if(a[mid]==key)
{
printf("%d fount at location %d\n",n,key,mid+1);
break;
}
else
last=mid-1;
mid=(first+last)/2;
}
if(first>last)
printf("Elements is not found\n");
getch();
return 0;
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

OUTPUT:
Enter the size of array
5
Enter 5 integers
10
20
30
40
50
Enter the key element
40
5 found at location 40

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

5. Program to search for student information using roll no as a key.


CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stud
{
int roll;
char name[15];
int marks;
};
void main()
{
struct stud a[50];
int n,reply,sroll,option,i;
do
{
printf("\n1.Create records");
printf("\n2.Search record");
printf("\n3.exit");
printf("\nSelect proper option:");
scanf("%d",&option);
switch(option)
{
case 1:printf("\nEnter value of n:");
scanf("%d",&n);
read_data(a,n);

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

break;
case 2:
printf("\nEnter sroll:");
scanf("%d",&sroll);
reply=search_data(a,n,sroll);
if (reply == -1)
printf("\nNot found");
else
{
printf("\nRoll\tName\tMarks");
printf("\n%d\t%s\t%d",a[reply].roll,a[reply].name,a[reply].marks);
}
break;
case3:exit(0);
}
}while(1);
}
int read_data(struct stud a[],int n)
{
int i;
printf("Enter %d records\n",n);
for(i=0;i<n;i++)
{
printf("\nRoll:");
scanf("%d",&a[i].roll);
printf("\nName:");
Dept. Of CS, VNC, Hosapete. Page No.
DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

scanf("%s",a[i].name);
printf("\Marks:");
scanf("%d",&a[i].marks);
}
return;
}
int search_data(struct stud a[],int n,int sroll)
{
int i;
for(i=0;i<n;i++)
{
if(a[i].roll==sroll)
return(i);
}
return(-1);
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

OUTPUT:
1.Create records
2.Search record
3.exit
Select proper option:1

Enter value of n:2


Enter 2 records
Roll:1
Name:xyz
Marks:75
Roll:2
Name:abcMarks:8
1.Create records
2.Search record
3.exit

Select proper option:2


Enter sroll:2
Roll Name Marks
2 abc 80
1.Create records
2.Search record
3.exit
Select proper option:3
Process proper option :3

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

6. Program to implement singly linked list perform Search, Insert and Delete
operation using dynamic memory allocation.

CODE:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node
{
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node,
next_node;
int data;
int main()
{
int option = 0;
printf("Singly Linked List Example - All Operations\n");
while (option < 5)
{
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("4 : Count Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option:");
scanf("%d", &option);
switch (option)
{
case 1: insert();
break;
case 2: delete();
break;

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

case 3: display();
break;
case 4: count();
break;
default: break;
}
}
return 0;
}

void insert()
{
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);

temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));

temp_node->value = data;

if (first_node == 0)
{
first_node = temp_node;
}
else
{
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete()
{
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nDisplay Linked List : \n");
printf("\nEnter Position for Delete Element : \n");
scanf("%d", &pos);
if (pos > 0 && pos <= countvalue)

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

{
if (pos == 1)
{
temp_node = temp_node -> next;
first_node = temp_node;
printf("\nDeleted Successfully \n\n");
}
else
{
while (temp_node != 0)
{
if (i == (pos - 1))
{
prev_node->next = temp_node->next;
if(i == (countvalue - 1))
{
head_node = prev_node;
}
printf("\nDeleted Successfully \n\n");
break;
}
else
{
i++;
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
}
else
printf("\nInvalid Position \n\n");
}
void display()
{
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}
int count()
{
int count = 0;
temp_node = first_node;
while (temp_node != 0)
{
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

OUTPUT:
Singly Linked List Example - All Operations
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1

Enter Element for Insert Linked List :


10

Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1

Enter Element for Insert Linked List :


20

Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1

Enter Element for Insert Linked List :


30

Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

Others : Exit()
Enter your option:3

Display Linked List :


# 10 # # 20 # # 30 #
No Of Items In Linked List : 3

Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:
No Of Items In Linked List : 3
Display Linked List :
Enter Position for Delete Element :
3
Deleted Successfully
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

7. Program to implement doubly linked list perform Search, Insert and Delete
operation using dynamic memory allocation.
CODE
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node *prev, *next;
};
struct node* start = NULL;

void traverse(){
if (start == NULL) {
printf("\nList is empty\n");
return;
}
struct node* temp;
temp = start;
while (temp != NULL) {
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}

void insertAtFront()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->data = data;
temp->prev = NULL;
temp->next = start;
start = temp;
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

void insertAtEnd()
{
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->data = data;
temp->next = NULL;
trav = start;
if (start == NULL)
{
start = temp;
}
else
{
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}

void insertAtPosition()
{
int data, pos, i = 1;
struct node *temp, *newnode;
newnode = malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
printf("\nEnter position : ");
scanf("%d", &pos);
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
newnode->data = data;
temp = start;
// If start==NULL,
if (start == NULL)

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

{
start = newnode;
newnode->prev = NULL;
newnode->next = NULL;
}

else if (pos == 1)
{
newnode->next = start;
newnode->next->prev = newnode;
newnode->prev = NULL;
start = newnode;
}
Else
{
while (i < pos - 1)
{
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
}
}
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
}
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

void deleteEnd()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
}
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = start;
if (start == NULL)
printf("\nList is empty\n");
else
{
printf("\nEnter position : ");
scanf("%d", &pos);
if (pos == 1)
{
position = start;
start = start->next;
if (start != NULL)
{
start->prev = NULL;
}
free(position);
return;
}
while (i < pos - 1) {
temp = temp->next;
i++;

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

}
position = temp->next;
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
free(position);
}
}
int main()
{
int choice;
while (1)
{
printf("\n\t1 To see list\n");
printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
switch (choice)
{
case 1: traverse();
break;
case 2: insertAtFront();
break;
case 3: insertAtEnd();
break;
case 4: insertAtPosition();
break;
case 5: deleteFirst();

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

break;
case 6: deleteEnd();
break;
case 7: deletePosition();
break;
case 8: exit(1);
break;
default: printf("Incorrect Choice. Try Again \n");
continue;
}
}
return 0;
}

OUTPUT:
1 To see list
2 For insertion at starting
3 For insertion at end
4 For insertion at any position
5 For deletion of first element
6 For deletion of last element
7 For deletion of element at any position
8 To exit
Enter Choice : 2
Enter number to be inserted: 4

1 To see list
2 For insertion at starting
3 For insertion at end
4 For insertion at any position
5 For deletion of first element
6 For deletion of last element
7 For deletion of element at any position
8 To exit
Enter Choice : 3
Enter number to be inserted: 11

1 To see list
2 For insertion at starting
3 For insertion at end

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

4 For insertion at any position


5 For deletion of first element
6 For deletion of last element
7 For deletion of element at any position
8 To exit
Enter Choice : 3
Enter number to be inserted: 9

1 To see list
2 For insertion at starting
3 For insertion at end
4 For insertion at any position
5 For deletion of first element
6 For deletion of last element
7 For deletion of element at any position
8 To exit

Enter Choice :
1
Data = 4
Data = 11
Data = 9

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

8. Program to implement stack using array implementation.

CODE:
#include<stdio.h>
#include<conio.h>
#define N 5
int stack[N];
int top=-1;
void push();
void pop();
void peek();
void display();
void main()
{
int ch,item;
clrscr();
do
{
printf("Enter your choice\n1.push\n 2.pop\n 3.peek\n 4.display\n
5.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peek();
break;

case 4:display();
break;
case 5:exit(0);
default:printf("Invalid choice\n");
}
}while(ch!=0);
getch();
}
void push()

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

{
int x,item;
printf("Enter the data you want to push\n");
scanf("%d",&x);
if(top==N-1)
{
printf("Overflow\n");
}
else
{
top++;
stack[top]=x;
}
}
void pop()
{
int item;
if(top==-1)
{
printf("underflow\n");
}
else
{
item=stack[top];
top--;
printf("popped item is %d\n",item);
}
}
void peek()
{
int item;
if(top==-1)
{
printf("stack is empty\n");
}
else
{
printf("top most element in stack %d\n",stack[top]);
}
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

void display()
{
int i;
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}

OUTPUT:
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
1
Enter the data you want to push
10
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
1
Enter the data you want to push
20
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
1
Enter the data you want to push
30
Enter your choice
1.push
2.pop

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

3.peek
4.display
5.exit
4
30
20
10
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
2
popped item is 30
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
3
top most element in stack 20
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit
4
20
10
Enter your choice
1.push
2.pop
3.peek
4.display
5.exit

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

9. Program to implement stack using linked list, using dynamic


memory allocation.
CODE:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();

int main()
{
int choice, value;
printf("\nIMPLEMENTING STACKS USING LINKED LISTS\n");
while(1)
{
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nInvalid Choice\n");
}
}
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

void push(int value)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("Node is Inserted\n\n");
}

void pop()
{
if(top == NULL)
printf("\nEMPTY STACK\n");
else
{
struct Node *temp = top;
printf("\nPopped Element : %d", temp->data);
printf("\n");
top = temp->next;
free(temp);
}
}

void display()
{
if(top == NULL)
printf("\nEMPTY STACK\n");
else
{
printf("The stack is \n");
struct Node *temp = top;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

printf("%d--->NULL\n\n",temp->data);
}
}

OUTPUT

IMPLEMENTING STACKS USING LINKED LISTS


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

Enter your choice : 1

Enter the value to insert: 10


Node is Inserted

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

Enter your choice : 1

Enter the value to insert: 20


Node is Inserted

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

Enter your choice : 1

Enter the value to insert: 30


Node is Inserted

1. Push
2. Pop
3. Display

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

4. Exit

Enter your choice : 3


The stack is
30--->20--->10--->NULL

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

Enter your choice : 2

Popped Element : 30
1. Push
2. Pop
3. Display
4. Exit

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

10. Reverse a string using dynamic memory allocation.


CODE:
#include<stdio.h>
int string_length(char*);
void reverse(char*);
int main()
{
char string[100];
printf("Enter a string\n");
gets(string); reverse(string);
printf("Reverse of entered string is \"%s\".\n", string);
return 0;
}
void reverse(char *string)
{
int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;
for ( c = 0 ; c < length/2 ; c++ )
{
temp = *end;
*end = *begin;
*begin = temp; begin++;
end--;
}
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer+c) != '\0' ) c++;
return c;
}
OUTPUT
Enter a string
vijayanagara college
Reverse of entered string is "egelloc araganayajiv".

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

11. Program to convert infix to postfix expression using stack, using dynamic
memory allocation.

CODE:
#include<stdio.h>
#include<conio.h>
#include<ctype.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;
}
void main()
{
char exp[20];
char *e,x;
printf("Enter the expression::");
scanf("%s",exp);
e=exp;
while(*e!='\0')
{
if(isalnum(*e))
printf("%c",*e);

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

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:
Enter the expression: A+B*C/D-F+A^E
ABC*D/+F-AE^+

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

13.Program to implement queue to perform enqueue and dequeue operations


using dynamic memory allocation.
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
struct node*front=0;
struct node*rear=0;
void enqueue(int x)
{
struct node*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=0;
if(front==0&&rear==0)
{
front=rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
void dequeue()
{
struct node*temp;
temp=front;
if(front==0&&rear==0)
{
printf("queue is empty\n");
}
else
{
printf("\nDequed element is=%d",front->data);

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

front=front->next;
free(temp);
}
}
void display()
{
struct node*temp;
temp=front;
if(front==0&&rear==0)
{
printf("queue is empty\n");
}
else
{
while(temp!=0)
{
printf("\n elements in a queue is=%d",temp->data);
temp=temp->next;
}
}
}
void peek()
{
if(front==0&&rear==0)
{
printf("queue is empty\n");
}
else
{
printf("\n peek element is=%d",front->data);
}
}
void main()
{
int ch,data;
printf("\n queue operations using linked list\n");
while(1)
{
printf("\n1.enqueue\n2.dequeue\n3.display\n4.peek\n5.exit\n");
scanf("%d",&ch);

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

switch(ch)
{
case 1:printf("\n enter data enqueue\n");
scanf("%d",&data);
enqueue(data);
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4:peek();
break;
case 5:exit(0);
default:printf("invalid choice\n");
}
}
}

OUTPUT:
queue operations using linked list
1.enqueue
2.dequeue
3.display
4.peek
5.exit
1
enter data enqueue
10
1.enqueue
2.dequeue
3.display
4.peek
5.exit
1
enter data enqueue
20
1.enqueue
2.dequeue
3.display

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

4.peek
5.exit
1
enter data enqueue
30
1.enqueue
2.dequeue
3.display
4.peek
5.exit
2
Dequed element is=10
1.enqueue
2.dequeue
3.display
4.peek
5.exit
2
Dequed element is=20
1.enqueue
2.dequeue
3.display
4.peek
5.exit
2
Dequed element is=30
1.enqueue
2.dequeue
3.display
4.peek
5.exit
3
elements in a queue is=10
elements in a queue is=20
elements in a queue is=30
4
peek element is=10

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

14. Program to implement queue to perform enqueue and dequeue


expression using array operation.
CODE:
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void insert();
void delete();
void display();
void main()
{
int choice;
printf("****MAIN MENU****\n");
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
while (1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch (choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3:display();
break;
case 4:exit(1);
default: printf("Wrong choice \n");
}
}
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

void insert()
{
int add_item;
if (rear == MAX - 1) printf("Queue Overflow \n"); else
{
if (front == - 1) front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n"); return ;
}
else
{
printf("Element deleted from queue is : %d\n",
queue_array[front]); front = front + 1;
}
}
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 ", queue_array[i]);
printf("\n");
}
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

OUTPUT:
****MAIN MENU****
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
Enter your choice : 1
Inset the element in queue : 20
Enter your choice : 1
Inset the element in queue : 30
Enter your choice : 3
Queue is :
10 20 30
Enter your choice : 2
Element deleted from queue is : 10
Enter your choice : 3
Queue is :
20 30
Enter your choice : 2
Element deleted from queue is : 20
Enter your choice : 3
Queue is :
30
Enter your choice : 4
Dept. Of CS, VNC, Hosapete. Page No.
DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

15. Program to implement double ended queue.


CODE:
#include<stdio.h>
#include<process.h>
#include<conio.h>
#define MAX 30
typedef struct dequeue
{
int data[MAX];
int rear,front;
}dequeue;
void initialize(dequeue *p);
int empty(dequeue *p);
int full(dequeue *p);
void enqueueR(dequeue *p,int x);
void enqueueF(dequeue *p,int x);
int dequeueF(dequeue *p);
int dequeueR(dequeue *p);
void print(dequeue *p);
void main()
{
clrscr();
int i,x,op,n;
dequeue q;
initialize(&q);
do
{
printf("\n1.Create\n2.Insert(rear\n3.Insert(front\n4.Delete(rear");
printf("\n5.Delete(front)\n6.Print\n7.Exit\nEnter your choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter number of elements:");
scanf("%d",&n);
initialize(&q);
printf("\nEnter the data:");
for(i=0;i<n;i++)
{
scanf("%d",&x);
if(full(&q))

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

{
printf("\nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
}
break;
case 2: printf("\nEnter element to be inserted:");
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
break;
case 3: printf("\nEnter the element to be inserted:");
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueF(&q,x);
break;
case 4: if(empty(&q))
{
printf("\nQueue is empty!!");
exit(0);
}
x=dequeueR(&q);
printf("\nElement deleted is %d\n",x);
break;
case 5: if(empty(&q))
{
printf("\nQueue is empty!!");
exit(0);
}
x=dequeueF(&q);
printf("\nElement deleted is %d\n",x);

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

break;
case 6: print(&q);
break;
default: break;
}
}while(op!=7);
getch();
}
void initialize(dequeue *P)
{
P->rear=-1;
P->front=-1;
}
int empty(dequeue *P)
{
if(P->rear==-1)
return(1);
return(0);
}
int full(dequeue *P)
{
if((P->rear+1)%MAX==P->front)
return(1);
return(0);
}
void enqueueR(dequeue *P,int x)
{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
else
{
P->rear=(P->rear+1)%MAX;
P->data[P->rear]=x;
}
}
void enqueueF(dequeue *P,int x)

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
else
{
P->front=(P->front-1+MAX)%MAX;
P->data[P->front]=x;
}
}
int dequeueF(dequeue *P)
{
int x;
x=P->data[P->front];
if(P->rear==P->front) //delete the last element
initialize(P);
else
P->front=(P->front+1)%MAX;
return(x);
}
int dequeueR(dequeue *P)
{
int x;
x=P->data[P->rear];
if(P->rear==P->front)
initialize(P);
else
P->rear=(P->rear-1+MAX)%MAX;
return(x);
}
void print(dequeue *P)
{
if(empty(P))
{
printf("\nQueue is empty!!");
exit(0);
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

int i;
i=P->front;
while(i!=P->rear)
{
printf("\n%d",P->data[i]);
i=(i+1)%MAX;
}
printf("\n%d\n",P->data[P->rear]);
}

OUTPUT
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:1
Enter number of elements:5
Enter the data:10
20
30
40
50
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:2
Enter element to be inserted:25
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

7.Exit
Enter your choice:6
10
20
30
40
50
25
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:4
Element deleted is 25
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:6
10
20
30
40
50
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:5
Element deleted is 10
1.Create

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:6
20
30
40
50
1.Create
2.Insert(rear
3.Insert(front
4.Delete(rear
5.Delete(front)
6.Print
7.Exit
Enter your choice:7

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

16. Program to implement priority queue.


CODE:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10

void create_queue();
void insert_element(int);
void delete_element(int);
void check_priority(int);
void display_priorityqueue();

int pqueue[MAX];
int front, rear;

void main()
{
int n, choice;
printf("\nEnter 1 to insert element by priority ");
printf("\nEnter 2 to delete element by priority ");
printf("\nEnter 3 to display priority queue ");
printf("\nEnter 4 to exit");
create_queue();
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nEnter element to insert : ");
scanf("%d",&n);
insert_element(n);
break;
case 2:
printf("\nEnter element to delete : ");
scanf("%d",&n);
delete_element(n);
break;
case 3:

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

display_priorityqueue();
break;
case 4:
exit(0);
default:
printf("\n Please enter valid choice");
}
}
}
void create_queue()
{
front = rear = -1;
}
void insert_element(int data)
{
if (rear >= MAX - 1)
{
printf("\nQUEUE OVERFLOW");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pqueue[rear] = data;
return;
}
else
check_priority(data);
rear++;
}
void check_priority(int data)
{
int i,j;
for (i = 0; i <= rear; i++)
{
if (data >= pqueue[i])
{
for (j = rear + 1; j > i; j--)
{

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

pqueue[j] = pqueue[j - 1];


}
pqueue[i] = data;
return;
}
}
pqueue[i] = data;
}
void delete_element(int data)
{
int i;
if ((front==-1) && (rear==-1))
{
printf("\nEmpty Queue");
return;
}
for (i = 0; i <= rear; i++)
{
if (data == pqueue[i])
{
for (; i < rear; i++)
{
pqueue[i] = pqueue[i + 1];
}
pqueue[i] = -99;
rear--;
if (rear == -1)
front = -1;
return;
}
}
printf("\n%d element not found in queue", data);
}
void display_priorityqueue()
{
if ((front == -1) && (rear == -1))
{
printf("\nEmpty Queue ");
return;
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

for (; front <= rear; front++)


{
printf(" %d ", pqueue[front]);
}
front = 0;
}
OUTPUT

Enter 1 to insert element by priority


Enter 2 to delete element by priority
Enter 3 to display priority queue
Enter 4 to exit
Enter your choice : 1
Enter element to insert : 10
Enter your choice : 1
Enter element to insert : 20
Enter your choice : 1
Enter element to insert : 30
Enter your choice : 3
30 20 10
Enter your choice : 2
Enter element to delete : 10
Enter your choice : 3
30 20
Enter your choice : 1
Enter element to insert : 80
Enter your choice : 1
Enter element to insert : 15
Enter your choice : 3
80 30 20 15
Enter your choice :4

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

17. Program to implement Tower of Hanoi using recursion.

CODE:
#include<stdio.h>
#include<conio.h>
void toh(int,char,char,char);
void main()
{
int n;
printf("Enter the number of disk:\n");
scanf("%d",&n);
printf("Here is the sequence of elements of tower of hanoi\n");
toh(n,'A','B','C');
getch();
}
void toh(int no,char source,char destination,char spare)
{
if(no==1)
{
printf("move disk 1 from source %c to destination
%c\n",source,destination);
return;
}
else
toh(no-1,source,spare,destination);
printf("move disk %d from source %c to destination
%c\n",no,source,destination);
toh(no-1,spare,destination,source);
}
OUTPUT:
Enter the number of disk:
3
Here is the sequence of elements of tower of hanoi
move disk 1 from source A to destination B
move disk 2 from source A to destination C
move disk 1 from source B to destination C
move disk 3 from source A to destination B
move disk 1 from source C to destination A
move disk 2 from source C to destination B
move disk 1 from source A to destination B

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

18. Program to implement Fibonacci series using recursion.


CODE:

#include<stdio.h>
#include<conio.h>
int fibonacci(int term);
int main()
{
int terms,counter;
printf("enter number of terms in fibonacci series:");
scanf("%d",&terms);
printf("fibonacci series till %d terms/n",terms);
for(counter=0;counter<terms;counter++){
printf("%d\n", fibonacci(counter));
}
getch();
return 0;
}
int fibonacci(int term)
{
if(term<2)
return term;
return fibonacci(term-1)+fibonacci(term-2);
}

OUTPUT:

enter number of terms in fibonacci series:6


fibonacci series till 6 terms/n
0
1
1
2
3
5

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

19. Program to implement binary tree traversal.


CODE:
#include <stdio.h>
#include <stdlib.h>
struct treenode
{
int data;
struct treenode *left, *right;
};

struct treenode *root = NULL;


struct treenode * createNode(int data)
{
struct treenode *newNode;
newNode = (struct treenode *) malloc(sizeof(struct
treenode));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return (newNode);
}
void insertion(struct treenode **node, int data)
{
if (!*node)
{
*node = createNode(data);
}
else if (data < (*node)->data)
{
insertion(&(*node)->left, data);
}
else if (data > (*node)->data)
{
insertion(&(*node)->right, data);
}
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

void inorder(struct treenode *node)


{
if (node)
{
inorder(node->left);
printf("%d ", node->data);
inorder(node->right);
}
return;
}
void preorder(struct treenode *node)
{
if (node)
{
printf("%d ", node->data);
preorder(node->left);
preorder(node->right);
}
return;
}
void postorder(struct treenode *node)
{
if (node)
{
postorder(node->left);
postorder(node->right);
printf("%d ", node->data);
}
return;
}

int main()
{
int data, ch;
while (1)
{
printf("\n 1. Insertion\n 2. In-order\n 3. Pre-order\n
4. Post-order\n 5. Exit\n Enter your choice:");
scanf("%d",&ch);
switch (ch)

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

{
case 1: printf("Enter the data:");
scanf("%d", &data);
insertion(&root, data);
break;
case 2: inorder(root);
break;
case 3: preorder(root);
break;
case 4: postorder(root);
break;
case 5: exit(0);

default: printf("You have entered wrong opetion\n");


break;
}
}
return(0);
}

OUTPUT
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:9

1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:20

1. Insertion
2. In-order
3. Pre-order

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

4. Post-order
5. Exit
Enter your choice:1
Enter the data:5

1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:1

1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:3

1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:65

1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:1
Enter the data:15

1. Insertion
2. In-order
3. Pre-order

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

4. Post-order
5. Exit
Enter your choice:2
1 3 5 9 15 20 65
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:3
9 5 1 3 20 15 65
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:4
3 1 5 15 65 20 9
1. Insertion
2. In-order
3. Pre-order
4. Post-order
5. Exit
Enter your choice:

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

20. Program to implement Hash Table with open addressing.


CODE:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 10
void main()
{
int a[max],num,key,i;
char ans;
int create(int);
void hash(int[],int ,int);
void display(int[]);
printf("\nHash table\n");
for(i=0;i<max;i++)
a[i]=-1;
do
{
printf("\n enter the number:");
scanf("%d",&num);
key=create(num);
hash(a,key,num);
printf("\n do you want to continue");
ans=getche();
}
while(ans=='y');
display(a);
}
int create(int num)
{
int key;
key=num%10;
return key;
}
void hash(int a[max],int key,int num)
{

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

int flag,i,count=0;
void display(int a[]);
flag=0;
if(a[key]==-1)
a[key]=num;
else
{
i=0;
while(i<max)
{
if(a[i]!=-1)
count++;
i++;
}
if(count==max)
{
printf("\n hashtable is full");
display(a);
getch();
exit(1);
}
for(i=key+1;i<max;i++)
if(a[i]==-1)
{
a[i]=num;
flag=1;
break;
}
for(i=0;i<key&&flag==0;i++)
if(a[i]==-1)
{
a[i]=num;
flag=1;
break;
}
}
}

Dept. Of CS, VNC, Hosapete. Page No.


DSC3P1: DATA STRUCTURES & ALGORITHMS LAB MSc I Semester

void display(int a[max])


{
int i;
printf("\n the hash table...\n");
for(i=0;i<max;i++)
printf("\n%d%d",i,a[i]);
}

OUTPUT:

Hash table
enter the number:10
do you want to continue
the hash table...
010
1-1
2-1
3-1
4-1
5-1
6-1
7-1
8-1
9-1

Dept. Of CS, VNC, Hosapete. Page No.

You might also like