You are on page 1of 113

To implement traversal,insertion,deletion in a linear array.

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.

Step 1: Repeat for I=0 to 9


Apply search operation for A[I]==NUMB.
End of loop.
Step 2: Exit.

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.

Step 1: Set I=10.

[Initialize counter.]

Step 2: Repeat steps 3 and 4 while I>=POS.


Step 3: Set A[I+1]=A[I]. [Move Jth element downwards.]
Step 4: Set I=I-1.

[Decrease counter.]

Step 5: Set A[POS]=NUMB. [Insert element.]


Step 6: Set 10 to 11.

[Reset number of elements.]

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.

Step 1: Repeat for I=0 to 9


Apply search operation for A[I]==NUMB.
End of loop.
Step 2: Repeat for I=POS to 8
Set A[I]=A[I+1]

[Move Ith element upwards.]

End of loop
Step 3: Set 9 to 8.
Step 4: Exit.
FLOW CHART:

[Reset number of elements.]

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)

{cout<<"Enter the number to insert: "<<endl;


cin>>numb;
cout<<"Enter the position at which you want to insert: "<<endl;
cin>>pos;
for(int i=10;i>pos-1;i--)
{a[i]=a[i-1];}
a[pos-1]=numb;
cout<<"The new Array is: "<<endl;
for(int j=0;j<11;j++)
{cout<<a[j]<<endl;}
}
else if (x==3)
{cout<<"Enter the number to delete: "<<endl;
cin>>numb;
for(int i=0;i<10;i++)
{if(a[i]==numb)
{for(int j=i;j<10;j++)
{a[j]=a[j+1];}
}
}
cout<<"the new array is: "<<endl;
for(int j=0;j<9;j++)
{cout<<a[j]<<endl;}
}
getch();
}

SCREEN SHOTS:

To implement Stacks using arrays.

ALGORITHM:

PUSH OPERATION:

Step 1: {Check for stack overflow}


If TOS>=Size-1
Output Stack Overflow
Step 2: {Increment the counter value by one}
TOS=TOS+1
Step 3: {Perform Insertion}
a[TOS]=value
Step 4: Exit.

POP OPERATION:

Step 1: {Check whether the stack is empty}


If TOS=0
Output Stack Underflow
Step 2: {Pop the TOS value}
value=a[TOS}
TOS=TOS-1
Step 3: {Print the top most value of the stack}
Return (value)
Step 4: Exit.

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:

To perform linear or binary search as per the users choice.

ALGORITHM:

LINEAR SEARCH:

Step 1: For each item in the list


Check if value matches any item in the array.
Step 2: If it matches.
Return its index.
Step 3: If it does not match.
Continue searching until the end of the array.
Step 4: Exit.

BINARY SEARCH:

While (beg<= end)


Step 1: mid=(beg+end)/2
Step 2: if x==a[mid]
Return mid
Step 3: else if x>a[mid]
beg=mid+1
Step 4: else
end=mid-1
Step 5: Return x not found in the array.
Step 6: Exit.

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.";}
}

void Search::sort(int a[])


