Posts Tagged Data structure
Programs that implement Queue (its operations) using Pointers in C.
Posted by admin in C Examples on August 7th, 2009
#define true 1
#define false 0
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct q_point
{
int ele;
struct q_point* n;
};
struct q_point *f_ptr = NULL;
int e_que(void);
void add_ele(int);
int rem_ele(void);
void show_ele();
/*main function*/
void main()
{
int ele,choice,j;
while(1)
{
clrscr();
printf("\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n");
printf("==============================================");
printf("\n\t\t MENU\n");
printf("==============================================");
printf("\n\t[1] To insert an element");
printf("\n\t[2] To remove an element");
printf("\n\t[3] To display all the elements");
printf("\n\t[4] Exit");
printf("\n\n\tEnter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("\n\tElement to be inserted:");
scanf("%d",&ele);
add_ele(ele);
getch();
break;
}
case 2:
{
if(!e_que())
{
j=rem_ele();
printf("\n\t%d is removed from the queue",j);
getch();
}
else
{
printf("\n\tQueue is Empty.");
getch();
}
break;
}
case 3:
show_ele();
getch();
break;
case 4:
exit(1);
break;
default:
printf("\n\tInvalid choice.");
getch();
break;
}
}
}
/* Function to check if the queue is empty*/
int e_que(void)
{
if(f_ptr==NULL)
return true;
return false;
}
/* Function to add an element to the queue*/
void add_ele(int ele)
{
struct q_point *queue = (struct q_point*)malloc(sizeof(struct q_point));
queue->ele = ele;
queue->n = NULL;
if(f_ptr==NULL)
f_ptr = queue;
else
{
struct q_point* ptr;
ptr = f_ptr;
for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
ptr->n = queue;
}
}
/* Function to remove an element from the queue*/
int rem_ele()
{
struct q_point* queue=NULL;
if(e_que()==false)
{
int j = f_ptr->ele;
queue=f_ptr;
f_ptr = f_ptr->n;
free (queue);
return j;
}
else
{
printf("\n\tQueue is empty.");
return -9999;
}
}
/* Function to display the queue*/
void show_ele()
{
struct q_point *ptr=NULL;
ptr=f_ptr;
if(e_que())
{
printf("\n\tQUEUE is Empty.");
return;
}
else
{
printf("\n\tElements present in Queue are:\n\t");
while(ptr!=NULL)
{
printf("%d\t",ptr->ele);
ptr=ptr->n;
}
}
}
Programs that implement Queue (its operations) using Arrays.
Posted by admin in C Examples on August 7th, 2009
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#define size 10
#define true 1
#define false 0
struct q_arr
{
int f,r;
int num;
int a[size];
};
void init(struct q_arr* queue);
int e_que(struct q_arr* queue);
int f_que(struct q_arr* queue);
int add_ele(struct q_arr* queue,int);
int rem_ele(struct q_arr* queue);
void display_ele(struct q_arr* queue);
/*main function*/
void main()
{
int ele,k;
int ch;
struct q_arr *queue = (struct q_arr*)malloc(sizeof(struct q_arr));
init(queue);
while(1)
{
clrscr();
printf("\n\n****IMPLEMENTATION OF QUEUE USING ARRAYS****\n");
printf("============================================");
printf("\n\t\tMENU\n");
printf("============================================");
printf("\n\t[1] To insert an element");
printf("\n\t[2] To remove an element");
printf("\n\t[3] To display all the elements");
printf("\n\t[4] Exit");
printf("\n\n\t Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("\nElement to be inserted:");
scanf("%d",&ele);
add_ele(queue,ele);
break;
}
case 2:
{
if(!e_que(queue))
{
k=rem_ele(queue);
printf("\n%d element is removed\n",k);
getch();
}
else
{
printf("\tQueue is Empty. No element can be removed.");
getch();
}
break;
}
case 3:
{
display_ele(queue);
getch();
break;
}
case 4:
exit(0);
default:
printf("\tInvalid Choice.");
getch();
break;
}
}
}
/*end main*/
void init(struct q_arr* queue)
{
queue->f = 0;
queue->r = -1;
queue->num = 0;
}
/* Function to check is the queue is empty*/
int e_que(struct q_arr* queue)
{
if(queue->num==0)
return true;
return false;
}
/* Function to check if the queue is full*/
int f_que(struct q_arr* queue)
{
if(queue->num == size)
return true;
return false;
}
/* Function to add an element to the queue*/
int add_ele(struct q_arr* queue,int j)
{
if(f_que(queue))
return false;
if(queue->r == size - 1)
queue->r = -1;
queue->a[++queue->r] = j;
queue->num++;
return true;
}
/* Function to remove an element of the queue*/
int rem_ele(struct q_arr* queue)
{
int j;
if(e_que(queue))
return -9999;
j = queue->a[queue->f++];
if(queue->f == size)
queue->f = 0;
queue->num--;
return j;
}
/* Function to display the queue*/
void display_ele(struct q_arr* queue)
{
int j;
if(e_que(queue))
{
printf("Queue is Empty. No records to display.");
return;
}
printf("\nElements present in the Queue are: ");
for(j=queue->f;j<=queue->r;j++)
printf("%d\t",queue->a[j]);
printf("\n");
}
Function for deleting an item from linked list.
Posted by admin in C Examples on August 7th, 2009
node *delete(node *head)
{
node *find(node *p, int a);
int key; /* item to be deleted */
node *n1; /* pointer to node preceding key node */
node *p; /* temporary pointer */
printf("\n What is the item (number) to be deleted?");
scanf("%d", &key);
if(head->number == key) /* first node to be deleted) */
{
p = head->next; /* pointer to 2nd node in list */
free(head); /* release space of key node */
head = p; /* make head to point to 1st node */
}
else
{
n1 = find(head, key);
if(n1 == NULL)
printf("\n key not found \n");
else /* delete key node */
{
p = n1->next->next; /* pointer to the node
following the keynode */
free(n1->next); /* free key node */
n1->next = p; /* establish link */
}
}
return(head);
}
/* USE FUNCTION find() HERE */
Program to create linked list and basic operations in C++.
Posted by admin in Uncategorized on July 5th, 2009
#include<iostream>
using namespace std;
class Polynomial
{
struct node
{
int coeff,a,b,c;
struct node * next;
};
//definition of the structure node
char ans;
int flag;
public:
struct node * head,* tail,* current_ptr,* s,* start,* corpse;
Polynomial()
{
head=new node;
current_ptr=head;
start=current_ptr;
}
//constructor
void getdatafor_node(struct node *);
void insert_node();
Polynomial add_two_polynomials(Polynomial,Polynomial);
struct node * Previous(struct node * current_ptr)
{
struct node * temp=start;
while(temp->next != current_ptr)
{
temp=temp->next;
}
return temp;
}
//to find the previous node of the current pointer
};
//definition of class
void Polynomial::insert_node()
{
ans='y';
struct node * temp;
while(ans=='y')
{
temp=new node;
getdatafor_node(temp);
current_ptr->next=temp;
current_ptr=current_ptr->next;
cout<<"continue?(y/n):";
cin>>ans;
}
if(ans=='n')
{
start=start->next;
current_ptr->next=start;
}
}
//for inserting a node in the circular linked list
void Polynomial::getdatafor_node(struct node * temp)
{
cout<<"Enter the input for the node"<<endl;
cout<<"Enter the coefficient:";
cin>>temp->coeff;
cout<<endl;
cout<<"Enter the exponents of x,y,z:";
cin>>temp->a>>temp->b>>temp->c;
}
//to get the data from the user
Polynomial Polynomial::add_two_polynomials(Polynomial P,Polynomial Q)
{
current_ptr=current_ptr->next;
start=current_ptr;
Q.current_ptr=Q.current_ptr->next;
Q.start=Q.current_ptr;
//intialising
if(current_ptr->a==Q.current_ptr->a && current_ptr->b==Q.current_ptr->b && current_ptr->c==Q.current_ptr->c)
{
flag=0;
Q.current_ptr->coeff=Q.current_ptr->coeff+current_ptr->coeff;
}
else
{
flag=1;
Q.current_ptr=Q.current_ptr->next;
}
//to compare first nodes of both
while(Q.current_ptr!=Q.start)
{
if(current_ptr->a==Q.current_ptr->a && current_ptr->b==Q.current_ptr->b && current_ptr->c==Q.current_ptr->c)
{
flag=0;
Q.current_ptr->coeff=Q.current_ptr->coeff+current_ptr->coeff;
Q.current_ptr=Q.start;
}
else
{
flag=1;
Q.current_ptr=Q.current_ptr->next;
}
}
//compare first node of P with Q
if(flag==1)
{
Q.s=Q.Previous(Q.current_ptr);
cout<<(Q.Previous(Q.current_ptr))->coeff;
current_ptr=current_ptr->next;
Q.s->next=Previous(current_ptr);
(Previous(current_ptr))->next=Q.current_ptr;
Q.current_ptr=Q.current_ptr->next;
}
//inserting the node of P in Q
else
current_ptr=current_ptr->next;
while(current_ptr!=start)
{
if(current_ptr->a==Q.current_ptr->a && current_ptr->b==Q.current_ptr->b && current_ptr->c==Q.current_ptr->c)
{
flag=0;
Q.current_ptr->coeff=Q.current_ptr->coeff+current_ptr->coeff;
}
else
{
flag=1;
Q.current_ptr=Q.current_ptr->next;
}
while(Q.current_ptr!=Q.start)
{
if(current_ptr->a==Q.current_ptr->a && current_ptr->b==Q.current_ptr->b && current_ptr->c==Q.current_ptr->c)
{
flag=0;
Q.current_ptr->coeff=Q.current_ptr->coeff+current_ptr->coeff;
Q.current_ptr=Q.start;
}
else
{
flag=1;
Q.current_ptr=Q.current_ptr->next;
}
}
if(flag==1)
{
Q.s=Q.Previous(Q.current_ptr);
cout<<(Q.Previous(Q.current_ptr))->coeff;
current_ptr=current_ptr->next;
cout<<current_ptr->coeff;
Q.s->next=Previous(current_ptr);
(Previous(current_ptr))->next=Q.current_ptr;
cout<<Q.current_ptr->coeff;
Q.current_ptr=Q.current_ptr->next;
}
else
current_ptr=current_ptr->next;
}
cout<<Q.current_ptr->coeff;
cout<<current_ptr->coeff;
return Q;
}
main()
{
Polynomial P,Q,R;
P.insert_node();
Q.insert_node();
R=P.add_two_polynomials(P,Q);
R.current_ptr=R.start;
cout<<(R.Previous(R.current_ptr))->coeff;
while(R.current_ptr->next!=R.start)
{
cout<<"Print the coefficient of resulting polynomial:"<<R.current_ptr->coeff<<endl;
cout<<"Print the exponent of x:"<<R.current_ptr->a<<endl;
cout<<"Print the exponent of y:"<<R.current_ptr->b<<endl;
cout<<"Print the exponent of z:"<<R.current_ptr->c<<endl;
R.current_ptr=R.current_ptr->next;
}
cout<<"Print the coefficient of resulting polynomial:"<<R.current_ptr->coeff<<endl;
cout<<"Print the exponent of x:"<<R.current_ptr->a<<endl;
cout<<"Print the exponent of y:"<<R.current_ptr->b<<endl;
cout<<"Print the exponent of z:"<<R.current_ptr->c<<endl;
//to print the resulting polynomial
}
Program to generate a stack and basic operations of stack in C++.
Posted by admin in C++ Examples on July 5th, 2009
#include<iostream>
#include<cstring>
using namespace std;
class Stack
{
int top;
string arr[3];
public:
Stack()
{
top=0;
cout<<"Stack is instantiated"<<endl;
}
bool isfull()
{
return(top>3);
}
bool isempty()
{
return (top==-1);
}
void push(string str)
{
if(isfull())
{
cout<<"stack is full";
return;
}
else
arr[top]=str;
top++;
}
void pop()
{
top--;
if(isempty())
{
cout<<"cannot be popped";
return;
}
else
cout<<arr[top];
}
};
main()
{
Stack S;
int a,i=0;
string str;
char b='y';
while(b=='y')
{
cout<<"1.Push the names"<<endl<<"2.Pop the names"<<endl;
cin>>a;
if(a==1)
{
cout<<"Enter the name:";
getline(cin,str);
cin>>str;
S.push(str);
}
if(a==2)
{
S.pop();
}
cout<<"continue?(y/n)";
cin>>b;
}
}
program for pascal traingle in C++.
Posted by admin in C++ Examples on July 5th, 2009
#include<iostream.h>
class PascalTriangle
{
int i,j,k,r,m,n,p;
int temp,x;
public:
void getinput();
void getpascaltriangle();
int factorial(int);
};
void PascalTriangle::getinput()
{
cout<<"enter no of lines";
cin>>n;
if(n<=0)
cout<<"not possible";
}
void PascalTriangle::getpascaltriangle()
{
for(i=n;i>=1;i--)
{
for(j=1;j<=(i-1);j++)
{
cout<<" ";
}
r=n-i;
for(k=0;k<=r;k++)
{
x=factorial(r)/(factorial(k)*factorial(r-k));
cout<<x;
if(r==k)
cout<<endl;
else
cout<<" ";
}
}
}
int PascalTriangle::factorial(int m)
{
if(m!=0)
{
return m* factorial(m-1);
}
if(m==0)
return 1;
}
main()
{
PascalTriangle P;
P.getinput();
P.getpascaltriangle();
}
PROGRAM TO GENERATE 30000 RANDOM NUMBERS AND WRITE TO FILE RECORDS.TXT IN SORTED ORDER (USED INORDER TRAVERSAL FOR THIS).
Posted by admin in C++ Examples on June 14th, 2009
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int total=0;
fstream file;
struct bintree
{
int data;
bintree *left,*right;
};
void inorder(bintree *node)
{
if(node->left!=NULL)
inorder(node->left);
file<<" "<<node->data;
if(node->right!=NULL)
inorder(node->right);
}
void makenode(bintree **garb,int num)
{
(*garb)->data=num;
(*garb)->left=NULL;
(*garb)->right=NULL;
}
void makebintree(bintree *garb,bintree **base)
{
if((*base)==NULL)
{
*base=garb;
total++;
return;
}
else if(garb->data==(*base)->data)
{
return;
}
else if(garb->data<(*base)->data)
makebintree(garb,&((*base)->left));
else
makebintree(garb,&((*base)->right));
}
void dump(bintree **node)
{
if(*node==NULL)
return;
if((*node)->left==NULL&&(*node)->right==NULL)
{
delete(*node);
*node=NULL;
}
else
{
dump(&((*node)->left));
dump(&((*node)->right));
dump(node);
}
}
void main()
{
bintree *garb,*root=NULL;
file.open("records.txt",ios::out);
while(total<30000)
{
garb=new bintree;
makenode(&garb,rand());
makebintree(garb,&root);
}
inorder(root);
dump(&root);
file.close();
}
PROGRAM TO GENERATE 20000 RANDOM KEYS AND WRITE TO FILE KEYS.TXT
Posted by admin in C++ Examples on June 14th, 2009
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int total=0;
fstream file;
struct bintree
{
int data;
bintree *left,*right;
};
void makenode(bintree **garb,int num)
{
(*garb)->data=num;
(*garb)->left=NULL;
(*garb)->right=NULL;
}
void makebintree(bintree *garb,bintree **base)
{
if((*base)==NULL)
{
*base=garb;
total++;
file<<garb->data<<" ";
return;
}
else if(garb->data==(*base)->data)
{
return;
}
else if(garb->data<(*base)->data)
makebintree(garb,&((*base)->left));
else
makebintree(garb,&((*base)->right));
}
void dump(bintree **node)
{
if(*node==NULL)
return;
if((*node)->left==NULL&&(*node)->right==NULL)
{
delete(*node);
*node=NULL;
}
else
{
dump(&((*node)->left));
dump(&((*node)->right));
dump(node);
}
}
void main()
{
bintree *garb,*root=NULL;
file.open("keys.txt",ios::out);
while(total<20000)
{
garb=new bintree;
makenode(&garb,rand());
makebintree(garb,&root);
}
dump(&root);
file.close();
}


Recent Comments