Tuesday 24 April 2012

FRONT PAGE of RECORD





---------------------------------------------------------------------------------------------------------------------------------------
                                                                          INDEX PAGE
---------------------------------------------------------------------------------------------------------------------------------------

1 st Experiment OPERATION ON STACK ADT(abstract)


                                                                Algorithm for STACK ADT:
--------------------------------------------------------------------------------------------------------------------------------------

Step-1: Start
Step-2: Create an object for the class stack.
Step-3: do the following:
a)      Print the options
b)     read the choice
c)      i) If case is ‘push’, the value to be pushed and use the object of the class to call the member function push() and display()
ii) If case is ‘pop’, use the object to call member function pop() and display().
iii) If case is ‘quit’, display ‘bye’.
Step-4: while the case is not quit, repeat the step-3.
Step-5: Stop.

--------------------------------------------------------------------------------------------------------------------------------------
                                                               FLOW CHARTs
-------------------------------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
-------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#define max 50
class stack
{
private:
int sarr[max];
int top;
public:
stack()
{
top=-1;
}
void push(int i)
{
if(top==max-1)
{
cout<<"\n Stack is full"<<endl;
getch();
}
top++;
sarr[top]=i;
}
int pop()
{
int d;
if(top==-1)
{
cout<<"\n Stack is empty"<<endl;
getch();
return NULL;
}
else
{
d=sarr[top];
top--;
}
return d;
}
void display()
{
cout<<endl<<"\nThe elements in the stack\n"<<endl;
for(int i=top;i>=0;i--)
{
cout<<sarr[i]<<endl;
}
}
};
void main()
{
clrscr();
stack s1;
int ch,item;
do
{
cout<<"1. Push"<<endl;
cout<<"2. Pop"<<endl;
cout<<"3. exit"<<endl;
cout<<"\n Enter the choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Enter the value:";
cin>>item;
s1.push(item);
s1.display();
getch();
break;
case 2:
item=s1.pop();
cout<<"\n Popped item="<<item;
s1.display();
getch();
break;
case 3:
cout<<"\n Bye";
getch();
break;
}}
while(ch!=3);
}

OUTPUT:

1.Push
2.Pop
3.Exit
Enter your choice:
1
Enter the element: 1
The elements in the stack
1

1.Push
2.Pop
3.Exit
Enter your choice:
1
Enter the element: 2
The elements in the stack
2
1

1.Push
2.Pop
3.Exit
Enter your choice:
2
Popped item=2
The elements in the stack
1

1.Push
2.Pop
3.Exit
Enter your choice:
3
Bye




2 nd Experiment QUEUE OPERATION ON ADT(abstract)


Algorithm: -QUEUE ADT:
------------------------------------------------------------------------------------------------------------------------------------

Step-1:  Start.
Step- 2: Create an object for the class queue.
Step- 3: do the following.
a)      Print the options
b)     read the choice
c)      i) If case is ‘addqueue’, read the value and use the object of the class to call the member function addqueue() and display()
ii) If case is ‘deletequeue’, use the object to call the member function delqueue() and display()
iii) If case is ‘quit’, display’Bye’.
Step- 4: while the case is not ‘quit’, repeat the step-3.
Step- 5: Stop.
---------------------------------------------------------------------------------------------------------------------------------------
FLOW CHARTs
---------------------------------------------------------------------------------------------------------------------------------------


-----------------------------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
----------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h> #include<conio.h> #define max 50 class queue { private: int qarr[max]; int front,rear; public: queue() { front=-1; rear=-1; } void addqueue(int i) { if(rear==max-1) { cout<<"\n The queue is full\n"; getch(); } rear++; qarr[rear]=i; if(front==-1) front=0; } int delqueue() { int d; if(front==-1) { cout<<"\n Queue is empty\n"; getch(); return NULL; } else d=qarr[front]; if(front==rear) { front=-1; rear=-1; } else front++; return d; } void display() { cout<<"\n The elements in the queue:\n"; for(int i=front;i<=rear;i++) { cout<<qarr[i]<<endl; } getch(); } }; void main() { queue q1; int ch,item; clrscr(); do { cout<<"\n 1. Add queue \n 2. Delete queue \n 3. Quit \n Enter ur choice: "; cin>>ch; switch(ch) { case 1: cout<<"\n Enter the value: "; cin>>item; q1.addqueue(item); q1.display(); getch(); break; case 2: item=q1.delqueue(); cout<<"\n Deleted item: "<<item<<endl; q1.display(); getch(); break; case 3: cout<<"\n Bye"; break; } } while(ch!=3); } OUTPUT: 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 1 Enter the no: 1 The elements present in queue 1 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 1 Enter the no: 2 The elements present in queue 1 2 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 2 popped element is 1 The elements present in queue 2 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 3 Bye

