Sunday, March 18, 2012

Evaluation of Postfix Expression

/* Program Name: Evaluation of Postfix Expression
Done By: Prof.Dhumane A.V. VIIT, Pune
*/

#include
#include

int isoperator(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
return 1;
else
return 0;
}

int eval(int operand1,int operand2,char ch)
{
if(ch=='+')
{
return (operand1+operand2);
}
if(ch=='-')
{
return (operand1-operand2);
}
if(ch=='*')
{
return (operand1*operand2);
}
if(ch=='/')
{
return (operand1/operand2);
}


}
void main()
{
char *exp;
int *stack,top=-1,result,i,operand1,operand2,val;

clrscr();

printf("\nEnter the expression ");
scanf("%s",exp);

for(i=0;exp[i]!='\0';i++)
{
if(isoperator(exp[i]))
{
operand1=stack[top];
top--;
operand2=stack[top];
top--;
result=eval(operand1,operand2,exp[i]);
top++;
stack[top]=result;
}
else
{
printf("\nEnter the value of %c",exp[i]);
scanf("%d",&val);
top++;
stack[top]=val;
}
}
printf("\n\nresult =%d",stack[top]);
getch();
}

queue

#include
#include
#include
#include

struct queue
{
int data;
struct queue*next;
};

void main()
{
struct queue*front=NULL,*rear=NULL,*temp,*trav;
int choice;

clrscr();
while(1)
{
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(rear==NULL)
{
front=(struct queue*)malloc(sizeof(struct queue));
printf("\nEnter Data");
scanf("%d",&front->data);
front->next=NULL;
rear=front;
}
else
{
temp=(struct queue*)malloc(sizeof(struct queue));
printf("\nEnter Data");
scanf("%d",&temp->data);
temp->next=NULL;

rear->next=temp;
rear=temp;
}
break;
case 2:
if(front!=rear)
{
printf("Element %d deleted !!",front->data);
front=front->next;
}
else
{
if(front!=NULL)
{
printf("Element %d deleted !!",front->data);
front=NULL;
rear=NULL;
}
else
{
printf("\nQueue is EMPTY !!");
}
}
break;
case 3:
trav=front;
while(trav!=NULL)
{
printf("%d ",trav->data);
trav=trav->next;
}
break;
case 4:
exit(0);
}
}

getch();

}

Stack using arrays

/* Program Name: Stack using arrays
Done By: Prof.Dhumane A.V. VIIT, Pune
*/

#include
#include
#include

int push(int*queue,int rear)
{
int element;

if(rear<4)
{
printf("\nEnter the element ");
scanf("%d",&element);
rear++;
queue[rear]=element;
}
else
{
printf("\nQueue FULL !!!");
}
return rear;
}
void display(int *queue,int rear)
{
int i=0;
printf("\nThe Queue is :\n");
while(i<=rear)
{
printf("%d ",queue[i]);
i++;
}
}
int pop(int*queue,int rear)
{
int front=0,j;

if(rear==-1)
{
printf("\nQueue Empty !!!");
}
else
{
printf("Element poped is: %d",queue[front]);

for(j=1;j<=rear;j++)
{
queue[j-1]=queue[j];
}
rear--;
}
return (rear);
}
void main()
{
int queue[10],front=-1,rear=-1,choice;
clrscr();

while(1)
{
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(front==-1)
front++;

rear=push(queue,rear);
break;
case 2: rear=pop(queue,rear);
break;
case 3: display(queue,rear);
break;
case 4:
exit(0);
}
}

}

Stack using Linked List

/* Program Name: Stack using Linked List
Done By: Prof.Dhumane A.V. VIIT, Pune
*/

#include
#include

struct stack
{
int data;
struct stack *next;
};


struct stack* push(struct stack*head)
{

struct stack*trav,*temp;

if(head==NULL)
{
head=(struct stack*)malloc(sizeof(struct stack));
printf("\nEnter the data to be PUSHed ");
scanf("%d",&head->data);
head->next=NULL;
}
else
{
trav=head;
while(trav->next!=NULL)
{
trav=trav->next;
}

temp=(struct stack*)malloc(sizeof(struct stack));
printf("\nEnter the data to be PUSHed ");
scanf("%d",&temp->data);
temp->next=NULL;
trav->next=temp;
}

return head;
}

void display(struct stack*head)
{
struct stack*trav;
printf("\nContent of the stack is :\n");

for(trav=head;trav!=NULL;trav=trav->next)
{
printf("[%d]->",trav->data);
}
}

struct stack* pop(struct stack*head)
{
struct stack*trav;

if(head==NULL)
{
printf("\n Stack Empty !!!");
}
else
{
if(head->next==NULL)
{
printf("\nElement poped is %d",head->data);
head=NULL;
}
else
{
trav=head;
while(trav->next->next!=NULL)
{
trav=trav->next;
}
printf("\nElement poped is %d",trav->next->data);
trav->next=NULL;
}
}
return head;
}

void main()
{
struct stack *head=NULL,*trav;
int choice;

clrscr();

while(1)
{
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);

switch(choice)
{
case 1: head=push(head);
break;
case 2: head=pop(head);
break;
case 3: display(head);
break;
case 4: exit(0);
}

}

}

Stack using arrays

/* Program Name: Stack using arrays
Done By: Prof.Dhumane A.V. VIIT, Pune
*/

#include
#include

int push(int *stack,int top)
{
int element;

if(top<9)
{
printf("\nEnter the element to be pushed");
scanf("%d",&element);
top++;
stack[top]=element;
}
else
{
printf("\n Stack FULL !!!");
}
return top;
}
int pop(int*stack,int top)
{
if(top>-1)
{
printf("\n Element %d poped ",stack[top]);
top--;
}
else
{
printf("\n Stack Empty !!!");
}
return top;
}

void display(int*stack,int top)
{

while(top>-1)
{
printf("%d ",stack[top]);
top--;
}
}
void main()
{
int stack[20],top=-1,choice;
clrscr();

while(1)
{
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: top=push(stack,top);
break;
case 2: top=pop(stack,top);
break;
case 3: display(stack,top);
break;
case 4:
exit(0);
}
}
}

Singly Linked List (with functions)

/* Program Name: Singly Linked List
Done By: Prof.Dhumane A.V. VIIT, Pune
*/

#include
#include
#include
#include


struct node
{
int data;
struct node*next;
};

struct node*add_node(struct node*head)
{
struct node*trav,*temp;
if(head==NULL)
{
head=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data ");
scanf("%d",&head->data);
head->next=NULL;
}
else
{
trav=head;
while(trav->next!=NULL)
{
trav=trav->next;
}
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data ");
scanf("%d",&temp->data);
temp->next=NULL;
trav->next=temp;
}
return head;
}

void display(struct node*head)
{
struct node*trav;
printf("\n");
for(trav=head;trav!=NULL;trav=trav->next)
{
printf("[%d]->",trav->data);
}
}
void search(struct node*head)
{
struct node*trav;
int KEY, found=0;

printf("\nEnter the KEY to be searched ");
scanf("%d",&KEY);

while(trav!=NULL)
{
if(trav->data==KEY)
{
printf("\n KEY found !!!");
found=1;
break;
}
trav=trav->next;
}

if(found==0)
{
printf("\nKEY not found !!!");
}
}
struct node*delete_node(struct node*head)
{
int KEY,found=0;
struct node*trav;

printf("\nEnter the KEY to be deleted ");
scanf("%d",&KEY);

if(head->data==KEY)
{
head=head->next;
printf("\nKEY deleted ..");
}
else
{
trav=head;

while(trav->next!=NULL)
{
if(trav->next->data==KEY)
{
trav->next=trav->next->next;
printf("\nKEY deleted ..");
found=1;
break;
}
trav=trav->next;
}
if(found==0)
{
printf("\nKEY not present to be deleted ..");
}
}

return head;

}

void main()
{
struct node*head=NULL,*trav,*temp;
int choice;

clrscr();

while(1)
{
printf("\n1.Add");
printf("\n2.Delete");
printf("\n3.Search");
printf("\n4.Display");
printf("\n5.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);

switch(choice)
{
case 1: head=add_node(head);
break;
case 2: head=delete_node(head);
break;
case 3: search(head);
break;
case 4: display(head);
break;
case 5:
exit(0);
}
}

}

SINGLY LINKED LIST (without functions)

/****************************************************************************
SINGLY LINKED LIST (without functions):
Important Things to remember:
1. Create a structure containing a integer value and a pointer (*), which
points to the structure that we have created.
2. Structure always ends with semicolon (;)
3. Create *head, this pointer is going to point the very first node of the
linked list. Initialize this pointer by NULL.
4. For addding new node into Linked List:
a) Check if head==NULL, if it is NULL, allocate memory of the size of
struct node (here the memory will be 4 bytes) from heap, and make the
head to point to that allocated memory. else go to b)
b) if head!=NULL, traverse the linked list till the last node of it, after
that create a temp node, attach the node at the end of the linked list.

Memory Allocation:
1. Use a function malloc for allocating the memory from the heap area.
2. The function prototype is
void *malloc(size_t size);
3. Return type of the function is void*, which is also called as generic
pointer.
4. The speciality of void* is, it can be typecasted into any other type of
pointer, such as int*, chat*, float* and in this program we have
typecasted it into struct node*.
As it can be typecasted into any other type of pointers, it is called as
"generic pointer".
5. Malloc function accepts one argument, that argument tells malloc, how much
memory it has to allocate from the heap area.
6. In this program, we used sizeof() operator for calculating the size memory
that malloc is going to allocate dynamically.
7. The important thing about the sizeof() is that, eventhough it looks like
a function, but it is not a function, it is an "Operator".

****************************************************************************/

#include
#include
#include
#include


struct node
{
int data;
struct node*next; //this pointer is pointing to the struct node
};

void main()
{
struct node*head=NULL,*trav,*temp;
int choice;

clrscr();

while(1)
{
printf("\n1.Add");
printf("\n2.Display");
printf("\n3.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);

switch(choice)
{
case 1:
if(head==NULL)
{
head=(struct node*)malloc(sizeof(node));
printf("\nEnter the data ");
scanf("%d",&head->data);
head->next=NULL;
}
else
{
trav=head;
while(trav->next!=NULL)
{
trav=trav->next;
}
temp=(struct node*)malloc(sizeof(node));
printf("\nEnter the data ");
scanf("%d",&temp->data);
temp->next=NULL;
trav->next=temp;

}
break;
case 2:
printf("\n");
for(trav=head;trav!=NULL;trav=trav->next)
{
printf("[%d]->",trav->data);
}
break;
case 3:
exit(0);
}
}
}
/*****************************************************************************
Done By: Prof. Dhumane A.V
*****************************************************************************/

Polynomial Addition using array of structure

/* Program Name: Polynomial Addition using array of structure
Done By: Prof.Dhumane A.V. VIIT, Pune
*/

#include
#include
#include

struct poly
{
int coeff;
int expo;
};

void accept_poly(struct poly*poly,int degree)
{
int i;
printf("\n Enter the Coefficient of the polynomial");
for(i=degree;i>=0;i--)
{
poly[i].expo=i;
printf("Coefficient of X^%d=",i);
scanf("%d",&poly[i].coeff);
}
}
void display_poly(struct poly*poly,int degree)
{
int i;
for(i=degree;i>=0;i--)
{
printf("%dX^%d + ",poly[i].coeff,poly[i].expo);
}
}
void add_poly(struct poly*poly1,int degree1,struct poly*poly2,int degree2)
{
struct poly*poly3;
int degree3,k=0,i,j;

if(degree1>degree2)
degree3=degree1;
else
degree3=degree2;


for(i=degree1,j=degree2;degree3>=0;)
{
if(poly1[i].expo==poly2[j].expo)
{
poly3[k].expo=poly1[i].expo;
poly3[k].coeff=poly1[i].coeff+poly2[j].coeff;
k++;
i--;
j--;
}
else
{
if(poly1[i].expo>poly2[j].expo)
{
poly3[k]=poly1[i];
k++;
i--;
}
else
{
poly3[k]=poly2[j];
k++;
j--;
}
}
degree3--;
}

k--;

for(i=0;i<=k;i++)
{
printf("%dX^%d+",poly3[i].coeff,poly3[i].expo);
}

}

void main()
{
struct poly poly1[20],poly2[20],poly3[20];
int degree1,degree2,cho;
clrscr();

while(1)
{
printf("\n1.Accept FIRST polynomial");
printf("\n2.Accept SECOND polynomial");
printf("\n3.Display FIRST polynomial ");
printf("\n4.Display SECOND polynomial ");
printf("\n5.Add polynomials");

printf("\nEnter your choice ");
scanf("%d",&cho);
switch(cho)
{
case 1:
printf("\nEnter the DEGREE of first polynomial ");
scanf("%d",°ree1);
accept_poly(poly1,degree1);
break;
case 2:
printf("\nEnter the DEGREE of second polynomial ");
scanf("%d",°ree2);
accept_poly(poly2,degree2);
break;
case 3:
display_poly(poly1,degree1);
break;
case 4:
display_poly(poly2,degree2);
break;
case 5:
add_poly(poly1,degree1,poly2,degree2);
getch();
exit(0);
}

}
}

Database management using array of structures

/* Program Name: Database management using array of structures
Done By: Prof.Dhumane A.V. VIIT, Pune
*/

#include
#include

struct student
{
int roll;
char name[30];
char clas[20];
};
int insert_record(struct student *stud,int records_count)
{
records_count++;

printf("\nEnter the roll number of the student ");
scanf("%d",&stud[records_count].roll);

printf("\n Enter the name of the student");
scanf("%s",stud[records_count].name);

printf("\n Enter the class of the student");
scanf("%s",stud[records_count].clas);

return records_count;
}
void display_records(struct student *stud,int records_count)
{
int i;
clrscr();
printf("\n----------------------------------------------------------");
printf("\nROLL\t\tNAME \t\tCLASS");
printf("\n----------------------------------------------------------");
for(i=0;i<=records_count;i++)
{
printf("\n%d\t\t%s\t\t%s",stud[i].roll,stud[i].name,stud[i].clas);
}

getch();
}

int delete_record(struct student*stud,int records_count)
{
int roll,i=0,found=0;

printf("\nEnter the roll number of the student to be deleted ..");
scanf("%d",&roll);

while(i<=records_count)
{
if(stud[i].roll==roll)
{
found=1;
while(i {
stud[i]=stud[i+1];
i++;
}
records_count--;
break;
}
i++;
}
if(found==0)
{
printf("\nRecord not found");
}
else
{
printf("\nRecord deleted successfully");
}
return records_count;
}

void modify_record(struct student*stud,int records_count,int roll)
{
int i=0;
char ans;

while(i<=records_count)
{
if(stud[i].roll==roll)
{
printf("\n Want to change roll number? [y/n]");
ans=getche();
if(ans!='n')
{
printf("\n Enter new roll number ");
scanf("%d",&stud[i].roll);
}
printf("\n Want to change Name? [y/n]");
ans=getche();
if(ans!='n')
{
printf("\n Enter new Name ");
scanf("%s",&stud[i].name);
}

printf("\n Want to change Class? [y/n]");
ans=getche();
if(ans!='n')
{
printf("\n Enter new Class ");
scanf("%s",&stud[i].clas);
}

}
i++;
}
}
void main()
{
struct student stud[50];
int records_count=-1,choice,roll;

clrscr();

while(1)
{
printf("\n1. Insert Record");
printf("\n2. Display records");
printf("\n3. Delete record");
printf("\n4. Modify record");
printf("\n5. Exit");
printf("\n Enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: records_count=insert_record(stud,records_count);
break;
case 2: display_records(stud,records_count);
break;
case 3: records_count=delete_record(stud,records_count);
break;
case 4:
printf("\nEnter the roll number of the record to be modified ");
scanf("%d",&roll);
modify_record(stud,records_count,roll);
break;
case 5: exit(0);
}
}
}

Bubble and Insertion sort

/* Program Name: Bubble and Insertion sort
Done By: Prof.Dhumane A.V. VIIT, Pune
*/

#include
#include
#include

void get_array(int*array,int no_of_elements)
{
int i;

for(i=0;i {
scanf("%d",&array[i]);
}

}
void display_sorted_array(int *array,int no_of_elements)
{
int i;
printf("\nSorted array is \n");
for(i=0;i {
printf("%d ",array[i]);
}
}
void bubble_sort()
{
int *array;
int no_of_elements,i,j,temp;

printf("\nHow many elements you want to sort ");
scanf("%d",&no_of_elements);

printf("\nEnter the array ");
get_array(array,no_of_elements);

for(i=0;i {
for(j=i+1;j {
if(array[j] {
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
display_sorted_array(array,no_of_elements);

}
void insertion_sort()
{
int *array;
int no_of_elements,i,j,temp;

printf("\nHow many elements you want to sort ");
scanf("%d",&no_of_elements);

printf("\nEnter the array ");
get_array(array,no_of_elements);

for(i=1;i {
temp=array[i];

for(j=i-1;j>=0&&temp {
array[j+1]=array[j];
}
array[j+1]=temp;
}

display_sorted_array(array,no_of_elements);

}

void main()
{
int choice;

clrscr();

while(1)
{
printf("\n1.Bubble Sort");
printf("\n2.Insertion Sort");
printf("\n3.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: bubble_sort();
break;
case 2: insertion_sort();
break;
case 3:
exit(0);
}
}
}

Linear and Binary Search

#include
#include

void Linear_Search()
{
int element,found_flag=0,i;
int array[20],no_of_elements;

printf("\n How many elements you want to insert in the array ?");
scanf("%d",&no_of_elements);

printf("\nEnter the elements in the array ");
for(i=0;i {
scanf("%d",&array[i]);
}

printf("\n Enter the element that you want to search ");
scanf("%d",&element);

for(i=0;i {
if(array[i]==element)
{
printf("\n Element Found !!!");
found_flag=1;
break;
}
}
if(found_flag==0)
printf("\nElement Not Found..");
}

void Binary_Search()
{
int element,found_flag=0,i,mid,low,high;
int array[20],no_of_elements;

printf("\n How many elements you want to insert in the array ?");
scanf("%d",&no_of_elements);

printf("\nEnter the elements in the array ");
for(i=0;i {
scanf("%d",&array[i]);
}

printf("\n Enter the element that you want to search ");
scanf("%d",&element);

high=no_of_elements-1;
low=0;

while(low<=high)
{
mid=(low+high)/2;
if(array[mid]==element)
{
printf("\nElement Found !!!");
found_flag=1;
break;
}
else
{
if(array[mid]>element)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
}

if(found_flag==0)
printf("\nElement not found ");
}

void main()
{
int choice;

clrscr();


while(1)
{
printf("\n1.Linear Search");
printf("\n2.Binary Search");
printf("\n3.Exit");
printf("\nEnter your Choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: Linear_Search();
break;
case 2: Binary_Search();
break;
case 3: exit(0);
}
}
}