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

SCREEN SHOTS: .

.

.To implement Stacks using arrays. 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. 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.

a[TOS]=value. do {if(ch==1) {if(TOS>4) {cout<<"Stack overflow".h> #include<process.h> void main() { clrscr().i. cout<<endl<<"1:Push"<<endl<<"2:Pop"<<endl<<"3:Exit"<<endl.PROGRAM CODE: #include<iostream. cin>>ch. int TOS=0. cin>>value. TOS=TOS+1. } cout<<endl<<"Enter "<<endl<<"1:Push"<<endl<<"2:Pop"<<endl<<"3:Exit"<<endl. cout<<endl<<"Enter the operation you ant to perform".ch. } if(ch==2) {cout<<endl<<"The top most element is: "<<endl.} else { cout<<endl<<"Enter the element you want to insert: ".h> #include<conio.value.a[5]. cin>>ch. .

} }while(ch==1 || ch==2). } . cin>>ch. } cout<<endl<<"Enter "<<endl<<"1:Push"<<endl<<"2:Pop"<<endl<<"3:Exit"<<endl.} else {value= a[TOS]. } if(ch==3) {exit(0). TOS=TOS-1. cout<<value<<endl.if(TOS==-1) {cout<<"Stack Underflow"<<endl.

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 4: Exit. Step 3: If it does not match. Step 2: If it matches. Return its index. Continue searching until the end of the array. 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.

} else {cout<<"Invalid choice.i<10.flag. }. cout<<endl<<"Select any one:"<<endl<<"1-Linear Search"<<endl<<"2-Binary Search"<<endl.". void Search::getdata() {cout<<"Enter 10 numbers: "<<endl.y.i. cin>>y.} cout<<endl<<"Enter the number to be searched: "<<endl. for(i=0.j. void getdata(). void binary(int x).PROGRAM CODE: #include<iostream. void sort(int a[]).} } . if(y==1) {linear(x). void linear(int x).temp.h> #include<conio. cin>>x.i++) {cin>>a[i].} else if(y==2) {sort(a).h> class Search { public: int a[10]. binary(x).x.

i<10.mid.} } void Search::binary(int x) {flag=0. for(int i=0.".j++) {if(a[i]<a[j]) {temp=a[i].i++) {if(a[i]==x) {cout<<"The element exists at position: "<<i+1. a[j]=temp. } } if(flag==0) {cout<<"The element doesnt exist. break. } } } cout<<endl<<"The sorted array is: "<<endl.void Search::linear(int x) {flag=0.i++) {cout<<a[i]<<endl. int beg=0.} } void Search::sort(int a[]) {for(i=0.last=10.i<10.j<10. for(i=0. flag++. a[i]=a[j].i++) {for(j=0. while(beg<=last) .i<10.

getdata(). break.} else if (x>a[mid]) {beg=mid. s. if(x==a[mid]) {flag++. Search s.} else {last=mid-1. cout<<"The element exists at position: "<<mid+1.". getch(). } .} } void main() { clrscr().{mid=(beg+last)/2.} } if(flag==0) {cout<<"The element doesnt exist.

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.

} getch(). a[i]=a[j].i++) {cin>>a[i]. a[j]=temp.h> #include<conio.PROGRAM CODE: #include<iostream.i.i++) {for(j=0. } } } cout<<endl<<"The sorted array is: "<<endl.i<5. int a[5].j++) {if(a[i]<a[j]) {temp=a[i]. } .} for(i=0.i<5.j.j<5.i++) {cout<<a[i]<<endl. for(i=0.temp. for(i=0. cout<<"Enter 5 numbers: "<<endl.h> void main() {clrscr().i<5.

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

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: .Inserting an element to circular Queue Step 1: Initialize FRONT = – 1.

} else {cout<<endl<<"Enter item: ".h> #include<process.item. void insert() {if(rear==MAX-1) {cout<<"Queue is full.".h> #include<conio.} queue[rear]=item.h> #define MAX 50 int queue[50]. cin>>item.} . cout<<"Item inserted: "<<item<<endl<<endl. } else {rear++.Linear Queue: #include<iostream.rear=-1.". front=0. if(rear==-1 && front==-1) {rear=0. } } void del() {if(front==-1) {cout<<endl<<"Queue is empty.front=-1.

} void main() {clrscr(). } else {front++.i++) {cout<<" "<<queue[i]. do {cout<<endl<<"1=Insert"<<endl<<"2=Delete"<<endl<<"3=Display"<<endl<<"4=Exit"<<endl<<" Choice: ". rear=-1.". int ch. if(front==-1) {cout<<endl<<"Queue is empty.i<=rear. for(i=front. .} cout<<"Item deleted: "<<item<<endl<<endl. if(front==rear) {front=-1.else {item=queue[front]. } } void display() {int i.} } cout<<endl.} else {cout<<endl.

default:cout<<endl<<"Invalid entry. break. case 3:display(). case 4:exit(0). break."<<endl. break. switch(ch) {case 1:insert().cin>>ch. } OUTPUT: Linear Queue: . } }while(1). case 2:del().

.

} else if(rear==SIZE-1) {rear=0. if(rear==-1) {rear=0. front=0.item.rear=-1. } } void del() {if(front==-1) .h> #include<process.} queue[rear]=item. void insert() {if((front==0 && rear==SIZE-1) || (front==rear+1)) {cout<<endl<<"Queue is full.Circular Queue: #include<iostream.h> #include<conio.} else {cout<<endl<<"Enter item: ". cin>>item.h> #define SIZE 5 int queue[SIZE]. cout<<"Item Inserted: "<<item<<endl<<endl.front=-1.".} else {rear++.

rear=-1. } else if(front==SIZE-1) {front=0. int ch. do . for(i=front.".i<=rear. } void main() {clrscr(). if(front==rear) {front=-1.". if((front==-1) || (front==rear+1)) {cout<<"Queue is empty.} else {cout<<endl.{cout<<endl<<"Queue is empty.} else {front++.} } cout<<endl.} cout<<endl<<"Item Deleted: "<<item<<endl<<endl.} else {item=queue[front].i++) {cout<<" "<<queue[i]. } } void display() {int i.

case 2:del(). break. } }while(1). case 4:exit(0). switch(ch) {case 1:insert().{cout<<endl<<"1=Insert"<<endl<<"2=Delete"<<endl<<"3=Display"<<endl<<"4=Exit"<<endl<<" Choice: ". } . break."<<endl. break. default:cout<<endl<<"Invalid entry. cin>>ch. case 3:display().

OUTPUT: Circular Queue: .

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: {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. 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.To perform various operations on a singly linked list. . B) AT THE END OF A LINKED LIST Step 1: {Check for free space} If new = NULL output “OVERFLOW” and exit.

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. Step 2: {Perform deletion operation } If node=NULL . Step 2: {Initializaton} Node_number=0 Node=Start. 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.C) AT A DESIRED PLACE.WHEN NODE NUMBER IS KNOWN Step 1: {Check for free space} If new = NULL output “OVERFLOW” 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. B) FROM THE END OF A LINKED LIST Step 1: {Initialization} Node=start.Output “UNDERFLOW” and exit. .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.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. 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.

*start=NULL.Exit". cin>>ch. cout<<"\n 1. *prev.Deletion Of Node From Beginning".Insertion Of Node At Specific Position".item. cout<<"\n 3. switch(ch) {case 1: ptr=new node.h> #include<process.h> struct node { int info. do {cout<<"\n MENU". cout<<"\n 2.*ptr.h> #include<conio.PROGRAM CODE: #include<iostream.Traversal". node *temp. }. void main() { clrscr().loc. cout<<"\n 5. if(ptr==NULL) .*prev.i. cout<<"\n\n Enter Your Choice : ". node *next. int ch.Insertion Of Node At Beginning". cout<<"\n 4. char wish.

ptr->info=item. start=ptr. } else .{cout<<"\n OVERFLOW". } else {ptr->prev=NULL. start=ptr.} else {cout<<"\n Enter the element to be inserted : ". ptr->next=NULL. cin>>item. if(ptr==NULL) {cout<<"\n OVERFLOW". if(start==NULL) {ptr->prev=NULL.} else {cout<<"\n Enter the element to be inserted : ". } } break. ptr->next=NULL. case 2: ptr=new node. if(start==NULL) {ptr->prev=NULL. ptr->info=item. cin>>item. start=ptr. ptr->next=start.

i++. delete ptr. ptr->prev=temp. } } break. delete ptr. cin>>loc. . } ptr->next=temp->next. } else {start=start->next. else {ptr=start. case 3: if(start==NULL) cout<<"\n UNDERFLOW". temp=start. cout<<"\n Enter the location where the element is to be inserted in the list: ". if(start->next==NULL) {start=NULL. while(i<loc-1) {temp=temp->next. cout<<"\n "<<ptr->info<<" IS DELETED FROM THE BEGINNING".{i=1. cout<<"\n "<<ptr->info<<" IS DELETED ". start->prev=NULL. temp->next=ptr.

} else {cout<<"\nTHE ELEMENTS OF THE LIST : ". } cout<<"\n\t Do you wish to continue.} } break.(y/n) ". while(ptr!=NULL) {cout<<" "<<ptr->info. case 4: if(start==NULL) {cout<<"\nTHE LIST CONTAINS NO ELEMENT". case 5: exit(0). }while(wish=='y' || wish=='Y'). } ... ptr=ptr->next... } } break. getch().. cin>>wish. break. ptr=start. default:cout<<"\n\n\t WRONG CHOICE ENTERED ".

OUTPUT SCREEN: .

.

i = i +1 Step 5: If (TEMP not equal to NULL) and (i equal to POS) (a) Create a New Node (b) NewNodeD  ATA = DATA (c) NewNodeRPoint = TEMP RPoint (d) NewNodeLPoint = 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. Step 1: Input the POS Step 2: Initialize TEMP = START. After deletion. 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. DATA will contain the information on the deleted node. Step 1: Input the POS Step 2: Initialize TEMP = START. TEMP is a temporary pointer to hold the node address. TEMP is the temporary pointer to hold the address of the node. Let POS is the position of the node to be deleted. i = i +1 . POS is the position where the NewNode is to be inserted. 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.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.

Step 5: If (TEMP not equal to NULL) and (i equal to POS) (a) Create a New Node (b) NewNodeD  ATA = DATA (c) NewNodeRPoint = TEMP RPoint (d) NewNodeLPoint = TEMP (e) (TEMP RPoint) LPoint = NewNode (f ) TEMP RPoint = New Node Step 6: Else (a) Display “Position NOT found” Step 7: Exit .

int ch. void create_list(int).n. cout<<" \n 2. cout<<" \n 6. struct node *prev. cin>>ch.int). cout<<"\n\n Enter yout choice: ". Add at begining ". void del(int). cout<<" \n 3. void add_after(int. void display(). start=NULL. cout<<" \n 5.h> struct node {int info.PROGRAM CODE: #include<iostream.m. void main() {clrscr(). Create list ".h> #include<conio. Add after ". cout<<" \n 4. .pos. void add_at_beg(int). struct node *next.i. Quit ".h> #include<process. Delete ". while(1) {cout<<endl<<" \n 1. Display ". }*start.

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;

while(q!=NULL) {cout<<q->info<<" ". } if(flag==1) {cout<<"\n Element "<<num<<" deleted.q->next = temp->next.". temp->next->prev=q. flag=1. } } .} else {cout<<"\n Element "<<num<<" not found... delete temp. q=q->next.". } q = q->next. cout<<"\n List is: \n".} } void display() {struct node *q. flag=1. if(start==NULL) {cout<<"List is Empty. delete temp..} q=start. q->next = NULL. } if(q->next->info == num) {temp=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( ) .

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] .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.

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 .

struct node *last. }.int). public: //Constructor is defined Circular_Linked() {last=NULL. . void addafter(int.} void create_list(int).tmp. void del(). typedef struct node *NODE. }.PROGRAM CODE: #include<iostream. //New node is created tmp = (NODE)new(struct node). //A circular list created in this function void Circular_Linked::create_list(int num) {NODE q. struct node *link.h> #include<process.h> #include<conio. void display().h> //class is created for the circular linked list class Circular_Linked {//structure node is created struct node {int info. void addatbeg(int).

last->link = tmp.int pos) {NODE tmp. q = last->link.tmp->info = num. tmp->info = num. i < pos-1. }/*End of addatbeg()*/ //Function to add new node at any position of the circular list void Circular_Linked::addafter(int num. tmp->link = last->link. last = tmp. tmp = (NODE)new(struct node). } }/*End of create_list()*/ //This function will add new node at the beginning void Circular_Linked::addatbeg(int num) {NODE tmp.q. tmp->link = last. } else {tmp->link = last->link. //finding the position to insert a new node for(i=0. /*added at the end of list*/ last->link = tmp. i++) {q = q->link. if (last == NULL) {last = tmp. int i. . if (q == last->link) {cout<<"There are less than "<<pos<<" elements\n".

if(q->info == num) . //deleting the node delete(tmp). } }/*End of for*/ //creating the new node tmp = (NODE)new(struct node).} }/*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". tmp->link = q->link. } cout<<"\nEnter the number for deletion:". return. cin>>num.q. return. last = NULL. if( last->link == last && last->info == num) /*Only one element*/ {tmp = last. if(q==last) /*Element inserted at the end*/ {last=tmp. } q = last->link. q->link = tmp.return. NODE tmp. tmp->info = num.

q->link = tmp->link. }/*End of while*/ if(q->link->info == num) /*Last element deleted q->link=last*/ {tmp = q->link. //deleting the node delete(tmp). q->link = last->link. delete(tmp). }/*End of del()*/ //Function to display all the nodes in the circular linked list void Circular_Linked::display() {NODE q. last->link = q->link. if(last == NULL) {cout<<"\nList is empty\n". cout<<"\n"<<num<<" deleted\n". return. return. return. } cout<<"\nElement "<<num<<" not found\n". } q = q->link. . last = q. } while(q->link != last) {if(q->link->info == num) /*Element deleted in between*/ {tmp = q->link. delete(tmp).{tmp = q.

Circular_Linked co.n.Add after \n". cout<<"\nEnter your choice:".m.return. } cout<<last->info<<endl.Add at begining\n". cout<<"6. int choice.Delete\n". cout<<"3. cin>>choice.Quit\n". while(q != last) {cout<<q->info<<" ". } q = last->link.Display\n". cout<<"4.Create List\n". i < n. for(i=0. cin>>n.i++) {cout<<"\nEnter the element:". q = q->link. cout<<"2. cin>>m.po.//Object is created for the class while(1) {//Menu options cout<<"\n1. cout<<"5. }/*End of display()*/ void main() {clrscr(). cout<<"\nList is:\n". .i. switch(choice) {case 1:cout<<"\nHow many nodes you want:".

break.create_list(m). case 5:co. }/*End of switch*/ }/*End of while*/ }/*End of main()*/ . case 2:cout<<"\nEnter the element:". default:cout<<"\nWrong choice\n". cout<<"\nEnter the position after which this element is inserted:". cin>>m.co.addatbeg(m). co.display().po). } break. break. break.addafter(m. break. case 4:co. cin>>m.del(). cin>>po. case 6:exit(0). co. case 3:cout<<"\nEnter the element:".

OUTPUT SCREEN: .

.

DATA is the information on the node which is just deleted. Step 1: Input the DATA to be pushed Step 2: Creat a New Node Step 3: NewNodeDATA = DATA Step 4: NewNodeNext = TOP Step 5: TOP = NewNode Step 6: Exit POP OPERATION Suppose TOP is a pointer.To Implement Stacks using linked list. DATA is the data item to be pushed. TOP is NULL when the stack is empty. ALGORITHM: PUSH OPERATION Suppose TOP is a pointer. TEMP is pointer variable to hold any nodes address. TOP is NULL when the stack is empty. which is pointing towards the topmost element of the stack. which is pointing towards the topmost element of the stack. 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 .

. tmp->link=top. if(tmp==NULL) {cout<<endl<<"Stack Full". class S_LL {private: struct node {int data. node *link.h> #include<conio. node* top.h> #include<process. int pop(). }.} tmp->data=n. public: S_LL() {top=NULL. top=tmp.h> void push(int). tmp=new node.PROGRAM CODE: #include<iostream.} void push(int n) {node *tmp. display(top).

} } ~S_LL() {if(top==NULL) {return. n=tmp->data. return n. display(top). top=top->link. } void display(node *x) {while(x!=NULL) {cout<<x->data<<" ".cout<<endl. tmp=top. return NULL. return. } int pop() {if(top==NULL) {cout<<"Stack Empty". .} node *tmp. delete tmp. } node *tmp. int n. x=x->link. cout<<endl.

break.pop()."<<endl. switch(ch) {case 1:cout<<"Enter a number: ". S_LL s. } . s. void main() {clrscr(). case 3:exit(0). delete tmp. break. cin>>num.push(num). } }while(1). cin>>ch.num. default:cout<<endl<<"Invalid entry. do {cout<<endl<<"1=Push"<<endl<<"2=Pop"<<endl<<"3=Exit"<<endl<<"Choice: ". } } }. int ch. top=top->link.while(top!=NULL) {tmp=top. case 2:s.

OUTPUT SCREEN: .

Step 2: Creat a New Node Step 3: NewNodeDATA = DATA Step 4: NewNodeNext = NULL Step 5: If(REAR not equal to NULL) (a) REAR next = NewNode. 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. FRONT is a pointer. Step 7: Exit POP OPERATION REAR is a pointer in queue where the new elements are added. DATA is an element to be pushed. Step 3: Exit . ALGORITHM: PUSH OPERATION REAR is a pointer in queue where the new elements are added.To Implement Queues using linked list. Step 6: REAR =NewNode. Step 1: Input the DATA element to be pushed. which is pointing to the queue where the elements are popped. DATA is an element popped from the queue. FRONT is a pointer. which is pointing to the queue where the elements are popped.

.h> #include<conio.h> void add(int).h> #include<process. node *link. }.} tmp->data=n. tmp->link=NULL. tmp=new node. display(front).PROGRAM CODE: #include<iostream. node *front. if(tmp==NULL) {cout<<endl<<"Queue Full". int del(). class Q_LL {private: struct node {int data. if(front==NULL) {rear=front=tmp. } void add (int n) {node *tmp. public: Q_LL() {front=NULL.*rear. rear=NULL.

} void display(node *x) {while(x!=NULL) {cout<<x->data<<" ". } node *tmp. } int del() {if(front==NULL) {cout<<endl<<"Queue Empty". display(front).cout<<endl. x=x->link. return n. display(front). front=front->link. n=front->data. int n. cout<<endl. cout<<endl. } } . return NULL. return. delete tmp. tmp=front. rear=rear->link. } rear->link=tmp.

} } }. } }while(1). switch(ch) {case 1:cout<<"Enter a number: ". while(front!=NULL) {tmp=front. void main() {clrscr(). case 2:q. Q_LL q. delete tmp.num.del()."<<endl. default:cout<<endl<<"Invalid entry. break.} node *tmp. cin>>ch. q. } .add(num). front=front->link. cin>>num. break.~Q_LL() {if(front==NULL) {return. case 3:exit(0). do {cout<<endl<<"1=Add"<<endl<<"2=Delete"<<endl<<"3=Exit"<<endl<<"Choice: ". int ch.

OUTPUT SCREEN: .

Step 2: NEWNODE = Create a New Node. ALGORITHM: Insertion: NEWNODE is a pointer variable to hold the address of the newly created node. Step 2: If (DATA == ROOT → Info) (a) Display “The DATA exist in the tree” .To Implement Binary Search Tree. 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 1: Input the DATA to be pushed and ROOT node of the tree. DATA is the information to be pushed.

(b) GoTo Step 6 Step 3: If (ROOT == NULL) (a) Display “The DATA does not exist” (b) GoTo Step 6 Step 4: If(DATA > ROOT→Info) (a) ROOT = ROOT→RChild (b) GoTo Step 2 Step 5: If(DATA < ROOT→Info) (a) ROOT = ROOT→Lchild (b) GoTo Step 2 Step 6: Exit Deletion: NODE is the current position of the tree. DATA is the information of node to be deleted. LOC is the place where node is to be replaced. which is in under consideration. 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 . Step 1: Find the location NODE of the DATA to be deleted.

void inorder(NODE). public: struct node *root.h> //Class is created for the implementation of BST class BST { struct node {int info. BST() {root=NULL.NODE).h> #include<process. void case_b(NODE.h> #include<conio.NODE *. }. void case_a(NODE.NODE).} //public functions declarations void find(int. .NODE *). void case_c(NODE. void insert(int). struct node *rchild. typedef struct node *NODE.PROGRAM CODE: #include<iostream. struct node *lchild.NODE). }. void del(int).

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

} if(par==NULL ) /*Item to be deleted is root node*/ {root=child.*loc=NULL.} else {if(loc==par->lchild) {par->lchild=NULL.NODE loc) {NODE child.} } }/*End of case_b()*/ . } void BST::case_a(NODE par. //Initialize child if(loc->lchild==NULL) //Item to be deleted is lchild {child=loc->lchild.} else /*item is rchild of its parent*/ {par->rchild=child.} } } void BST::case_b(NODE par.} else {if( loc==par->lchild) /*item is lchild of its parent*/ {par->lchild=child.NODE loc) {if(par==NULL) //Item to be deleted is root node {root=NULL.} else {par->rchild=NULL.} else //Item to be deleted is rchild {child=loc->rchild. //Item not found *par=ptrsave.

find(item.} else {case_b(parsuc.void BST::case_c(NODE par.&location). /*Find inorder successor and its parent*/ ptrsave=loc. .suc).} suc->lchild=loc->lchild. ptr=loc->rchild.} if(par==NULL) /*if item to be deleted is root node */ {root=suc.ptrsave. if(suc->lchild==NULL && suc->rchild==NULL) {case_a(parsuc.location.&parent.parsuc.suc. suc->rchild=loc->rchild. while(ptr->lchild!=NULL) {ptrsave=ptr.suc).} else if(loc==par->lchild) {par->lchild=suc.} else {par->rchild=suc.NODE loc) {NODE ptr. }/*End of case_c()*/ //This function will insert an element to the tree void BST::insert(int item) { NODE tmp.parent. parsuc=ptrsave. ptr=ptr->lchild. } suc=ptr.

if(parent==NULL) {root=tmp. tmp->lchild=NULL. if(location==NULL) {cout<<"\nItem not present in tree".} else {parent->rchild=tmp.} else {if(item<parent->info) {parent->lchild=tmp.&location).&parent. getch(). } find(item. getch(). return. return.if(location!=NULL) {cout<<"\nItem already present". if(root==NULL) {cout<<"\nTree is empty".} } }/*End of insert()*/ //Function to delete a node void BST::del(int item) {NODE parent. . } //creating new node to insert tmp=(NODE)new(struct node).location. tmp->info=item. tmp->rchild=NULL.

} if(ptr!=NULL) {inorder(ptr->lchild).location).} if(location->lchild!=NULL && location->rchild!=NULL) {case_c(parent.num. int choice. inorder(ptr->rchild). cout<<" "<<ptr->info. }/*End of del()*/ //Function for Inorder traversal void BST::inorder(NODE ptr) {if(root==NULL) {cout<<"Tree is empty".} if(location->lchild==NULL && location->rchild!=NULL) {case_b(parent. } } void main() {clrscr().return. } if(location->lchild==NULL && location->rchild==NULL) {case_a(parent.} if(location->lchild!=NULL && location->rchild==NULL) {case_b(parent.location). .location). return.location). getch().} delete(location).

break. cin>>num.inorder(bo. case 4:exit(0). break. }/*End of switch */ }/*End of while */ }/*End of main()*/ .Delete\n".BST bo.Insert\n". switch(choice) {case 1:cout<<"\nEnter the number to be inserted : ". case 3:bo. bo.Quit\n".insert(num). cout<<"2. break. cout<<"\nEnter your choice : ". cin>>choice. cout<<"3. case 2:cout<<"\nEnter the number to be deleted : ". getch().Inorder Traversal\n". default:cout<<"\nWrong choice\n". cout<<"4. getch(). bo. cin>>num.root).del(num). while(1) {//Menu options cout<<"\n1.

OUTPUT SCREEN: .

To Implement Tree Traversal. Step 3: Traverse the right sub tree in preorder. INORDER TRAVERSAL Step 1: Traverse the left sub tree in inorder. Step 3: Visit the root node. Step 2: Traverse the right sub tree in postorder. . ALGORITHM: PREORDER TRAVERSAL Step 1: Visit the root node. Step 2: Visit the root node. Step 2: Traverse the left sub tree in preorder. Step 3: Traverse the right sub tree in inorder. POSTORDER TRAVERSAL Step 1: Traverse the left sub tree in postorder.

struct leaf *p. if(p==NULL) {return.PROGRAM CODE: #include<iostream. .h> #include<conio. leaf *r. leaf *l.} q=p. parent=NULL.leaf *&parent) {leaf *q. while(q!=NULL) {if(q->data==n) {found=YES. }.h> #define YES 1 #define NO 0 class BST {private: struct leaf {int data. found=NO.int &found.} void findparent(int n. public: BST() {p=NULL.

t->data=n.parent). q=q->l.return.found. if(found==YES) {cout<<endl<<"Such a node exists". if(parent==NULL) {p=t.} else {t=new leaf. } } } void add(int n) {int found.} } } .} else {parent->data>n?parent->l=t:parent->r=t. } if(q->data>n) {parent=q.*parent. t->l=NULL. } else {parent=q. findparent(n. q=q->r. leaf *t. t->r=NULL.

break. } } void pre(leaf *q) {if(q!=NULL) {cout<<" pre(q->l). case 2: pre(p).void traverse() {int c. switch(c) {case 1: in(p). break. } } void in(leaf *q) {if(q!=NULL) {in(q->l). cout<<endl<<"1=InOrder"<<endl<<"2=PreOrder"<<endl<<"3=PostOrder"<<endl<<"Choice: ". cin>>c. . break. cout<<" "<<q->data<<endl. } } "<<q->data<<endl. pre(q->r). case 3: post(p). in(q->r).

void post(leaf *q) {if(q!=NULL) {post(q->l).traverse().traverse(). post(q->r). } } }. cout<<"Enter 7 elements for BST: "<<endl. for(int i=0. } t.add(data[i]).i++) {cin>>data[i]. cout<<" "<<q->data<<endl.traverse(). void main() {clrscr(). getch(). int data[7]. t. t.i<7. } . BST t. t.

OUTPUT SCREEN: .

. (a) If (i < = n – 1) (b) Swap = A [I]. Step 1: Input an array A of n numbers Step 2: Initialize i = 1 and repeat through steps 4 by incrementing i by one. ALGORITHM: Let A be a linear array of n numbers A [1].To perform Insertion Sort on an array.... (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.A [n].Swap be a temporary variable to interchange the two values.... A [3]. ... A [2]. Pos is the control variable to hold the position of each pass. . ..

int a[20].i++) {cin>>a[i]. for(int k=0. for(i=0. j=i-1.temp.j.} } . cin>>n. cout<<"\n The array after "<<i+1<<" iteration is : ".} cout<<"\n".i<n. cout<<"\nEnter the elements of the array: "<<endl.k++) {cout<<" "<<a[k].i<n.} cout<<"\nThe array is: ". while(temp<a[j] && j>=0) {a[j+1]=a[j].n. for(int i=0.i<n.h> #include<conio. cout<<"\nEnter the number of elements of Array: ". j=j-1. for(i=0.i++) {cout<<" "<<a[i].k<n.h> void main() {clrscr().i++) {temp=a[i].PROGRAM CODE: #include<iostream. } a[j+1]=temp.t.

} getch(). for(i=0. cout<<"\t\t".cout<<"\n\n\nThe sorted array is "<<endl. } .i++) {cout<<" "<<a[i].i<n.

OUTPUT SCREEN: .

ALGORITHM: Let A be a linear array of n numbers.To perform Bubble Sort on an array. 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. .

n.h> #include<conio.k<n. int a[20].i++) {cout<<" "<<a[i]. cout<<"\nEnter the elements of the array: "<<endl.j<n-1.h> void main() {clrscr(). for(i=0. a[j+1]=temp.} cout<<"\nThe array is: ". cin>>n.t.i++) {for(int j=0.i<n. } } cout<<"\n Elements after "<<i+1<<" pass : " .} cout<<"\n".temp.PROGRAM CODE: #include<iostream. a[j]=a[j+1]. for(int i=0.i<n. for(int k=0.i<n.k++) {cout<<" "<<a[k].i++) {cin>>a[i].j++) {if(a[j]>a[j+1]) {temp=a[j].} } . for(i=0. cout<<"\nEnter the number of elements of Array: ".

cout<<"\t\t". for(i=0.i++) {cout<<" "<<a[i]. } OUTPUT SCREEN: .} getch().i<n.cout<<"\n\n\n The sorted array is "<<endl.

