Ds Full File
Ds Full File
ALGORITHM:
TRAVERSAL:
This algorithm traverses a linear array A with lower bound 0 and upper bound 9. It traverses A
searching the desired number NUMB.
INSERTION:
In this A is a linear array with 10 elements and NUMB is any number and POS is the potion at
which the NUMB is to be inserted.
[Initialize counter.]
[Decrease counter.]
Step 7: Exit.
DELETION:
In this A is a linear array with 10 elements and NUMB is any number and this deletes the NUMB
number from the array A.
End of loop
Step 3: Set 9 to 8.
Step 4: Exit.
FLOW CHART:
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
int a[20],numb,pos,x,flag=0;
cout<<"Enter 10 numbers: "<<endl;
for(int i=0;i<10;i++)
{cin>>a[i];}
cout<<"1-TRAVERSE"<<endl;
cout<<"2-INSERION"<<endl;
cout<<"3-DELETION"<<endl;
cin>>x;
if(x==1)
{cout<<"Enter number to be searched: "<<endl;
cin>>numb;
for(i=0;i<=10;i++)
{if(a[i]==numb)
{pos=i;
cout<<"The position of th enumber is: "<<pos+1;
flag++:
}
}
if(flag==0)
{cout<<"Number not in the series.";}
}
else if (x==2)
SCREEN SHOTS:
ALGORITHM:
PUSH OPERATION:
POP OPERATION:
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int TOS=0,i,a[5],value,ch;
cout<<endl<<"Enter the operation you ant to perform";
cout<<endl<<"1:Push"<<endl<<"2:Pop"<<endl<<"3:Exit"<<endl;
cin>>ch;
do
{if(ch==1)
{if(TOS>4)
{cout<<"Stack overflow";}
else
{
cout<<endl<<"Enter the element you want to insert: ";
cin>>value;
TOS=TOS+1;
a[TOS]=value;
}
cout<<endl<<"Enter "<<endl<<"1:Push"<<endl<<"2:Pop"<<endl<<"3:Exit"<<endl;
cin>>ch;
}
if(ch==2)
{cout<<endl<<"The top most element is: "<<endl;
if(TOS==-1)
{cout<<"Stack Underflow"<<endl;}
else
{value= a[TOS];
TOS=TOS-1;
cout<<value<<endl;
}
cout<<endl<<"Enter "<<endl<<"1:Push"<<endl<<"2:Pop"<<endl<<"3:Exit"<<endl;
cin>>ch;
}
if(ch==3)
{exit(0);}
}while(ch==1 || ch==2);
}
SCREEN SHOTS:
ALGORITHM:
LINEAR SEARCH:
BINARY SEARCH:
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
class Search
{
public:
int a[10],x,y,flag,i,j,temp;
void getdata();
void linear(int x);
void sort(int a[]);
void binary(int x);
};
void Search::getdata()
{cout<<"Enter 10 numbers: "<<endl;
for(i=0;i<10;i++)
{cin>>a[i];}
cout<<endl<<"Enter the number to be searched: "<<endl;
cin>>x;
cout<<endl<<"Select any one:"<<endl<<"1-Linear Search"<<endl<<"2-Binary
Search"<<endl;
cin>>y;
if(y==1)
{linear(x);}
else if(y==2)
{sort(a);
binary(x);}
else
{cout<<"Invalid choice.";}
}
void Search::linear(int x)
{flag=0;
for(int i=0;i<10;i++)
{if(a[i]==x)
{cout<<"The element exists at position: "<<i+1;
flag++;
break;
}
}
if(flag==0)
{cout<<"The element doesnt exist.";}
}
{mid=(beg+last)/2;
if(x==a[mid])
{flag++;
cout<<"The element exists at position: "<<mid+1;
break;}
else if (x>a[mid])
{beg=mid;}
else
{last=mid-1;}
}
if(flag==0)
{cout<<"The element doesnt exist.";}
}
void main()
{
clrscr();
Search s;
s.getdata();
getch();
}
SCREEN SHOTS:
ALGORITHM:
BUBBLE SORT:
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[5],temp,i,j;
cout<<"Enter 5 numbers: "<<endl;
for(i=0;i<5;i++)
{cin>>a[i];}
for(i=0;i<5;i++)
{for(j=0;j<5;j++)
{if(a[i]<a[j])
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<endl<<"The sorted array is: "<<endl;
for(i=0;i<5;i++)
{cout<<a[i]<<endl;}
getch();
}
SCREEN SHOTS:
ALGORITHM:
Linear Queue:
Circular Queue:
Linear Queue:
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 50
int queue[50],rear=-1,front=-1,item;
void insert()
{if(rear==MAX-1)
{cout<<"Queue is full.";}
else
{cout<<endl<<"Enter item: ";
cin>>item;
if(rear==-1 && front==-1)
{rear=0;
front=0;
}
else
{rear++;}
queue[rear]=item;
cout<<"Item inserted: "<<item<<endl<<endl;
}
}
void del()
{if(front==-1)
{cout<<endl<<"Queue is empty.";}
else
{item=queue[front];
if(front==rear)
{front=-1;
rear=-1;
}
else
{front++;}
cout<<"Item deleted: "<<item<<endl<<endl;
}
}
void display()
{int i;
if(front==-1)
{cout<<endl<<"Queue is empty.";}
else
{cout<<endl;
for(i=front;i<=rear;i++)
{cout<<" "<<queue[i];}
}
cout<<endl;
}
void main()
{clrscr();
int ch;
do
{cout<<endl<<"1=Insert"<<endl<<"2=Delete"<<endl<<"3=Display"<<endl<<"4=Exit"<<endl<<"
Choice: ";
cin>>ch;
switch(ch)
{case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:exit(0);
default:cout<<endl<<"Invalid entry."<<endl;
}
}while(1);
}
OUTPUT:
Linear Queue:
Circular Queue:
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define SIZE 5
int queue[SIZE],rear=-1,front=-1,item;
void insert()
{if((front==0 && rear==SIZE-1) || (front==rear+1))
{cout<<endl<<"Queue is full.";}
else
{cout<<endl<<"Enter item: ";
cin>>item;
if(rear==-1)
{rear=0;
front=0;
}
else if(rear==SIZE-1)
{rear=0;}
else
{rear++;}
queue[rear]=item;
cout<<"Item Inserted: "<<item<<endl<<endl;
}
}
void del()
{if(front==-1)
{cout<<endl<<"Queue is empty.";}
else
{item=queue[front];
if(front==rear)
{front=-1;
rear=-1;
}
else if(front==SIZE-1)
{front=0;}
else
{front++;}
cout<<endl<<"Item Deleted: "<<item<<endl<<endl;
}
}
void display()
{int i;
if((front==-1) || (front==rear+1))
{cout<<"Queue is empty.";}
else
{cout<<endl;
for(i=front;i<=rear;i++)
{cout<<" "<<queue[i];}
}
cout<<endl;
}
void main()
{clrscr();
int ch;
do
{cout<<endl<<"1=Insert"<<endl<<"2=Delete"<<endl<<"3=Display"<<endl<<"4=Exit"<<endl<<"
Choice: ";
cin>>ch;
switch(ch)
{case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:exit(0);
default:cout<<endl<<"Invalid entry."<<endl;
}
}while(1);
}
OUTPUT:
Circular Queue:
Node_number=Node_number+1
Step 6: Exit.
DELETION OF A NODE:
A) FROM THE BEGINNING OF A LINKED LIST
Step 1: {Initialization}
Node=start.Next{Points to the first node in the list}
Previous=assign address of start.
Step 2: {Perform deletion operation }
If node=NULL
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int info;
node *next, *prev;
};
node *temp,*prev,*start=NULL,*ptr;
void main()
{
clrscr();
int ch,item,i,loc;
char wish;
do
{cout<<"\n MENU";
cout<<"\n 1.Insertion Of Node At Beginning";
cout<<"\n 2.Insertion Of Node At Specific Position";
cout<<"\n 3.Deletion Of Node From Beginning";
cout<<"\n 4.Traversal";
cout<<"\n 5.Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{case 1: ptr=new node;
if(ptr==NULL)
{cout<<"\n OVERFLOW";}
else
{cout<<"\n Enter the element to be inserted : ";
cin>>item;
ptr->info=item;
if(start==NULL)
{ptr->prev=NULL;
ptr->next=NULL;
start=ptr;
}
else
{ptr->prev=NULL;
ptr->next=start;
start=ptr;
}
}
break;
{i=1;
cout<<"\n Enter the location where the element is to be inserted in
the list: ";
cin>>loc;
temp=start;
while(i<loc-1)
{temp=temp->next;
i++;
}
ptr->next=temp->next;
ptr->prev=temp;
temp->next=ptr;
}
}
break;
case 3: if(start==NULL)
cout<<"\n UNDERFLOW";
else
{ptr=start;
if(start->next==NULL)
{start=NULL;
cout<<"\n "<<ptr->info<<" IS DELETED ";
delete ptr;
}
else
{start=start->next;
start->prev=NULL;
cout<<"\n "<<ptr->info<<" IS DELETED FROM THE
BEGINNING";
delete ptr;
}
}
break;
case 4: if(start==NULL)
{cout<<"\nTHE LIST CONTAINS NO ELEMENT";}
else
{cout<<"\nTHE ELEMENTS OF THE LIST : ";
ptr=start;
while(ptr!=NULL)
{cout<<" "<<ptr->info;
ptr=ptr->next;
}
}
break;
case 5: exit(0);
break;
getch();
}
OUTPUT SCREEN:
INSERTING A NODE
Suppose START is the first position in linked list. Let DATA be the element to be
inserted in the new node. POS is the position where the NewNode is to be inserted. TEMP
is a temporary pointer to hold the node address.
Step 1: Input the POS
Step 2: Initialize TEMP = START; i = 0
Step 3: Repeat the step 4 if (i less than POS) and (TEMP is not equal to NULL)
Step 4: TEMP = TEMP RPoint; i = i +1
Step 5: If (TEMP not equal to NULL) and (i equal to POS)
(a) Create a New Node
(b) NewNodeD
ATA = DATA
(c) NewNodeRPoint = TEMP RPoint
(d) NewNodeLPoint = TEMP
(e) (TEMP RPoint) LPoint = NewNode
(f ) TEMP RPoint = New Node
Step 6: Else
(a) Display Position NOT found
Step 7: Exit
DELETING A NODE
Suppose START is the address of the first node in the linked list. Let POS is the
position of the node to be deleted. TEMP is the temporary pointer to hold the address of the
node. After deletion, DATA will contain the information on the deleted node.
Step 1: Input the POS
Step 2: Initialize TEMP = START; i = 0
Step 3: Repeat the step 4 if (i less than POS) and (TEMP is not equal to NULL)
Step 4: TEMP = TEMP RPoint; i = i +1
PROGRAM CODE:
#include<iostream.h>
#include<process.h>
#include<conio.h>
struct node
{int info;
struct node *prev;
struct node *next;
}*start;
void create_list(int);
void add_at_beg(int);
void add_after(int,int);
void del(int);
void display();
void main()
{clrscr();
int ch,n,m,pos,i;
start=NULL;
while(1)
{cout<<endl<<" \n 1. Create list ";
cout<<" \n 2. Add at begining ";
cout<<" \n 3. Add after ";
cout<<" \n 4. Delete ";
cout<<" \n 5. Display ";
cout<<" \n 6. Quit ";
cout<<"\n\n Enter yout choice: ";
cin>>ch;
switch(ch)
{case 1: cout<<" \n How many nodes you want: ";
cin>>n;
for(i=0;i<n;i++)
{cout<<"\n Enter "<< i+1<<" element: ";
cin>>m;
create_list(m);
}
break;
case 2: cout<<"\n Enter the element: ";
cin>>m;
add_at_beg(m);
break;
case 3: cout<<"\n Enter the element: ";
cin>>m;
cout<<"\n Enter the position after which this element is
inserted: ";
cin>>pos;
add_after(m,pos);
break;
case 4: cout<<"\n Enter the element for deletion: ";
cin>>m;
del(m);
break;
case 5: display();
break;
case 6: exit(0);
break;
default:cout<<"\n\n WRONG CHOICE.....!!";
}
}
q->next = temp->next;
temp->next->prev=q;
delete temp;
flag=1;
}
q = q->next;
}
if(q->next->info == num)
{temp=q->next;
delete temp;
q->next = NULL;
flag=1;
}
if(flag==1)
{cout<<"\n Element "<<num<<" deleted.";}
else
{cout<<"\n Element "<<num<<" not found.";}
}
void display()
{struct node *q;
if(start==NULL)
{cout<<"List is Empty....";}
q=start;
cout<<"\n List is: \n";
while(q!=NULL)
{cout<<q->info<<" ";
q=q->next;
}
}
OUTPUT SCREEN:
FIRST = TEMPHEAD
Step 4: [Deletion of the first node]
Repeat while LINK (TEMPHEAD)! =NULL
Step 5: [Delete the node]
LINK (TEMPHEAD) = LINK (FIRST)
LINK (FIRST) = FIRST
Return (FIRST)
Step 6: [Finding desire node]
Repeat while INFO (LINK (TEMPHEAD)) = KEY
Step 7: [Deletes the node]
TEMP = LINK (TEMPHEAD)
LINK (TEMPHEAD) = LINK (LINK (TEMPHEAD))
Free (TEMP)
Step 8: [Finished]
Return (FIRST)
PROGRAM CODE:
#include<iostream.h>
#include<process.h>
#include<conio.h>
tmp->info = num;
if (last == NULL)
{last = tmp;
tmp->link = last;
}
else
{tmp->link = last->link; /*added at the end of list*/
last->link = tmp;
last = tmp;
}
}/*End of create_list()*/
return;
}
}/*End of for*/
//creating the new node
tmp = (NODE)new(struct node);
tmp->link = q->link;
tmp->info = num;
q->link = tmp;
if(q==last) /*Element inserted at the end*/
{last=tmp;}
}/*End of addafter()*/
{tmp = q;
last->link = q->link;
//deleting the node
delete(tmp);
return;
}
while(q->link != last)
{if(q->link->info == num) /*Element deleted in between*/
{tmp = q->link;
q->link = tmp->link;
delete(tmp);
cout<<"\n"<<num<<" deleted\n";
return;
}
q = q->link;
}/*End of while*/
if(q->link->info == num) /*Last element deleted q->link=last*/
{tmp = q->link;
q->link = last->link;
delete(tmp);
last = q;
return;
}
cout<<"\nElement "<<num<<" not found\n";
}/*End of del()*/
return;
}
q = last->link;
cout<<"\nList is:\n";
while(q != last)
{cout<<q->info<<" ";
q = q->link;
}
cout<<last->info<<endl;
}/*End of display()*/
void main()
{clrscr();
int choice,n,m,po,i;
Circular_Linked co;//Object is created for the class
while(1)
{//Menu options
cout<<"\n1.Create List\n";
cout<<"2.Add at begining\n";
cout<<"3.Add after \n";
cout<<"4.Delete\n";
cout<<"5.Display\n";
cout<<"6.Quit\n";
cout<<"\nEnter your choice:";
cin>>choice;
switch(choice)
{case 1:cout<<"\nHow many nodes you want:";
cin>>n;
for(i=0; i < n;i++)
{cout<<"\nEnter the element:";
cin>>m;
co.create_list(m);
}
break;
case 2:cout<<"\nEnter the element:";
cin>>m;
co.addatbeg(m);
break;
case 3:cout<<"\nEnter the element:";
cin>>m;
cout<<"\nEnter the position after which this element is inserted:";
cin>>po;
co.addafter(m,po);
break;
case 4:co.del();
break;
case 5:co.display();
break;
case 6:exit(0);
default:cout<<"\nWrong choice\n";
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
OUTPUT SCREEN:
PUSH OPERATION
Suppose TOP is a pointer, which is pointing towards the topmost element of the
stack. TOP is NULL when the stack is empty. DATA is the data item to be pushed.
POP OPERATION
Suppose TOP is a pointer, which is pointing towards the topmost element of the
stack. TOP is NULL when the stack is empty. TEMP is pointer variable to hold any nodes
address. DATA is the information on the node which is just deleted.
Step 1: If (TOP is equal to NULL)
(a) Display The stack is empty
Step 2: Else
(a) TEMP = TOP
(b) Display The popped element TOP DATA
(c) TOP = TOP Next
(d) TEMP Next = NULL
(e) Free the TEMP node
Step 3: Exit
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
#include<process.h>
void push(int);
int pop();
class S_LL
{private:
struct node
{int data;
node *link;
};
node* top;
public:
S_LL()
{top=NULL;}
void push(int n)
{node *tmp;
tmp=new node;
if(tmp==NULL)
{cout<<endl<<"Stack Full";}
tmp->data=n;
tmp->link=top;
top=tmp;
display(top);
cout<<endl;
return;
}
int pop()
{if(top==NULL)
{cout<<"Stack Empty";
return NULL;
}
node *tmp;
int n;
tmp=top;
n=tmp->data;
top=top->link;
delete tmp;
display(top);
cout<<endl;
return n;
}
~S_LL()
{if(top==NULL)
{return;}
node *tmp;
while(top!=NULL)
{tmp=top;
top=top->link;
delete tmp;
}
}
};
void main()
{clrscr();
S_LL s;
int ch,num;
do
{cout<<endl<<"1=Push"<<endl<<"2=Pop"<<endl<<"3=Exit"<<endl<<"Choice: ";
cin>>ch;
switch(ch)
{case 1:cout<<"Enter a number: ";
cin>>num;
s.push(num);
break;
case 2:s.pop();
break;
case 3:exit(0);
default:cout<<endl<<"Invalid entry."<<endl;
}
}while(1);
}
OUTPUT SCREEN:
PUSH OPERATION
REAR is a pointer in queue where the new elements are added. FRONT is a pointer,
which is pointing to the queue where the elements are popped. DATA is an element to be
pushed.
POP OPERATION
REAR is a pointer in queue where the new elements are added. FRONT is a pointer,
which is pointing to the queue where the elements are popped. DATA is an element popped
from the queue.
Step 1: If (FRONT is equal to NULL)
(a) Display The Queue is empty
Step 2: Else
(a) Display The popped element is FRONT DATA
(b) If(FRONT is not equal to REAR)
(i) FRONT = FRONT Next
(c) Else
(d) FRONT = NULL;
Step 3: Exit
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
#include<process.h>
void add(int);
int del();
class Q_LL
{private:
struct node
{int data;
node *link;
};
node *front,*rear;
public:
Q_LL()
{front=NULL;
rear=NULL;
}
cout<<endl;
return;
}
rear->link=tmp;
rear=rear->link;
display(front);
cout<<endl;
}
int del()
{if(front==NULL)
{cout<<endl<<"Queue Empty";
return NULL;
}
node *tmp;
int n;
n=front->data;
tmp=front;
front=front->link;
delete tmp;
display(front);
cout<<endl;
return n;
}
void display(node *x)
{while(x!=NULL)
{cout<<x->data<<" ";
x=x->link;
}
}
~Q_LL()
{if(front==NULL)
{return;}
node *tmp;
while(front!=NULL)
{tmp=front;
front=front->link;
delete tmp;
}
}
};
void main()
{clrscr();
Q_LL q;
int ch,num;
do
{cout<<endl<<"1=Add"<<endl<<"2=Delete"<<endl<<"3=Exit"<<endl<<"Choice: ";
cin>>ch;
switch(ch)
{case 1:cout<<"Enter a number: ";
cin>>num;
q.add(num);
break;
case 2:q.del();
break;
case 3:exit(0);
default:cout<<endl<<"Invalid entry."<<endl;
}
}while(1);
}
OUTPUT SCREEN:
Insertion:
NEWNODE is a pointer variable to hold the address of the newly created node. DATA
is the information to be pushed.
Step 1: Input the DATA to be pushed and ROOT node of the tree.
Step 2: NEWNODE = Create a New Node.
Step 3: If (ROOT == NULL)
(a) ROOT=NEW NODE
Step 4: Else If (DATA < ROOT Info)
(a) ROOT = ROOT Lchild
(b) GoTo Step 4
Step 5: Else If (DATA > ROOT Info)
(a) ROOT = ROOT Rchild
(b) GoTo Step 4
Step 6: If (DATA < ROOT Info)
(a) ROOT LChild = NEWNODE
Step 7: Else If (DATA > ROOT Info)
(a) ROOT RChild = NEWNODE
Step 8: Else
(a) Display (DUPLICATE NODE)
(b) EXIT
Step 9: NEW NODE Info = DATA
Step 10: NEW NODE LChild = NULL
Step 11: NEW NODE RChild = NULL
Step 12: EXIT
Searching:
Step 1: Input the DATA to be searched and assign the address of the root node to ROOT.
Step 2: If (DATA == ROOT Info)
(a) Display The DATA exist in the tree
PROGRAM CODE:
#include<iostream.h>
#include<process.h>
#include<conio.h>
//Class is created for the implementation of BST
class BST
{
struct node
{int info;
struct node *lchild;
struct node *rchild;
};
public:
struct node *root;
BST()
{root=NULL;}
{child=loc->lchild;}
else
if(location!=NULL)
{cout<<"\nItem already present";
getch();
return;
}
//creating new node to insert
tmp=(NODE)new(struct node);
tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(parent==NULL)
{root=tmp;}
else
{if(item<parent->info)
{parent->lchild=tmp;}
else
{parent->rchild=tmp;}
}
}/*End of insert()*/
return;
}
if(location->lchild==NULL && location->rchild==NULL)
{case_a(parent,location);}
if(location->lchild!=NULL && location->rchild==NULL)
{case_b(parent,location);}
if(location->lchild==NULL && location->rchild!=NULL)
{case_b(parent,location);}
if(location->lchild!=NULL && location->rchild!=NULL)
{case_c(parent,location);}
delete(location);
}/*End of del()*/
void main()
{clrscr();
int choice,num;
BST bo;
while(1)
{//Menu options
cout<<"\n1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Inorder Traversal\n";
cout<<"4.Quit\n";
cout<<"\nEnter your choice : ";
cin>>choice;
switch(choice)
{case 1:cout<<"\nEnter the number to be inserted : ";
cin>>num;
bo.insert(num);
break;
case 2:cout<<"\nEnter the number to be deleted : ";
cin>>num;
bo.del(num);
break;
case 3:bo.inorder(bo.root);
getch();
break;
case 4:exit(0);
default:cout<<"\nWrong choice\n";
getch();
}/*End of switch */
}/*End of while */
}/*End of main()*/
OUTPUT SCREEN:
PREORDER TRAVERSAL
INORDER TRAVERSAL
POSTORDER TRAVERSAL
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
#define YES 1
#define NO 0
class BST
{private:
struct leaf
{int data;
leaf *l;
leaf *r;
};
struct leaf *p;
public:
BST()
{p=NULL;}
return;
}
if(q->data>n)
{parent=q;
q=q->l;
}
else
{parent=q;
q=q->r;
}
}
}
void add(int n)
{int found;
leaf *t,*parent;
findparent(n,found,parent);
if(found==YES)
{cout<<endl<<"Such a node exists";}
else
{t=new leaf;
t->data=n;
t->l=NULL;
t->r=NULL;
if(parent==NULL)
{p=t;}
else
{parent->data>n?parent->l=t:parent->r=t;}
}
}
void traverse()
{int c;
cout<<endl<<"1=InOrder"<<endl<<"2=PreOrder"<<endl<<"3=PostOrder"<<endl<<"Choice: ";
cin>>c;
switch(c)
{case 1: in(p);
break;
case 2: pre(p);
break;
case 3: post(p);
break;
}
}
"<<q->data<<endl;
in(q->r);
}
}
"<<q->data<<endl;
"<<q->data<<endl;
}
}
};
void main()
{clrscr();
BST t;
int data[7];
cout<<"Enter 7 elements for BST: "<<endl;
for(int i=0;i<7;i++)
{cin>>data[i];
t.add(data[i]);
}
t.traverse();
t.traverse();
t.traverse();
getch();
}
OUTPUT SCREEN:
ALGORITHM:
Let A be a linear array of n numbers A [1], A [2], A [3], ...... ,A [n]......Swap be a temporary
variable to interchange the two values. Pos is the control variable to hold the position of
each pass.
Step 1: Input an array A of n numbers
Step 2: Initialize i = 1 and repeat through steps 4 by incrementing i by one.
(a) If (i < = n 1)
(b) Swap = A [I],
(c) Pos = i 1
Step 3: Repeat the step 3 if (Swap < A[Pos] and (Pos >= 0))
(a) A [Pos+1] = A [Pos]
(b) Pos = Pos-1
Step 4: A [Pos +1] = Swap
Step 5: Exit.
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[20],t,n,temp,j;
cout<<"\nEnter the number of elements of Array: ";
cin>>n;
cout<<"\nEnter the elements of the array: "<<endl;
for(int i=0;i<n;i++)
{cin>>a[i];}
cout<<"\n";
for(i=0;i<n;i++)
{temp=a[i];
j=i-1;
while(temp<a[j] && j>=0)
{a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
cout<<"\n The array after "<<i+1<<" iteration is : ";
for(int k=0;k<n;k++)
{cout<<" "<<a[k];}
}
OUTPUT SCREEN:
ALGORITHM:
Let A be a linear array of n numbers. Swap is a temporary variable for swapping (or interchange)
the position of the numbers.
Step 1: Input n numbers of an array A
Step 2: Initialize i = 0 and repeat through step 4 if (i < n)
Step 3: Initialize j = 0 and repeat through step 4 if (j < n i 1)
Step 4: If (A[j] > A[j + 1])
(a) Swap = A[j]
(b) A[j] = A[j + 1]
(c) A[j + 1] = Swap
Step 5: Display the sorted numbers of array A
Step 6: Exit.
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[20],t,n,temp;
cout<<"\nEnter the number of elements of Array: ";
cin>>n;
cout<<"\nEnter the elements of the array: "<<endl;
for(int i=0;i<n;i++)
{cin>>a[i];}
cout<<"\n";
for(i=0;i<n;i++)
{for(int j=0;j<n-1;j++)
{if(a[j]>a[j+1])
{temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
cout<<"\n Elements after "<<i+1<<" pass : " ;
for(int k=0;k<n;k++)
{cout<<" "<<a[k];}
}
OUTPUT SCREEN:
ALGORITHM:
Let A be a linear array of n numbers A [1], A [2], A [3] A [k], A [k+1] A [n]. Swap be a
temporary variable for swapping (or interchanging) the position of the numbers. Min is the variable
to store smallest number and Loc is the location of the smallest element.
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[20],t,n,temp,min_index;
cout<<"\nEnter the number of elements of Array: ";
cin>>n;
cout<<"\nEnter the elements of the array : "<<endl;
for(int i=0;i<n;i++)
{cin>>a[i];}
cout<<"\nThe array is : ";
for(i=0;i<n;i++)
{cout<<" "<<a[i];}
cout<<"\n";
for(i=0;i<n;i++)
{min_index=i;
for(int j=i;j<n;j++)
{if(a[min_index]>a[j])
{min_index=j;}
}
temp=a[i];
a[i]=a[min_index];
a[min_index]=temp;
cout<<"\nThe array after "<<i+1<<" pass is : ";
for(int k=0;k<n;k++)
{cout<<" "<<a[k];}
}
cout<<"\n\n\nThe sorted array is: "<<endl;
cout<<"\t\t";
for(i=0;i<n;i++)
{cout<<" "<<a[i];}
getch();
}
OUTPUT SCREEN:
ALGORITHM:
function quicksort(array)
if length(array) > 1
pivot := select any element of array
left := first index of array
right := last index of array
while left right
while array[left] < pivot
left := left + 1
while array[right] > pivot
right := right - 1
if left right
swap array[left] with array[right]
left := left + 1
right := right - 1
quicksort(array from first index to right)
quicksort(array from left to last index)
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
int partition(int* ,int ,int);
void quick(int* ,int ,int);
if(right<left)
{temp=arr[right];
arr[right]=arr[left];
arr[left]=temp;
}
}
temp=arr[lower];
arr[lower]=arr[left];
arr[left]=temp;
return left;
}
void main()
{clrscr();
int n,a[20],high,low;
cout<<"\nEnter no of elements of the array: ";
cin>>n;
low=0;
high=n-1;
cout<<"\nEnter the elements of the array: "<<endl;
for(int i=0;i<n;i++)
{cin>>a[i];}
quick(a,low,high);
cout<<"\nThe sorted array is: ";
for(i=0;i<n;i++)
{cout<<" "<<a[i];}
getch();
}
OUTPUT SCREEN:
ALGORITHM:
PROGRAM CODE:
#include <iostream.h>
#include <conio.h>
void main()
{int i,n,j,gap,temp,a[30];
clrscr();
cout<<"\nWith how many elements you want to create an array: ";
cin>>n;
cout<<"\nEnter the elements: "<<endl;
for(i=0;i<n;i++)
{cin>>a[i];}
cout<<"\n\n\nThe Unsorted array is ->\n\n\t ";
for(i=0;i<n;i++)
{cout<<a[i]<<" ";}
for(gap=n/2;gap>0;gap=gap/2)
{for(i=0;i<n;i=i+gap)
{temp=a[i];
for(j=i;j>0 && a[j-gap]>temp;j=j-gap)
{a[j]=a[j-gap];}
a[j]=temp;
}
}
cout<<"\n\n\nThe Sorted array is ->\n\n\t ";
for(i=0;i<n;i++)
{cout<<a[i]<<" ";}
getch();
}
OUTPUT SCREEN:
ALGORITHM:
function merge_sort(m)
if length(m) 1
return m
var list left, right, result
var integer middle = length(m) / 2
for each x in m up to middle
add x to left
for each x in m after or equal middle
add x to right
left = merge_sort(left)
right = merge_sort(right)
result = merge(left, right)
return result
function merge(left,right)
var list result
while length(left) > 0 or length(right) > 0
if length(left) > 0 and length(right) > 0
if first(left) first(right)
append first(left) to result
left = rest(left)
else
append first(right) to result
right = rest(right)
else if length(left) > 0
append first(left) to result
left = rest(left)
else if length(right) > 0
append first(right) to result
right = rest(right)
end while
return result
PROGRAM CODE:
#include <iostream.h>
#include <conio.h>
int arrays[30];
int temp[30];
void merge(int arrays[], int temp[], int left, int mid, int right)
{int i,left_end,num_ele,temp_pos;
left_end=mid-1;
temp_pos=left;
num_ele=right-left+1;
while((left<=left_end)&&(mid<=right))
{if(arrays[left]<=arrays[mid])
{temp[temp_pos]=arrays[left];
temp_pos=temp_pos+1;
left=left+1;
}
else
{temp[temp_pos]=arrays[mid];
temp_pos=temp_pos+1;
mid=mid+1;
}
}
while(left<=left_end)
{temp[temp_pos]=arrays[left];
left=left+1;
temp_pos=temp_pos+1;
}
while(mid<=right)
{temp[temp_pos]=arrays[mid];
mid=mid+1;
temp_pos=temp_pos+1;
}
for(i=0;i<=num_ele;i++)
{arrays[right]=temp[right];
right=right-1;
}
}
void main()
{clrscr();
int i,n;
cout<<" \nEnter the number of which th array is to be made: ";
cin>>n;
cout<<"\n\nEnter the elements: "<<endl;
for(i=0;i<n;i++)
{cin>>arrays[i];}
cout<<" \n\n\nThe Unsorted array is -> \n\n\t\t";
for(i=0;i<n;i++)
// Before Merging
{cout<<arrays[i]<<" ";}
MergeSort(arrays,temp,n);
//After merging
{cout<<arrays[i]<<" ";}
getch();
}
OUTPUT SCREEN:
ALGORITHM:
A directed graph G with M nodes is aintained in memory by the adjacency matrix A. This
algorithm finds the (Boolean) path matrix P of the graph G.
Step 1: Repeat for I,J=1,2..M[Initialise P]
If A[I,J] =0 then set p[I,J]:=0
Else: Set P[I,J] :=1
Step 2: Repeat steps 3 and 4 for k=1,2 .M[update P]
Step 3:Repeat step 4 for I=1,2..M:
Step 4:Repeat for J=1,2..M
Set P[I,J]:=P[I,J] V P[I,K] ^ P[K,J]
Step 5:Exit
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int adj[max][max],path[max][max],i,j,k,n;
cout<<"Enter number of vertices : ";
cin>>n;
OUTPUT SCREEN:
DELETION OF A NODE:
C) FROM THE BEGINNING OF A LINKED LIST
Step 1: {Initialization}
Node=start.Next{Points to the first node in the list}
Previous=assign address of start.
Step 2: {Perform deletion operation }
If node=NULL
Output UNDERFLOW and exit.
PROGRAM CODE:
#include <iostream.h>
#include <conio.h>
void main()
{int b[30],no,i,j,c,p,temp;
clrscr();
cout<<"\n\nEnter number of which array is to be created:";
cin>>no;
cout<<"\nEnter elements: "<<endl;
for(i=0;i<no;i++)
{cin>>b[i];}
cout<<"\n\n\nThe Unsorted array is ->\n\n\t";
for(i=0;i<no;i++)
{cout<<b[i]<<" ";}
for(i=1;i<no;i++)
{c=i;
do
{p=(c-1)/2;
if(b[p]<b[c])
{temp=b[p];
b[p]=b[c];
b[c]=temp;
}
c=p;
}while(c!=0);
}
for(j=no-1;j>=0;j--)
{temp=b[0];
b[0]=b[j];
b[j]=temp;
p=0;
do
{c=2*p+1;
if((b[c]<b[c+1])&&c<j-1)
{c++;}
if(b[p]<b[c]&&c<j)
{temp=b[p];
b[p]=b[c];
b[c]=temp;
}
p=c;
}while(c<j);
}
cout<<"\n\n\nThe Sorted array is ->\n\n\t";
for(i=0;i<no;i++)
{cout<<b[i]<<" ";}
getch();
}
OUTPUT SCREEN: