if u have any contents to share with others pls mail it to mycollectionsmanoj@gmail.com
Sunday, November 30, 2008
unix e-notes
unix e-notes chap 1
Sunday, November 16, 2008
recursion-1
a] Searching an element on a given list of integers using binary search method.
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
int binary_search(int,int a[],int,int); /*FUNCTION PROTOTYPE*/
void main() /*MAIN FUNCTION*/
{
int item,i,n,pos; /*VARIABLE DECLARATION*/
int *a;
clrscr();
printf("\n Enter the no. of elements:\n");
scanf("%d",&n);
a=(int *)malloc(sizeof(int) *n);
printf("\nEnter the elements:\n");
for(i=0;i< pos="binary_search(item,a,0,n-1);" pos="="">high)
return-1;
mid=(low+high)/2;
if(item==a[mid])
return mid;
else
{
if(item< style="font-weight: bold;">
b] Solving the tower of Hanoi problem.

#include"stdio.h"
#include"conio.h"
void towers(int,char,char,char);
int count=0;
main()
{
int n;
clrscr();
printf("\n Enter the no of disks:");
scanf("%d",&n);
towers(n,'a','c','b');
printf("\nTotal no of moves required is %d",count);
getch();
}
void towers(int n,char frompeg,char topeg,char temppeg)
{
if(n==1)
{
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
count++;
return;
}
towers(n-1,frompeg,temppeg,topeg);
count++;
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
towers(n-1,temppeg,topeg,frompeg);
}
binary tree
Write a C program:
a] To construct a binary search tree of integers.
b] To traverse the tree using all methods i.e., inorder , preorder and postorder.
c] To display the elements in the tree.
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
#include"alloc.h"
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *Node;
/*FUNCTION PROTOTYPE DECLARATION*/
Node insert(int,Node);
void preorder(Node);
void inorder(Node);
void postorder(Node);
void main()
{
Node root=NULL; /*VARIBLE DECLARATION*/
int choice,item,ch;
clrscr();
do{ /*DO-WHILE LOOP*/
printf("1:Insert item\n");
printf("2:Preorder\n");
printf("3:Inorder\n");
printf("4:Postorder\n");
printf("5:Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to be inserted\n");
scanf("%d",&item);
root=insert(item,root);
break;
case 2:if(root==NULL)
printf("Tree is Empty\n");
else
{
printf("Preorder Traversal\n");
preorder(root);
printf("\n");
}
break;
case 3:if(root==NULL)
printf("Tree is Empty\n");
else
{
printf("Inorder Traversal\n");
inorder(root);
printf("\n");
}
break;
case 4:if(root==NULL)
printf("Tree is Empty\n");
else
{
printf("Postorder Traversal\n");
postorder(root);
printf("\n");
}
break;
case 5:exit(1);
break;
default:printf("Invalid Choice\n");
} /*END OF SWITCH STATEMENT*/
printf("Do you want to continue(y/n)?:");
ch=getche();
}
while(ch!='n'); /*END OF DO-WHILE LOOP*/
getch();
}
/*FUNCTION TO ALLOCATE MEMORY DYNAMICALLY*/
Node getnode()
{
Node temp;
temp=(Node)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Out of Memory\n");
exit(1);
}
return temp;
}
/*FUNCTION TO INSERT ELEMENTS INTO THE TREE*/
Node insert(int item,Node root)
{
Node temp,p,q;
temp=getnode();
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
return temp;
p=q=root;
while(item!=p->info && q!=NULL)
{
p=q;
if(item< p->info)
q=p->llink;
else
{
q=p->rlink;
}
}
if(item==p->info)
printf("%d is Duplicate\n",item);
else
{
if(item< p->info)
p->llink=temp;
else
p->rlink=temp;
}
return root;
}
/*FUNCTION TO TRAVERSE THE TREE USING PREORDER*/
void preorder(Node root)
{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->llink);
preorder(root->rlink);
}
}
/*FUNCTION TO TRAVERSE THE TREE USING INORDER*/
void inorder(Node root)
{
if(root!=NULL)
{
inorder(root->llink);
printf("%d\t",root->info);
inorder(root->rlink);
}
}
/*FUNCTION TO TRAVERSE THE TREE USING POSTORDER*/
void postorder(Node root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->info);
}
}
doubly linked list
a] Create a doubly linked list by adding each node at the front.
b] Insert new nodes to the left of the node whose key value is read as an input
c] Delete the node of the given data, if it is found, otherwise display appropriate message.
d] Display the contents of the file.
Program:
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
#include"alloc.h"
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *nod;
/*FUNCTION PROTOTYPE DECLARATION*/
nod getnode();
void freenode(nod);
nod insertf(int,nod);
nod insertat(int,nod);
nod del(int,nod);
void display(nod);
void main()
{
nod head; /*VARIABLE DECLARATION*/
int item,ch,choice;
clrscr();
head=getnode();
head->info=0;
head->rlink=head->llink=head;
do /*DO-WHILE LOOP*/
{
printf("1:Insert front\n");
printf("2:Insert to left of given node\n");
printf("3:Delete a node\n");
printf("4:Display\n");
printf("5:Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to insert\n");
scanf("%d",&item);
head=insertf(item,head);
break;
case 2:printf("Enter the key\n");
scanf("%d",&item);
head=insertat(item,head);
getch();
break;
case 3:printf("Enter the item to be deleted\n");
scanf("%d",&item);
head=del(item,head);
getch();
break;
case 4:display(head);
getch();
break;
} /*END OF SWITCH STATEMENT*/
}
while(choice!=5); /*END OF DO-WHILE LOOP*/
getch();
}
/*FUNCTION TO ALLOCATE MOEMORY DYNAMICALLY*/
nod getnode()
{
nod temp;
temp=(nod)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Memory not available\n");
exit(0);
}
return temp;
}
/*FUNCTION TO DEALLOCATE MEMORY OCCUPIED BY A NODE*/
void freenode(nod p)
{
free(p);
}
/*FUNCTION TO INSERT A NODE AT THE FRONT*/
nod insertf(int item,nod head)
{
nod temp;
nod cur;
temp=getnode();
temp->info=item;
cur=head->rlink;
head->rlink=temp;
temp->llink=head;
temp->rlink=cur;
cur->llink=temp;
return head;
}
/*FUNCTION TO INSERT A NEW NODE TO THE LEFT OF A NODE*/
nod insertat(int item,nod head)
{
nod temp,cur,prev;
if(head->rlink==head)
{
printf("List is empty\n");
return head;
}
cur=head->rlink;
while(cur!=head&&item!=cur->info)
cur=cur->rlink;
if(cur==head)
{
printf("Key not found\n");
return head;
}
prev=cur->llink;
temp=getnode();
printf("Enter the item to insert towards left of %d:",item);
scanf("%d",&temp->info);
prev->rlink=temp;
temp->llink=prev;
temp->rlink=cur;
cur->llink=temp;
return head;
}
/*FUNCTION TO DELETE A NODE OF GIVEN DATA*/
nod del(int item,nod head)
{
nod prev,cur,next;
if(head->rlink==head)
{
printf("List is Empty\n");
return head;
}
cur=head->rlink;
while(cur!=head&&item!=cur->info)
cur=cur->rlink;
if(cur==head)
{
printf("\nItem not found\n");
return head;
}
prev=cur->llink;
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
freenode(cur);
return head;
}
/*FUNCTION TO DISPLAY THE CONTENTS OF THE LIST*/
void display(nod head)
{
nod temp;
if(head->rlink==head)
{
printf("\n list is empty\n");
return;
}
temp=head->rlink;
printf("\nList Contents\n");
while(temp!=head)
{
printf("%d\t",temp->info);
temp=temp->rlink;
}
printf("\n");
return;
}
queue implementation in lists(lap prog 11)
a. Insert
b. Delete
c. Display
It must print appropriate messages for queue full and empty.
Program:
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/*/
#include"conio.h"
#include"alloc.h"
#define QSIZE 3 /*SYMBOLIC CONSTANTS*/
int count=0;
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int data;
struct node *next;
};
typedef struct node *Node;
/*FUNCTION PROTOTYPE DECLARATION*/
Node insert_rear(int,Node);
Node delete_front(Node);
Node getnode();
void freenode(Node);
void display(Node);
void main()
{
int item,ch,choice; /*VARIABLE DECLARATION*/
Node first=NULL;
clrscr();
do{ /*DO-WHILE LOOP*/
printf("\n1:INSERT, 2:DELETE, 3:DISPLAY, 4:EXIT\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to be inserted\n");
scanf("%d",&item);
first=insert_rear(item,first);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
case 4:exit(1);
break;
} /*END OF SWITCH STATEMENT*/
printf("Do you want to continue?(y/n):\n");
ch=getche();
}
while(ch!='n'); /*END OF DO-WHILE LOOP*/
getch();
}
/*FUNCTION TO ALLOCATE MEMORY DYNAMICALLY*/
Node getnode()
{
Node temp;
temp=(Node)malloc(sizeof(struct node));
return temp;
}
/*FUNCTION TO DEALLOCATE MEMORY OCCUPIED BY A NODE*/
void freenode(Node p)
{
free(p);
}
/*FUNCTION TO INSERT A NODE AT THE FRONT END*/
Node insert_rear(int item,Node first)
{
Node temp,cur;
temp=getnode();
if(count==QSIZE)
{
printf("Queue Full\n");
return first;
}
else
{
count++;
/*temp=getnode();*/
temp->data=item;
temp->next=NULL;
if(first==NULL)
return temp;
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=temp;
return first;
}
}
/*FUNCTION TO DELETE A NODE FROM THE END*/
Node delete_front(Node first)
{
Node temp;
if(first==NULL)
{
printf("Queue is Empty\n");
}
else
{
count--;
temp=first;
first=first->next;
printf("Deleted data is %d\n",temp->data);
freenode(temp);
}
return first;
}
/*FUNCTION TO DISPLAY THE CONTENTS OF THE QUEUE*/
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("Queue is Empty\n");
}
else
{
printf("Queue status:\n");
temp=first;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
}
Wednesday, November 12, 2008
Sunday, November 9, 2008
lab program 10
a.Push
b.Pop
c.Display
It must print appropriate messages for Stack overflow and empty.
#include"stdio.h" /* Including header files*/
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
#include"string.h"
struct node /* Structure template declaration*/
{
int data;
struct node *next;
};
typedef struct node *Node;
Node getnode(); /*function prototype*/
void freenode(Node);
Node insertf(int,Node);
Node delete_info(Node);
void display (Node);
int top=-1;
void main()
{
Node first=NULL;
int item,ch,choice;
clrscr();
do{
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.exit\n");
printf("\n enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the items to be inserted:");
scanf("%d",&item);
first=insertf(item,first);
break;
case 2:
first=delete_info(first); /*delete function call*/
break;
case 3:
display(first); /*display function call*/
break;
case 4:
exit(0);
break;
}
printf("\n do you want to continue...(y/n)");
ch=getche();
}while(ch!='n');
getch();
}
Node getnode() /*getnode function defination*/
{
Node temp;
temp=(Node)malloc(sizeof(struct node));/*creating node using malloc function*/
return temp;
}
void freenode(Node p)
{
free(p); /*removing a node using free function*/
}
Node insertf(int item,Node first) /*insertf function defination*/
{
Node temp; /* pointer temp to type Node*/
temp=getnode();
top++;
temp->data=item;
temp->next=first;
first=temp;
return(first);
}
Node delete_info(Node first)
{
Node temp;
if(first==NULL)
{
printf("\nstack overflow");
}
else
{
top--;
temp=first;
first=first->next;
printf("\n deleted data is %d",temp->data);
freenode(temp);
}
return first;
}
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("\nstack is empty");
}
else
{
printf("\n stack status");
temp=first;
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->next;
}
}
}
link lists lab program
a] The insertion operation:
i] At the front of the list
ii] At the back of the list
iii] At any position of the list
b] Deleting a node based on student ID. If the specified node is not present in the list, an error message to be displayed.
c] Searching a node based on student ID and update the information content. If the specified node is not present in the list, an error message to be displayed.
d] Displaying all nodes of a file.
#include"stdio.h" /* Including header files*/
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
#include"string.h"
struct node /* Structure template declaration*/
{
char name[20];
int id,sem;
struct node *next;
};
typedef struct node *Node;
Node getnode(); /*function prototype*/
void freenode(Node);
Node insertf(struct node,Node);
Node insertr(struct node,Node);
Node insert_at(struct node,int,Node);
Node delete_info (int,Node);
void search_up(int,Node);
void display (Node);
Node create(struct node *);
void main()
{
Node first=NULL;
struct node s;
int ch,tid,pos;
clrscr();
do /*do while statement*/
{
clrscr();
printf("\n1.Insert at front of list");
printf("\n2.Insert at back of list");
printf("\n3.Insert at any position of list");
printf("\n4.delete a student id");
printf("\n5.Search a Node");
printf("\n6.Display the contents\n7. exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch) /* switch statement begin*/
{
case 1:
create(&s); /*create function call*/
first=insertf(s,first); /*insertf function call*/
break;
case 2:
create(&s);
first=insertr(s,first); /*insertr function call*/
break;
case 3:
create(&s);
printf("\n Enter the position");
scanf("%d",&pos);
first=insert_at(s,pos,first); /*insert_at function call*/
break;
case 4:
printf("\n Enter the student id");
scanf("%d",&tid);
first=delete_info(tid,first); /*delete function call*/
getch();
break;
case 5:
printf("\n Enter the student id to be searched");
scanf("%d",&tid);
search_up(tid,first); /*search_up function call*/
getch();
break;
case 6:
printf("\n Student Information\n");
display(first); /*display function call*/
getch();
break;
case 7:
exit(0);
} /*end of switch statement*/
} /*end of do while loop*/
while(1);
} /*end of function main*/
Node insertf(struct node s,Node first) /*insertf function defination*/
{
Node temp; /* pointer temp to type Node*/
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL)
return(temp);
else
{
temp->next=first;
return(temp);
}
}
Node getnode() /*getnode function defination*/
{
Node temp;
temp=(Node)malloc(sizeof(struct node));/*creating node using malloc function*/
return temp;
}
Node insert_at(struct node s,int pos,Node first)
{
Node temp,prev,cur;
int count;
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL && pos==1)
{
return temp;
}
if(first==NULL)
{
printf("\n invalid position....");
getch();
return first;
}
if(pos==1)
{
temp->next=first;
return temp;
}
prev=NULL;
cur=first;
count=1;
while(cur!=NULL && pos!=count)
{
prev=cur;
cur=cur->next;
count++;
}
if(pos==count)
{
prev->next=temp;
temp->next=cur;
return first;
}
printf("\n invalid position");
getch();
return first;
}
void freenode(Node p)
{
free(p); /*removing a node using free function*/
}
Node insertr(struct node s,Node first) /*insertr function defination*/
{
Node temp,cur;
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL)
return(temp);
else
{
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=temp;
return first;
}
}
Node create(struct node *p) /*create node function defination*/
{
printf("\n Enter student name");
scanf("%s",p->name);
printf("\n Enter student id");
scanf("%d",&p->id);
printf("\n Enter the semester");
scanf("%d",&p->sem);
return;
}
Node delete_info(int id,Node first)
{
Node prev,cur;
if(first==NULL)
{
printf("\n list is empty");
getch();
return first;
}
if(id==first->id)
{
cur=first;
first=first->next;
freenode(cur);
printf("\n deletipn successful");
return first;
}
prev=NULL;
cur=first;
while(cur!=NULL&&id!=cur->id)
{
prev=cur;
cur=cur->next;
}
if(cur==NULL)
{
printf("\n student with given id was not found");
getch();
return first;
}
prev->next=cur->next;
freenode(cur);
printf("\n deletion successful");
return first;
}
void search_up(int id,Node first)
{
Node prev,cur;
int choice,ch;
if(first==NULL)
{
printf("\n list is empty");
getch();
return;
}
cur=first;
while(cur!=NULL&&id!=cur->id)
{
cur=cur->next;
}
if(cur==NULL)
{
printf("\n student with given id was not found");
getch();
}
else
{
printf("\nstudent found");
printf("\n wnt to update...(y/n)");
ch=getche();
if(ch=='y')
{
printf("\n update field...");
printf("\n1:Name");
printf("\n2:Id");
printf("\n3:Sem");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n enter the new name:");
scanf("%s",&cur->name);
break;
case 2:printf("\n enter the new ud:");
scanf("%d",&cur->id);
break;
case 3:printf("\n enter the new sem:");
scanf("%s",&cur->sem);
break;
defualt:printf("\n invalid choice");
}
}
}
}
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("\n list is empty");
return;
}
temp=first;
printf("%20s%10s%5s\n","student name","id","sem");
printf("________________________________________\n");
while(temp!=NULL)
{
printf("%20s",temp->name);
printf("%10d",temp->id);
printf("%5d",temp->sem);
temp=temp->next;
printf("\n");
}
}
Saturday, November 8, 2008
convertion of infix exp to postfix
Program:
#include"stdio.h"
#include"conio.h"
#define MAX 30 /*definition section*/
#define TRUE 1
#define FALSE 0
struct stack /*stack declaration*/
{
int top;
char items[MAX];
};
void postfix (char *,char *); /*function prototype declarations*/
char pop(struct stack *s);
void push (struct stack *s,char);
int empty(struct stack *);
char stacktop(struct stack *);
int prcd(char);
void main()
{
char infix[MAX],post[MAX];
clrscr();
printf("\n Enter the infix expression:");
scanf("%s",infix); /*reading the infix expression*/
postfix(infix,post);
printf("\n The equivalent postfix expression is %s",post);
getch();
}
void push(struct stack *s,char ele) /*push function definition*/
{
if(s->top==MAX-1)
{
printf("\n stack overflow");
exit(1);
}
else
s->items[++(s->top)]=ele;
}
char pop(struct stack *s) /*pop function definition*/
{
int x;
if(empty(s)) /*empty function call*/
{
printf("\n stack underflow");
getch();
exit(1);
}
else
{
x=s->items[s->top--];
return x;
}
}
int empty(struct stack *s) /*empty function definition*/
{
if(s->top==-1)
return(TRUE);
else
return(FALSE);
}
void postfix(char infix[],char post[]) /*postfix function definition*/
{
int infixpos=0,postpos=0;
struct stack stk;
char symb;
stk.top=-1;
push(&stk,'#');
while(infix[infixpos]!='\0')
{
symb=infix[infixpos];
switch(symb)
{
case'(':
push(&stk,symb);
break;
case')':
while(stacktop(&stk)!='(')
{
post[postpos++]=pop(&stk);
}
pop(&stk);
break;
case'$':
case'*':
case'/':
case'+':
case'-':
/* prcd function call */
while(prcd(stacktop(&stk))>=prcd(symb)) /*prcd function call*/
{
post[postpos++]=pop(&stk);
}
push(&stk,symb);
break;
default:post[postpos++]=symb;
}
infixpos++;
}
while(stacktop(&stk)!='#')
{
post[postpos++]=pop(&stk);
}
post[postpos]='\0';
}
int prcd(char symb) /*prcd function definition*/
{
int p;
switch(symb)
{
case'$':
p=3;
break;
case'/':
case'*':
p=2;
break;
case'+':
case'-':
p=1;
break;
case'(':
p=0;
break;
case'#':
p=-1;
break;
}
return p;
}
char stacktop(struct stack *ps) /*stacktop function definition*/
{
int x;
if(empty(ps))
{
printf("\n stack underflow");
getch();
exit(1);
}
else
{
x=ps->items[ps->top];
return x;
}
}
circular q another type display function
a. Insert
b. Delete
c. Display
Program:
#include"stdio.h"
#include"conio.h"
#define MAX 20 /*definition section*/
#define TRUE 1
#define FALSE 0
struct Queue /*queue declaration*/
{
int front;
int rear;
int items[MAX];
int count;
}cq;
void insert(struct Queue *cq, int ); /*function prototype declarations*/
void display(struct Queue *cq);
int remov(struct Queue *cq);
int empty(struct Queue *cq);
main()
{
int i,n;
int choice,x;
cq.rear=cq.front=MAX-1;
clrscr();
printf("\n1.INSERT\n2.DISPLAY\n3.REMOVE\n");
while(1) /*using while loop*/
{
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice) /*switch statement*/
{
case 1:
printf("\nenter the values of n:");
scanf("%d",&n);
printf("\n enter the items");
for(i=0;i< n;i++)
{
scanf("%d",&x);
insert(&cq,x); /*insert function call*/
}
printf(" the queue items are:\n");
display(&cq); /*display function call*/
break;
case 2:
display(&cq);
break;
case 3:
x=remov(&cq); /*remov function call*/
printf("\n the value of x=%d",x);
break;
default:
printf("\n Invalid Entry");
getch();
exit(1);
} /*end switch*/
} /*end while*/
getch();
}
void insert(struct Queue *cq,int y) /*insert function definition*/
{
if(cq->rear==MAX-1)
cq->rear=0;
else
cq->rear=cq->rear+1;
if(cq->rear==cq->front)
{
printf("Queue overflow");
getch();
exit(0);
}
cq->items[cq->rear]=y;
cq->count++;
}
int remov(struct Queue *cq) /*remov function definition*/
{
int x;
if(empty(cq)) /*empty function call*/
{
printf("\n queue underflow");
getch();
exit(1);
}
if(cq->front==MAX-1)
{
cq->front=0;
x=cq->items[cq->front];
}
else
{
cq->front=cq->front+1;
x=cq->items[cq->front];
}
cq->count--;
return(x);
}
int empty(struct Queue *cq) /*empty function definition*/
{
if(cq->rear==cq->front)
return(TRUE);
else
return(FALSE);
}
void display(struct Queue *cq) /*display function definition*/
{
int i,j,x;
if(empty(cq))
{
printf("\n queue is empty");
getch();
exit(0);
}
printf("\n The items of Queue are :");
j=(cq->front+1)%MAX;
for(i=0;i< cq->count;i++)
{
x=cq->items[j];
printf("\n %d",x);
j=(j+1)%MAX;
}
getch();
}
Saturday, November 1, 2008
DS lab program 7 (circular q)
#include"conio.h"
#define MAX 20
#define TRUE 1
#define FALSE 0
struct Queue
{
int front;
int rear;
int items[MAX];
}cq;
void insert(struct Queue *cq, int );
void display(struct Queue *cq);
int remov(struct Queue *cq);
int empty(struct Queue *cq);
main()
{
int i,n;
int choice,x;
cq.rear=cq.front=MAX-1;
clrscr();
printf("\n1.INSERT\n2.DISPLAY\n3.REMOVE\n");
while(1)
{
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the values of n:");
scanf("%d",&n);
printf("\n enter the items");
for(i=0;i< n;i++)
{
scanf("%d",&x);
insert(&cq,x);
}
printf(" the queue items are:\n");
display(&cq);
break;
case 3:
x=remov(&cq);
printf("\n the value of x=%d",x);
break;
case 2:
display(&cq);
break;
default:
printf("\n Invalid Entry");
getch();
exit(1);
}
}
getch();
}
void insert(struct Queue *cq,int y)
{
if(cq->rear==MAX-1)
cq->rear=0;
else
cq->rear=cq->rear+1;
if(cq->rear==cq->front)
{
printf("Queue overflow");
getch();
exit(0);
}
cq->items[cq->rear]=y;
}
int remov(struct Queue *cq)
{
int x;
if(empty(cq))
{
printf("\n queue underflow");
getch();
exit(1);
}
if(cq->front==MAX-1)
{
cq->front=0;
x=cq->items[cq->front];
}
else
{
cq->front=cq->front+1;
x=cq->items[cq->front];
}
return(x);
}
int empty(struct Queue *cq)
{
if(cq->rear==cq->front)
return(TRUE);
else
return(FALSE);
}
void display(struct Queue *cq)
{
int i,x;
if(cq->rear==cq->front)
{
printf("\n queue is empty");
getch();
exit(0);
}
if(cq->front==MAX-1)
{
for(i=0;i<= cq->rear;i++)
{
x=cq->items[i];
printf("\n %d",x);
}
getch();
}
else
for(i=cq->front;i< cq->rear;i++)
{
x=cq->items[i+1];
printf("\n %d",x);
}
}
DS lab program 6 (Queue operation)
#include"conio.h"
#define MAX 5
#define TRUE 1
#define FALSE 0
struct Queue
{
int front;
int rear;
int items[MAX];
}q;
void insert(struct Queue *q, int );
void display(struct Queue *q);
int remov(struct Queue *q);
int empty(struct Queue *q);
main()
{
int i,n;
int choice,x;
q.rear=-1;
q.front=0;
clrscr();
while(1)
{
printf("\n1.INSERT\n2.DISPLAY\n3.REMOVE\n");
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the values of n:");
scanf("%d",&n);
printf("\n enter the items");
for(i=0;i< n;i++)
{
scanf("%d",&x);
insert(&q,x);
}
printf(" the queue items are:\n");
display(&q);
break;
case 2:
display(&q);
break;
case 3:
x=remov(&q);
printf("\n the value of x=%d",x);
break;
default:
printf("\n Invalid Entry");
getch();
exit(1);
}
}
getch();
}
void insert(struct Queue *q,int y)
{
if(q->rear==MAX-1)
{
printf("\n Queue overflow");
getch();
exit(1);
}
else
q->items[++q->rear]=y;
}
int remov(struct Queue *q)
{
int x;
if(empty(q))
{
printf("\n queue underflow");
getch();
exit(1);
}
else
x=q->items[q->front++];
return(x);
}
int empty(struct Queue *q)
{
if(q->rear<>front)
return(TRUE);
else
return(FALSE);
}
void display(struct Queue *q)
{
int i,x;
for(i=q->front;i<= q->rear;i++)
{
x=q->items[i];
printf("\n %d",x);
}
}
DS lab program 5(evaluation of postfix exp)
#include
#define MAX 30
#define TRUE 1
#define FALSE 0
struct stack /* stack declaration */
{
int top;
float items[MAX];
};
float eval(char[]); /* function declarations */
float pop(struct stack *s);
void push (struct stack *s,float);
int isdigit(char);
float oper(char,float,float);
int empty(struct stack *);
void main()
{
char expr[MAX];
clrscr();
printf("\n Enter the postfix expression:");
scanf("%s",expr);
printf("\n Result is %.2f",eval(expr));
getch();
}
/* function to push items to stack */
void push(struct stack *s,float ele)
{
if(s->top==MAX-1)
{
printf("\n stack overflow");
exit(1);
}
else
s->items[++(s->top)]=ele;
}
/* function to pop item from stack */
float pop(struct stack *s)
{
int x;
if(empty(s))
{
printf("\n stack underflow");
getch();
exit(1);
}
else
{
x=s->items[s->top--];
return x;
}
}
/* function to check whether the stack is empty or not */
int empty(struct stack *s)
{
if(s->top==-1)
return(TRUE);
else
return(FALSE);
}
/* function to evaluate postfix expression */
float eval(char expr[])
{
int e,pos;
float opnd1,opnd2,value;
struct stack stk;
stk.top=-1;
for(pos=0;(e=expr[pos])!='\0';pos++)
{
if(isdigit(e))
push(&stk,(float)(e-'0'));
else
{
opnd2=pop(&stk);
opnd1=pop(&stk);
value=oper(e,opnd1,opnd2);
push(&stk,value);
}
}
return(pop(&stk));
}
/* function to evaluate various arithmatic expressions */
float oper(char symb,float opnd1,float opnd2)
{
switch(symb)
{
case'*':
return(opnd1*opnd2);
case'/':
return(opnd1/opnd2);
case'+':
return(opnd1+opnd2);
case'-':
return(opnd1-opnd2);
}
}
/* function to check whether the character is integer */
int isdigit(char symb)
{
return(symb>='0'&&symb<='9');
}
DS lab program-4 (stack operation)
#include
#include
#define MAX 20
#define TRUE 1
#define FALSE 0
void push();
void display();
struct stack /* stack declarations */
{
int top,items[MAX];
}s;
main()
{
int i,n;
int choice,x;
clrscr();
s.top=-1;
while(1)
{
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.TOPTEST\n");
printf("\n enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the values of n:");
scanf("%d",&n);
printf("\n enter the items");
for(i=0;i
{
scanf("%d",&x);
push(&s,x);
}
printf(" the stack items are:\n");
display(&s);
break;
case 2:
x=pop(&s);
printf("\n the value of x=%d\n",x);
break;
case 3:
display(&s);
break;
case 4:
x=toptest(&s);
printf("\n The top most item is %d\n",x);
break;
default:
printf("\n Invalid Entry");
getch();
exit(1);
}
}
getch();
}
/* function to push items to stack */
void push(struct stack *ps,int y)
{
if(ps->top==MAX-1)
{
printf("\n stack overflow");
getch();
exit(1);
}
else
ps->items[++(ps->top)]=y;
}
/* function to pop items from stack */
int pop(struct stack *ps)
{
int x;
if(empty(ps))
{
printf("\n stack underflow");
getch();
exit(1);
}
else
x=ps->items[ps->top--];
return(x);
}
/* function to check whether stack is empty */
int empty(struct stack *p)
{
if(p->top==-1)
return(TRUE);
else
return(FALSE);
}
/* function to display stack items */
void display(struct stack *ps)
{
int i,x;
for(i=ps->top;i>=0;i--)
{
x=ps->items[i];
printf("\n %d",x);
}
printf("\n");
}
/* fuction to check the top of stack */
int toptest(struct stack *ps)
{
int x;
if (empty(ps))
{
printf("\n Underflow");
getch();
exit(1);
}
else
{
x=ps->items[ps->top];
return(x);
}
}





















