New soham
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
charstk[20];
int top = -1;
int isEmpty(){
return top == -1;
}
int isFull(){
return top == MAX - 1;
}
char peek(){
return stk[top];
}
char pop(){
if(isEmpty())
return -1;
char ch = stk[top];
top--;
return(ch);
}
void push(char oper){
if(isFull())
printf("Stack Full!!!!"); else{
top++;
stk[top] = oper;
}
}
int checkIfOperand(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
int precedence(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
int covertInfixToPostfix(char* expression)
{
int i, j; for (i = 0, j = -1; expression[i]; ++i)
{
if (checkIfOperand(expression[i]))
expression[++j] = expression[i];
else if (expression[i] == '(')
push(expression[i]);
else if (expression[i] == ')')
{
while (!isEmpty() && peek() != '(')
expression[++j] = pop();
if (!isEmpty() && peek() != '(')
return -1; // invalid expression
else
pop();
}
else // if an opertor
{
while (!isEmpty() && precedence(expression[i]) <= precedence(peek()))
expression[++j] = pop();
push(expression[i]);
}
}
while (!isEmpty())
expression[++j] = pop();
expression[++j] = '\0';
printf( "%s", expression);
} int main()
{
char expression[] = "((p+(q*r))-s)";
covertInfixToPostfix(expression);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{ int data;
struct node *next;
};
struct node *head;
void beginsert ();
void begin_delete();
void last_delete();
void random_delete();
void display();
void main ()
{
int choice =0;
while(choice != 6)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n"); printf("\n1.Insert in begining\n2.Delet e from Beginning\n3.Delete from last\n4..Delete
node after specified location\n5.Show\n6.exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice); switch(choice)
{
case 1: beg_insert();
break;
case 2: begin_delete();
break;
case 3: last_delete();
break;
case 4: random_delete();
break;
case 5: display();
break;
case 6: exit(0); break;
default: printf("Please enter valid choice..");
}
}
} void
beg_insert() {
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr>next = head;
head = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{ struct node *ptr; if(head == NULL)
{
printf("\nLis empty\n");
}
else {
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
} }
void last_delete() {
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else {
ptr = head;
while(ptr->next != NULL)
{ ptr1 = ptr;
ptr = ptr -
>next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
} }
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL) {
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void display() {
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
} else
{
printf("\nprinting values..........\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
Comments
Post a Comment