3 rd Experiment STACK OPERATION USING LINKED LIST

 ALGORITHM: STACK USING LINKED LIST:
---------------------------------------------------------------------------------------------------------------------------------------

Step- 1: Start.
Step- 2: do the following
a)      print the options
b)     read the choice
c)      i) if case is ‘push’, use the object of the class to call the member function push(). The members of the function are accessed through the pointers of the structure ‘node’. Thus stack is implemented using linked lists.
ii) If case is ‘pop’, use the object to call the member function pop(). The manipulation is done through pointers.
iii) If case is ‘display’, use the object to call the member function display().
vi) If case is ‘quit’, display’bye’.
Step-4: while the case is not ‘quit’, repeat the step-3.
Step-5: Stop.

---------------------------------------------------------------------------------------------------------------------------------------
                                                                            FLOW CHARTs
-----------------------------------------------------------------------------------------------------------------------------




-------------------------------------------------------------------------------------------------------------------------------------
                                                                 C++ IMPLEMENTATION
------------------------------------------------------------------------------------------------------------------------------------

//program for implementing Linked list on Stack
#include<iostream.h>
#include<conio.h>
#define NULL 0
class lstack
{
private:
struct node
{
int no;
node * next;
};
node * head;
public:
lstack();
~lstack();
void push();
void pop();
void display();
};
//******** Function Definition*******************************
lstack::lstack()
{
head=NULL;
}
lstack::~lstack()
{
//--------------
}
void lstack::push()
{
node * temp;
temp=new node;
cout<<"\n Enter the element:\n";
cin>>temp->no;
if (head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
cout<<"\n Element "<<temp->no<<" is pushed into the stack\n";
}
void lstack::pop()
{
node*p=head;
if(head==NULL)
{
cout<<"\n Stack is empty\n";
}
else
{
head=head->next;
cout<<"\n Element "<<p->no<<"is popped from stack\n";
delete p;
}
}
void lstack::display()
{
node *p;
if(head==NULL)
{
cout<<"\n Stack is Empty\n:";
}
else
{
p=head;
cout<<"\nTHE ELEMENTS IN THE STACK:\n";
while(p!=NULL)
{
cout<<p->no<<endl;
p=p->next;
}
}
}
//*********************MAIN PROGRAM***********************
void main()
{
clrscr();
int ch;
lstack ls;
do
{
cout<<"\n1.PUSH\n2.POP\n3.QUIT\n";
cout<<"\n ENTER YOUR CHOICE:\n";
cin>>ch;
switch(ch)
{
case 1:
ls.push();
ls.display();
getch();
break;
case 2:
ls.pop();
ls.display();
getch();
break;

case 3:
cout<<"\n BYE...";
getch();
}
}
while(ch!=3);
}

OUTPUT:
1.Push
2.Pop
3.Exit
Enter your choice:
1
Enter the element:
1
The element 1 is pushed into the stack
The elements in stack
1

1.Push
2.Pop
3.Exit
Enter your choice:
1
Enter the element:
2
The element 2 is pushed into the stack
The elements in stack
2
1

1.Push
2.Pop
3.Exit
Enter your choice:
2
Element 2 is popped from stack
The elements in stack
1

1.Push
2.Pop
3.Exit
Enter your choice:
3
Bye..

4 th Experiment QUEUE OPERATION USING LINKED LISt


 ALGORITHM:-- QUEUE USING LINKED LIST:
-------------------------------------------------------------------------------------------------------------------------------------


Step-1: Start.
Step- 2: Create an object for the class lque.
Step- 3: do the following
a)      print the options
b)     read the choice
c)      i) if case is ‘Add queue’, use the object of the class to call the member function enque(). the members of the function are accessed through the pointers of the structure ‘node’. Thus queue is implemented using linked lists.
ii) If case is ‘delete queue’, use the object of the class to call the member function dequeue(). The manipulation is done through pointers.
iii) If case is ‘display’, use the object to call the member function display().
vi) If case is ‘quit’, display ‘bye’.
Step- 4: while the case is not ‘quit’, repeat Step-3.
Step- 5: Stop.

-----------------------------------------------------------------------------------------------------------------------------------
FLOW CHARTs
-----------------------------------------------------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
-----------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h> #include<conio.h> #define NULL 0 class lque { private: struct node { int no; node *next; }; node *fptr,*rptr,*p,*temp; public: lque(); ~lque(); void enque(); void deque(); void display(); }; lque::lque() { fptr=rptr=NULL; } lque::~lque(){} void lque::enque() { node *temp; temp=new node; cout<<"\n Enter the no. :"; cin>>temp->no; temp->next=NULL; if(fptr==NULL) { fptr=temp; rptr=temp; } else { rptr->next=temp; rptr=rptr->next; } } void lque::deque() { if(fptr==NULL) { cout<<"\n Queue is empty"; return; } node *delptr,*temp; delptr=fptr; if(delptr!=NULL) { cout<<"\n popped element is "<<delptr->no; temp=fptr; fptr=fptr->next; delete delptr; } } void lque::display() { node *p; p=fptr; if(p==NULL) { cout<<"\n Queue is empty"; return; } cout<<"\n The elements present in queue\n"; while(p!=NULL) { cout<<p->no<<endl; p=p->next; } } void main() { int ch; lque lq; clrscr(); do { cout<<"\n 1. Add queue \n 2. Delete queue \n 3. Quit \n Enter ur choice:\n"; cin>>ch; switch(ch) { case 1: lq.enque(); lq.display(); getch(); break; case 2: lq.deque(); lq.display(); getch(); break; case 3: cout<<"\n Bye\n"; getch(); break; } } while(ch!=3); } OUTPUT: 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 1 Enter the no: 1 The elements present in queue 1 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 1 Enter the no: 2 The elements present in queue 1 2 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 2 popped element is 1 The elements present in queue 2 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 3 Bye

5 th Experiment BINARY TREE TRAVERSAL


                                                          ALGORITHM:-TREE TRAVERSAL:
--------------------------------------------------------------------------------------------------------------------------------------

Step-1: Start.
Step-2: Create pointer ‘*b’ to structure ‘bintree’ to insert elements and traverse the tree.
Step-3: Read the total no of elements.
Step-4: Read the elements of the tree using the function insert (&b,data) which accesses data from the structure bintree.
Step-5: For preorder traversal, invoke the function preorder(b).
Step-6: For inorder traversal, invoke the function inorder (b).
Step-7: For postorder traversal, invoke the function postorder (b).
Step-8: Stop.

---------------------------------------------------------------------------------------------------------------------------------------
                                                                 FLOW CHARTs
--------------------------------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
-------------------------------------------------------------------------------------------------------------------------------------

// program for tree traversing using linked list

#include<iostream.h>
#include<conio.h>
#define NULL 0
struct bintree
{
struct bintree *left;
int data;
struct bintree *right;
};
void insert(struct bintree **sr,int num)
{
if(*sr==NULL)
{
*sr=new bintree;
(*sr)->left=NULL;
(*sr)->data=num;
(*sr)->right=NULL;
}
else
{
if(num<(*sr)->data)
insert(&(*sr)->left,num);
else
insert(&(*sr)->right,num);
}
return;
}
void inorder(bintree *sr)
{
if(sr!=NULL)
{
inorder(sr->left);
cout<<"\n\t\t\t"<<sr->data;
inorder(sr->right);
}
else
return;
}
void postorder(bintree *sr)
{
if(sr!=NULL)
{
postorder(sr->left);
postorder(sr->right);
cout<<"\n\t\t\t"<<sr->data;
}
else
return;
}
void preorder(bintree *sr)
{
if(sr!=NULL)
{
cout<<"\n\t\t\t"<<sr->data;
preorder(sr->left);
preorder(sr->right);

}
else
return;
}
//***********************************************************
void main()
{
int no,data,i=1;
bintree *b;
b=NULL;
clrscr();
cout<<"\n\t\t\t BINARY TREE TRAVERSAL\n";
cout<<"\n\t\t\t ---------------------\n";
cout<<"\n\t      Enter Total no of elment in BINARY TREE\n";
cin>>no;
cout<<"entered no="<<no<<"\n";
while(i++<=no)
{
cout<<"\n\t\t\t Enter ELEMENTS:\n";
cin>>data;
cout<<data<<"\n";
insert(&b,data);
}
clrscr();
cout<<"\n\t\t\t PREORDER TRAVERSAL\n";
cout<<"\n\t\t\t-------------------\n";
preorder(b);
getch();
clrscr();

cout<<"\n\t\t\t INORDER TRAVERSAL\n";
cout<<"\n\t\t\t -----------------\n";
inorder(b);
getch();
clrscr();

cout<<"\n\t\t\t POST ORDER TRAVERSAL\n";
cout<<"\n\t\t\t --------------------\n";
postorder(b);
getch();
clrscr();

cout<<"\n\t\t THANKS FOR USING PROGRAM:\n";
cout<<"\n\t    BYE !!! press any key2 EXIT\n";
getch();

}
//---------------------------------------------------------------------------------------------------------------
out put
tree traversal

enter how many element u want to process
3
enter your elements
25
23
26
pre order traversal
25
23
26
inorder traversal
23
25
26
post order traversal
23
26
25
Thanks for using program
BYE…!



6 th Experiment INSERTION & DELETION IN BINASY SEACH TREE


Algorithm for insertion & deletion of elements in a binary search tree
--------------------------------------------------------------------------------------------------------------------------------

Step 1- start
Step 2-create an object for the class BINTREE
Step 3- DO the following….
a)       Print the Options
b)       Read the choice
Step 4-
A)       If the choice is “insertion”        then
a.1) read the element to be Inserted
a.1) call the member function INSERT with the obj.
a.3) Return
                B)  if the choice is “deletion” then
                b.1) Read the element to be Deleted
                b.2) call the member function “delete” with obj
                b.3) display the pointer location as “deleted Item Location” & RETURN
                C) if the choice is”DISPLAY” then
                c.1)call the member function “display” with the class obj. & RETURN
                D) If the choice is “EXIT”    then
                                D1)  display Bye
UNTIL the choice is not  "EXIT"
Step 5- STOP

------------------------------------------------------------------------------------------------------------------------------------
FLOW CHARTs
-----------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
------------------------------------------------------------------------------------------------------------------------------------
//program for INSERTION & DELETION ON BINARY TREE -
#include<iostream.h>
#include<conio.h>
//----------------------FUNCTION DECLARATION--------------
void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[10],t=1,s,x,i;
//------------------MAIN FUNCTION---------------------
void main()
{
clrscr();
int ch,y;
for(i=1;i<10;i++)
tree[i]=-1;
cout<<"\n\t BINARY TREE INSERTION & DELETION \n\n";
do
{

cout <<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT\nEnter your choice:\n";
cin >> ch;
switch(ch)
{
case 1:
cout <<"\nEnter the element to insert";
cin >> ch;
insert(1,ch);
break;
case 2:
cout <<"\nEnter the element to delete\n";
cin >>x;
y=search(1);
if(y!=-1)
{
delte(y);
cout<<"\n"<<y<<" ELEMENT IS DELETED\n";
}
else
cout<<"\nNo such element in tree\n";
break;
case 3:
display(1);
cout<<"\n";
for(int i=0;i<=10;i++)
cout <<i<<endl;
cout <<"\n";
break;
case 4:
cout <<"\nEnter the element to search:\n";
cin >> x;
y=search(1);
if(y == -1) cout <<"\nNo such element in tree\n";
else cout <<x << "  is in  " <<y <<"  position\n";
break;
case 5:
cout<<"\nBYE...!";
getch();
break;

}
}
while(ch!=5);
}
//------------------function DEFINITION---------------------------

void insert(int s,int ch )
{
int x;
if(t==1)
{
tree[t++]=ch;
return;
}
x=search1(s,ch);
if(tree[x]>ch)
tree[2*x]=ch;
else
tree[2*x+1]=ch;
t++;
}
//-----------------------FUNCTION DEFINITION FOR DELETION--------
void delte(int x)
{
if( tree[2*x]==-1 && tree[2*x+1]==-1)
tree[x]=-1;
else if(tree[2*x]==-1)
     { tree[x]=tree[2*x+1];
tree[2*x+1]=-1;
     }
else if(tree[2*x+1]==-1)
     { tree[x]=tree[2*x];
tree[2*x]=-1;
     }
else
{
 tree[x]=tree[2*x];
 delte(2*x);
}
t--;
}
//------SEARCHING AN ELEMENT IN TREE------------
int search(int s)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
}
//-----------------DISPLAY FUNCTION DEFINITION------------------
void display(int s)
{
if(t==1)
{cout <<"\n No element in tree:\n";
return;}
for(int i=1;i<10;i++)
if(tree[i]==-1)
cout <<" ";
else cout <<tree[i]<<endl;
return ;
}

int search1(int s,int ch)
{
if(t==1)
{
cout <<"\nNo element in tree\n";
return -1;
}
if(tree[s]==-1)
return s/2;
if(tree[s] > ch)
search1(2*s,ch);
else search1(2*s+1,ch);
}
OUTPUT:
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:3
no element in tree:
0
1
2
3
4
5
6
7
8
9
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:1
Enter the element to insert 10
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:4
Enter the element to search: 10
10 is in 1 position
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:5
Bye..!