Professional Documents
Culture Documents
DB - S - Inf - Post - Deq
DB - S - Inf - Post - Deq
a[i]=tmp;
percdown(a,0,i);
}
}
struct node
{
int data;
struct node *next; // Pointer to next node in DLL
struct node *prev; // Pointer to previous node in DLL
};
void create()
{
int item;
struct node *new1;
char resp;
printf("\nCreating the Double Linked List...");
do
{
printf("\nEnter the value:");
scanf("%d",&item);
new1=(struct node *)malloc(sizeof(struct node));
new1->prev=NULL;
new1->data=item;
new1->next=NULL;
if(start==NULL)
{
start=new1;
ptr=new1;
}
else
{
ptr->next=new1;
new1->prev=ptr;
ptr=new1;
}
printf("\nDo you want to continue?Enter Y or N:");
resp=getche();
}while(resp=='y' || resp=='Y');
}
void del_beg()
{
if(start==NULL)
printf("\nThe DLL is empty.So cannot delete anything.");
else
{
start=start->next;
start->prev=NULL;
}
}
void del_end()
{
if(start==NULL)
printf("\nThe DLL is empty.So cannot delete anything.");
else if(start->next==NULL)
start=NULL;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->prev->next=NULL;
ptr->prev=NULL;
}
}
Push Function:
void push()
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("\n enter data of item:");
scanf("%d",&temp->data);
temp->link=top;
top=temp;
}
POP function
void pop()
{
struct node*temp;
if(top==NULL)
printf("\n stack is empty \n");
else
{
temp=top;
printf("popped item is %d \n",temp->data);
top=top->link;
free(temp);
}
}
void display()
{
struct node*temp;
temp=top;
if(top==NULL)
printf("stack is empty \n");
else
{
printf("stack elements:\n");
while(temp!=NULL)
{
printf("->%d",temp->data);
temp=temp->link;
}
}
}
TYPES OF EXPRESSIONS
Arithmetic expressions can be represented in three ways:
Infix notation: The operator is in between the two operands.
Examples: A+B 2+3
Prefix (Polish notation): The operator precedes the operands.
Examples: +AB +23
Postfix (Reverse Polish Notation): The operator follows the operands.
Examples: AB+ 23+
void push(int y)
{
stack[++top]=y;
}//end of push()
int pop()
{
return(stack[top--]);
}//end of pop()
int prec(char c)
{
if(c=='*' || c=='/')
return(5);
else if(c=='+' || c=='-')
return(3);
else if(c=='(')
return(1);
}//end of prec()
Algorithm to evaluate Postfix Expression:
1. Initialize an empty stack.
2. Do
l Get next token (constant, arithmetic operator) in postfix expression.
l If token is an operand, push it on the stack.
l If token is an operator do the following:
l Pop top two values from the stack. (If the stack does not
contain two items, report error.)
l Apply operator token to these two values.
l Push the resulting value onto the stack.
3. Continue the process until the end of the expression is encountered.
4. The value of the expression is on the top of the stack (and stack should
contain only this value).
Postfix evaluation function
void evaluate(char pf[15])
{
float a,b,x;
for(i=0;pf[i]!='\0';i++)
{
if(isalpha(pf[i]))
{
printf("enter value of %c",pf[i]);
scanf("%f",&x);
push(x);
}//end of if
else//if operator
{
b=pop();
a=pop();
switch(pf[i])
{
case '+':push(a+b);
break;
case '-':push(a-b);
break;
case '*':push(a*b);
break;
case '/':push(a/b);
break;
default: printf("invalid operator\n");
}//end of switch
}//end of else
}//end of for
printf("value of expression %f",stack[top--]);
}//end of evaluate()
void push(float y)
{
stack[++top]=y;
}
float pop()
{
return(stack[top--]);
}
Symbol Balancing:
void main()
{
char exp[MAX],temp;
int i, flag=1;
clrscr();
printf("Enter an expression : ");
gets(exp);
for(i=0;i<strlen(exp);i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push(exp[i]);
if(exp[i]==')' || exp[i]=='}'|| exp[i]==']')
{
if(top == -1)
flag=0;
else
{
temp=pop();
if(exp[i]==')' && (temp=='{' || temp=='['))
flag=0;
if(exp[i]=='}' && (temp=='(' || temp=='['))
flag=0;
if(exp[i]==']' && (temp=='(' || temp=='{'))
flag=0;
}
}
}
if(top>=0)
flag=0;
if(flag==1)
printf("\n Valid expression");
else printf("\n Invalid expression");
}
void push(char c)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
top=top+1; stk[top] = c;
}
}
char pop()
{
if(top == -1)
printf("\n Stack Underflow");
else
return(stk[top--]);
}
void enqueue()
{
int item;
struct queue *temp;
/*No need of checking for Overflow as it is DMA*/
temp=(struct queue *)malloc(sizeof(struct queue));
printf("\nEnter the item to enqueue:");
scanf("%d",&item);
temp->data=item;
temp->ptr=NULL;
if(f==NULL && r==NULL)
{
f=temp;
r=temp;
}
else
{
r->ptr=temp;
r=temp;
}
}
void dequeue()
{
if(r==NULL && f==NULL)
{
printf("\nQueue is underflown i.e no nodes to be deleted.");
}
else if(r==f)
{
printf("\nThe deleted element is:%d",f->data);
f=NULL;
r=NULL;
}
else
{
printf("\nThe deleted element is:%d",f->data);
f=f->ptr;
}
}
void display()
{
if(r==NULL)
printf("\nQueue is underflown i.e no nodes to be displayed.");
else
{
struct queue *i=f;
printf("\nThe elements in the queue are:");
while(i!=NULL)
{
printf("%d\t",i->data);
i=i->ptr;
}
}
}