1. LINEAR SEARCH
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int search;
class linear_s
{
public: int flag;
input(int*,int,int);
int n,key,list[200];
void linear_search(int*,int,int);
void display(int*,int);
};
void linear_s::linear_search(int l[],int n,int element)
{
flag=1;
for(int k=0;k<n-1;k++)
{
if(l[k]==element)
{
cout<<"nSEARCH IS SUCCESSFULLn"<<"n";
cout<<"ELEMENT:t"<<element<<"tFOUND AT LOCATIONt"<<(k+1)<<"n";
flag=0;
}
}
if(flag)
cout<<"nSEARCH IS UNSUCCESSFULLn";
}
void linear_s::display(int list[],int n)
{
for(int i=0;i<n;i++)
{
cout<<" "<<list[i];
}
}
linear_s::input(int list[],int number,int key)
{
cout<<"INPUT THE NUMBER OF ELEMENTS IN THE LIST:";
cin>>number;
cout<<"NUMBER OF ELEMENTS IN THE LIST IS:"<<number<<"n";
for(int i=0;i<number;i++)
{
2. cout<<"INPUT THE ELEMENTS OF THE LIST:"<<i+1<<":";
cin>>list[i];
}
cout<<"ENTER THE ELEMENT TO BE SEARCHED:";
cin>>key;
search=key;
return number;
}
void main()
{
linear_s sort;
int number,key,list[200];
clrscr();
number=sort.input(list,number,key);
key=search;
cout<<"nENTERED LIST AS FOLLOWS:n";
sort.display(list,number);
sort.linear_search(list,number,key);
cout<<"nIN THE FOLLOWING LIST n";
sort.display(list,number);
getch();
}
3. OUTPUT: LINEAR SEARCH
INPUT THE NUMBER OF ELEMENTS IN THE LIST: 5
NUMBER OF ELEMENTS IN THE LIST IS: 5
INPUT THE ELEMENTS OF THE LIST:1
INPUT THE ELEMENTS OF THE LIST:2
INPUT THE ELEMENTS OF THE LIST:3
INPUT THE ELEMENTS OF THE LIST:4
INPUT THE ELEMENTS OF THE LIST:5
ENTER THE ELEMENT TO BE SEARCHED: 4
ENTERED LIST AS FOLLOWS:
1 2 3 4 5
SEARCH IS SUCCESSFUL
ELEMENT 4 FOUND AT LOCATION 4 IN THE FOLLOWING LIST
1 2 3 4 5
INPUT THE NUMBER OF ELEMENTS IN THE LIST: 5
NUMBER OF ELEMENTS IN THE LIST IS: 5
INPUT THE ELEMENTS OF THE LIST:1
INPUT THE ELEMENTS OF THE LIST:2
INPUT THE ELEMENTS OF THE LIST:3
INPUT THE ELEMENTS OF THE LIST:4
INPUT THE ELEMENTS OF THE LIST:5
ENTER THE ELEMENT TO BE SEARCHED: 14
ENTERED LIST AS FOLLOWS:
1 2 3 4 5
SEARCH IS UNSUCCESSFUL
4. BINARY SEARCH
#include<iostream.h>
#include<conio.h>
class binary_s
{
public: void binary_search(int,int,int*);
void sort(int*,int);
void display(int*,int);
};
void binary_s::binary_search(int element,int n,int l[])
{
int flag=1,low,mid,high;
low=0,high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(element<l[mid])//ELEMENTS IN UPPER HALF
high=mid-1;
else if(element>l[mid])//ELEMENT IN LOWER HALF
low=mid+1;
else if(element==l[mid])
{
cout<<"nSEARCH IS SUCCESSFULLn";
cout<<"ELEMENT:t"<<element<<"tFOUND AT LOCATION:t"<<(mid+1);
flag=0;
break;
}
}
if(flag)
{
cout<<"n SEARCH IS UNSUCCESSFULL";
}
}
void binary_s::sort(int list[],int number)
{
for(int j=0;j<number;j++)
for(int k=0;k<number-1;k++)
{
if(list[k]>list[k+1])
{
int temp=list[k];
list[k]=list[k+1];
5. list[k+1]=temp;
}
}
}
void binary_s::display(int list[],int number)
{
cout<<"nENTERED LIST AS FOLLOWS IN ASCENDING ORDERn";
for(int i=0;i<number;i++)
cout<<" "<<list[i];
}
void main()
{
binary_s search;
int number,key,list[200];
clrscr();
cout<<"ENTER NUMBER OF ELEMENTS IN THE LIST:";
cin>>number;
cout<<"NUMBER OF ELEMENTS IN THE LIST IS :"<<number<<"n";
for(int i=0;i<number;i++)
{
cout<<"INPUT THE ELEMENTS OF THE LIST:"<<(i+1)<<":";
cin>>list[i];
}
cout<<"ENTERED LIST IS AS FOLLOWS";
for(i=0;i<number;i++)
cout<<" "<<list[i];
cout<<"nENTER THE ELEMENT TO BE SEARCHED:";
cin>>key;
search.sort(list,number);
search.display(list,number);
search.binary_search(key,number,list);
getch();
}
6. OUTPUT: BINARY SEARCH
INPUT THE NUMBER OF ELEMENTS IN THE LIST: 5
NUMBER OF ELEMENTS IN THE LIST IS: 5
INPUT THE ELEMENTS OF THE LIST:11
INPUT THE ELEMENTS OF THE LIST:2
INPUT THE ELEMENTS OF THE LIST:13
INPUT THE ELEMENTS OF THE LIST:4
INPUT THE ELEMENTS OF THE LIST:15
ENTER THE ELEMENT TO BE SEARCHED: 4
ENTERED LIST AS FOLLOWS IN ASCENDING ORDER:
2 4 11 13 15
SEARCH IS SUCCESSFUL
INPUT THE NUMBER OF ELEMENTS IN THE LIST: 5
NUMBER OF ELEMENTS IN THE LIST IS: 5
INPUT THE ELEMENTS OF THE LIST:1
INPUT THE ELEMENTS OF THE LIST:2
INPUT THE ELEMENTS OF THE LIST:3
INPUT THE ELEMENTS OF THE LIST:4
INPUT THE ELEMENTS OF THE LIST:5
ENTER THE ELEMENT TO BE SEARCHED: 14
ENTERED LIST AS FOLLOWS IN ASCENDING ORDER:
2 4 11 13 15
SEARCH IS UNSUCCESSFUL
7. Insertion sort
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class insertion
{
private: int i,k;
public: void insert(int*,int);
void display(int*,int);
};
void insertion::insert(int l[],int n)
{
l[0]=-0;
for(int i=1;i<=n;i++)
{
int pointer=i-1;
int temp=l[i];
while(temp<l[pointer])
{
l[pointer+1]=l[pointer];
pointer--;
}
l[pointer+1]=temp;
cout<<"nSTEP="<<i;
for(k=1;k<=n;k++)
cout<<" "<<l[k];
}
}
void insertion::display(int l[],int n)
{
cout<<"n SORTED LIST IS AS FOLLOWS:";
for(i=1;i<=n;i++)
cout<<" "<<l[i];
}
void main()
{
insertion inssort;
int number,list[100];
clrscr();
cout<<"ENTER NUMBER OF ELEMENTS:";
cin>>number;
for(int i=1;i<=number;i++)
cin>>list[i];
10. cout<<"n";
}
cout<<"BEFORE SORTING ENTERED LIST IS AS FOLLOWS:n";
sort.display(list,n);
sort.bsort(n,list);
cout<<"nAFTER SORTING THE LIST IS AS FOLLOWS:n";
sort.display(list,n);
getch();
}
OUTPUT: BUBBLE SORT
ENTER NUMBER OF ELEMENTS: 5
ENTER THE ELEMENTS:
11
2
14
23
3
BEFORE SORTING ENTERED LIST IS AS FOLLOWS:
11 2 14 23 3
AFTER SORTING THE LIST IS AS FOLLOWS:
2 3 11 14 23
11. SELECTION SORT
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class selection
{
private:int temp,current,j;
public: void select(int*,int);
void display(int*,int);
};
void selection::select(int a[],int size)
{
for(current=0;current<size-1;current++)
for(j=current+1;j<size;j++)
if(a[current]>a[j])
{
temp=a[current];
a[current]=a[j];
a[j]=temp;
}
}
void selection::display(int list[],int n)
{
cout<<"SORTED LIST IS AS FOLLOWS:n";
for(int i=0;i<n;i++)
{
cout<<" "<<list[i];
}
}
void main()
{
selection sort;
int list[30],number;
clrscr();
cout<<"ENTER NUMBER OF ELEMENTS:";
cin>>number;
for(int i=0;i<number;i++)
{
cout<<"ENTER THE VALUES FOR:"<<i+1<<"t";
cin>>list[i];
}
sort.select(list,number);
sort.display(list,number);
getch();
}
12. OUTPUT: SELECTION SORT
ENTER NUMBER OF ELEMENTS: 5
ENTER THE VALUES FOR: 1 11
ENTER THE VALUES FOR: 2 2
ENTER THE VALUES FOR: 3 14
ENTER THE VALUES FOR: 4 23
ENTER THE VALUES FOR: 5 3
SORTED LIST IS AS FOLLOWS:
2 3 11 14 23
18. tmp=(struct node*)new(struct node);
tmp->info=data;
tmp->link=start;
start=tmp;
}
void Linked_List::AddAfter(int data,int pos)
{
struct node *tmp,*q;
int i;
q=start;
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)
{
cout<<"nnThere are less than "<<pos<<" elements";
getch();
return;
}
}
tmp=(struct node*)new(struct node);
tmp->link=q->link;
tmp->info=data;
q->link=tmp;
}
void Linked_List::Delete()
{
struct node *tmp,*q;
int data;
if(start==NULL)
{
cout<<"nnList is empty";
getch();
return;
}
cout<<"nnEnter the element for deletion : ";
cin>>data;
if(start->info == data)
{
tmp=start;
start=start->link;
delete(tmp);
return;
}
q=start;
19. while(q->link->link != NULL)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
delete(tmp);
return;
}
q=q->link;
}/*End of while */
if(q->link->info==data)
{
tmp=q->link;
delete(tmp);
q->link=NULL;
return;
}
cout<<"nnElement "<<data<<" not found";
getch();
}
void Linked_List::traverse()
{
struct node *q;
if(start == NULL)
{
cout<<"nnList is empty";
return;
}
q=start;
cout<<"nnList is : ";
while(q!=NULL)
{
cout<<q->info;
q=q->link;
}
cout<<"n";
getch();
}/*End of display() */
void main()
{
int choice,n,m,position,i;
Linked_List po;
while(1)
{
20. clrscr();
cout<<"1.Create Listn";
cout<<"2.Add at beginingn";
cout<<"3.Add after n";
cout<<"4.Deleten";
cout<<"5.Traversen";
cout<<"6.exitn";
cout<<"nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"nnHow many nodes you want:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"nEnter the element:";
cin>>m;
po.Create_List(m);
}
break;
case 2:
cout<<"nnEnter the element:";
cin>>m;
po.AddAtBeg(m);
break;
case 3:
cout<<"nnEnter the element:";
cin>>m;
cout<<"nEnter the position after which this element is inserted:";
cin>>position;
po.AddAfter(m,position);
break;
case 4:
po.Delete();
break;
case 5:
po.traverse();
break;
case 6:
exit(0);
default:
cout<<"nnWrong choice";
}/*End of switch */
}/*End of while */
21. }/*End of main()*/
OUTPUT : SINGLE LINKED LIST
1.Create List
2.Add at begining
3.Add after
4.Delete
5.Traverse
6.exit
Enter your choice: 1
How many nodes you want:5
Enter the element: 11
Enter the element: 22
Enter the element: 33
Enter the element: 44
Enter the element: 55
1.Create List
2.Add at begining
3.Add after
4.Delete
5.Traverse
6.exit
Enter your choice: 2
Enter the element: 66
1.Create List
2.Add at begining
3.Add after
4.Delete
5.Traverse
6.exit
Enter your choice: 3
Enter the element:77
Enter the position after which this element is inserted: 3
1.Create List
2.Add at begining
3.Add after
4.Delete
5.Traverse
6.exit
22. Enter your choice: 4
Enter the element for deletion : 44
1.Create List
2.Add at begining
3.Add after
4.Delete
5.Traverse
6.exit
Enter your choice: 5
List is :
66 11 22 77 33 55
23. SORTED LIST
#include<iostream.h>
#include<conio.h>
#include<process.h>
class Linked_List
{
//Structure declaration for the node
struct node
{
int info;
struct node *link;
};
//private structure variable declared
struct node *start;
public:
Linked_List()//Constructor defined
{
start = NULL;
}
//public fucntion declared
void Create(int);
void Sort();
void Disp();
};
//This function will create a new linked list of elements
void Linked_List::Create(int data)
{
struct node *q,*tmp;
//New node is created with new operator
tmp= (struct node *)new(struct node);
tmp->info=data;
tmp->link=NULL;
if (start==NULL) /*If list is empty */
start=tmp;
else
{ /*Element inserted at the end */
q=start;
while(q->link!=NULL)
q=q->link;
q-> link=tmp;
}
}/*End of create_list()*/
25. void main()
{
int n,m;
Linked_List po;
cout<<"nnEnter size of the list:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"nEnter data element:";
cin>>m;
po.Create(m);
}
cout<<"nnlist before sorting";
po.Disp();
cout<<"n nlist after sorting:";
po.Sort();
po.disp();
}/*End of main()*/
OUTPUT : SORTED LIST
Enter size of the list: 5
Enter data element: 23
Enter data element: 6
Enter data element: 78
Enter data element: 3
Enter data element: 1
list before sorting
23 6 78 3 1
list after sorting
1 3 6 23 78
26. STACKS USING LINKED LIST
#include<conio.h>
#include<iostream.h>
#include<process.h>
//Class is created for the linked list
class Stack_ Linked
{
//Structure is created for the node
struct node
{
int info;
struct node *link;//A link to the next node
};
//A variable top is been declared for the structure
struct node *top;
//NODE is defined as the data type of the structure node
typedef struct node *NODE;
public:
//Constructer is defined for the class
Stack_ Linked()
{
//top pointer is initialized
top=NULL;
}
//function declarations
void push();
void pop();
void display();
};
//This function is to perform the push operation
void Stack_ Linked::push()
{
NODE NewNode;
int pushed_ item;
//A new node is created dynamically
NewNode=(NODE)new(struct node);
cout<<"nInput the new value to be pushed on the stack:";
cin>>pushed_item;
NewNode->info=pushed_ item;//Data is pushed to the stack
NewNode->link=top;//Link pointer is set to the next node
top=NewNode;//Top pointer is set
}/*End of push()*/
//Following function will implement the pop operation
27. void Stack_ Linked::pop()
{
NODE tmp;
if(top == NULL)//checking whether the stack is empty or not
cout<<"nStack is emptyn";
else
{ tmp=top;//popping the element
cout<<"nPopped item is:"<<tmp->info;
top=top->link;//resetting the top pointer
tmp->link=NULL;
delete(tmp);//freeing the popped node
}
}/*End of pop()*/
//This is to display all the element in the stack
void Stack_ Linked::display()
{
if(top==NULL)//Checking whether the stack is empty or not
cout<<"nStack is emptyn";
else
{
NODE ptr=top;
cout<<"nStack elements:n";
while(ptr != NULL)
{
cout<<"n"<<ptr->info;
ptr = ptr->link;
}/*End of while */
}/*End of else*/
}/*End of display()*/
void main()
{
char opt;
int choice;
Stack_ Linked So;
do
{
clrscr();
//The menu options are listed below
cout<<"n1.PUSHn";
cout<<"2.POPn";
cout<<"3.DISPLAYn";
cout<<"4.EXITn";
cout<<"nEnter your choice : ";
cin>>choice;
switch(choice)
28. {
case 1:
So.push();//push function is called
break;
case 2:
So.pop();//pop function is called
break;
case 3:
So.display();//display function is called
break;
case 4:
exit(1);
default:
cout<<"nWrong choicen";
}/*End of switch */
cout<<"nnDo you want to continue (Y/y) = ";
cin>>opt;
}while((opt == 'Y') || (opt == 'y'));
}/*End of main() */
29. OUTPUT: STACK USING LINKED LIST
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice : 1
Input the new value to be pushed on the stack:11
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice : 1
Input the new value to be pushed on the stack:12
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice : 1
Input the new value to be pushed on the stack:13
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice : 2
Popped item is: 13
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice : 3
12 11
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice : 4