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
*****************************************************************************/