{for(i=0;i<10;i++)
{for(j=0;j<10;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<10;i++)
{cout<<a[i]<<endl;}
}
void Search::binary(int x)
{flag=0;
int beg=0,last=10,mid;
while(beg<=last)

{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:

To perform sorting on a linear array.

ALGORITHM:

BUBBLE SORT:

Step 1: Repeat Steps 2 and 3 for I=1 to 10


Step 2: Set J=1
Step 3: Repeat while J<=N
(A): if a[i]<a[j]
Then interchange a[i] and a[j]
{End of if}
(B): Set J++
{End of Inner Loop ie Step 3}

{End of Step1 Outer Loop}


Step 4: Exit.

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:

To implement Linear and Circular Queue using arrays.

ALGORITHM:

Linear Queue:

Inserting an element to linear Queue


Step 1: If FRONT =1 and REAR=N or if FRONT=REAR+1 then :
Print : Overflow and return
Step 2: IF FRONT = NULL then
Set FRONT =1 and REAR =1
Else If REAR =N then
Set REAR = 1
Else
Set REAR =REAR +1
Step 3: Set QUEUE[REAR]=ITEM
Step 4: RETURN

Deleting an element from a linear queue


Step 1: If FRONT =NULL then :
Print : Underflowflow and return
Step 2: Set ITEM = QUEUE[FRONT]
Step 3: IF FRONT = REAR then
Set FRONT =0 and REAR =0
Else If FRONT =N then
Set FRONT = 1
Else
Set FRONT =FRONT +1
Step 4: RETURN

Circular Queue:

Inserting an element to circular Queue


Step 1: Initialize FRONT = 1; REAR = 1
Step 2: REAR = (REAR + 1) % SIZE
Step 3: If (FRONT is equal to REAR)
(a) Display Queue is full
(b) Exit
Step 4: Else
(a) Input the value to be inserted and assign to variable DATA
Step 5: If (FRONT is equal to 1)
(a) FRONT = 0
(b) REAR = 0
Step 6: Q[REAR] = DATA
Step 7: Repeat steps 2 to 5 if we want to insert more elements
Step 8: Exit

Deleting an element from a circular queue


Step 1: If (FRONT is equal to 1)
(a) Display Queue is empty
(b) Exit
Step 2: Else
(a) DATA = Q[FRONT]
Step 3: If (REAR is equal to FRONT)
(a) FRONT = 1
(b) REAR = 1
Step 4: Else
(a) FRONT = (FRONT +1) % SIZE
Step 5: Repeat the steps 1, 2 and 3 if we want to delete more elements
Step 6: Exit
PROGRAM CODE:

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:

To perform various operations on a singly linked list.


ALGORITHM:
INSERTION OF A NODE:
A) AT THE BEGINNING OF A LINKED LIST
Step 1: {Check for free space}
If new = NULL output OVERFLOW and exit.
Step 2: {Allocate free space}
New=new Link
Step 3: {Read value of information part of a new node}
Info[New]= Value
Step 4: {Link address part of the currently created node with the address of start}
Next[Next]=Start
Step 5: {Now assign address of newly created node to the start}
Start=New
Step 6: Exit.

B) AT THE END OF A LINKED LIST


Step 1: {Check for free space}
If new = NULL output OVERFLOW and exit.
Step 2: {Allocate free space}
New=new Link
Step 3: {Read value of information part of a new node}
Info[New]= Value
Step 4: {Move the pointer to the end of the list}
Repeat while Node<>NULL
Node=Next[Node]
Previous=Next[Previous]
Step 5: {Link currently created node with the last node of the list}
Next[New]=Node
Next[Previous]=New
Step 6: Exit.

C) AT A DESIRED PLACE,WHEN NODE NUMBER IS KNOWN


Step 1: {Check for free space}
If new = NULL output OVERFLOW and exit.
Step 2: {Initializaton}
Node_number=0
Node=Start.Next[Points to first node of the list]
Previous=Address of Start[Assign address of start to previous]
Step 3: {Read node number that we want to insert}
Input Insert_node
Step 4: {Perform insertion operation}
Repeat through Step 5 while NODE<>NULL
Step 5: {Check if Insert_node is equal to the Node_number}
If Node_number+1=Insert_node
Next[New]=Node
Previous->Next=New
Info[New]=Value
Return
Else {Move the pointer forward}
Node=Next[Node]
Previous=Next[Previous]

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

Output UNDERFLOW and exit.


Else {Delete first node}
Next[Previous]=Next[Node] {Move pointer to next node in the list}
Free the space associated with Node
Step 3: Exit.
B) FROM THE END OF A LINKED LIST
Step 1: {Initialization}
Node=start.Next {Points to the first node in the list}
Previous=assign address of start
Node_number=0
Step 2: {Check list is empty or not}
If node=NULL
Output UNDERFLOW and exit.
Step 3: {Scan the list to count the number of node in the list}
Repeat while node<>NULL
Node=Next[Node]
Previous=next[Previous]
Node_number=Node_number+1
Step 4: {Initialization once again}
Node=start.Next
Previous=Assign address of start
Step 5: {Scan list for 1 less to size of the list}
Repeat while Node_Number<>1
Node=next [Node]
Previous=next [Node]
Node_number=Node_number-1
Step 6: {Check if last node is arising}
If node_number=1
Next[Previous]=Next[Node]
Free(node)
Step7: Exit.

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;

