You are on page 1of 54

DATA STRUCTURES & ALGORITHMS LAB

NITJ

LINEAR SEARCH IN ARRAY

#include <iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
clrscr();
int a[5], b, state=0;
cout<<"Please enter the elements of the array: \n"<<endl;
for(int i=0; i<5; i++)
{
cout<<i+1<<" element... ";
cin>>a[i];
cout<<endl;
}
cout<<"\nPlease enter the number you want to
search in array list: ";
cin>>b;
for (int j=0; j<5; j++)
{
if(b==a[j])
{
cout<<"\n\nThe number you have entered is
at "<<j+1<<"position. ";
state=0;
break;
}
else
{
state=1;
}
}
if(state==1)
{
cout<<"\n\nThe number you searched was not found.";
}
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

BINARY SEARCH IN ARRAY

#include<iostream.h>
#include<conio.h>

void main()
{
int a[9], b, num, middle, state=0, p=0, q=8;
cout<<"Please enter the elements of array in ascending order:\n\n" ;

for(int i=0;i<9;i++)
{
cout<<(i+1)<<" element : ";
cin>>a[i];
cout<<endl;
}
cout<<"\nPlease enter the number that you want to search for... ";
cin>>b;
while(p<=q)
{
middle=(p+q)/2;
if(b==a[middle])
{
cout<<"\nThe number you searched is at " <<
(middle+1)<<" position.\n";
state=0;
break;
}
else if(b<a[middle])
{
q=middle-1;
}
else
{
p=middle+1;
}
state=1;
}
if(state==1)
cout<<"Number not Found";
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

BUBBLE SORT IN ARRAY

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n=5;
int a[5];
cout<<"Enter the elements of the array which
you want to sort:\n"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
for(int j=0;j<n-(i+1);j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nThe array in sorted form is:"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

BUBBLE SORT IN STRING

#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
clrscr();
char a[20][20];
int n;
cout<<"Please enter the number of strings you want to enter... ";
cin>>n;
cout<<"\nEnter the strings you want to sort :\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(strcmp(a[j],a[j+1])>0)
{
char temp[50];
strcpy(temp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],temp);
}
}
}
cout<<"\nThe strings in sorted form are :\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

SELECTION SORT

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a[6], s, loc;
cout<<"Enter the elements of array :\n";
for(int i=0; i<6; i++)
{
cin>>a[i];
}
int temp;
for(i=0;i<6;i++)
{
s=a[i];
loc=i;
for(int j=i+1; j<6; j++)
{
if(s>a[j])
{
s=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"\nThe array in sorted form is:\n";
for(i=0; i<6; i++)
{
cout<<a[i]<<endl;
}
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

INSERTION SORT

#include<iostream.h>
#include<conio.h>

void main()
{
int arr[5], temp;
cout<<"Please enter the elements of the array :\n";
cin>>arr[0];
for (int i=1; i<5; i++)
{
cin>>arr[i];
for (int j=i; j>0; j--)
{
if (arr[j-1]>arr[j])
{
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
cout<<"\nThe sorted array is as follows :";
for (int k=0; k<5; k++)
{
cout<<endl<<arr[k];
}
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

TRAVERSING A LINKED LIST

#include<iostream.h>
#include<conio.h>

void main()
{
struct node
{
char info[11];
node *next;
};
node *first,*last,*temp;
int i,n;
cout<<"Please enter the size of linked list : ";
cin>>n;
first=new node;
cout<<"\nEnter data for 1 node :\n";
cin>>first->info;
first->next=NULL;
last=first;
for(i=1; i<n; i++)
{
temp=new node;
cout<<"\nEnter data for "<<(i+1)<<" node :\n";
cin>>temp->info;
temp->next=NULL;
last->next=temp;
last=temp;
}
cout<<"\n\nThe list which you have created is :\n";
last=first;
while(last!=NULL)
{
cout<<last->info<<endl;
last=last->next;
}
delete first;
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

SEARCH IN A LINKED LIST

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
clrscr();
struct node
{
char info[10];
node *next;
};
node *first,*last,*temp;
int i, n, fnd;
char srch[10];
cout<<"Please enter the size of linked list : ";
cin>>n;
first=new node;
cout<<"\nEnter data for 1 node : ";
cin>>first->info;
first->next=NULL;
last=first;
for(i=1; i<n; i++)
{
temp=new node;
cout<<"\nEnter data for "<<(i+1)<<" node : ";
cin>>temp->info;
temp->next=NULL;
last->next=temp;
last=temp;
}
cout<<"\n\nEnter the data to be searched ... ";
cin>>*srch;
if(first == NULL)
{
cout<<"List is empty !";
}

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

last=first;
fnd=0;
while((last!=NULL) && (! fnd))
{
if(last->info==srch)
{
fnd=1;
}
else
{
last=last->next;
}
}
if(fnd==1)
{
cout<<"The searched data exists in the linked list.";
}
else
{
cout<<"\nThe searched data was not found.";
}
delete first;
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

INSERTION IN LINKED LIST

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
clrscr();
struct node
{
char name[10];
int roll;
node *next;
};
node *first,*last,*temp;
int i,n,roll;
cout<<"Please enter the size of linked list : ";
cin>>n;
first=new node;
cout<<"\nEnter data for 1 node : \n";
cin>>first->name>>first->roll;
first->next=NULL;
last=first;
for(i=1;i<n;i++)
{
temp=new node;
cout<<"Enter data for "<<(i+1)<<" node : \n";
cin>>temp->name>>temp->roll;
temp->next=NULL;
last->next=temp;
last=temp;
}
cout<<"\nEnter the number of node after which
insertion is desired ... ";
cin>>roll;
last=first;

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

while((last->roll!=roll)&&(last!=NULL))
{
last=last->next;
}
if(last!=NULL)
{
temp=new node;
cout<<"\nEnter the data of new node : \n";
cin>>temp->name>>temp->roll;
temp->next=last->next;
last->next=temp;
}
else
{
cout<<"Insertion is not possible in this situation.";
}
cout<<"\nThe new list is: \n";
last=first;
while(last!=NULL)
{
cout<<last->name<<endl<<last->roll<<endl;
last=last->next;
}
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

DELETION IN LINKED LIST

#include<iostream.h>
#include<conio.h>

void main()
{
struct node
{
char name[10];
int roll;
node *next;
};
node *first,*last,*temp;
int i,n,roll;
cout<<"Please enter the size of linked list : ";
cin>>n;
first=new node;
cout<<"\nEnter data of 1 node : \n";
cin>>first->name>>first->roll;
first->next=NULL;
last=first;
for(i=1;i<n;i++)
{
temp=new node;
cout<<"\nEnter data of "<<(i+1)<<" node : \n";
cin>>temp->name>>temp->roll;
temp->next=NULL;
last->next=temp;
last=temp;
}

cout<<"\nEnter the number of node that you want to delete... \n";


cin>>roll;
if(first->roll==roll)
{
last=first;
first=first->next;
delete last;
}

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

else
{
last=first->next;
while(last!=NULL)
{
if(last->roll==roll)
{
last=last->next;
delete last;
break;
}
last=last->next;
}

}
cout<<"\nThe new list is:\n";
last=first;
while(last!=NULL)
{
cout<<last->name<<endl<<last->roll<<endl;
last=last->next;
}
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

INSERTION AND DELETION IN QUEUE USING ARRAY

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
# define MAX 5
int insert();
int del();
int display();
int queue_arr[MAX];
int front = -1, rear = -1;
int main()
{
int choice;
while(1)
{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"Wrong choice\n";
}
}

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

return 0;
}
insert()
{
int added_item;
if (rear==MAX-1)
cout<<"Queue Overflow\n";
else
{
if (front==-1) /*If queue is initially empty */
front=0;
cout<<"Input the element for adding in queue : ";
cin>>added_item;
rear++;
queue_arr[rear] = added_item ;
}
} // e n d o f i n s e r t ( )
del()
{
if (front == -1 || front > rear)
{
cout<<"Queue Underflow\n";
return 0;
}
else
{
cout<<"Element deleted from queue is : "<<
queue_arr[front]<<endl;
front=front+1;
}
}// e n d o f d e l ( )
display()
{
int i;
if (front == -1)
cout<<"Queue is empty\n";
else
cout<<"Queue is :\n";
for(i=front;i<= rear;i++)
cout<<queue_arr[i]<<" ";
cout<<"\n";
} // e n d o f p r o g r a m
GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

STACK IMPLEMENTATION USING ARAY

#include<iostream.h>
#include<conio.h>
int main()
{
int arr[10];
int top=-1;
int ch;
while(1)
{
cout<<"\t1--->PUSH\t2----->POP\t3----->DISPLAY\t4-----
>QUIT\nPlease enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"Now enter the element to be pushed: ";
top=top+1;
cin>>arr[top];
}
break;
case 2:
{
if(top<=0)
{
cout<<"STACK EMPTY !!!:\n";
break;
}
else
{
cout<<"The element \""<<arr[top]<<"\" has
been successfully popped from STACK.\n";
top=top-1;
}
}
break;

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

case 3:
{
if(top==-1)
{
cout<<"UNDERFLOW !!!:\n";
break;
}
else
{
cout<<"STACK is displayed below:\n";
for(int k=0;k<=top;k++)
{
cout<<arr[k]<<" ";
}
cout<<endl;
}
}
break;
case 4:
{
return 0;
}
default:
cout<<"SORRY!!! Enter Proper Choice...\n";
}
}
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

POSTFIX EXPRESSION EVALUATION

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char ex[20];
int str[20],j=0,len,top=-1;
cout<<"\nPlease enter the postfix expression to be evaluated : ";
cin>>ex;
len=strlen(ex);
ex[len]=')';
while(ex[j]!=')')
{
if((ex[j]!='+')&&(ex[j]!='-')&&(ex[j]!='*')&&(ex[j]!='/'))
{
top++;
str[top]=(int)(ex[j])-48;
}
else
{
switch(ex[j])
{
case '+':
str[top-1]=(str[top-1])+(str[top]);
break;
case '-':
str[top-1]=(str[top-1])-(str[top]);
break;
case '*':
str[top-1]=(str[top-1])*(str[top]);
break;
case '/':
str[top-1]=(str[top-1])/(str[top]);
break;
default: cout<<"Wrong Postfix Expression...\n";
}
top=top-1;
} j++;
}
cout<<"\nThe evaluated result is : "<<str[top];
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM TO IMPLEMENT QUICK SORT

#include<iostream.h >
#include<stdio.h >
#include<conio.h >
void print(int a[])
{
cout<<"The array in sorted order is\t";
for (int i = 0; i < 10; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int partition(int a[], int p, int r)
{
int x = a[r];
int j = p - 1;
for (int i = p; i < r; i++)
{
if (x <= a[i])
{
j = j + 1;
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
a[r] = a[j + 1];
a[j + 1] = x;
return (j + 1);
}
void quickSort(int a[], int p, int r)
{
if (p < r)
{
int q = partition(a, p, r);
quickSort(a, p, q - 1);
quickSort(a, q + 1, r);
}
}
C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

void main()
{
int a[10];
cout<<"Please enter the number to be sorted...\n";
for (int t=0; t<10; t++)
{
cin>>a[t];
}
quickSort(a, 0, 9);
print(a);
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

INDEX
S.No
LIST OF PROGRAMS DATE REMARKS
.
1. Program to implement LINEAR SEARCH
2. Program to implement BINARY SEARCH
3. Program to implement BUBBLE SORT of numbers
4. Program to implement BUBBLE SORT of strings
5. Program to implement SELECTION SORT
6. Program to implement INSERTION SORT
7. Program to implement QUICK SORT
8. Program to implement TRAVERSAL OF A LINK LIST
9. Program to implement SEARCHING IN A LINK LIST
10. Program to implement INSERTION IN A LINK LIST
11. Program to implement DELETION IN A LINK LIST
Program to implement PUSH, PULL AND DISPLAY
12.
OPERATIONS in a stack using array
Program to implement PUSH, PULL AND DISPLAY
13.
OPERATIONS in a stack using linked list
14. Program to implement POSTFIX EVALUATION
15. Program to convert INFIX TO POSTFIX NOTATION
Program to implement TRAVERSAL, INSERTION AND
16.
DELETION in a circular queue
Program to implement TRAVERSAL, INSERTION AND
17.
DELETION in a queue
Program to implement INSERTION AND DELETION IN
18.
A BINARY SEARCH TREE
Program to implement PREORDER TRAVERSAL OF A
19.
BINARY SEARCH TREE
Program to implement INORDER TRAVERSAL OF A
20.
BINARY SEARCH TREE
Program to implement POSTORDER TRAVERSAL OF A
21.
BINARY SEARCH TREE

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PREORDER TRAVERSAL OF BST

#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node* llink;
node* rlink;
};
node* insert(node* );
void pretraversal(node*);
node *root=NULL;
void main()
{
clrscr();
char choice, ch='y';
do
{
cout<<"\nEnter the desired option...";
cout<<"\n1:Insertion\t2:Preorder Traversal\n";
cin>>choice;
switch(choice)
{
case '1' :insert(root);
break;
case '2':pretraversal(root);
break;
default:cout<<"\nWrong Choice";
}
cout<<"\nDo you want to proceed (y/n)... ";
cin>>ch;
}while(ch!='n');
getch();
}
node* insert(node*)
{
node* ptr;
ptr=new node;
int val;

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

cout<<"Enter the value : ";


cin>>val;
ptr->info=val;
if(root==NULL)
{
root=ptr;
root->rlink=NULL;
root->llink=NULL;
}
else
{
node* save;
node* p;
p=root;
while(p!=NULL)
{
if(val<p->info)
{
save=p;
p=p->llink;
}
else
{
save=p;
p=p->rlink;
}
}
if(val<save->info)
{
save->llink=ptr;
}
else
save->rlink=ptr;
ptr->rlink=NULL;
ptr->llink=NULL;
}
return root;
}
C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

void pretraversal(node* root)


{
int stack[100];
int top=-1;
cout<<"\nthe elements are ";
node* p;
p=root;
while(p!=NULL)
{
cout<<p->info<<" ";
if(p->rlink!=NULL)
{
top=top+1;
stack[top]=(p->rlink)->info;
}
if(p->llink!=NULL)
{
p=p->llink;
}
else
{
node* s;
s=root;
while(s->info != stack[top])
{
if(stack[top]<s->info)
s=s->llink;
else
s=s->rlink;
}
top=top-1;
p=s;
}
}
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PREORDER TRAVERSAL OF BST

#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node* llink;
node* rlink;
};
node* insert(node* );
void intraversal(node*);
node *root=NULL;
void main()
{
clrscr();
char choice, ch='y';
do
{
cout<<"\nEnter the desired option...";
cout<<"\n1:Insertion\t2:Inorder Traversal\n";
cin>>choice;
switch(choice)
{
case '1' :insert(root);
break;
case '2':intraversal(root);
break;
default:cout<<"\nWrong Choice...";
}
cout<<"Do you want to proceed (y/n)... ";
cin>>ch;
}while(ch!='n');
getch();
}
node* insert(node*)
{
node* ptr;
ptr=new node;
int val;

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...
cout<<"Enter the value : ";
cin>>val;
ptr->info=val;
if(root==NULL)
{
root=ptr;
root->rlink=NULL;
root->llink=NULL;
}
else
{
node* save;
node* p;
p=root;
while(p!=NULL)
{
if(val<p->info)
{
save=p;
p=p->llink;
}
else
{
save=p;
p=p->rlink;
}
}
if(val<save->info)
{
save->llink=ptr;
}
else
save->rlink=ptr;
ptr->rlink=NULL;
ptr->llink=NULL;
}
return root;
}
void intraversal(node *root)
{
int stack[100];
C O N T I N U E D ...
GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...
int top=-1;
cout<<"\nThe elements are ";
node *p;
p=root;
rd:while(p!=NULL)
{
top=top+1;
stack[top]=p->info;
p=p->llink;
}
node* s;
s=root;
while(s->info!=stack[top])
{
if(stack[top]<s->info)
s=s->llink;
else
s=s->rlink;
}
top=top-1;
p=s;
while(p!=NULL)
{
cout<<p->info<<" ";
if(p->rlink!=NULL)
{
p=p->rlink;
goto rd;
}
s=root;
while(s->info!=stack[top])
{
if(stack[top]<s->info)
{s=s->llink;}
else
{s=s->rlink;}
}
top=top-1;
p=s;
}
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

POSTORDER TRAVERSAL OF BST

#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node* llink;
node* rlink;
};
node* insert(node* );
void posttraversal(node*);
node *root=NULL;
void main()
{
clrscr();
char choice, ch='y';
do
{
cout<<"\nEnter the desired option...";
cout<<"\n1:Insertion\t2:Postorder Traversal\n";
cin>>choice;
switch(choice)
{
case '1' :insert(root);
break;
case '2':posttraversal(root); cout<<"\n";
break;
default:cout<<"\nWrong Choice";
}
cout<<"Do you want to proceed (y/n)... ";
cin>>ch;
}while(ch!='n');
getch();
}
node* insert(node*)
{
node* ptr;
ptr=new node;
int val;

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

cout<<"Enter the value : ";


cin>>val;
ptr->info=val;
if(root==NULL)
{
root=ptr;
root->rlink=NULL;
root->llink=NULL;
}
else
{
node* save;
node* p;
p=root;
while(p!=NULL)
{
if(val<p->info)
{
save=p;
p=p->llink;
}
else
{
save=p;
p=p->rlink;
}
}
if(val<save->info)
{
save->llink=ptr;
}
else
save->rlink=ptr;
ptr->rlink=NULL;
ptr->llink=NULL;
}
return root;
}

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...
void posttraversal(node *root)
{
cout<<"\nThe elements are ";
node *p;
node *s;
p=root;
int stack[100];
int top=-1;
dd:while(p!=NULL)
{
top=top+1;
stack[top]=p->info;
if(p->rlink!=NULL)
{
top=top+1;
node *k;
k=p->rlink;
stack[top]=-(k->info);
}
p=p->llink;
}
while((stack[top]>0)&&top>=0)
{
cout<<stack[top]<<" ";
top=top-1;
}
if(stack[top]<0)
{
stack[top]=-stack[top];
s=root;
while(s->info!=stack[top])
{
if(stack[top]<s->info)
s=s->llink;
else
s=s->rlink;
}
p=s;
top=top-1;
goto dd;
}
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

INFIX TO POSTFIX CONVERSION

#include<iostream.h>
#include<ctype.h>
#include<conio.h>
void push(char a[],char x,int& top)
{
top++;
a[top]=x;
}
char pop(char a[],int& top)
{
return a[top];
}
void postExp( char a[],char x,int& num)
{
a[num]=x;
num++;
}
void main()
{
char infix;
char stack[100];
char Q[100];
char postfix[100];
int num=0;
int top=0;
int count=0;
Q[count]='(';
cout<<"Enter the infix expression (press c to
convert)...\n"<<endl;
cin>>infix;
while(infix!='c')
{
count++;
Q[count]=infix;
cin>>infix;
}
count++;
Q[count]=')';

C O N T I N U E D ...

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

C O N T I N U E D ...

cout<<"\nInfix Expression is ";


for(int m=0;m<=count;m++)
{
cout<<Q[m];
}
for(int i=0;i<=count;i++)
{
if(isdigit(Q[i]))
{
postExp(postfix,Q[i],num);
}
else if(Q[i]=='(')
{
push(stack,Q[i],top);
}
else if(Q[i]=='+')
{
while(stack[top]=='+'||stack[top]=='*'||
stack[top]=='/'||stack[top]=='^'||stack[top]=='-')
{
postExp(postfix,pop(stack,top),num);
int temp=stack[top];
top--;
}
push(stack,Q[i],top);
}
else if(Q[i]=='-')
{
while(stack[top]=='+'||stack[top]=='*'||
stack[top]=='/'||stack[top]=='^'||stack[top]=='-')
{
postExp(postfix,pop(stack,top),num);
int temp=stack[top];
top--;
}
push(stack,Q[i],top);
}

C O N T I N U E D ...

C O N T I N U E D ...
GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

else if(Q[i]=='^')
{
push(stack,Q[i],top);
}
else if(Q[i]=='/')
{
push(stack,Q[i],top);
}

else if(Q[i]=='*')
{
push(stack,Q[i],top);
}
else if(Q[i]==')')
{
while(stack[top]!='(')
{
char x=pop(stack,top);
postExp(postfix,x,num);
int temp=stack[top];
top--; }
pop(stack,top);
int temp1=stack[top];
top--;}
}
cout<<endl<<"\nPostfix expression is ";
for(int j=0;j<=num;j++)
{
cout<<postfix[j];
}
cout<<"\b";
getch();
} // e n d o f p r o g r a m

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

PROGRAM OUTPUT

GAGANDEEP SINGH
08104017
DATA STRUCTURES & ALGORITHMS LAB
NITJ

GAGANDEEP SINGH
08104017

You might also like