example of structurestruct


Example of structure:

struct item

{

                int element;

                node_ptr next;

};

typedef node_ptr stack;

stack create(void)

{

                stack s;

                s=(stack)malloc(sizeof(struct item));

                s->next=NULL;

                return s;

}

void push(stack s,int a)

{

                node_ptr temp;

                temp=(node_ptr)malloc(sizeof(struct item));

                temp->element=a;

                temp->next=s->next;

                s->next=temp;

}

int is_empty(stack s)

{

                return(s->next==NULL);

}

void  pop(stack s)

{

                node_ptr temp;

                temp=s->next;

                s->next=temp->next;

                free(temp);

 

}

int top(stack s)

{

                return(s->next->element);

}

void main()

{

                stack s;

                char exp[100];

                int i;

                s=create();

                clrscr();

                printf("Enter an expression\n");

                scanf("%s",exp);

                for(i=0;exp[i];i++)

                {

                                if(exp[i]=='('||exp[i]=='['||exp[i]=='{')

                                                push(s,exp[i]);

                                else

                                if(exp[i]==')')

                                                if(top(s)!='(')

                                                {

                                                                printf("Invalid Expression");

                                                                return;

                                                }

                                                else

                                                                pop(s);

                                else

                                if(exp[i]=='}')

                                                if(top(s)!='{')

                                                 {

                                                                printf("Invalid Expression");

                                                                return;

                                                 }

                                                else

                                                                pop(s);

                                else

                                if(exp[i]==']')

                                                if(top(s)!='[')

                                                {

                                                                                printf("Invalid Expression");

                                                                return;

                                                }

                                                else

                                                                pop(s);

                }

                if(!is_empty(s))

                                                {

                                                printf("Invalid Expression");

 

                                }

}

 

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: example of structurestruct
Reference No:- TGS0174761

Expected delivery within 24 Hours