You are on page 1of 54

SRM UNIVERSITY

DEPARTMENT OF INFORMATION TECHNOLOGY


M.TECH DATABASE SYSTEMS
I SEMESTER

IT0501 DATA STRUCTURES AND


ALGORITHMS
LAB MANUAL
JULY 2012

Objective:
In order to develop efficient software systems, it is essential that
efficient algorithms and appropriate data structures are used. This course
will help the students to develop efficient data structures and algorithms
in a systematic manner. The students are advised to follow the following
steps for each experiment.
1.
2.
3.
4.

Problem definition
Algorithm Design
Program development either in C or C++ language
Execution of the program and demonstration to the faculty
members
5. Maintain a suitable observation book for the same.

Prepared by
1. Prof. Dr. R. Subburaj
2. Ms. S. Christobel Diana (Assistant Professor O.G)

SRM University
Department of Information Technology
M.Tech Database Systems
IT0501 Data Structures Lab Manual

List of Experiments
1. Linear Search
2. Finding the maximum element in an array
3. Create 5 nodes in singly linked list
4. Insert an element in the beginning of singly linked list.
5. Insert an element in the end of singly linked list.
6. Insert an element at any position in singly linked list.
7. Counting the number of nodes in singly linked list.
8. Implement stack using array.
9. Implement stack using linked list.
10. Implement circular queue
11. Insert an element at any position in doubly linked list.
12. Delete a node at given position in doubly linked list.
13. Tower of Hanoi.
14. Reverse a string using stack

Ex.no:1

LINEAR SEARCH

AIM:
To implement the linear search algorithm using c.
PROGRAM:

#include<stdio.h>
int main()
{
int a[10],i,n,c;
printf("Enter the n value:");
scanf("%d",&n);
printf("enter the elements of an array:");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the value to search");
scanf("%d",&c);
for(i=0;i<=n;i++)
{
if(c==a[i])
{
printf("\n Element found");
}
else
{printf("\n Element not found");
}
}
getch();
4

Ex.no:2

FINDING MAXIMUM ELEMENT IN AN ARRAY

AIM:
To find maximum number in an array algorithm using c.

PROGRAM:
#include<stdio.h>
int main()
{
int a[50],size,i,big;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array:",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++)
{
if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
return 0;
}

Ex.no:3

CREATING 5 NODES OF SINGLY LINKED LIST

AIM:
To create 5 nodes for implementing singly linked list using c.

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}node;
void insert(node *pointer, int data)
{

while(pointer->next!=NULL)
{
pointer = pointer -> next;
}

pointer->next = (node *)malloc(sizeof(node));


pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
6

void print(node *pointer)


{
if(pointer==NULL)
{
return;
}
printf("%d ",pointer->data);
print(pointer->next);
}
int main()
{
int i;
node *start,*temp;
start = (node *)malloc(sizeof(node));
temp = start;
temp -> next = NULL;

printf("1. Insert\n");
printf("2. Print\n");

while(1)
{
int query;
scanf("%d",&query);
if(query==1)
7

{
printf("\nEnter 5 nodes");
for(i=1;i<=5;i++)
{

int data;
scanf("%d",&data);
insert(start,data);
}
}

else if(query==2)
{
printf("The list is ");
print(start->next);
printf("\n");
}

}
}

Ex.no:4

INSERTING A NODE IN THE BEGINNING IN SINGLY


LINKED LIST

AIM:
To insert a node in the beginning in singly linked list using c.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int data,c,ch;
struct node
{
int element;
struct node *next;
}*L;
void create();
void display(void);
void beg(void);
void count(void);
void main()
{
int ch,n,i,loc;
//clrscr();
while(1)
{
printf("\n\n1.Create\n");
printf("2.Display\n");
printf("3.Insert at the beginning\n");
printf("4.Count\n");
printf("5.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:

create();
getch();
break;
case 2:
display();
getch();
break;
case 3:
beg();
display();
getch();
break;
case 4:
getch();
break;
case 5:
exit(0);
break;
default:
printf("\nWrong Choice....");
break;
}
}
}
void create()
{
struct node *temp,*p;
printf("\nEnter the data");
scanf("%d",&data);
temp=malloc(sizeof(struct node));
temp->element=data;
10

temp->next=NULL;
if(L==NULL)
{
L=temp;
}
else{
p=L;
while(p->next!=NULL)
{
p=p->next;
p->next=temp;
}
}
}
void display()
{
struct node*p;
p=L;
if(L==NULL)
{
printf("\nEmpty");
return;
}
while(p!=NULL)
{
printf("%d \t",p->element);
p=p->next;
}
}
void beg(void)
{
struct node *temp,*p;
printf("\nEnter the element:");
scanf("%d",&data);
11

p=L;
temp=malloc(sizeof(struct node));
temp->element=data;
temp->next=p;
L=temp;
printf("\n");
}

12

Ex.no:5

INSERTING A NODE IN THE END IN SINGLY LINKED


LIST

AIM:
To insert a node in the end in singly linked list using c.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int data,c,ch;
struct node
{
int element;
struct node *next;
}*L;
void create();
void display(void);
void end(void);
void count(void);
void main()
{
int ch,n,i;

while(1)
{
printf("\n\n1.Create\n");
13

printf("2.Display\n");
printf("3.Insert at the end\n");
printf("4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:

create();

getch();
break;
case 2:
display();
getch();
break;

case 3:
end();
getch();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice...");
14

break;
}
}
}
void create()
{
struct node *temp,*p;
printf("\nEnter the data");
scanf("%d",&data);
temp=malloc(sizeof(struct node));
temp->element=data;
temp->next=NULL;
if(L==NULL)
{

L=temp;
}
else{
p=L;
while(p->next!=NULL)
{
p=p->next;
p->next=temp;
}
}
}

15

void display()
{
struct node*p;
p=L;
if(L==NULL)
{
printf("\nEmpty");
return;
}
while(p!=NULL)
{
printf("%d \t",p->element);
p=p->next;
}

void end(void)
{
struct node *element,*temp,*p;
printf("\nEnter the element");
scanf("%d",&data);
if(L==NULL)
{
printf("\nList is empty");
return;
}
16

temp=malloc(sizeof(struct node));
temp->element=data;
temp->next=NULL;
p=L;
while(p->next!=NULL)

p=p->next;
p->next=temp;
printf("\n");

17

Ex.no:6

INSERTING A NODE AT ANY POSITION IN SINGLY


LINKED LIST

AIM:
To insert a node at any position in singly linked list using c.

PROGRAM:
#include<stdio.h>
#include<malloc.h>

void display();
int insert();
int insertn();

int num,loc;
char name[20];

struct node
{
int a;
char n[20];
struct node *next;
};
struct node *first,*last;

int main()
{
18

int ch;
do
{
printf("\n Enter the choice \n1.display\n2.Insert\n3.insert at any position\n4.exit \n");
scanf("%d",&ch);
switch(ch)
{

case 1:
display();
break;
case 2:
insert();
break;

case 3:
printf("\n enter the location \n");
scanf("%d",&loc);
insertn(loc);
break;

}
}while(ch!=4);
}
int insert()
{
struct node *temp;
19

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


printf("\n enter val\n");
scanf("%d",&temp->a);

if(first==NULL)
{
first=temp;
first->next=NULL;
}
else
{
temp->next=first;
first=temp;
}
display();
}

void append()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("\n memory not allocated");
}
printf("\n enter the val\n");
scanf("%d",&newnode->a);
20

printf("\n enter the name\n");


scanf("%s",newnode->n);
newnode->next=NULL;
if(first==NULL)
{
first=newnode;
}
else
{
last->next=newnode;
}
last=newnode;

}
int del(int num)
{
struct node *temp,*m;
temp=first;

while(temp!=NULL)
{
if(temp->a==num)
{
if(temp==first)
{
first=temp->next;
free(temp);
21

return;
}
else
{
m->next=temp->next;
free(temp);
return;
}
}
else
{
m=temp;
temp=temp->next;
}
}
}
int insertn(int loc)
{
int i;
struct node *temp,*t,*pre;
pre=first;
if(loc==1)
{
insert();
return;
}
else
22

{
for(i=1;i<loc;i++)
{
t=pre;
pre=pre->next;
}
temp=(struct node *)malloc(sizeof(struct node));
printf("\n enter the value\n");
scanf("%d",&temp->a);
t->next=temp;
t=temp;
t->next=pre;
pre=pre->next;
}
display();
}
int search(int num)
{
struct node *temp;
temp=first;
while(temp!=NULL)
{
if(temp->a==num)
{
printf("%d is exist",num);
printf("\t its name is %s",temp->n);
break;
23

}
else
temp=temp->next;

}
if(temp->a!=num)
{

printf("not exist");
}
}
void rev(struct node *st)
{
struct node *temp,*n,*s;
temp=st;
n=NULL;
while(temp!=NULL)
{
s=n;
n=temp;
temp=temp->next;
n->next=s;
}
first=n;
display();
}

24

void display()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp=first;
while(temp!=NULL)
{
printf("%d\t",temp->a);
temp=temp->next;
}
}

25

Ex.no:7

COUNTING NODES IN SINGLY LINKED LIST

AIM:
To count number of nodes in singly linked list using c.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int data,c,ch;
struct node
{
int element;
struct node *next;
}*L;
void create();
void display(void);
void count(void);
void main()
{
int ch,n,i;
//clrscr();
while(1)
{
printf("\nSINGLY LINKED LIST\n");

26

printf("\n\n1.Create\n");
printf("2.Display\n");
printf("3.Count\n");
printf("4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:

create();
getch();
break;
case 2:
display();
getch();
break;
case 3:
count();
getch();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice...HA HA HA");
27

break;
}
}
}
void create()
{
struct node *temp,*p;
printf("\nEnter the data");
scanf("%d",&data);
temp=malloc(sizeof(struct node));
temp->element=data;
temp->next=NULL;
if(L==NULL)
{

L=temp;
}
else{
p=L;
while(p->next!=NULL)
{
p=p->next;
p->next=temp;
}
}
28

void display()
{
struct node*p;
p=L;
if(L==NULL)
{
printf("\nEmpty");
return;
}
while(p!=NULL)
{
printf("%d \t",p->element);
p=p->next;
}

}
void count()
{
int c=1;
struct node *temp,*next,*p;
if(L==NULL)
{
printf("\nThe lis is empty");
29

}
p=L;
while(p->next!=NULL)
{
p=p->next;
c++;
}
printf("\nThe no.of elements is %d",c);
}

30

Ex.no:8

IMPLEMENTATION OF STACK USING ARRAY

AIM:
To implement stack using array using c.

PROGRAM:
#include<stdio.h>
main()
{
int a[10]={0},i,top=-1,max=10,n,x;
printf("\n\tMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
if(top==max-1)
printf("Overflow\n");
else
{
printf("Enter the element\n");
scanf("%d",&x);
a[++top]=x;
}
break;
case 2:
if(top<0)
printf("Underflow\n");
else
printf("The deleted item =%d",a[top--]);
31

break;
case 3:
if(top<0)
printf("The stack is empty\n");
else
{
printf("The elements of the stack are :");
for(i=0;i<=top;i++)
printf("%d\n",a[i]);
}
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}

32

Ex.no:9

IMPLEMENTATION OF STACK USING LINKED LIST

AIM:
To implement linked stack using array using c.

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int data,ch,c;
struct node
{
int element;
struct node*next;
}*s=NULL;
void push(void);
void pop();
void display(void);
void main()
{
int ch,c,i;
printf("\n\t\t IMPLEMENTATION OF STACK USING LINKED LIST\n");
while(1)
{
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
33

pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
break;
}
}
}
void push(void)
{
struct node*temp;
temp=malloc(sizeof(struct node));
printf("\nEnter the element:");
scanf("%d",&data);
temp->element=data;
temp->next=s;
s=temp;
}
void display(void)
{
struct node*P;
P=s;
if(s==NULL)
{
printf("\nStack is empty");
}
while(P!=NULL)
{
printf("\t%d",P->element);
P=P->next;
}
}
void pop(void)
34

{
struct node*temp,*next,*p;
if(s==NULL)
{
printf("\nStack is empty");
return;
}
temp=s;
printf("\nThe popped element is %d",temp->element);
s=temp->next;
free(temp);
}

35

Ex.no:10

IMPLEMENTATION OF CIRCULAR QUEUE

AIM:
To implement array based circular queue using C.

PROGRAM:
# include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;

36

case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}
}
}
insert()
{
int added_item;
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if (front == -1) {
front = 0;
rear = 0;
}
else
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
printf("Input the element for insertion in queue : ");
scanf("%d", &added_item);
cqueue_arr[rear] = added_item ;
}
37

del()
{
if (front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
{

if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
38

if( front_pos <= rear_pos )


while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}

39

Ex.no:11

INSERTING A NODE AT ANY POSITION IN DOUBLY


LINKED LIST

AIM:
To implement doubly linked list at any position using c.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int data,c,i,n,pos;
struct node
{
int element;
struct node*next;
struct node*prev;
}*D=NULL;
void create(int);
void mbeg(void);
void display(void);
int count(void);
void main()
{
int ch;
while(1)
{
printf("\n\n1.Create\n");
printf("\n2.Insertion at any position\n");
printf("\n3.Display\n");
40

printf("\n4.Count\n");
printf("\n5.Exit\n");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the no.of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element");
scanf("%d",&data);
create(data);
}
break;
case 2:
mbeg();
display();
break;
case 3:
display();
break;
case 4:
count();
break;
case 5:
41

exit(0);
break;
default: printf("\nWrong choice....");
}
}
}
void create(int data)
{
struct node *temp,*P;
temp=malloc(sizeof(struct node));
temp->element=data;
temp->next=NULL;
if(D==NULL)
{
temp->prev=NULL;
D=temp;
return;
}
P=D;
while(P->next!=NULL)
P=P->next;
temp->prev=P;
P->next=temp;
}
void display(void)
{
struct node *P,*temp;
42

if(D==NULL)
{
printf("List empty");
return;
}
P=D;
printf("\nThe Elements are :");
while(P!=NULL)
{
printf("\t%d->",P->element);
P=P->next;
}
}
void mbeg(void)
{
struct node *temp,*P;
int i,pos,data;
printf("\nEnter the element");
scanf("%d",&data);
printf("\nEnter the position of the element to be inserted");
scanf("%d",&pos);
if(D==NULL)
{
printf("\nList is empty");
return;
}
if(count()<pos)
43

{
printf("\nEnter a position less than a count %d",count());
return;
}
P=D;
temp=malloc(sizeof(struct node));
temp->element=data;
for(i=0;i<pos-1;i++)
{
P=P->next;
temp->next=P->next;
temp->prev=P;
P->next->prev=P;
P->next=temp;
printf("\n");
}
}
int count(void)
{

int c=0;
struct node *temp,*P;
if(D==NULL)
{
printf("\nThe list empty");
return 0;

44

}
P=D;
while(P!=NULL)
{

P=P->next;
c++;

}
printf("\nThe elements are is %d",c);
return c;
}

