Example of structure, C/C++ Programming

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

 

                                }

}

 

 

Posted Date: 10/18/2012 6:20:20 AM | Location : United States







Related Discussions:- Example of structure, Assignment Help, Ask Question on Example of structure, Get Answer, Expert's Help, Example of structure Discussions

Write discussion on Example of structure
Your posts are moderated
Related Questions
Define How Passing Arrays to a Function? A complete array can be passed to a function as an argument. The manner in which the array is passed be different from that of an ordin

How do I create two functions: one to put a collection of criteria into normal form, and one to compute the score of a document. A collection of criteria will be represented using

What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than a


Write a program to create a binary file and store the following data "hello", 0x0030,'1', 1.234  Using visual studio examine the binary file and note how the day is stored   Answe

Program for Swapping the Two Integers                 For example, Swapping the two value                                 int x=4,y=6,t;                                 t

We need an E Commerce site likefifacointrader. you must have experience with that kind of work You have to show a sample of work like that site. Skills required are C Prog

Increment and Decrement Operators.   The operator for increment is '++' and decrement is '-'. These operators enhances or decrease the value of a variable on which they are ope

Call by Reference Passing variables(parameters) to a function in C can be done in two ways - pass by value, also called as call by value and pass by address or also known as ca

convert BST into sorted doubly linked list