efficient algorithms and appropriate data structures are used. This course will
help the students to develop efficient data structures and algorithms in a
systematic ...
SRM UNIVERSITY DEPARTMENT OF INFORMATION TECHNOLOGY M.TECH DATABASE SYSTEMS I SEMESTER
IT0501 DATA STRUCTURES AND ALGORITHMS LAB MANUAL JULY 2012 1
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)
2
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
3
Ex.no:1
LINEAR SEARCH
AIM: To implement the linear search algorithm using c. PROGRAM:
#include 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;idata = 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;inext); printf("\n"); }
} }
8
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 #include #include 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:
9
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 #include #include
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 #include
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;inext; } 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 #include #include
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 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(topelement); 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 # 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 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()element=data; for(i=0;inext; 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 #include #include 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;ielement=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;inext; 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 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 #include #include #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