case 2: 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

{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;

default:cout<<"\n\n\t WRONG CHOICE ENTERED ";


}

cout<<"\n\t Do you wish to continue......(y/n) ";


cin>>wish;
}while(wish=='y' || wish=='Y');

getch();
}

OUTPUT SCREEN:

To Implement Doubly Linked List.


ALGORITHM:

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

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

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.....!!";
}
}

void create_list(int num)


{struct node *q,*temp;
temp=new node;
temp -> info=num;
temp -> next=NULL;
if(start==NULL)
{temp->prev=NULL;
start->prev=temp;
start=temp;
}
else
{q=start;
while(q->next!=NULL)
{q=q -> next;}
q->next=temp;
temp->prev=q;
}
}

void add_at_beg(int num)


{struct node *temp;
temp=new node;
temp -> prev=NULL;
temp -> info=num;
temp -> next=start;
start-> prev=temp;
start=temp;
}

void add_after(int num, int p)


{struct node *q, *temp;
int i;
q=start;
for(i=0;i<p-1;i++)
{q = q->next;
if(q==NULL)
{cout<<"There are less than "<<p<<" elements.";}
}
temp=new node;
temp->info=num;
q->next->prev = temp;
temp->next = q->next;
temp->prev=q;
q->next = temp;
}

void del(int num)