To perform Selection Sort on an array. 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. A [3] … A [k]. 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. . A [2]. A [k+1]… A [n]. ALGORITHM: Let A be a linear array of n numbers A [1].

cout<<"\nEnter the number of elements of Array: ". cin>>n.i<n. cout<<"\nThe array after "<<i+1<<" pass is : ". a[i]=a[min_index]. int a[20].i<n.} cout<<"\nThe array is : ".min_index.k++) {cout<<" "<<a[k].i++) {cout<<" "<<a[i].} } temp=a[i].i++) {cin>>a[i]. for(int i=0.h> #include<conio. cout<<"\nEnter the elements of the array : "<<endl. a[min_index]=temp.i++) {min_index=i. for(i=0. for(i=0.j++) {if(a[min_index]>a[j]) {min_index=j.t.n.i<n.PROGRAM CODE: #include<iostream.} } cout<<"\n\n\nThe sorted array is: "<<endl.} cout<<"\n".temp. for(int j=i. .k<n. for(int k=0.h> void main() {clrscr().j<n.

for(i=0.cout<<"\t\t".i<n. } OUTPUT SCREEN: .i++) {cout<<" "<<a[i].} getch().

To perform Quick Sort on an array.1 quicksort(array from first index to right) quicksort(array from left to last index) . 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 .

h> #include<conio. quick(arr. void quick(int arr[].higher).PROGRAM CODE: #include<iostream.left.int .j+1.int).int lower.lower.int . } } int partition(int arr[].h> int partition(int* .right.int lower. if(higher>lower) {j=partition(arr. arr[left]=temp. while(right<=left) {while(arr[right]<j) {right++. right=lower+1.int). j=arr[lower]. left=higher. quick(arr.int higher) {int j.int higher) {int j.} if(right<left) {temp=arr[right].j-1). .} while(arr[left]>j) {left--.temp. arr[right]=arr[left]. void quick(int* .higher).lower.

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

i=i+gap) {temp=a[i]. } } cout<<"\n\n\nThe Sorted array is ->\n\n\t ".j=j-gap) {a[j]=a[j-gap]. for(i=0.i<n.j>0 && a[j-gap]>temp. } OUTPUT SCREEN: .} a[j]=temp.} getch(). for(j=i.i++) {cout<<a[i]<<" ".i<n.{for(i=0.

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 . 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.To perform Merge Sort on an array. right) return result function merge(left. right. ALGORITHM: function merge_sort(m) if length(m) ≤ 1 return m var list left.

h> #include <conio. int right).mid+1.temp_pos.left.temp. int temp[30].left.h> void MergeSort(int arrays[]. m_sort(arrays. int temp[].right). int mid. int right). int left.size-1). int temp[].mid+1. void MergeSort(int arrays[]. temp_pos=left.right). if(right>left) {mid=(right+left)/2.PROGRAM CODE: #include <iostream. int left. left_end=mid-1.mid). while((left<=left_end)&&(mid<=right)) .temp. int arrays[30].} void m_sort(int arrays[]. int temp[]. void merge(int arrays[].temp.temp. merge(arrays.left_end. int right) {int mid. int temp[]. void m_sort(int arrays[]. int right) {int i. num_ele=right-left+1. int size). int left.0. int left. int temp[]. int mid. int temp[]. } } void merge(int arrays[]. int size) {m_sort(arrays. m_sort(arrays.num_ele.

temp_pos=temp_pos+1. mid=mid+1. } } void main() {clrscr(). } while(mid<=right) {temp[temp_pos]=arrays[mid]. left=left+1. cout<<" \nEnter the number of which th array is to be made: ". right=right-1. } } while(left<=left_end) {temp[temp_pos]=arrays[left]. temp_pos=temp_pos+1. } for(i=0. left=left+1.i<=num_ele.i++) {arrays[right]=temp[right].{if(arrays[left]<=arrays[mid]) {temp[temp_pos]=arrays[left]. int i. temp_pos=temp_pos+1.n. . temp_pos=temp_pos+1. } else {temp[temp_pos]=arrays[mid]. mid=mid+1.

} OUTPUT SCREEN: .i++) //After merging {cout<<arrays[i]<<" ". cout<<" \n\n\nThe Sorted array is -> \n\n\t\t".i<n. cout<<"\n\nEnter the elements: "<<endl.i<n.i++) {cin>>arrays[i].n). for(i=0.} MergeSort(arrays.} getch(). for(i=0.} cout<<" \n\n\nThe Unsorted array is -> \n\n\t\t". for(i=0.temp.i++) // Before Merging {cout<<arrays[i]<<" ".cin>>n.i<n.

J] Step 5:Exit ...2……. This algorithm finds the (Boolean) path matrix P of the graph G..J] =0 then set p[I.J] V P[I.J]:=0 Else: Set P[I. ALGORITHM: A directed graph G with M nodes is aintained in memory by the adjacency matrix A.Study of Floyd Warshall’s Algorithm.2….M[update P] Step 3:Repeat step 4 for I=1. Step 1: Repeat for I.K] ^ P[K.J]:=P[I.2 ….M Set P[I.2….M[Initialise P] If A[I.M: Step 4:Repeat for J=1.J=1.J] :=1 Step 2: Repeat steps 3 and 4 for k=1.

k.h> #define infinity 9999 #define max 10 int minimum(int a.j++) {cout<<" ". cin>>n. cout<<matrix[i][j].j. for(j=0.n.h> #include<conio. } } } void main() {clrscr().j<n.i.int b) {if(a<=b) {return a. cout<<"Enter number of vertices : ". for(i=0.} } void display(int matrix[max][max].i++) {cout<<endl.i<n. int adj[max][max].int n) {int i.} else {return b. .path[max][max].j.PROGRAM CODE: #include<iostream.

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

OUTPUT SCREEN: .

To perform Heap Sort on an array. 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. 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: {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. 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. .

DELETION OF A NODE: C) FROM THE BEGINNING OF A LINKED LIST Step 1: {Initialization} Node=start.WHEN NODE NUMBER IS KNOWN Step 1: {Check for free space} If new = NULL output “OVERFLOW” and exit. Step 2: {Perform deletion operation } If node=NULL Output “UNDERFLOW” and exit. Step 2: {Initializaton} Node_number=0 Node=Start. .F) AT A DESIRED PLACE.Next{Points to the first node in the list} Previous=assign address of 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.

D) FROM THE END OF A LINKED LIST Step 1: {Initialization} 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. 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 {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.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. .

do {p=(c-1)/2. for(i=0.no.i<no.} for(i=1. cin>>no.i<no.temp. b[j]=temp. b[c]=temp. for(i=0. } for(j=no-1. }while(c!=0).j. b[p]=b[c].i++) {cin>>b[i]. clrscr(). if(b[p]<b[c]) {temp=b[p].h> void main() {int b[30].j>=0. } c=p.p.h> #include <conio. b[0]=b[j]. cout<<"\nEnter elements: "<<endl.i++) {c=i.PROGRAM CODE: #include <iostream. cout<<"\n\nEnter number of which array is to be created:".i.} cout<<"\n\n\nThe Unsorted array is ->\n\n\t". .j--) {temp=b[0].i<no.i++) {cout<<b[i]<<" ".c.

b[p]=b[c].} if(b[p]<b[c]&&c<j) {temp=b[p]. if((b[c]<b[c+1])&&c<j-1) {c++. for(i=0. } . b[c]=temp. do {c=2*p+1.p=0. } cout<<"\n\n\nThe Sorted array is ->\n\n\t".i<no. } p=c. }while(c<j).} getch().i++) {cout<<b[i]<<" ".

OUTPUT SCREEN: .