NITJ DATA STRUCTURES & ALGORITHMS LAB 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 and algorithms as implemented on c++ computer language.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
}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 SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
PROGRAM OUTPUT
GAGANDEEP SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
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 SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
PROGRAM OUTPUT
GAGANDEEP SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
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 SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
PROGRAM OUTPUT
GAGANDEEP SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
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) {
};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) {
};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 SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
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){
};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;
}
GAGANDEEP SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
C O N T I N U E D ...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){
#include<iostream.h>#include<stdlib.h>#include<conio.h># define MAX 5int 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 ...
C O N T I N U E D ...
GAGANDEEP SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
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 SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
PROGRAM OUTPUT
STACK IMPLEMENTATION USING ARAY
GAGANDEEP SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
#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 SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
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++){
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 SINGH08104017
NITJDATA STRUCTURES & ALGORITHMS LAB
C O N T I N U E D ...
cout<<"\nInfix Expression is ";for(int m=0;m<=count;m++){
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