DataStructure Lab Manual
DataStructure Lab Manual
Write up 05
Program1
Execution 10
Write up 10
Program2
Execution 15
Viva-Voce 10
Total 50
1
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
Contents
1) Write a program to store and retrieve array elements using pointers.
2) Write a program to maintain the student information using structure and pointers.
3) Write a C program for the following operations on a stack of integers (use arrays).
i) Push
ii) Pop
iii) Display
4) Write a program to implement the stack using structures.
5) Write a program to convert an expression in infix to an expression in postfix form.
6) Write a program to evaluate a postfix expression
7) Write a program to find factorial of a number using recursion.
8) Write a program to search an element in the given array using binary search (Use recursion).
9) Write a program to implement tower of Hanoi problem using recursion.
10) Write a program to implement queue of integers with the following operations.
i) Insert at the rear end
ii) Delete at the front end
iii) Display the contents of the queue
11) Write a program to implement the queue of integers using structures and implement the above
operations on the queue.
12) Write a program to implement the circular queue.
13) Write a program to implement the priority queue of integers.
14) Write a program to implement singly linked list and implement the following operations on it.
i) Insert a node at the front of the list
ii) Insert a node at any given position
iii) Delete a node from the front of the list
iv) Delete any node from the list.
15) Write a program to implement stack using dynamic memory allocation.
16) Write a program to implement queue using dynamic memory allocation.
17) Write a program to implement circular linked list.
18) Write a program to implement doubly linked list.
19) Write a program to implement circular doubly linked list.
20) Write a program to create a binary tree and implement the inorder, preorder and postorder
travarsals.
21) Write a program to construct a binary search tree and construct postfix expression tree.
22) 22. Write a C program to implement a binary search tree of integers and perform the following
traversal techniques:
i) In-order traversal
ii) Find the maximum element
iii) Search an element
iv) Display "Duplication is not allowed" if same element is inserted again. Display "Key
element not found" if searched element is not present in the tree.Display "Search is
successful" if searched element is present in the tree.
v) Display "Maximum element is 15" if the maximum element is 15 in the tree.
2
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
#include <stdio.h>
int main()
{
int arr1[25], i,n;
printf("\n\n Pointer : Store and retrieve elements from an array :\n");
printf("------------------------------------------------------------\n");
printf(" Input the number of elements to store in the array :");
scanf("%d",&n);
3
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
2. Write a program to maintain the student information using structure and pointers.
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
4
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
3. Write a C program for the following operations on a stack of integers (use arrays).
a) Push
b) Pop
c) Display
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int top=-1;
void push(int a[],int item)
{
top=top+1;
a[top]=item;
}
int pop(int a[])
{
int item;
item=a[top];
top=top-1;
return item;
}
void display(int a[])
{
int i;
if(top==-1)
printf("The stack is empty\n");
else if(top!=-1)
{
printf("The stack elements are\n");
for(i=top; i>=0;i--)
printf("%d ",a[i]);
printf("\n");
}
}
int main()
{
int s[10],choice,item;
while(1)
{
printf("Enter the choice\n");
printf("1 Push\n2 Pop\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: if(top==SIZE-1)
{
5
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
6
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
8
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int main()
{
char exp[100],ch,post[100];
//char *e, x;
int i,j=0;
printf("Enter the expression : ");
scanf("%s",exp);
//e = exp;
i=0;
while(exp[i] != '\0')
{
ch=exp[i];
if(isalnum(ch))
9
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
10
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
void push(char x)
{
stack[++top] = x;
}
int pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int main()
{
char exp[100],ch;;
//char *e, x;
int i,j=0,op1,op2,result;
printf("Enter the expression : ");
scanf("%s",exp);
//e = exp;
i=0;
while(exp[i] != '\0')
{
ch=exp[i];
if(isdigit(ch))
push(ch-'0');
switch(ch)
{
case '+': op1=pop();
op2=pop();
result=op1+op2;
push(result);
break;
11
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
12
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
13
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
8. Write a program to search an element in the given array using binary search (Use recursion).
#include<stdio.h>
int binsearch(int a[],int low, int high, int key)
{
int mid;
if(low>high)
return -1;
mid=(low+high)/2;
if(a[mid]==key)
return mid;
else if(key<a[mid])
return binsearch(a,low,mid-1,key);
else
return binsearch(a,mid+1,high,key);
}
int main()
{
int n,a[100],i,key,res;
res=binsearch(a,0,n-1,key);
if(res==-1)
printf("Element entered is not present in the list\n");
else
printf("Element entered is present at the position %d\n",res+1);
return 0;
}
14
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
int main()
{
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
printf("\n");
return 0;
}
15
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
10. Write a program to implement queue of integers with the following operations.
(i) Insert at the rear end
(ii) Delete at the front end
(iii) Display the contents of the queue
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
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");
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");
}
}
}
void insert()
{
int item;
if(rear == MAX - 1)
printf("Queue Overflow n");
else
{
if(front== - 1)
16
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
17
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
11. Write a program to implement the queue of integers using structures and implement the above
operations on the queue.
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
struct queue
{
int rear,front;
int q[MAX];
};
void INSERT(int,struct queue *);
void DELETE(struct queue *);
void DISPLAY(struct queue *);
int main()
{
int choice, item;
struct queue s;
s.rear=-1;
s.front=0;
for(;;)
{
printf("Enter your choice\n1 Insertion\n2 Deletion\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: if(s.rear==MAX-1)
printf("Queue is full\n");
else
{
printf("Enter the element to be inserted\n");
scanf("%d",&item);
INSERT(item,&s);
}
break;
case 2: DELETE(&s);
break;
case 3: DISPLAY(&s);
break;
case 4: exit(0);
}
}
return 0;
}
void INSERT(int item,struct queue *s)
{
18
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
s->rear=s->rear+1;
s->q[s->rear]=item;
}
19
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
void delete()
{
int element;
if(isEmpty())
printf("Queue is empty\n");
else
{
element=items[front];
if(front==rear)
{
front=-1; rear=-1;
}
else
front=(front+1)%SIZE;
20
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
int main()
{
int choice;
for(;;)
{
printf("Enter your choice\n1 Insertion\n2 Deletion\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
21
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
void delete()
{
int flag=0;
if(front>rear)
printf("There are no elements to delete\n");
else
{
int ele;
printf("Enter value to delete\n");
scanf("%d",&ele);
for(i=front;i<=rear;i++)
{
if(q[i]==ele)
{
flag=1;
for(j=i;j>front;j--)
q[j]=q[j-1];
front++;
break;
}
}
if(flag==0)
printf("%d not found\n",ele);
22
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
23
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
14. Write a program to implement singly linked list and implement the following operations on it.
(i) Insert a node at the front of the list
(ii) Insert a node at any given position
(iii) Delete a node from the front of the list
(iv) Delete any node from the list.
#include<stdio.h>
#include<stdlib.h>
int count=0;
struct node
{
int student_id;
int semester;
char student_name[20];
struct node *link;
};
void insert(struct node **s,int pos)
{
int i;
if(pos>count+1)
{
printf("Invalid position\n");
return;
}
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter id\n");
scanf("%d",&(newnode->student_id));
printf("Enter name\n");
scanf("%s",newnode->student_name);
printf("Enter semester\n");
scanf("%d",&(newnode->semester));
struct node *temp=*s; if((*s)==NULL)
{
(*s)=newnode; count++;
}
else if(pos==0 || pos==1)
{
for(i=2; i<pos; i++)
{
temp=temp->link;
}
newnode->link=temp->link; temp->link=newnode; count++;
}
else
24
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
int main()
{
int choice,pos;
struct node *s=NULL;
for(;;)
{
printf("Enter your choice\n1 Insert at position\n2 Delete front\n3 Display\n4
Exit\n");
scanf("%d",&choice);
switch(choice)
25
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
26
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
28
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
30
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
32
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
33
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
int main()
{
NODE first=NULL;
int choice,val;
while(1)
{
printf("Enter your choice\n1 Insert front\n2 Delete rear\n3 Display\n4
Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the data\n");
scanf("%d",&val);
first=insert_front(first,val);
break;
case 2: first=delete_rear(first);
break;
case 3: display(first);
35
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
36
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink, *rlink;
};
typedef struct node * NODE;
NODE insert_front(NODE first,int val)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->info=val;
if(first==NULL)
{
first=newnode;
newnode->llink=newnode;
newnode->rlink=newnode;
return first;
}
else
{
NODE last=first->llink;
newnode->rlink=first;
newnode->llink=last;
last->rlink=newnode;
first->llink=newnode;
return newnode;
}
}
38
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
39
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
20. Write a program to create a binary tree and implement the inorder, preorder and postorder
travarsals.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
};
typedef struct node * NODE;
NODE create_B_Tree()
{
NODE newnode; int data=0;
newnode=NULL;
printf("Enter data ('0' if no data)");
scanf("%d",&data);
if(data)
{
newnode=(NODE)malloc(sizeof(struct node));
newnode->info=data;
printf("\nLeft child of %d\n",newnode->info);
newnode->lchild=create_B_Tree();
printf("\nRight child of %d\n",newnode->info);
newnode->rchild=create_B_Tree();
}
return newnode;
}
void pre_order(NODE root)
{
if(root!=NULL)
{
printf("%d\n",root->info);
pre_order(root->lchild);
pre_order(root->rchild);
}
}
void post_order(NODE root)
{
if(root!=NULL)
{
post_order(root->lchild);
post_order(root->rchild);
printf("%d\n",root->info);
}
}
void in_order(NODE root)
{
40
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
41
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
21. Write a program to construct a binary search tree and construct postfix expression tree.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define max 100
struct node
{
int data;
struct node *llink,*rlink;
};
typedef struct node * NODE;
NODE construct_tree(char postfix[max])
{
if(postfix[0]==’\0’)
return NULL;
char sym;
NODE newnode,stack[max];
int i,top=-1;
for(i=0;postfix[i]!='\0';i++)
{
sym=postfix[i];
newnode=(NODE)malloc(sizeof(struct node));
newnode->data=sym;
newnode->llink=NULL;
newnode->rlink=NULL;
if(isdigit(sym))
{
stack[++top]=newnode;
continue;
}
switch(sym)
{
case '+':
case '-':
case '*':
case '/': newnode->rlink=stack[top--];
newnode->llink=stack[top--];
stack[++top]=newnode;
break;
default: return NULL;
}
}
return stack[top--];
}
float evaluate(NODE root)
{
switch(root->data)
{
42
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
43
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
22. Write a C program to implement a binary search tree of integers and perform the following
traversal techniques:
i) In-order traversal
ii )Find the maximum element
iii) Search an element
Display "Duplication is not allowed" if same element is inserted
again. Display "Key element not found" if searched element is not
present in the tree. Display "Search is successful" if searched
element is present in the tree.
Displpay "Maximum element is 15" if the maximum element is 15 in the tree.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *llink,*rlink;
};
typedef struct node * NODE;
NODE create_node(int val)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->data=val;
newnode->llink=NULL;
newnode->rlink=NULL;
return newnode;
}
void setleft(NODE p,int val)
{
if(p==NULL)
printf("Insertion is not possible\n");
else if(p->llink!=NULL)
printf("Invalid insertion\n");
else
p->llink=create_node(val);
}
44
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
45
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC
46