Example of structure, C/C++ Programming

Assignment Help:

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");

 

                                }

}

 

 


Related Discussions:- Example of structure

Implement the c++ code in assembly language, Selecting Array Elements Imple...

Selecting Array Elements Implement the following C++ code in assembly language, using the block-structured .IF and .WHILE directives. Assume that all variables are 32-bit signed in

Program create a user defined data structure - c++ program, Create a user-d...

Create a user-defined data structure (struct) called Node that represents a node within a linked list where the "data" stored in each node is a pointer to a Car object.    a) Wr

Stack and queues, Using Figure 10.2 as a model, illustrate the result of ea...

Using Figure 10.2 as a model, illustrate the result of each operation in the sequence ENQUEUE.Q; 4/, ENQUEUE.Q; 1/, ENQUEUE.Q; 3/, DEQUEUE.Q/, ENQUEUE.Q; 8/, and DEQUEUE.Q/ on an i

Explain the class invariant, Explain the class invariant. - It's a cond...

Explain the class invariant. - It's a condition that ensures correct working of a class and defines all the valid states for an object. - When an object is created class inv

Determine the types of container class, Determine the types of Container cl...

Determine the types of Container class Container class can be of 2 types: - Heterogeneous container - Here container class comprise a group of mixed objects - Homogeneou

Data structure, convert BST into sorted doubly linked list

convert BST into sorted doubly linked list

Advantages of using pointers over arrays, Question : (a) Define a Poin...

Question : (a) Define a Pointer. Provide an example of an integer pointer variable. (b) Give advantages of using pointers over arrays. (c) Declare an integer pointer

Write a program to change the matrix program, Change the matrix program (pr...

Change the matrix program (program 3) slightly. Overload == operator to compare two matrices to be added or subtracted. i.e., whether the column of first and the row of second

Function, #q•Design and code a new function that accepts as parameters the ...

#q•Design and code a new function that accepts as parameters the gross pay by value and the federal tax, state tax, local tax, SS tax, and net Pay by reference. Calculate the taxes

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd