Data Structures Lab Manual2
Data Structures Lab Manual2
#include<stdio.h> #include<conio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removeSpecific(int);
void main(){
int choice,value,choice1,loc1,loc2; clrscr();
while(1){
mainMenu: printf("\n\n********* MENU ************\n1. Insert\n2. Display\n3. Delete\n4. Exit\nEnter your choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: printf("Enter the value to be insert: "); scanf("%d",&value);
while(1){
printf("Where you want to insert: \n1. At Beginning\n2. At End\n3. Between\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1) {
case 1: insertAtBeginning(value);
break;
case 2: insertAtEnd(value); break;
case 3: printf("Enter the two values where you wanto insert: "); scanf("%d
%d",&loc1,&loc2); insertBetween(value,loc1,loc2);
break;
default: printf("\nWrong Input!! Try again!!!\n\n"); goto mainMenu;
}
goto subMenuEnd;
}
subMenuEnd:
break;
case 2: display();
break;
case 3: printf("How do you want to Delete: \n1. From Beginning\n2. From End\n3. Spesific\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1) {
case 1: removeBeginning(); break;
case 2: removeEnd(value); break;
case 3: printf("Enter the value which you wanto delete: "); scanf("%d",&loc2);
removeSpecific(loc2); break;
default: printf("\nWrong Input!! Try again!!!\n\n"); goto mainMenu;
}
break;
case 4: exit(0);
default: printf("\nWrong input!!! Try again!!\n\n");
}
}
}
Output
Write a program that use functions to perform the following operations on doubly linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include<stdio.h>
#include<conio.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertAfter(int,int);
void deleteBeginning();
void deleteEnd();
void deleteSpecific(int);
void display();
struct Node
{
int data;
struct Node *previous, *next;
}*head = NULL;
void main()
{
int choice1, choice2, value, location; clrscr();
while(1)
{
printf("\n*********** MENU *************\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: "); scanf("%d",&choice1);
switch(choice1)
{
case 1: printf("Enter the value to be inserted: "); scanf("%d",&value);
while(1)
{
printf("\nSelect from the following Inserting options\n"); printf("1. At Beginning\n2. At End\
n3. After a Node\n4.
Cancel\nEnter your choice: ");
scanf("%d",&choice2); switch(choice2)
{
case 1: insertAtBeginning(value); break;
case 2: insertAtEnd(value);
break;
case 3: printf("Enter the location after which you want to
}
option!!!\n"); }
case 2: while(1)
{
printf("\nSelect from the following Deleting options\n"); printf("1. At Beginning\n2. At End\
n3. Specific Node\n4.
Cancel\nEnter your choice: ");
scanf("%d",&choice2); switch(choice2)
{
case 1: deleteBeginning(); break;
case 2: deleteEnd();
break;
case 3: printf("Enter the Node value to be deleted: "); scanf("%d",&location);
deleteSpecific(location); break;
case 4: goto EndSwitch;
default: printf("\nPlease select correct Deleting
}
}
option!!!\n");
EndSwitch: break; case 3:
display();
break; case 4:
exit(0);
default: printf("\nPlease select correct option!!!");
}
}
}
Solution:
#include<stdio.h>
#include<conio.h>
#define SIZE 100
int top = -1;
char stack[SIZE];
void push(char item);
char pop();
int is_operator(char symbol);
int precedence(char symbol);
void main(){
int i;
int j;
char infix_exp[SIZE], postfix_exp[SIZE];
char item;
char x;
clrscr();
printf("\nEnter Infix expression in parentheses: \n");
gets(infix_exp);
i=0;
j=0;
item=infix_exp[i++];
while(item != '\0') {
if(item == '(') {
push(item);
}
else if((item >= 'A' && item <= 'Z') || (item >= 'a' && item <= 'z'))
{ postfix_exp[j++] = item;
}
else if(is_operator(item) == 1){
x=pop();
while(is_operator(x) == 1 && precedence(x)>= precedence(item))
{ postfix_exp[j++] = x;
x = pop();
}
push(x);
push(item);
}
else if(item == ')'){
x = pop();
while(x != '(')
{ postfix_exp[j++] =
x; x = pop();
}
}
else{
printf("\nInvalid Arithmetic Expression.\n");
getch();
exit(0);
}
item = infix_exp[i++];
}
postfix_exp[j++] = '\0';
printf("\nArithmetic expression in Postfix notation: ");
puts(postfix_exp);
getch();
}
void push(char item)
{ if(top >= SIZE-1){
printf("\nStack Overflow. Push not possible.\n");
}
else{
top = top+1;
stack[top] = item;
}
}
char pop(){
char item = NULL;
if(top <= -1){
printf("\nStack Underflow. Pop not possible.\n");
}
else {
item = stack[top];
stack[top] = NULL;
top = top-1;
}
return(item);
}
int is_operator(char symbol){
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-')
{ return 1;
}
else{ retu
rn 0;
}
}
int precedence(char symbol)
{ if(symbol == '^'){
return(3);
}
else if(symbol == '*' || symbol == '/'){
return(2);
}
else if(symbol == '+' || symbol == '-'){
return(1);
}
else {
return(0);
}
}
Output:
Week 4:
Write C programs to implement a double ended queue ADT using i)array and ii)doubly
linked list respectively.
Output:
Solution: Double ended queue ADT using doubly linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *previous;
struct node *next;
};
struct node *front, *rear;
int count;
void display();
void insert_begin(int x);
void insert_last(int x);
int delete_begin();
int delete_last();
int main()
{
int ch, ele;
printf("\n1. Insert-begin\n2. Insert-last\n3. Delete-begin\n4. Delete-last\n5. Display \[Link]");
while(1)
{
printf("Enter your
choice:"); scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter value for insertion
:"); scanf("%d",&ele);
insert_begin(ele);
break;
case 2:
printf(" Enter the value for insertion:");
scanf("%d",&ele);
insert_last(ele);
break;
case 3:
ele = delete_begin();
if(ele!=-1)
printf("%d is deleted .",ele);
break;
case 4:
ele = delete_last();
if(ele!=-1)
printf("%d is deleted .",ele);
break;
case 5:
display();
break;
case 6: exit(0);
}
}
}
void display()
{
struct node * ptr;
ptr = front;
if(front==NULL || rear==NULL)
{
printf("List is empty");
return;
}
while(ptr != NULL)
{
printf( "%d -> ",ptr ->data);
ptr = ptr->next;
}
printf("\n");
}
void insert_begin(int x)
{
struct node *new1;
new1 = (struct node*)malloc(sizeof(struct node));
new1 -> data =x;
new1 ->previous = new1 ->next =NULL;
if(front == NULL||rear==NULL)
front = rear = new1;
else
{
new1 ->next = front;
front ->previous = new1;
front = new1;
}
}
void insert_last(int x)
{
struct node *new1;
new1 = (struct node*)malloc(sizeof(struct node));
new1 ->data = x;
new1 -> previous = new1 ->next = NULL;
if (front == NULL||rear==NULL)
front = rear = new1;
else
{
rear ->next = new1;
new1 ->previous = rear;
rear = new1;
}
}
int delete_begin()
{
int x;
struct node *temp;
if (front == NULL || rear==NULL)
{
printf( " LIST IS EMPTY ");
return -1;
}
else
{
temp = front;
x= temp->data;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front = front->next;
front->previous = NULL;
}
count --;
free(temp);
return x;
}
}
int delete_last( )
{ int x;
struct node *temp;
if(rear == NULL || front==NULL)
{
printf( " LIST IS EMPTY ");
return -1;
}
else
{
temp = rear;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
rear = rear->previous;
rear -> next = NULL;
}
x= temp ->data;
free(temp);
count --;
return x;
}
}
Output:
Week 5:
Write a C program that uses functions to perform the following:
a) Create a binary search tree of characters
b) Traverse the above Binary search tree recursively in Postorder.
Solution:
#include<stdio.h>
#include<stdlib.h>
typedef struct BST {
char d; /*declaring a structure to create a
node*/ struct BST *lc,*rc;
}node;
/*main program*/
void main() {
int choice;
char ans='N';
int key;
node *nn,*root,*parent;
root=NULL;
while(1) {
printf("\n\n ***** MENU - Binary search tree
*****"); printf("\n 1. Create\n 2. Tree Traversals\n 3.
Exit"); printf("\n Please select the operation: ");
scanf("%d",&choice);
switch(choice) {
case 1: do {
nn=(node *)malloc(sizeof(node));
printf("\n Please enter the element to be insert:
"); nn->lc=NULL;
nn->rc=NULL;
scanf(" %c",&nn->d);
if(root==NULL)
root=nn;
else
insert(root,nn);
printf("\n Want to insert more elements?(Y/N): ");
scanf(" %c",&ans);
}while(ans=='y');
break;
case 2: if(root==NULL)
printf("\n\n Tree is not created");
else {
printf("\n\n The inorder display : ");
inorder(root);
printf("\n\n The preorder display : ");
preorder(root);
printf("\n\n The postorder display:");
postorder(root);
}
break;
case 3: exit(0);
}
}
}
Output:
Week 6:
Write a C program that uses functions to perform the following:
a) Create a binary search tree of integers.
b) Traverse the above Binary search tree non recursively in inorder.
Solution:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
void main()
{ int choice;
char ans = 'N';
int key;
node *newNode, *root, *temp, *parent;
node *getNode();
root = NULL;
clrscr();
while(1){
printf("\n\n****** Binary Search Tree MENU ********");
printf("\n1. Create");
printf("\n2. Search");
printf("\n3. Display - Traversals");
printf("\n4. Exit");
printf("\nPlease enter your choice :");
scanf("%d", &choice);
switch (choice)
{ case 1:
do {
newNode = getNode();
printf("\nPlease enter the Element to be insert: ");
scanf("%d", &newNode->data);
case 2:
printf("\nEnter Element to be search: ");
scanf("%d", &key);
case 3:
if (root == NULL)
printf("\nTree Is Not Created");
else {
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
case 4: exit(0);
default: printf("\nPlease select correct operations!!!");
}
}
}
/* Creating a new Node */
node *getNode() {
node *temp;
temp = (node *)
malloc(sizeof(node)); temp-
>leftChild = NULL;
temp->rightChild = NULL;
return temp;
}
/* Inserting new Node into binary search tree */
void insert(node *root, node *newNode) {
if (newNode->data < root->data) {
if (root->leftChild == NULL)
root->leftChild = newNode;
else
insert(root->leftChild, newNode);
}
Output:
Week 7:
Write C programs for implementing the following sorting methods to arrange a list of
integers in Ascending order :
a) Insertion sort
b) Merge sort
Solution:
- Insertion Sort
# include <stdio.h>
# include <conio.h>
#define MAXSIZE 100
void main()
{
int list[MAXSIZE],size, count, i, temp;
clrscr();
printf("Please enter the actual size of the List: ");
scanf("%d", &size);
getch();
}
Output:
- Merge Sort
#include <stdio.h>
#include <conio.h>
void mergesort(int[],int,int);
void mergearray(int[],int,int,int);
void main() {
int list[MAX],size,i;
clrscr();
printf("\n\n Please enter size of the list: ");
scanf("%d",&size);
printf("\n\n Please enter %d number of elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&list[i]);
mergesort(list,0,size-1); printf("\
n\n List after sorting: ");
for(i=0;i<size;i++)
printf("%d ",list[i]);
getch();
}
Output:
Week 8:
Write C programs for implementing the following sorting methods to arrange a list of
integers in ascending order:
a) Quick sort
b) Selection sort
Solution:
- Quick Sort
#include<stdio.h>
#include<conio.h>
void main(){
int list[20],size,i;
clrscr();
printf("\n\nEnter size of the list: ");
scanf("%d",&size);
quickSort(list,0,size-1);
getch();
}
temp = list[pivot];
list[pivot] = list[j];
list[j] = temp;
quickSort(list,first,j-1);
quickSort(list,j+1,last);
}
}
Output:
- Selection Sort
#include<stdio.h>
#include<conio.h>
void main(){
int size,i,j,temp,list[100];
clrscr();
getch();
}
Output:
Week 9:
i) Write a C program to perform the following
operation: a)Insertion into a B-tree.
ii)Write a C program for implementing Heap sort algorithm for sorting a given list
of integers in ascending order.
Solution: B- Tree
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <alloc.h>
#define MAX 4
#define MIN 2
struct btnode
{
int count ;
int value[MAX + 1] ;
struct btnode *child[MAX + 1] ;
};
void main( )
{
struct node *root ;
root = NULL ;
clrscr( ) ;
getch( ) ;
}
if ( k <= MIN )
mid = MIN ;
else
mid = MIN + 1 ;
if ( k <= MIN )
fillnode ( val, c, n, k ) ;
else
fillnode ( val, c, *newnode, k - mid ) ;
/* removes the value from the node and adjusts the values */
void clear ( struct btnode *node, int k )
{
int i ;
for ( i = k + 1 ; i <= node -> count ; i++ )
{
node -> value [i - 1] = node -> value [i] ;
node -> child [i - 1] = node -> child [i] ;
}
node -> count-- ;
}
/* adjusts the values and children while shifting the value from parent to right
child */
void rightshift ( struct btnode *node, int k )
{
int i ;
struct btnode *temp ;
/* adjusts the values and children while shifting the value from parent to left
child */
void leftshift ( struct btnode *node, int k )
{
int i ;
struct btnode *temp ;
if ( root != NULL )
{
for ( i = 0 ; i < root -> count ; i++ )
{
display ( root -> child [i] ) ;
printf ( "%d\t", root -> value [i + 1] ) ;
}
display ( root -> child [i] ) ;
}
}
Output:
Solution: Heap Sort
#include <stdio.h>
#include <conio.h>
int p(int);
int left(int);
int right(int);
void heapify(int[],int,int);
void buildheap(int[],int);
void heapsort(int[],int);
void main() {
int x[20],n,i;
clrscr();
printf("\n\nPlease enter the number of elements to be sorted : ");
scanf("%d",&n);
printf("\n\nPlease enter %d integer elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
heapsort(x,n);
printf("\n\nList of elements after sort : ");
for(i=0;i<n;i++)
printf("%d ",x[i]);
getch();
}
int p(int i)
{ return i/2;
}
int left(int i)
{
return 2*i+1;
}
int right(int i)
{ return 2*i+2;
}
void heapify(int a[],int i,int n) {
int l,r,large,t;
l=left(i);
r=right(i);
if((l<=n-1)&&(a[l]>a[i]))
large=l; else large=i;
if((r<=n-1)&&(a[r]>a[large]))
large=r;
if(large!=i) {
t=a[i];
a[i]=a[large];
a[large]=t;
heapify(a,large,n);
}
}
void buildheap(int a[],int n)
{ int i;
for(i=(n-1)/2;i>=0;i--)
heapify(a,i,n);
}
void heapsort(int a[],int n)
{ int i,m,t;
buildheap(a,n);
m=n ;
for(i=n-1;i>=1;i--) {
t=a[0];
a[0]=a[i];
a[i]=t;
m=m-1;
heapify(a,0,m);
}
}
Output:
Week 10:
Write a C program to implement all the functions of a dictionary (ADT) using hashing.
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int b;
int hsearch(int key,int d,int *ht,int *empty)
{ int i=key%(d);
int j=i, c=0;
do {
if(empty[j]||(*(ht+j)==key))
return j;
c++;
j=(i+c)%(d);
}while(j!=i);
return 0;
}
int search(int key,int d,int *ht,int *empty) {
b=hsearch(key,d,ht,empty);
if(empty[b]==1)
return -1;
else if(b==0)
return 1;
else
return b;
}
void insert(int key,int d,int *ht,int *empty) {
b=hsearch(key,d,ht,empty);
if(empty[b]) {
empty[b]=0;
*(ht+b)=key;
printf("\n Elements is inserted successfully!!!\n");
}
}
void delete(int key,int d,int *ht,int *empty) {
int b=hsearch(key,d,ht,empty);
*(ht+b)=0;
empty[b]=1;
printf("\n Element is deleted\n");
}
void display(int d,int *ht,int *empty)
{ int i;
printf("\n Hash table elements are\n");
for(i=0;i<d;i++) {
if(empty[i])
printf(" 0");
else
printf("%5d",*(ht+i));
}
printf("\n");
}
void main() {
int choice=1, key, d,i,s, *empty,*ht;
clrscr();
printf("\n\n Please enter the hash table size: ");
scanf("%d",&d);
ht=(int *)malloc(d *sizeof(int));
empty=(int *)malloc(d *sizeof(int));
for(i=0;i<d;i++)
empty[i]=1;
while(1) {
printf("\n\n ***** MENU - LINEAR PROBING *****"); printf("\
n 1: Insert\n 2: Delete\n 3: Search\n 4: Display\n 5: Exit"); printf("\
n Please enter your choice : ");
scanf("%d",&choice);
switch(choice) {
case 1: printf("\n Please enter the elemant to be insert : ");
scanf("%d",&key);
insert(key,d,ht,empty);
break;
case 2: printf("\n Please enter the element to be remove : ");
scanf("%d",&key);
delete(key,d,ht,empty);
break;
case 3: printf("\n Please enter the search element to be search : ");
scanf("%d",&key);
s=search(key,d,ht,empty);
if(s==-1||s==0)
printf("\n Given element is not found\n");
else
printf("\n Given element is found at index %d",hsearch(key,d,ht,empty));
break;
case 4: display(d,ht,empty);
break;
case 5: exit(0);
}
}
}
Output:
Week 11:
Write a C program for implementing Knuth-Morris- Pratt pattern matching algorithm.
Solution:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char string[100], matchcase[20], c;
int i = 0, j = 0, index;
clrscr();
/*Reading string*/ printf("\
nEnter string: ");
scanf("%s",string);
i = strlen(string);
string[i - 1] = '\0';
/* Reading pattern to be search*/
printf("\nEnter substring: ");
scanf("%s",matchcase);
i = strlen(matchcase);
matchcase[i - 1] = '\0';
for (i = 0; i < strlen(string) - strlen(matchcase) + 1; i++)
{
index = i;
if (string[i] == matchcase[j])
{
do
{
i++;
j++;
} while(j != strlen(matchcase) && string[i] == matchcase[j]);
if (j == strlen(matchcase))
{
printf("\nMatch found from position %d\n", index + 1);
goto end;
}
else
{
i = index + 1;
j = 0;
}
}
}
printf("\nNo substring match found in the string.\n");
end: getch();
}
Output:
Week 12:
Write C programs for implementing the following graph traversal algorithms:
a) Depth first traversal
b) Breadth first traversal
Solution: DFS
#include <stdio.h>
#include<conio.h>
void dfs(int);
int g[10][10],visited[10],n,vertex[10];
void main() {
int i,j;
clrscr();
printf("\n\nPlease enter number of vertices:");
scanf("%d",&n);
printf("Please enter the values of vertices:");
for(i=0;i<n;i++)
scanf("%d",&vertex[i]);
printf("\nPlease enter adjecency matrix of the graph:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&g[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
dfs(0);
getch();
}
void dfs(int i) {
int j;
printf("%d ",vertex[i]);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&g[i][j]==1)
dfs(j);
}
Output: