Lab Manual FOR Data Structures (C-09) : Diploma in Computer Engineering
This document contains instructions and exercises for a data structures lab manual. It includes instructions for students on proper lab conduct as well as a list of exercises involving different data structure implementations like linked lists, stacks, queues, binary trees, and sorting algorithms. The exercises provided allow students to practice creating, inserting, deleting and displaying elements in singly linked lists, implementing stacks and queues, and performing sorting algorithms like selection, insertion and bubble sort.
Lab Manual FOR Data Structures (C-09) : Diploma in Computer Engineering
This document contains instructions and exercises for a data structures lab manual. It includes instructions for students on proper lab conduct as well as a list of exercises involving different data structure implementations like linked lists, stacks, queues, binary trees, and sorting algorithms. The exercises provided allow students to practice creating, inserting, deleting and displaying elements in singly linked lists, implementing stacks and queues, and performing sorting algorithms like selection, insertion and bubble sort.
SANKETIKA POLYTECHNIC COLLEGE AFFILIATED TO STATE BOARD OF EDUCATION AND TRAINING , HYDERABAD
2
INSTRUCTIONS TO STUDENTS
Do?s and Don? ts:
Do?s: Be punctual Follow dress code Remove the foot wears Keep personal belonging in allotted place Submit the observation record of previous experiments Use the allotted system and login as per instruction only Take the printouts regularly While leaving the lab, arrange the chairs you are seated in a proper way. Help to maintain the cleanliness of the lab Maintain strict discipline
Don?ts:
Don?t use the system for typing letters, reports etc., during lab hours Don?t use others login Don?t change the configuration and system settings Don?t bring floppies, cds inside lab without permission Don?t load unauthorized software
3
Lab Title : Data Structures Lab Periods per Week : 04 Lab Code : CM ? 306 Periods per Semester: 60
LIST OF EXCERCISES
1. Exercises on creation, insertion, deletions & display of elements in a singly linked List. 2. Write a program to implement a singly circular linked list 3. Exercises on creation, insertion, deletions & display of elements in a doubly linked List. 4. Write a program to Implement a stack 5. Write a program to implement a queue 6. Write a program to create a sparse matrix 7. Write a program to create a binary tree & its traversal operations 8. Exercise on Selection sort 9. Exercise on insertion sort 10. Exercise on bubble sort 11. Implement a program for merge sort on two sorted lists of elements 12. Exercises on linear search 13. Exercise on binary search
4
1.Write programs to create,insert,delete and display elements in a singly linked list
1a) Aim: Write a Program to Insert Node at the beginning of the singly Linked List
Source code:
/**** Program to Insert Node at the beginning of the singly Linked List ****/ #include <stdio.h> void insert_first(); void display(); struct node { int info; struct node *link; }*start=NULL; int item; main() { int ch; do { printf("\n\n\n1. Insert First\n2. Display\n3. Exit\n"); printf("\nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1:insert_first(); break; case 2:display(); break; case 3:exit(0); default:printf("\n\nInvalid choice. Please try again.\n"); } }while (1); } void insert_first() { struct node *ptr; printf("\n\nEnter item: "); scanf("%d", &item); if(start ==NULL) { start =(struct node *)malloc(sizeof(struct node));
1c) Aim: Write a Program to Insert Node at Specific position in a singly Linked List
Source code:
/**** Program to Insert a node at Specific position in a singly Linked List ****/ #include <stdio.h> void insert_last(); void insert_specific(); void display(); struct node { int info; struct node *link; }*start=NULL; int item;
main() { int ch; do { printf("\n\n\n1. Insert Last\n2. Insert Specific\n3. Display\n4. exit\n"); printf("\nEnter your choice: "); scanf("%d", &ch); switch(ch) {
void delete_first() { struct node *ptr; if (start ==NULL) printf("\n\nLinked list is empty.\n"); else { ptr =start; item =start->info; start =start->link; free(ptr); printf("\n\nItem deleted: %d", item); } }
void display() { struct node *ptr =start; int i=1; if (ptr ==NULL) printf("\nLinklist is empty.\n"); else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
15
while(ptr !=NULL) { printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,ptr->link); ptr =ptr->link; i++;
}
} }
Output:
16
1e)Aim:Write a Program to Delete Last Node in a Linked List
Source code: /** Program to Delete Last Node in a singly Linked List ****/ #include <stdio.h> void insert_last(); void delete_last(); void display(); struct node { int info; struct node *link; }*start=NULL; int item; main() { int ch; do { printf("\n\n\n1. Insert Last\n2. Delete Last\n3. Display\n4.Exit\n"); printf("\nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1:insert_last(); break; case 2:delete_last(); break; case 3:display(); break; case 4:exit(0);
1f)Aim:Write a Program to Delete any Specific Node in a singly Linked List
Source code:
/**** Program to Delete any Specific Node in a singly Linked List ****/ #include <stdio.h> void insert_last(); void delete_specific(); void display(); struct node { int info; struct node *link; }*start=NULL; int item; main() { int ch; do { printf("\n\n\n1. Insert Last\n2. Delete Specific\n3. Display\n4.Exit\n"); printf("\nEnter your choice: "); scanf("%d", &ch); switch(ch) {
printf("\n\nEnter ITEM which is to be deleted: "); scanf("%d", &item); if (start ==NULL) printf("\n\nLinked list is empty.\n"); else if (start->info ==item) { ptr =start; start =start->link; free(ptr); } else { ptr =start; prev =start; while (ptr !=NULL) { if (ptr->info ==item) { prev->link =ptr->link; free(ptr); } else { prev =ptr; ptr =ptr->link; } } printf("\n\nItem deleted: %d", item); } } void display() {
21
struct node *ptr =start; int i=1; if (ptr ==NULL) printf("\nLinklist is empty.\n"); else { printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n"); while(ptr !=NULL) { printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,ptr->link); ptr =ptr->link; i++; } } } Output:
22
2.Write a program to implement a singly circular linked list
Aim: Write a program to implement a singly circular linked list
Source code: #include<alloc.h> #include<conio.h> #include<stdio.h> struct node { int data; struct node *next; }; struct node *head=NULL; struct node *tail=NULL; void main() { void addrecord(); void deleterecord(); void disrecord(); int ch; clrscr(); do { printf("\n 1. To add records\n"); printf("\n 2. To delete a records\n"); printf("\n 3. To view the records\n"); printf("\n 4. To exit\n"); printf("\n Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1:addrecord(); break; case 2:deleterecord(); break; case 3: disrecord(); break; case 4:exit(0); } }while (ch!=4); } void addrecord() { int new_data; char ans='y'; struct node *ptr,*prev,*temp;
23
while (ans=='y') { temp=(struct node*)malloc(sizeof(struct node)); printf("\n Enter the new element:\n"); scanf("%d",&new_data); temp->data=new_data; temp->next=NULL; if (head==NULL) { head=tail=temp; temp->next=head; } else { tail->next=temp; tail=temp; } printf("\n Would you like to enter another data(y\\n): \n"); ans =getchar(); } } void deleterecord() { struct node *ptr,*prev,*delnode; int elt; printf("\n Enter the enrollment number to be deleted \n"); scanf("%d",&elt); if (head==NULL) { printf("\n No elements in the list \n"); return; } else { if (head->data==elt) { delnode=head; if (head==tail) head=tail=NULL; else { head=head->next; tail->next=head; } } else if (tail->data==elt) {for(ptr=head;(ptr!=tail);prev=ptr,ptr=ptr->next); delnode=tail;
24
tail=prev; tail->next=head; } else { for(prev=ptr=head;(ptr->data!=elt)&&(ptr!=tail); prev=ptr,ptr=ptr->next); if(ptr->data==elt) { delnode=ptr; prev->next=ptr->next; printf("yes..."); } else { printf("Given element not found in the list"); getch(); return; } } } free(delnode); } void disrecord() { struct node *ptr,*prev=NULL; if (head==NULL) { printf("\n No records to view\n"); return; } printf("\n The elements in the circular list are\n"); for (ptr=head;prev!=tail;prev=ptr,ptr=ptr->next) printf("\n\n %d",ptr->data); printf(" NULL\n\n "); getch(); }
25
Output:
1. To add records
2. To delete a records
3. To view the records
4. To exit
Enter your choice 1 Enter the new element: 10
Would you like to enter another data(y\n):
1. To add records
2. To delete a records
3. To view the records
4. To exit
Enter your choice 1 Enter the new element: 20
Would you like to enter another data(y\n):
1. To add records
2. To delete a records
3. To view the records
4. To exit
Enter your choice 1
Enter the new element: 30
Would you like to enter another data(y\n):
26
1. To add records
2. To delete a records
3. To view the records
4. To exit
Enter your choice 3 The elements in the circular list are
10
20
30 NULL
1. To add records
2. To delete a records
3. To view the records
4. To exit
Enter your choice 2
Enter the enrollment number to be deleted 20
yes... 1. To add records
2. To delete a records
3. To view the records
4. To exit
Enter your choice 3
The elements in the circular list are 10
30 NULL
27
3.Write programs to create,insert,delete and display elements in a doubly linked list
3a) Aim: Write a Program to insert element in the front of doubly linked list
Source code: /*Program to insert element in the front of doubly linked list */ #include <stdio.h> void insert_front(); void display(); struct node { int info; struct node *link; struct node *prev; }*start=NULL; int item; main() { int ch; do { printf("\n\n\n1. Insert front\n2.display\n3.exit\n"); printf("\nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1: insert_front(); break; case 2:display(); break; case 3: exit(0); default: printf("\n\nInvalid choice: Please try again.\n"); } }while (1); } void insert_front() { struct node *ptr,*nw; printf("\n\nEnter item: "); scanf("%d", &item);
printf("\n%d.\t\t%d\t\t%d\t\t%d\t\t%d\n", i, ptr->prev,ptr, ptr->info,ptr->link); ptr =ptr->link; i++; } } }
Output: 1. Insert last 2.delete front 3. display 4.exit
Enter your choice: 1
Enter item: 10 Item inserted: 10
1. Insert last 2.delete front 3. display 4.exit
Enter your choice: 1
Enter item: 20 Item inserted: 20
1. Insert last 2.delete front 3. display 4.exit
Enter your choice: 1
Enter item: 30 Item inserted: 30
1. Insert last 2.delete front
38
3. display 4.exit
Enter your choice: 3
Sr. No. Prev Address Info Link
1. 0 3436 10 3452
2. 3436 3452 20 3468
3. 3452 3468 30 0
1. Insert last 2.delete front 3. display 4.exit
Enter your choice: 2 deleted item is 10
1. Insert last 2.delete front 3. display 4.exit
Enter your choice: 3
Sr. No. Prev Address Info Link
1. 0 3452 20 3468
2. 3452 3468 30 0
1. Insert last 2.delete front 3. display 4.exit
Enter your choice:
39
4.Write a program to implement a stack
4 a) Stack using arrays
Aim: Write a program to implement Stack using arrays
Source code: /****** Program to Implement Stack using Array ******/ #include <stdio.h> #define MAX 50 void push(); void pop(); void display(); int stack[MAX], top=-1, item; main() { int ch; do { printf("\n\n\n\n1.\tPush\n2.\tPop\n3.\tDisplay\n4.\tExit\n"); printf("\nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1:push(); break; case 2:pop(); break; case 3:display(); break; case 4:exit(0); default:printf("\n\nInvalid entry. Please try again...\n"); } }while(ch!=4); getch(); }
Enter your choice: 1 Enter ITEM: 10 ITEM inserted =10
1. Push 2. Pop 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 20 ITEM inserted =20
1. Push 2. Pop 3. Display 4. Exit Enter your choice: 1 Enter ITEM: 30 ITEM inserted =30
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 3
30 20 10
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 2 ITEM deleted =30
45
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 3
20 10
1. Push 2. Pop 3. Display 4. Exit
Enter your choice:
46
5.Write a program to implement a queue. 5a )Queue using arrays Aim:Write a Program to Implement Queue using Array Source code: /****** Program to Implement Queue using Array ******/ #include <stdio.h> #define MAX 50 void insert(); void delete(); void display(); int queue[MAX], rear=-1, front=-1, item;
main() { int ch; do { printf("\n\n1. Insert\n2. Delete\n3. Display\n4. Exit\n"); printf("\nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1: insert(); break; case 2: delete(); break; case 3:display(); break; case 4: exit(0); default: printf("\n\nInvalid entry. Please try again...\n"); }
}while(1);
getch();
}
void insert()
{ if(rear ==MAX-1) printf("\n\nQueue is full."); else
47
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item); if (rear ==-1 && front ==-1) { rear =0; front =0; } else rear++; queue[rear] =item; printf("\n\nItem inserted: %d", item);
}
}
void delete()
{ if(front ==-1) printf("\n\nQueue is empty."); else { item =queue[front]; if (front ==rear) { front =-1; rear =-1; } else front++; printf("\n\nItem deleted: %d", item); } }
void display()
{ int i; if(front ==-1) printf("\n\nQueue is empty."); else { printf("\n\n");
void main() { node T= NULL; int data,ch,i=0,n; clrscr(); printf("\nEnter the number of elements in the Tree:"); scanf("%d",&n); printf("\n The elements are :\n"); while(i<n) { scanf("%d",&data); T=insert(data,T); i++; } printf("1. INORDER\t2.PREORDER\t3.POSTOTRDER\t4.EXIT"); do { printf("\nEnter your choide:"); scanf("%d",&ch); switch (ch) { case 1:printf ("Inorder traversal of the given Tree\n"); inorder(T); break; case 2:printf("Preoroder traversal of the given Tree\n"); preorder(T); break; case 3:printf("Postorder traversal of the given Tree\n"); postorder(T); break; default:printf("Exit");
The elements are : 30 20 40 25 1. INORDER 2.PREORDER 3.POSTOTRDER 4.EXIT
Enter your choide:1
Inorder traversal of the given Tree 20 25 30 40 Enter your choide:2
Preoroder traversal of the given Tree 30 20 25 40 Enter your choide:3
Postorder traversal of the given Tree 25 20 40 30 Enter your choide:
59
8. Exercise on Selection sort Aim: Write a program on selection sort
Source code: /* Selection sort. */ #include <stdio.h> #include <conio.h> void main( ) { int arr[5] ={25, 17, 31, 13, 2 }; int i, j, temp ; clrscr( ) ; printf ( "Selection sort.\n" ) ; printf ( "\nArray before sorting:\n") ; for ( i =0 ; i <=4 ; i++) printf ( "%d\t", arr[i] ) ; for ( i =0 ; i <=3 ; i++) { for ( j =i +1 ; j <=4 ; j++) { if ( arr[i] >arr[j] ) { temp =arr[i] ; arr[i] =arr[j] ; arr[j] =temp ; } } } printf ( "\n\nArray after sorting:\n") ; for ( i =0 ; i <=4 ; i++) printf ( "%d\t", arr[i] ) ; getch( ) ; } Output:
60
9. Exercise on insertion sort
Aim: Write a program on insertion sort
Source code: /* Insertion sort. */ #include <stdio.h> #include <conio.h> void main( ) { int arr[5] ={25, 17, 31, 13, 2 }; int i, j, k, temp ; clrscr( ) ; printf ( "Insertion sort.\n" ) ; printf ( "\nArray before sorting:\n") ; for ( i =0 ; i <=4 ; i++) printf ( "%d\t", arr[i] ) ; for ( i =1 ; i <=4 ; i++) { for ( j =0 ; j <i ; j++) { if ( arr[j] >arr[i] ) { temp =arr[j] ; arr[j] =arr[i] ; for ( k =i ; k >j ; k-- ) arr[k] =arr[k - 1] ; arr[k +1] =temp ; } } } printf ( "\n\nArray after sorting:\n") ; for ( i =0 ; i <=4 ; i++) printf ( "%d\t", arr[i] ) ; getch( ) ; } Output:
61
10.Excercise on Bubble sort
Aim:Write a program on Bubble sort.
Source code: /* Bubble sort. */ #include <stdio.h> #include <conio.h> void main( ) { int arr[5] ={25, 17, 31, 13, 2 }; int i, j, temp ; clrscr( ) ; printf ( "Bubble sort.\n" ) ; printf ( "\nArray before sorting:\n") ; for ( i =0 ; i <=4 ; i++) printf ( "%d\t", arr[i] ) ;
for ( i =0 ; i <=3 ; i++) { for ( j =0 ; j <=3 - i ; j++) { if ( arr[j] >arr[j +1] ) { temp =arr[j] ; arr[j] =arr[j +1] ; arr[j +1] =temp ; } } } printf ( "\n\nArray after sorting:\n") ; for ( i =0 ; i <=4 ; i++) printf ( "%d\t", arr[i] ) ; getch( ) ; }
Output:
62
11. Implement a program for merge sort on two sorted lists of elements Aim:Write a program for merge sort on two sorted lists of elements Sourcecode: /* Merge Sort. */
#include <stdio.h> #include <conio.h>
void main( ) { int a[5] ={2,6,9,12,35 }; int b[5] ={7,10 ,18,48,33}; int c[10] ; int i, j, k, temp ;
clrscr( ) ;
printf ( "Merge sort.\n" ) ;
printf ( "\nFirst array:\n" ) ; for ( i =0 ; i <=4 ; i++) printf ( "%d\t", a[i] ) ;
printf ( "\n\nSecond array:\n" ) ; for ( i =0 ; i <=4 ; i++) printf ( "%d\t", b[i] ) ;
for ( i =0 ; i <=3 ; i++) { for ( j =i +1 ; j <=4 ; j++) { if ( a[i] >a[j] ) { temp =a[i] ; a[i] =a[j] ; a[j] =temp ; } if ( b[i] >b[j] ) { temp =b[i] ; b[i] =b[j] ; b[j] =temp ; } } }
63
for ( i =j =k =0 ; i <=9 ; ) { if ( a[j] <= b[k] ) c[i++] =a[j++] ; else c[i++] =b[k++] ;
if ( j ==5 | | k ==5 ) break ; }
for ( ; j <=4 ; ) c[i++] =a[j++] ;
for ( ; k <=4 ; ) c[i++] =b[k++] ;
printf ( "\n\nArray after sorting:\n") ; for ( i =0 ; i <=9 ; i++) printf ( "%d\t", c[i] ) ;
getch( ) ; }
Output:
64
12.Exercises on linear search
12 a)Aim: Write a program on linear search of sorted array
Source code: /* Linear search in a sorted array. */ #include <stdio.h> #include <conio.h> void main( ) { int arr[10] ={1, 2, 3, 9, 11, 13, 17, 25, 57, 90 }; int i, num ; clrscr( ) ; printf ( "Enter number to search: " ) ; scanf ( "%d", &num ) ; for ( i =0 ; i <=9 ; i++) { if ( arr[9] <num | | arr[i] >=num ) { if ( arr[i] ==num ) printf ( "The number is at position %d in the array.", i ) ; else printf ( "Number is not present in the array." ) ; break ; } }
getch( ) ; } Output:
65
12 b)Aim: Write a program on linear search of unsorted array
Source code: /* Linear search in an unsorted array. */ #include <stdio.h> #include <conio.h> void main( ) { int arr[10] ={11, 2, 9, 13, 57, 25, 17, 1, 90, 3 }; int i, num ; clrscr( ) ; printf ( "Enter number to search: " ) ; scanf ( "%d", &num ) ; for ( i =0 ; i <=9 ; i++) { if ( arr[i] ==num ) break ; } if ( i ==10 ) printf ( "Number is not present in the array." ) ; else printf ( "The number is at position %d in the array.", i ) ; getch( ) ; } Output:
66
13 )Excercises on binary search
Aim: Write a program on binary search Source code: /* Binary search in a sorted array. */ #include <stdio.h> #include <conio.h> void main( ) { int arr[10] ={1, 2, 3, 9, 11, 13, 17, 25, 57, 90 }; int mid, lower =0 , upper =9, num, flag =1 ; clrscr( ) ; printf ( "Enter number to search: " ) ; scanf ( "%d", &num ) ; for ( mid =( lower +upper ) / 2 ; lower <=upper ; mid =( lower +upper ) / 2 ) { if ( arr[mid] ==num ) { printf ( "The number is at position %d in the array.", mid ) ; flag =0 ; break ; } if ( arr[mid] >num ) upper =mid - 1 ; else lower =mid +1 ; }
if ( flag ) printf ( "Element is not present in the array." ) ; getch( ) ; } Output: