www.4cnotes.info

visit xdeem.com also

4cnotes.info

4cnotes.info for Free College Notes, Placement trainng

visit www.xdeem.com also

Thanks for ure visit

Thanks for visiting our site

visit xdeem.com also.

4cnotes.info

Thank u

Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Dictionary Concepts in C++


#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
class dic
{
public:
void di(char *a)
{
FILE *f;
f=fopen("FIL.TXT","r");
while(!feof(f))
{
if(fgetc(f)=='\n')
{
char te[20];
fscanf(f,"%s",te);
if(strcmp(te,a)==0)
{
char ch=fgetc(f);
while(ch!='\n')
{
cout<<ch;
ch=fgetc(f);
}
return;
}
}
}
cout<<"Not Found";
}};
void main()
{
dic d;
char g[30];
cout<<"Enter the word:";
cin>>g;
d.di(g);
getch();
}

Quick Sort


#include<iostream.h>
class quick
{
int i,j,n,a[10];
public:
void getdata()
{
cout<<"\n*****Quick Sort*****\n";
cout<<"\n--------------------\n";
cout<<"\nEnter the no.of elements:";
cin>>n;
cout<<"\nEnter the elements:";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
}
void sort()
{
qsort(1,n);
}
void qsort(int low,int hi)
{
int i,j,temp;
if (low<=hi)
{
i=low;
j=hi+1;
do
{
do
i++;
while ((a[i]<=a[low])&&(i!=hi));
do
j--;
while (a[j]>=a[low] &&(j!=low));
if (i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
break;
}
while (i<=j);
temp=a[low];
a[low]=a[j];
a[j]=temp;
display();
qsort(low,j-1);
qsort(j+1,hi);
}
}
void display()
{
cout<<"\nThe elements are:";
for (i=1;i<=n;i++)
cout<<a[i]<<"  ";
}
};
main()
{
quick b1;
b1.getdata();
b1.sort();
b1.display();
}

Shell Sort


#include<iostream.h>
class shell
{
int a[100],size;
public:
void get()
{
int i;
cout<<"\nEnter the size of array :";
cin>>size;
cout<<"\nEnter the elements :";
for(i=0;i<size;i++)
cin>>a[i];
}
void sort()
{
int i,j;
int inc=size/2;
do
{
for(i=0;i<inc;i++)
inst(inc,i);
show();
inc=inc/2;
}
while(inc>0);
}
void show()
{
int i;
for(i=0;i<size;i++)
cout<<" "<<a[i];
cout<<endl;
}
void inst(int inc,int st)
{
int i,j,t;
for(i=st+inc;i<size;i=i+inc)
{
for(j=i;j>=inc;j=j-inc)
{
if(a[j]>=a[j-inc])
break;
{
t=a[j];
a[j]=a[j-inc];
a[j-inc]=t;
}}}}};
main()
{
shell s;
s.get();
s.sort();
}

Insertion Sort


#include<iostream.h>
main()
{
int a[20],i,t,j,n,k,c;
cout<<"enter the no.of.elements:\n";
cin>>n;
cout<<"Enter the elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
{
j=i+1;
t=a[j];
while(j!=0&&t<a[j-1])
{
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
cout<<"iteration:"<<++c;
for(k=0;k<n;k++)
cout<<"  "<<a[k];
cout<<"\n";
j--;
}}
cout<<"\n the sorted elements are:";
for(i=0;i<n;i++)
cout<<a[i]<<"\n\n ";

Tree Concepts in C++


#include<iostream.h>
class node
{
node *lchild;
node *rchild;
char data;
public:
node()
{
lchild=rchild=NULL;
data='0';
}
void getdata()
{
cout<<"\nEnter the data: ";
cin>>data;
}
friend class tree;
};
class tree
{
node *root;
public:
tree()
{
root=new node();
cout<<"\nCREATE BINARY TREE \n";
cout<<"\n================== \n";
create(root);
}
void create(node *nnode)
{
char ch;
nnode->getdata();
cout<<"\tIs there a left child(y/n)"<<nnode->data<<"\t";
cin>>ch;
if(ch=='Y'||ch=='y')
{
nnode->lchild=new node;
create(nnode->lchild);
}
cout<<"\tIs there a right child(y/n)"<<nnode->data<<"\t";
cin>>ch;
if(ch=='Y'||ch=='y')
{
nnode->rchild=new node;
create(nnode->rchild);
}
}
void inorder(node *node)
{
if(node!=NULL)
{
inorder(node->lchild);
cout<<node->data;
inorder(node->rchild);
}
}
void preorder(node *node)
{
if(node!=NULL)
{
cout<<node->data;
preorder(node->lchild);
preorder(node->rchild);
}
}
void postorder(node *node)
{
if(node!=NULL)
{
preorder(node->lchild);
preorder(node->rchild);
cout<<node->data;
}
}
void display(int order)
{
if(order==1)
{
cout<<"\n\t INORDER TRAVESAL\n";
cout<<"-------------------------------\n\n\t\t";
inorder(root);
}
else if(order==2)
{
cout<<"\n\tPreorder traversal\n";
preorder(root);
}
else if(order==3)
{
cout<<"\n\tPostorder traversal\n";
postorder(root);
}
}
};
main()
{

int choice;
tree t;
do
{
cout<<"\n\n\n1.INORDER TRAVESAL\n";
cout<<"\n2.PREORDER TRAVESAL\n";
cout<<"\n3.POSTORDER TRAVESAL\n";
cout<<"\n4.EXIT\n";
cout<<"\nEnter your choice  ::";
cin>>choice;
if(choice>0 && choice<5)
{
t.display(choice);
}
else
{
cout<<"\nInvalid choice";
cout<<"\nPress any key to continue";

}
}while(choice>0 && choice<4);
}

Doubly Linked List in C++


#include<iostream.h>
struct node
{
int data;
struct node *pre,*nxt;
};
typedef struct node db;
db *head=NULL;
void addfront();
void addafter();
void addbefore();
void display();
void delnode();
main()
{
int ch;
char c;
do
{
cout<<"\nDOUBLY LINKED LIST\n";
cout<<"The choices are:\n1.Insert at the beginning\n2.Insert after\n3.Insert before\n4.Display\n5.Delete a node\n6.Exit";
cout<<"\nEnter a choice:";
cin>>ch;
switch(ch)
{
case 1:
addfront();
break;

case 2:
addafter();
break;

case 3:
addbefore();
break;

case 4:
display();
break;

case 5:
delnode();
break;
case 6:
exit(0);

default:
cout<<"Invalid choice\n";
break;
}
cout<<"\nDo you want to continue?:";
cin>>c;
}
while(c=='y'||c=='Y');
}

void addfront()
{
db *np;
np=new node;
cout<<"Enter the data:";
cin>>np->data;
if(head==NULL)
{
np->nxt=head;
np->pre=NULL;
head=np;
}
else
{
np->nxt=head;
np->pre=NULL;
(np->nxt)->pre=np;
head=np;
}
}
void addafter()
{
db *np,*i;
int loc;
cout<<"\nAfter which item you want to insert?:";
cin>>loc;
np=new node;
cout<<"\nEnter the data:";
cin>>np->data;
for(i=head;i->data!=loc;i=i->nxt);
np->nxt=i->nxt;
np->pre=i;
if(i->nxt!=NULL)
(i->nxt)->pre=np;
i->nxt=np;
}


void addbefore()
{
db *np,*i;
int loc;
cout<<"\nBefore which item you want to insert?:";
cin>>loc;
np=new node;
cout<<"\nEnter the data:";
cin>>np->data;
for(i=head;i->data!=loc;i=i->nxt);
if(i==head)
{
np->nxt=i;
i->pre=np;
head=np;
np->pre=NULL;
}
else
{
np->nxt=i;
np->pre=i->pre;
(i->pre)->nxt=np;
i->pre=np;
}
}


void display()
{
db *i;
cout<<"The items in the list are:\n";
for(i=head;i->nxt!=NULL;i=i->nxt)
cout<<" "<<i->data;
cout<<" "<<i->data;
}

void delnode()
{
db *i;
int loc;
if(head==NULL)
cout<<"\nThe list is empty";
else
{
cout<<"\nEnter the item to be deleted:";
cin>>loc;
for(i=head;i->data!=loc;i=i->nxt);
cout<<"The deleted item is "<<i->data;
if(i==head)
{
head=i->nxt;
head->pre=NULL;
}
else
if(i->nxt==NULL)
{
(i->pre)->nxt=NULL;
}
else
{
(i->pre)->nxt=i->nxt;
(i->nxt)->pre=i->pre;
}
}
}
                                                               

Stack


PROGRAM:
#include<iostream.h>
void push();
void pop();
int s[5],top=0;
main()
{
int ch;
char c;
do
{
cout<<"\n1.PUSH";
cout<<"\n2.POP";
cout<<"\nEnter the choice:\n";
cin>>ch;
switch(ch)
{
 case 1:
   push();
   break;
 case 2:
   pop();
   break;

 default:
   cout<<"\nEnter the valid choice";
}
cout<<"\nDo you want to continue?";
cin>>c;
}while(c=='y'||c=='Y');
}
void push()
{
if(top==5)
cout<<"\nStack is full";
else
cout<<"\nEnter the element to be pushed:";
cin>>s[top];
top++;
}
void pop()
{
top=top-1;
if(top<0)
cout<<"\nstack is empty";
else
cout<<"\nPop out elements"<<s[top];
}

SELECTION SORT



AIM:
            To write a program to perform selection sort
ALGORITHM:
            STEP 1:  Start the program
            STEP 2:  Define a class select and declare data members
            STEP 3:  In member function set elements get the elements and store them in the array
            STEP 4:  In member function sort assume first element as minimum element.Find the  
                            Minimum element in the array and swap it.
            STEP 5:  Repeat the step 4 for remaining element
            STEP 6:  In member function print elements print the select element
            STEP 7 : Stop the program.

Queue



AIM:
            To implement queue in the list.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the necessary variables.
Step 3: Declare the structure node.
Step 4: Display the choices as
                  1. Addition
               2. Deletion
Step 5: Get the choice as input.
Step 6: If the choice is 1, then add the data into the queue.
Step 7: If the choice is 2, then pop the data entered first from the queue.
Step 8: Stop the program.


PROGRAM:
#include<iostream.h>
struct node
{
int data;
struct node*ptr;
};
typedef struct node queue;
queue *addnode(queue *);
queue *delnode(queue *);
queue *head=NULL;
queue *rear=NULL;
int k=1;
main()
{
int ch,k=1;
char d;
queue *q;
do
{
cout<<"\nQUEUE IMPLEMENTATION";
cout<<"\nThe Choices are:\n1.Addition\n2.Deletion\n";
cout<<"Enter a choice:";
cin>>ch;
switch(ch)
{
case 1:
if(k==1)
{
q=(queue*)malloc(sizeof(queue));
head=rear=q;
cout<<"Enter the data:";
cin>>q->data;
q->ptr=NULL;
k=0;
}
else if(k==0)
{
rear=addnode(rear);
}
break;

case 2:
if(k==1)
cout<<"Queue is empty";
else if(head->ptr==NULL)
{
k=1;
cout<<"The deleted data is "<<head->data;
head=NULL;
rear=NULL;
}
else
head=delnode(head);
break;
}
cout<<"\nDo you want to continue?:";
cin>>d;
}
while(d=='y'||d=='Y');
}
}

queue *addnode(queue *p)
{
queue *t;
t=new node;
cout<<"Enter the data:";
cin>>t->data;
p->ptr=t;
t->ptr=NULL;
return t;
}

queue *delnode(queue *b)
{
int c;
c=b->data;
cout<<"The deleted data is "<<c;
head=b->ptr;
return head;
}



Linear Search


Program:
#include<stdio.h>
main()
{
int a[10],i,n,s,item;
printf("Searching for a number\n");
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the value to be searched\n");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(item==a[i])
{
s=1;
break;
}
}
if(s==1)
printf("Value is found");
else
printf("Value is not found");
}


Output:

Searching for a number
Enter number of elements:
3
Enter the elements:
5
9
2
Enter the value to be searched:
9
Value is found

BINARY SEARCH



AIM:

            To sort the given array of elements using binary search also to find the position of the item in the array
ALGORITHM:
Step 1: start the process
Step 2: Declare all the variables
Step 3: Get the size of the array
Step 4: Get all the values in an array variable
Step 5: use sort function to perform sort
Step 6: Search(), this function is used to search a particular element
Step 7: If the element found, display its memory location else
             display not found
Step 8: After sorting print all the sorted elements
Step 9: Stop the process