Write A Program For Stack Operation Using Arrays.: Data Structures Using C++
Write A Program For Stack Operation Using Arrays.: Data Structures Using C++
Write A Program For Stack Operation Using Arrays.: Data Structures Using C++
USING C++
#include<iostream.h>
#include<conio.h>
int top=0;
int i;
class abc
{
int stack[5],item;
public:
void push();
void pop();
void dis();
};
void abc::push()
{
if(top==5)
{
cout<<"Stack is overflow";
}
else
{
cout<<"Enter item\n";
cin>>item;
top++;
stack[top]=item;
dis();
}
}
void abc::pop()
{
if(top==0)
cout<<"stack is underflow";
else
{
item=stack[top];
cout<<"deleted elemented is "<<item;
top--;
dis();
}
}
DATA STRUCTURES
USING C++
void abc::dis()
{
cout<<"elements in the stack are...";
for(i=0;i<=top;i++)
cout<<stack[i];
}
void main()
{
abc a;
clrscr();
int ch;
do
{
cout<<"\n 1.push()";
cout<<"\n 2.pop()";
cout<<"\n 3.exit()";
cout<<"Enter choice";
cin>>ch;
switch(ch)
{
case 1:
a.push();
break;
case 2:
a.pop();
}
}while(ch!=3);
}
DATA STRUCTURES
USING C++
OUTPUT:
1.push()
2.pop()
3.exit()
Enter choice 1
Enter item 1
Elements in the stack are…1
Enter choice 1
Enter item 2
Element in the stack are …1 2
Enter choice 3
DATA STRUCTURES
USING C++
#include<iostream.h>
#include<conio.h>
int front,rear,size=4;
int i;
class abc
{
int queue[5],item;
public:
abc()
{
Front=rear=-1;
void insert();
void del();
void dis();
};
void abc::insert()
{
if(rear>=size)
{
cout<<"Queue is overflow";
}
else
{
rear=rear+1;
cout<<"Enter item\n";
cin>>item;
queue[rear]=item;
dis();
}
}
void abc::del()
{
if(front==rear)
cout<<"Queue is underflow";
else
{
front=front+1;
item=queue[front];
cout<<"deleted elemented is "<<item;
DATA STRUCTURES
USING C++
}
}
void abc::dis()
{
cout<<"elements in the queue are..."<<endl;
for(i=front;i<=rear;i++)
cout<<queue[i];
}
void main()
{
abc a;
clrscr();
int ch;
do
{
cout<<"\n 1.insert()";
cout<<"\n 2.del()";
cout<<"\n 3.exit()";
cout<<"Enter choice";
cin>>ch;
switch(ch)
{
case 1:
a.insert();
break;
case 2:
a.del();
}
}while(ch!=3);
}
DATA STRUCTURES
USING C++
Output:
1.insert()
2.del()
3.exit()
Enter choice 1
Enter item 1
Elements in the Queue are…
1
Enter choice 2
Enter item 2
Elements in the Queue are…
12
Enter choice 3
DATA STRUCTURES
USING C++
#include<iostream.h>
#include<conio.h>
class node
{
public:
char d;
class node *ptr;
};
node *top,*p,*start,*bottom;
char ch;
class abc
{
public:
void create();
void dis();
void push();
void pop();
};
void abc::create()
{
start=new node;
cout<<”Enter data”;
cin>>start->d;
start->ptr=NULL;
bottom=start;
p=new node;
cin>>p->d;
p->ptr=start;
start=p;
cin>>ch;
while(ch!=’*’)
{
p=new node;
p->d=ch;
p->ptr=start;
start=p;
DATA STRUCTURES
USING C++
cin>>ch;
}
top=start;
dis();
}
void abc::dis()
{
p=top;
cout<<”top”;
while(p!=NULL)
{
cout<<”->”<<p->d;
p=p->ptr;
}
cout<<”->bottom”;
}
void abc::push()
{
p=new node;
cout<<”Enter data”;
cin>>p->d;
p-ptr=top;
top=p;
dis();
}
void abc::pop()
{
p=top;
top=top->ptr;
delete(p);
dis();
}
void main()
{
abc a;
clrscr();
int ch;
do
{
cout<<”\n 1.create()”;
cout<<”\n 2.push()”;
cout<<”\n 3.pop()”;
DATA STRUCTURES
USING C++
cout<<”\n 4.exit()”;
cout<<”Enter choice”;
cin>>ch;
switch(ch)
{
case 1:
a.create();
break;
case 2:
a.push();
break;
case 3:
a.pop();
}
}while(ch!=4);
}
DATA STRUCTURES
USING C++
Output:
1.create()
2.push()
3.pop()
4.exit()
Enter choice 1
Enter data
a
s
a
n
a
m
top->m->a->n->a->s->a->bottom
Enter choice 2
Enter data p
top->p->m->a->n->a->s->a->bottom
DATA STRUCTURES
USING C++
# include <iostream.h>
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
int ch;
struct node
{
int data;
node *link;
};
class queue
{
node *front, * rear;
public:
queue()
{
front=rear= NULL;
}
void insert(int);
void delet();
void disp();
};
p->data=e;
p->link=NULL;
if(rear==NULL)
front=rear=p;
else
rear->link = p;
rear=p;
}
{
int ch;
int ele;
clrscr();
queue s;
cout<<"1-INSERT\n 2- DELETE\n 3-DISPLAY \n 4- EXIT";
do
{
cout<<"\n Enter the choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n enter an element";
cin>>ele;
s.insert(ele);
break;
case 2: s.delet();
break;
case 3: s.disp();
break;
case 4: exit(0);
}
}while(ch<4);
getch();
}
Output:
1 – INSERT
2 – DELETE
3 – DISPLAY
4 – EXIT
Enter the choice :1
Enter an element : 12
Enter the choice :1
Enter an element : 13
Enter the choice :3
12
13
Enter the choice :2
12
DATA STRUCTURES
USING C++
#include<iostream.h>
#include<conio.h>
class node
{
public:
class node *rptr;
int d;
class node *lptr;
};
node *start,*p,*pre,*last;
int ch;
class abc
{
public:
void create();
void dis();
void ib();
void ie();
void db();
void de();
};
void abc::create()
{
start=new node;
cout<<”Enter data:”;
cin>>start->d;
start->lptr=NULL;
start->rptr=NULL;
p=new node;
DATA STRUCTURES
USING C++
cin>>p->d;
p->lptr=start;
p->rptr=NULL;
start->rptr==p;
cin>>ch;
while(ch!=999)
{
pre=p;
p=new node;
p->d=ch;
p->rptr=NULL;
pre->rptr=p;
pre->lptr=p;
cin>>ch;
}
last=p;
dis();
}
void abc::dis()
{
p=start;
cout<<”start”;
while(p!=NULL)
{
cout<<”<->”<<p->d;
p=p->rptr;
}
cout<<”<->last”;
}
void abc::ib()
{
p=new node;
cout<<”Enter data”;
cin>>p->d;
p->lptr=NULL;
p->rptr=start;
start->lptr=p;
start=p;
dis();
}
void abc::ie()
{
p=new node;
DATA STRUCTURES
USING C++
cout<<”Enter data”;
cin>>p->d;
p->rptr=NULL;
p->lptr=last;
last->rptr=p;
last=p;
dis();
}
void abc::db()
{
p=start;
start=start->ptr;
delete(p);
dis();
}
void abc::de()
{
if(start==NULL)
{
cout<<”empty”;
else
{
p=start;
while(p->rptr!=NULL)
{
pre=p;
p=p->rptr;
}
delete(p);
pre->rptr=NULL;
}
dis(); }
void main()
{
abc a;
clrscr();
int ch;
do
{
cout<<”\n 1.create()”;
cout<<”\n 2.ib()”;
cout<<”\n 3.ie()”;
cout<<”\n 4.db()”;
cout<<”\n 5.de()”;
DATA STRUCTURES
USING C++
cout<<”\n 6.exit()”;
cout<<”Enter choice”;
cin>>ch;
switch(ch)
{
case 1: a.create();
break;
case 2: a.ib();
break;
case 3: a.ie();
break;
case 4: a.db();
break;
case 5:
a.de();
}
}while(ch!=6);
}
Output:
1.create()
2.ib()
3.ie()
4.db()
5.de()
6.exit()
Enter choice 1
1 2 3 999
Start<->1<->2<->3<->last
DATA STRUCTURES
USING C++
#include<iostream.h>
#include<new.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int no;
struct node *left;
struct node *right;
};
class bst
{
private:
node *p,*root,*prev;
public:
bst();
void insert(int);
int search(int);
void del(int);
};
bst :: bst()
{
prev=NULL;
DATA STRUCTURES
USING C++
p=NULL;
root=NULL;
}
while(p!=NULL)
{
if(p->no==k)
{
found=1;
break;
}
else if(k<p->no)
{
if(p->left!=NULL)
{
prev=p;
p=p->left;
}
else
break;
}
else
{
if(p->right!=NULL)
{
prev=p;
p=p->right;
}
else
break;
}
DATA STRUCTURES
USING C++
}
}
return(found);
}
if(p==root)
delete root;
else
{
if(prev->left==p)
prev->left=NULL;
else
prev->right=NULL;
delete p;
}
}
else if(p->left!=NULL && p->right==NULL)
{
prev->left=p->left;
delete p;
}
else if(p->left==NULL && p->right!=NULL)
{
prev->right=p->right;
delete p;
}
else
{
temp=p->left;
if((temp->left==NULL) && (temp->right==NULL))
{
DATA STRUCTURES
USING C++
if(prev->left==p)
{
prev->left=temp;
temp->left=NULL;
temp->right=p->right;
}
else
{
prev->right=temp;
temp->left=NULL;
temp->right=p->right;
}
delete p;
}
else
{
while(temp->right!=NULL)
{
if(temp->no<temp->right->no)
temp=temp->right;
}
if(prev->left==p)
{
prev->left=temp;
temp->left=p->left;
temp->right=p->right;
}
else
{
prev->right=temp;
temp->left=p->left;
temp->right=p->right;
}
delete p;
}}
}}
p->right=NULL;
root=p;
}
else
{
node *temp;
temp=new node;
temp->no=n;
temp->left=NULL;
temp->right=NULL;
search(n);
if(p->no==n)
{
cout<<endl<<"There is already an element in tree";
return;
}
else if(n<p->no)
p->left=temp;
else
p->right=temp;
}}
void main()
{
int ch;
clrscr();
bst t;
do
{
cout<<"\n Menu";
cout<<"\n 1.search";
cout<<"\n 2.insert";
cout<<"\n 3.delete";
cout<<"\n 4.exit";
cout<<"\n Enter your choice [1..4]: ";
cin>>ch;
switch(ch)
{
case 1:
int k,p;
cout<<"\n Enter the element to be searched: ";
cin>>k;
p=t.search(k);
if(p==1)
DATA STRUCTURES
USING C++
OUTPUT
Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 2
Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 2
Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 2
Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 2
Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 1
Menu
1.search
2.insert
3.delete
DATA STRUCTURES
USING C++
4.exit
Enter your choice [1..4]: 3
Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 1
Enter the element to be searched: 20
Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 4
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *left;
class node *right;
int data;
};
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
DATA STRUCTURES
USING C++
while(p!=NULL)
{
cout <<p->data << " ";
if(p->right!=NULL)
{
stk[top]=p->right->data;
top++;
}
p=p->left;
}
};
void main()
{
tree t1;
int ch,n,i;
while(1)
{
cout <<"\n 1.INSERT \n 2.DISPLAY \n 3.PREORDER TRAVERSE \n
4.EXIT";
cout<<"\n Enter your choice [1..4]: ";
cin >> ch;
switch(ch)
{
case 1: cout <<" Enter no of elements to insert:";
cout<<" \n Enter the elements: \t";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.preorder(t1.root); break;
case 4: exit(1);
}
}}
Output:
1.INSERT
2.DISPLAY
3.PREORDER TRAVERSE
4.EXIT
Enter your choice [1..4]: 1
Enter no of elements to insert:
Enter the elements: 7
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY
3.PREORDER TRAVERSE
DATA STRUCTURES
USING C++
4.EXIT
1.INSERT
2.DISPLAY
3.PREORDER TRAVERSE
4.EXIT
1.INSERT
2.DISPLAY
3.PREORDER TRAVERSE
4.EXIT
Enter your choice [1..4]: 4
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *left;
class node *right;
int data;
};
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
DATA STRUCTURES
USING C++
} }
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
void main()
{
tree t1;
int ch,n,i;
while(1)
{
cout<<"\n 1.INSERT \n 2.DISPLAY \n 3.INORDER
TRAVERSE \n 4.EXIT";
cout<<"\n Enter your choice [1..4]: ";
cin >> ch;
switch(ch)
{
case 1: cout <<"\n Enter no of elements to insert: ";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.inorder(t1.root); break;
case 4: exit(1);
}
}
}
OUTPUT
1.INSERT
2.DISPLAY
3.INORDER TRAVERSE
4.EXIT
Enter your choice [1..4]: 1
Enter no of elements to insert: 7
5 24 36 11 44 2 21
1.INSERT
DATA STRUCTURES
USING C++
2.DISPLAY
3.INORDER TRAVERSE
4.EXIT
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *left;
class node *right;
int data;
DATA STRUCTURES
USING C++
};
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
void display(node *temp)
{
if(temp==NULL)
return ;
display(temp->left);
cout<<temp->data << " ";
display(temp->right);
}
void postorder( node *root)
{
node *p;
p=root;
top=0;
while(1)
{
while(p!=NULL)
{
stk[top]=p->data;
top++;
if(p->right!=NULL)
stk[top++]=-p->right->data;
p=p->left;
}
while(stk[top-1] > 0 || top==0)
{
if(top==0) return;
cout << stk[top-1] <<" ";
p=pop(root);
}
if(stk[top-1]<0)
{
stk[top-1]=-stk[top-1];
p=pop(root);
} }
}
DATA STRUCTURES
USING C++
1.INSERT
2.DISPLAY
DATA STRUCTURES
USING C++
3.POSTORDER TRAVERSE
4.EXIT
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY
3.POSTORDER TRAVERSE
4.EXIT
1.INSERT
2.DISPLAY
3.POSTORDER TRAVERSE
4.EXIT
1.INSERT
2.DISPLAY
3.POSTORDER TRAVERSE
4.EXIT
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
void main()
{
DATA STRUCTURES
USING C++
clrscr();
int m;
cout <<"\n Enter no of Vertices: ";
cin >> n;
cout <<"\n Enter no of Edges: ";
cin >> m;
cout <<"\n EDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
Output:
Enter no of Vertices: 9
Enter no of Edges: 9
EDGES
12
23
15
DATA STRUCTURES
USING C++
47
78
89
26
14
57
Enter Initial Vertex: 1
Visitied vertices
12 4 5 3 6 7 8 9
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
DATA STRUCTURES
USING C++
void main()
{
int m;
clrscr();
cout <<"\n Enter no of Vertices: ";
cin >> n;
cout <<"\n Enter no of Edges: ";
cin >> m;
cout <<"\n EDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
Output:
Enter no of Vertices: 9
Enter no of Edges: 9
EDGES
12
DATA STRUCTURES
USING C++
23
15
14
47
78
89
26
57
Enter Initial Vertex: 1
ORDER OF VISITED VERTICES
123647895
#include<iostream.h>
#include<conio.h>
DATA STRUCTURES
USING C++
int a[20],i,n,b[20];
void main()
{
clrscr();
cout <<"\n Enter no of Elements: ";
cin >> n;
cout <<"\n Enter the Elements: ";
for(i=0;i<n;i++)
cin >> a[i];
mergesort(a,0,n-1);
cout <<"\n Numbers after Sorting are... \n";
for(i=0;i<n;i++)
cout << a[i] << " ";
getch();
}
b[i]=a[j++];
i++;
}
if( h > mid)
for(k=j;k<=high;k++)
b[i++]=a[k];
else
for(k=h;k<=mid;k++)
b[i++]=a[k];
cout <<"\n";
for(k=low;k<=high;k++)
{
a[k]=b[k];
cout << a[k] <<" ";
}
Output:
DATA STRUCTURES
USING C++
Enter no of Elements: 8
Enter the Elements: 12 5 61 60 50 1 70 81
5 12
60 61
5 12 60 61
1 50
70 81
1 50 70 81
1 5 12 50 60 61 70 81
Numbers after Sorting are...
1 5 12 50 60 61 70 81
#include <iostream.h>
#include <conio.h>
int heapSize;
//int heapsize=0;
int parent(int i)
{
if(i==1)
return 0;
if(i%2==0)
return ( (i / 2)-1);
else
return ( (i / 2));
}
int left(int i)
{
return (2 * i) + 1;
}
int right(int i)
{
return (2 * i) + 2;
}
else
{
great = i;
}
if ( (a[r] > a[great]) && (r < nn))
{
great = r;
}
if (great != i)
{
int temp = a[i];
a[i] = a[great];
a[great] = temp;
heapify(a, great, nn);
}
}
}
DATA STRUCTURES
USING C++
void main()
{
clrscr();
int n=0;
int arr[10];
cout<<"\n Enter size of input list\n";
cin>>n;
heapSize=n;
for(int i=0;i<n;i++)
{
cout<<"Enter"<<i+1<<" st/nd/rd/th element\n";
cin>>arr[i];
}
for(int j=n+1;i<=10;i++)
arr[j]=0;
cout<<"Heapsort...........\n";
HeapSort(arr,n);
print(arr,n);
getch();
}
DATA STRUCTURES
USING C++
Output:
Heapsort...........
56-34-12-98-76-
56-98-12-34-76-
98-76-12-34-56-
12-34-56-76-98-
DATA STRUCTURES
USING C++
#include <string.h>
#include <iostream.h>
#include "avl_tree.h"
struct node
{
// Child pointers.
node *gt, *lt;
char *name;
char *value;
};
if (!n)
{
n = new node;
n->name = new char [strlen(name) + 2];
strcpy(n->name + 1, name);
tree.insert(n);
}
else
// Delete current value.
delete [] n->value;
if (value)
{
if (strlen(value) == 0)
value = 0;
}
if (value)
{
n->value = new char [strlen(value) + 1];
strcpy(n->value, value);
}
else
{
tree.remove(name);
delete [] n->name;
delete n;
}
}
DATA STRUCTURES
USING C++
void dump(void)
{
tree_t::iter it;
node *n;
it.start_iter_least(tree);
// Clear environment.
void clear(void)
{
tree_t::iter it;
node *n;
it.start_iter_least(tree);
tree.purge();
}
};
int main(void)
{
env e;
DATA STRUCTURES
USING C++
e.clear();
e.dump();
return(0);
}
DATA STRUCTURES
USING C++
Write a C++ program to implement all the functions of a dictionary (ADT) using
hashing.
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
struct EnrollRec {
long key;
int course[9];
int link;
};
int fd;
int getcommand(void);
void docommand(int);
void newrec(void);
void show(void);
void add();
printf("\n");
}
int getnode()
{
struct EnrollRec header, firstrecord;
int available;
readrecord(HEADER, &header);
if(header.link == 0)
return 0;
available = header.link;
readrecord(available, &firstrecord);
header.link = firstrecord.link;
header.course[8]--;
writerecord(HEADER, &header);
return available;
}
writerecord(HEADER, &header);
}
if(argc < 2)
{
fprintf(stderr, "Filename argument required.\n");
exit(-1);
}
fd = open(argv[1], O_RDWR | O_BINARY);
if(fd < 0)
{
perror(argv[1]);
exit(-1);
}
while(1)
docommand(getcommand());
}
int getcommand()
{
int command;
while( command = getchar(), isspace(command) );
while(getchar() != '\n');
return command;
}
long getkey()
{
long key;
is entered. */
printf("Enter the key: ");
while(scanf("%ld", &key) < 1 || key < 0 || key > 999999999)
{
/* Throw away invalid line. */
while(getchar() != '\n') ;
printf("Invalid key - try again: ");
}
return key;
}
int getcourse()
{
int coursenumber;
printf("Enter the course number: ");
while(scanf("%d", &coursenumber) < 1 || coursenumber < 1 || coursenumber > 9999)
{
while(getchar() != '\n') ;
printf("Invalid course number - try again: ");
}
return coursenumber;
}
if(thisrecord.key == key)
return address;
address = thisrecord.link;
if(address == 0)
DATA STRUCTURES
USING C++
return -1;
}
}
void newrec()
{
long key = getkey();
int found, home, address;
struct EnrollRec whatthe;
struct EnrollRec homerecord, foundrecord, newrecord = {0, {0,0,0,0,0,0,0,0,0}, 0};
found = search(key);
if(found >= 0)
{
readrecord(found, &foundrecord);
printf("Record found in file:\n");
display(foundrecord);
return;
}
newrecord.key = key;
home = hash(key);
readrecord(home, &homerecord);
/* If it is, put the new record there. */
if(homerecord.key == -1)
{
writerecord(home, &newrecord);
printf("Record written to address %d\n", home);
}
else
{
address = getnode();
if(address == 0)
{
printf("Out of free space in file.\n");
return;
}
newrecord.link = homerecord.link;
homerecord.link = address;
writerecord(address, &newrecord);
writerecord(home, &homerecord);
printf("Record written to address %d\n",address);
DATA STRUCTURES
USING C++
void show()
{
long key = getkey();
struct EnrollRec therecord;
int address = search(key);
if(address < 0)
printf("KEY NOT FOUND\n");
else
{
readrecord(address, &therecord);
display(therecord);
}
}
void add()
{
struct EnrollRec therecord;
int address, i, full, coursenumber;
long key = getkey();
address = search(key);
if(address == -1)
{
printf("KEY NOT FOUND: %ld\n", key);
return;
}
readrecord(address, &therecord);
full = 1;
for(i = 0; i < 9; i++)
{
if(therecord.course[i] == 0)
{
full = 0;
DATA STRUCTURES
USING C++
break;
}
}
if(full)
{
printf("CANNOT ADD ANY COURSES\n");
return;
}
coursenumber = getcourse();
for(i = 0; i < 9; i++)
{
if(therecord.course[i] == coursenumber)
{
printf("ALREADY ENROLLED IN THAT COURSE: %d\n", coursenumber);
return;
}
}
#include "match.h"
#include "lineb.h"
#include <stream.h>
void usage()
{
cerr << "usage: " << progname << "[-cr] word < file\n";
exit (1);
}
void
process(istream & in, string_match & target)
{
line_buffer para;
int was_eol = 1;
target.reset();
if (first_para) first_para = 0;
else cout.put('\n');
if (argc == 0) usage();
progname = argv[0];
while (argc > 2 && argv[1][0] == '-') {
argc--;
const char * cp = *++argv;
while (*++cp != '\0') switch (*cp) {
case 'c':
csense = 0;
break;
case 'r':
rest = 1;
break;
default:
usage();
}
}
if (argc != 2) usage();
string_match target(argv[1]);
suff[m - 1] = m;
g = m - 1;
for (i = m - 2; i >= 0; --i) {
if (i > g && suff[i + m - 1 - f] < i - g)
suff[i] = suff[i + m - 1 - f];
else {
if (i < g)
g = i;
f = i;
while (g >= 0 && x[g] == x[g + m - 1 - f])
--g;
suff[i] = f - g;
}
}
}
DATA STRUCTURES
USING C++
suffixes(x, m, suff);
/* Preprocessing */
preBmGs(x, m, bmGs);
preBmBc(x, m, bmBc);
/* Searching */
j = 0;
while (j <= n - m) {
for (i = m - 1; i >= 0 && x[i] == y[i + j]; --i);
if (i < 0) {
OUTPUT(j);
j += bmGs[0];
}
else
j += MAX(bmGs[i], bmBc[y[i + j]] - m + 1 + i);
}
}
DATA STRUCTURES
USING C++
INDEX
08 In-Order Traversal
09 Post-Order Traversal
12 Merge Sort
13 Heap Sort
14 AVL Tree
15 To implement all the functions of a dictionary (ADT)
using hashing
16 Knuth-Morris- Pratt pattern matching algorithm
17 Boyer-Moore Patten matching algorithm