45

Ex.no:12

INSERTING A NODE AT ANY POSITION IN DOUBLY


LINKED LIST

AIM:
To implement doubly linked list delete at any position using c.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int data,c,i,n,pos;
struct node
{
int element;
struct node*next;
struct node*prev;
}*D=NULL;
void create(int);
void mdel(void);
void display(void);
int count(void);
void main()
{
int ch;
while(1)
{
printf("\n\n1.Create\n");
printf("\n2.Delete at any position\n");
printf("\n3.Display\n");
46

printf("\n4.Count\n");
printf("\n5.Exit\n");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the no.of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element");
scanf("%d",&data);
create(data);
}
break;
case 2:
mdel();
display();
break;
case 3:
display();
break;
case 4:
count();
break;
case 5:
47

exit(0);
break;
default: printf("\nWrong choice....");
}
}
}
void create(int data)
{
struct node *temp,*P;
temp=malloc(sizeof(struct node));
temp->element=data;
temp->next=NULL;
if(D==NULL)
{
temp->prev=NULL;
D=temp;
return;
}
P=D;
while(P->next!=NULL)
P=P->next;
temp->prev=P;
P->next=temp;
}
void display(void)
{
struct node *P,*temp;
48

if(D==NULL)
{
printf("List empty");
return;
}
P=D;
printf("\nThe Elements are :");
while(P!=NULL)
{
printf("\t%d->",P->element);
P=P->next;
}
}
void mdel(void)
{
struct node *temp,*P;
if(D==NULL)
{
printf("\nList is empty");
return;
}
printf("\nEnter the position to delete");
scanf("%d",&pos);
if(pos>count())
{
printf("Deletion is not possible");
return;
49

}
P=D;
for(i=1;i<pos-1;i++)
P=P->next;
temp=P->next;
P->next=temp->next;
temp->next->prev=P;
printf("\nThe element is deleted is %d",temp->element);
free(temp);
}

int count(void)
{

int c=0;
struct node *temp,*P;
if(D==NULL)
{
printf("\nThe list empty");
return 0;

}
P=D;
while(P!=NULL)
{

P=P->next;
50

c++;

}
printf("\nThe elements are is %d",c);
return c;
}

51

Ex.No.13

TOWERS OF HANOI

Aim:
To implement Tower of Hanoi algorithm.
Program:
#include <stdio.h>
void towers(int,char,char,char);
void towers(int n,char fromplace,char toplace,char auxplace)
{ /* If only 1 disk, make the move and return */
if(n==1)
{ printf("\nMove disk 1 from place %c to place %c",fromplace,toplace);
return;
}
/* Move top n-1 disks from A to B, using C as auxiliary */
towers(n-1,fromplace,auxplace,toplace);
/* Move remaining disks from A to C */
printf("\nMove disk %d from place %c to place %c",n,fromplace,toplace);
/* Move n-1 disks from B to C using A as auxiliary */
towers(n-1,auxplace,toplace,fromplace);}
main()
{ int n;
printf("Enter the number of disks : ");
scanf("%d",&n);
printf("The Tower of Hanoi involves the moves :\n\n");
towers(n,'A','C','B');
return 0;}
52

Ex.No:14 REVERSE A STRING USING A STACK


Aim:
To reverse a string using a stack.
Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20
int top = -1;
char stack[MAX];
char pop();
void push(char);
main()
{
char str[20];
unsigned int i;
printf("Enter the string : " );
gets(str);
for(i=0;i<strlen(str);i++)
push(str[i]);
for(i=0;i<strlen(str);i++)
str[i]=pop();
printf("Reversed string is : ");
puts(str);
}
void push(char item)
{
if(top == (MAX-1))
{
printf("Stack Overflow\n");
return;
}
stack[++top] =item;
}
53

char pop()
{
if(top == -1)
{
printf("Stack Underflow\n");
exit(1);
}
return stack[top--];
}

54

You might also like