{struct node *q, *temp;
int flag=0;
if(start->info == num)
{temp=start;
start=start -> next;
start->prev=NULL;
delete temp;
flag=1;
}
q=start;
while(q->next->next != NULL)
{if(q->next->info == num)
{temp=q->next;

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:

To Implement Circular Linked List.


ALGORITHM:

Algorithm for the Creation of the Circular list


CREATE ( * TEMPHEAD)
[This function creates the circular list and TEMPHEAD is the pointer variable which
points the first element of the list]
Step 1:[Save the address of the first element]
SAVE = TEMPHEAD
Step 2: [Repeat thru step 5]
Repeat while Choice! = n
Step 3: [Allocate the New node]
NEW NODE()
Step 4: [Initialize the fields of new node]
INFO (NEW) = X
LINK (SAVE) = NEW
SAVE = NEW
Step 5: [Want to insert another node]
Read (Choice)
Step 6: [Set the LINK field of Last inserted element]
LINK (SAVE) = TEMPHEAD
Step 7: [Finished]
Return

Algorithm for the insertion of the node in the circular list


INSERT ( * TEMPHEAD, KEY)
[This Function inserts an element after the node which have the info field equal to the
KEY variable and TEMPHEAD is the pointer which points the first element of the list
and SAVE is the temp variable for the store address of the first element]
Step 1: [Allocate the Memory for the NEW node]
NEW NODE( )

Step 2: [Set fields of the NEW node]


INFO (NEW) = X
LINK (NEW) = NULL
Step 3: [Save address of the first node]
FIRST = TEMPHEAD
Step 4: [Insertion as first node and find last element of the list]
Repeat while LINK (TEMPHEAD)! = NULL
Step 5: [Insert the node]
LINK (TEMPHEAD) = NEW
LINK (NEW) = FIRST
FIRST = NEW
Return (FIRST)
Step 6: [Insert in the list other than the first node]
Repeat while INFO (LINK (TEMPHEAD)) = KEY
Step 7: [Set the link for the NEW node]
LINK (NEW) = LINK (TEMPHEAD)
LINK (TEMPHEAD) = NEW
Step 8: [Finished]
Return (FIRST)

Algorithm for the Deletion an element from the circular list


DELETE (*TEMPHEAD, KEY)
[This Function deletes an element from the circular list]
Step 1: [Check for the empty list]
If TEMPHEAD = NULL
Then write (Empty List)
Step 2: [List contain Single node]
if LINK (TEMPHEAD) = TEMPHEAD
Return NULL
Free (TEMPHEAD)
Step 3: [Save the address of the first node]

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)

Algorithm for display the element of the circular link list


DISPLAY (*TEMPHEAD)
Step 1: [Check for the empty list]
If TEMPHEAD = NULL
Then write (Empty list)
Return
Step 2: [Print the desire node]
Repeat while LINK (TEMPHEAD)! = TEMPHEAD
Write (INFO (TEMPHEAD))
Step 3: [Finished]
Return

PROGRAM CODE:

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

//class is created for the circular linked list


class Circular_Linked
{//structure node is created
struct node
{int info;
struct node *link;
};
struct node *last;
typedef struct node *NODE;
public:
//Constructor is defined
Circular_Linked()
{last=NULL;}
void create_list(int);
void addatbeg(int);
void addafter(int,int);
void del();
void display();
};

//A circular list created in this function


void Circular_Linked::create_list(int num)
{NODE q,tmp;
//New node is created
tmp = (NODE)new(struct node);

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()*/

//This function will add new node at the beginning


void Circular_Linked::addatbeg(int num)
{NODE tmp;
tmp = (NODE)new(struct node);
tmp->info = num;
tmp->link = last->link;
last->link = tmp;
}/*End of addatbeg()*/

//Function to add new node at any position of the circular list


void Circular_Linked::addafter(int num,int pos)
{NODE tmp,q;
int i;
q = last->link;
//finding the position to insert a new node
for(i=0; i < pos-1; i++)
{q = q->link;
if (q == last->link)
{cout<<"There are less than "<<pos<<" elements\n";

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()*/

//Function to delete a node from the circular linked list


void Circular_Linked::del()
{int num;
if(last == NULL)
{cout<<"\nList underflow\n";
return;
}
cout<<"\nEnter the number for deletion:";
cin>>num;
NODE tmp,q;
if( last->link == last && last->info == num) /*Only one element*/
{tmp = last;
last = NULL;
//deleting the node
delete(tmp);
return;
}
q = last->link;
if(q->info == num)

{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()*/

//Function to display all the nodes in the circular linked list


void Circular_Linked::display()
{NODE q;
if(last == NULL)
{cout<<"\nList is empty\n";

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:

To Implement Stacks using linked list.


ALGORITHM:

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.

Step 1: Input the DATA to be pushed


Step 2: Creat a New Node
Step 3: NewNodeDATA = DATA
Step 4: NewNodeNext = TOP
Step 5: TOP = NewNode
Step 6: Exit

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;
}

void display(node *x)


{while(x!=NULL)
{cout<<x->data<<" ";
x=x->link;
}
}

~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:

To Implement Queues using linked list.


ALGORITHM:

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.

Step 1: Input the DATA element to be pushed.


Step 2: Creat a New Node
Step 3: NewNodeDATA = DATA
Step 4: NewNodeNext = NULL
Step 5: If(REAR not equal to NULL)
(a) REAR next = NewNode;
Step 6: REAR =NewNode;
Step 7: Exit

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;
}

void add (int n)


{node *tmp;
tmp=new node;
if(tmp==NULL)
{cout<<endl<<"Queue Full";}
tmp->data=n;
tmp->link=NULL;
if(front==NULL)
{rear=front=tmp;
display(front);

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:

To Implement Binary Search Tree.


ALGORITHM:

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

(b) GoTo Step 6


Step 3: If (ROOT == NULL)
(a) Display The DATA does not exist
(b) GoTo Step 6
Step 4: If(DATA > ROOTInfo)
(a) ROOT = ROOTRChild
(b) GoTo Step 2
Step 5: If(DATA < ROOTInfo)
(a) ROOT = ROOTLchild
(b) GoTo Step 2
Step 6: Exit
Deletion:
NODE is the current position of the tree, which is in under consideration. LOC is
the place where node is to be replaced. DATA is the information of node to be deleted.
Step 1: Find the location NODE of the DATA to be deleted.
Step 2: If (NODE = NULL)
(a) Display DATA is not in tree
(b) Exit
Step 3: If(NODE Lchild = NULL)
(a) LOC = NODE
(b) NODE = NODE RChild
Step 4: If(NODE RChild= =NULL)
(a) LOC = NODE
(b) NODE = NODE LChild
Step 5: If((NODE Lchild not equal to NULL) && (NODE Rchild not equal to NULL))
(a) LOC = NODE RChild
Step 6: While(LOC Lchild not equal to NULL)
(a) LOC = LOC Lchild
Step 7: LOC Lchild = NODE Lchild
Step 8: LOC RChild= NODE RChild
Step 9: Exit

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;
};

typedef struct node *NODE;

public:
struct node *root;
BST()
{root=NULL;}

//public functions declarations


void find(int,NODE *,NODE *);
void case_a(NODE,NODE);
void case_b(NODE,NODE);
void case_c(NODE,NODE);
void insert(int);
void del(int);
void inorder(NODE);
};

//Function to find the item form the tree


void BST::find(int item,NODE *par,NODE *loc)
{NODE ptr,ptrsave;
if(root==NULL) /*tree empty*/
{*loc=NULL;
*par=NULL;
return;
}
if(item==root->info) /*item is at root*/
{*loc=root;
*par=NULL;
return;
}
/*Initialize ptr and ptrsave*/
if(item<root->info)
{ptr=root->lchild;}
else
{ptr=root->rchild;}
ptrsave=root;
while(ptr!=NULL)
{if(item==ptr->info)
{*loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
{ptr=ptr->lchild;}
else
{ptr=ptr->rchild;}
}

*loc=NULL; //Item not found


*par=ptrsave;
}

void BST::case_a(NODE par,NODE loc)


{if(par==NULL) //Item to be deleted is root node
{root=NULL;}
else
{if(loc==par->lchild)
{par->lchild=NULL;}
else
{par->rchild=NULL;}
}
}

void BST::case_b(NODE par,NODE loc)


{NODE child;
//Initialize child
if(loc->lchild==NULL)

//Item to be deleted is lchild

{child=loc->lchild;}
else

//Item to be deleted is rchild


{child=loc->rchild;}

if(par==NULL ) /*Item to be deleted is root node*/


{root=child;}
else
{if( loc==par->lchild) /*item is lchild of its parent*/
{par->lchild=child;}
else /*item is rchild of its parent*/
{par->rchild=child;}
}
}/*End of case_b()*/

void BST::case_c(NODE par,NODE loc)


{NODE ptr,ptrsave,suc,parsuc;
/*Find inorder successor and its parent*/
ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{ptrsave=ptr;
ptr=ptr->lchild;
}
suc=ptr;
parsuc=ptrsave;
if(suc->lchild==NULL && suc->rchild==NULL)
{case_a(parsuc,suc);}
else
{case_b(parsuc,suc);}
if(par==NULL) /*if item to be deleted is root node */
{root=suc;}
else
if(loc==par->lchild)
{par->lchild=suc;}
else
{par->rchild=suc;}
suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}/*End of case_c()*/

//This function will insert an element to the tree


void BST::insert(int item)
{ NODE tmp,parent,location;
find(item,&parent,&location);

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()*/

//Function to delete a node


void BST::del(int item)
{NODE parent,location;
if(root==NULL)
{cout<<"\nTree is empty";
getch();
return;
}
find(item,&parent,&location);
if(location==NULL)
{cout<<"\nItem not present in tree";

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()*/

//Function for Inorder traversal


void BST::inorder(NODE ptr)
{if(root==NULL)
{cout<<"Tree is empty";
getch();
return;
}
if(ptr!=NULL)
{inorder(ptr->lchild);
cout<<" "<<ptr->info;
inorder(ptr->rchild);
}
}

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:

To Implement Tree Traversal.


ALGORITHM:

PREORDER TRAVERSAL

Step 1: Visit the root node.


Step 2: Traverse the left sub tree in preorder.
Step 3: Traverse the right sub tree in preorder.

INORDER TRAVERSAL

Step 1: Traverse the left sub tree in inorder.


Step 2: Visit the root node.
Step 3: Traverse the right sub tree in inorder.

POSTORDER TRAVERSAL

Step 1: Traverse the left sub tree in postorder.


Step 2: Traverse the right sub tree in postorder.
Step 3: Visit the root node.

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;}

void findparent(int n,int &found,leaf *&parent)


{leaf *q;
found=NO;
parent=NULL;
if(p==NULL)
{return;}
q=p;
while(q!=NULL)
{if(q->data==n)
{found=YES;

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;
}
}

void in(leaf *q)


{if(q!=NULL)
{in(q->l);
cout<<"

"<<q->data<<endl;

in(q->r);
}
}

void pre(leaf *q)


{if(q!=NULL)
{cout<<"
pre(q->l);
pre(q->r);
}
}

"<<q->data<<endl;

void post(leaf *q)


{if(q!=NULL)
{post(q->l);
post(q->r);
cout<<"

"<<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:

To perform Insertion Sort on an array.

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<<"\nThe array is: ";


for(i=0;i<n;i++)
{cout<<" "<<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];}
}

cout<<"\n\n\nThe sorted array is "<<endl;


cout<<"\t\t";
for(i=0;i<n;i++)
{cout<<" "<<a[i];}
getch();
}

OUTPUT SCREEN:

To perform Bubble Sort on an array.

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<<"\nThe array is: ";


for(i=0;i<n;i++)
{cout<<" "<<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];}
}

cout<<"\n\n\n The sorted array is "<<endl;


cout<<"\t\t";
for(i=0;i<n;i++)
{cout<<" "<<a[i];}
getch();
}

OUTPUT SCREEN:

To perform Selection Sort on an array.

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.

Step 1: Input n numbers of an array A


Step 2: Initialize i = 0 and repeat through step5 if (i < n 1)
(a) min = a[i]
(b) loc = i
Step 3: Initialize j = i + 1 and repeat through step 4 if (j < n 1)
Step 4: If (a[j] < min)
(a) min = a[j]
(b) loc = j
Step 5: If (loc ! = i)
(a) swap = a[i]
(b) a[i] = a[loc]
(c) a[loc] = swap
Step 6: Display the sorted numbers of array A
Step 7: Exit.

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:

To perform Quick Sort on an array.

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);

void quick(int arr[],int lower,int higher)


{int j;
if(higher>lower)
{j=partition(arr,lower,higher);
quick(arr,lower,j-1);
quick(arr,j+1,higher);
}
}

int partition(int arr[],int lower,int higher)


{int j,right,left,temp;
right=lower+1;
left=higher;
j=arr[lower];
while(right<=left)
{while(arr[right]<j)
{right++;}
while(arr[left]>j)
{left--;}

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:

To perform Shell Sort on an array.

ALGORITHM:

void shell_sort (int *a, int n)


{ int h, i, j, k;
for (h = n; h /= 2;)
{
for (i = h; i < n; i++)
{k = a[i];
for (j = i; j >= h && k < a[j - h]; j -= h)
{a[j] = a[j - h];}
a[j] = k;
}
}
}

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:

To perform Merge Sort on an array.

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>

void MergeSort(int arrays[], int temp[], int size);


void m_sort(int arrays[], int temp[], int left, int right);
void merge(int arrays[], int temp[], int left, int mid, int right);

int arrays[30];
int temp[30];

void MergeSort(int arrays[], int temp[], int size)


{m_sort(arrays,temp,0,size-1);}

void m_sort(int arrays[], int temp[], int left, int right)


{int mid;
if(right>left)
{mid=(right+left)/2;
m_sort(arrays,temp,left,mid);
m_sort(arrays,temp,mid+1,right);
merge(arrays,temp,left,mid+1,right);
}
}

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);

cout<<" \n\n\nThe Sorted array is -> \n\n\t\t";


for(i=0;i<n;i++)

//After merging

{cout<<arrays[i]<<" ";}
getch();
}

OUTPUT SCREEN:

Study of Floyd Warshalls Algorithm.

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>

#define infinity 9999


#define max 10

int minimum(int a,int b)


{if(a<=b)
{return a;}
else
{return b;}
}

void display(int matrix[max][max],int n)


{int i,j;
for(i=0;i<n;i++)
{cout<<endl;
for(j=0;j<n;j++)
{cout<<" ";
cout<<matrix[i][j];
}
}
}

void main()
{clrscr();
int adj[max][max],path[max][max],i,j,k,n;
cout<<"Enter number of vertices : ";
cin>>n;

cout<<endl<<"Enter weighted matrix: "<<endl;


for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{cin>>adj[i][j];}
cout<<endl;
}
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{if(adj[i][j]==0)
{path[i][j]=infinity;}
else
{path[i][j]=adj[i][j];}
}
}
for(k=0;k<n;k++)
{for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{path[i][j]=minimum(path[i][j],(path[i][k]+path[k][j]));}
}
}
cout<<endl<<"Using WARSHELL'S Algorithm, shortest path Matrix is: ";
display(path,n);
getch();
}

OUTPUT SCREEN:

To perform Heap Sort on an array.


ALGORITHM:
INSERTION OF A NODE:
D) AT THE BEGINNING OF A LINKED LIST
Step 1: {Check for free space}
If new = NULL output OVERFLOW and exit.
Step 2: {Allocate free space}
New=new Link
Step 3: {Read value of information part of a new node}
Info[New]= Value
Step 4: {Link address part of the currently created node with the address of start}
Next[Next]=Start
Step 5: {Now assign address of newly created node to the start}
Start=New
Step 6: Exit.
E) AT THE END OF A LINKED LIST
Step 1: {Check for free space}
If new = NULL output OVERFLOW and exit.
Step 2: {Allocate free space}
New=new Link
Step 3: {Read value of information part of a new node}
Info[New]= Value
Step 4: {Move the pointer to the end of the list}
Repeat while Node<>NULL
Node=Next[Node]
Previous=Next[Previous]
Step 5: {Link currently created node with the last node of the list}
Next[New]=Node
Next[Previous]=New
Step 6: Exit.

F) AT A DESIRED PLACE,WHEN NODE NUMBER IS KNOWN


Step 1: {Check for free space}
If new = NULL output OVERFLOW and exit.
Step 2: {Initializaton}
Node_number=0
Node=Start.Next[Points to first node of the list]
Previous=Address of Start[Assign address of start to previous]
Step 3: {Read node number that we want to insert}
Input Insert_node
Step 4: {Perform insertion operation}
Repeat through Step 5 while NODE<>NULL
Step 5: {Check if Insert_node is equal to the Node_number}
If Node_number+1=Insert_node
Next[New]=Node
Previous->Next=New
Info[New]=Value
Return
Else {Move the pointer forward}
Node=Next[Node]
Previous=Next[Previous]
Node_number=Node_number+1
Step 6: Exit.

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.

Else {Delete first node}


Next[Previous]=Next[Node] {Move pointer to next node in the list}
Free the space associated with Node
Step 3: Exit.

D) FROM THE END OF A LINKED LIST


Step 1: {Initialization}
Node=start.Next {Points to the first node in the list}
Previous=assign address of start
Node_number=0
Step 2: {Check list is empty or not}
If node=NULL
Output UNDERFLOW and exit.
Step 3: {Scan the list to count the number of node in the list}
Repeat while node<>NULL
Node=Next[Node]
Previous=next[Previous]
Node_number=Node_number+1
Step 4: {Initialization once again}
Node=start.Next
Previous=Assign address of start
Step 5: {Scan list for 1 less to size of the list}
Repeat while Node_Number<>1
Node=next [Node]
Previous=next [Node]
Node_number=Node_number-1
Step 6: {Check if last node is arising}
If node_number=1
Next[Previous]=Next[Node]
Free(node)
Step7: 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:

You might also like