Exemplo n.º 1
0
main()
{stack s;
 char inp[20],op[20],ch;
 int i=0,j=0;
 printf("enter the infix expression\n");
 scanf("%s",inp);
 createstack(&s);
 push('\0',&s);
 while(inp[i]!='\0')
 {while((pre(inp[i]))<=pre(top(&s)))
  {pop(&ch,&s);
   op[j++]=ch;
   }
 push(inp[i],&s);
 i++;
}
while(!StackEmpty(&s))
 {pop(&ch,&s);
  op[j++]=ch;
  }
printf("the suffix expression is\n");
printf("%s\n",op);
 
 
}
Exemplo n.º 2
0
int main(int argc,char** argv)
{
	STACK *p=createstack();
	int i;
	
	for(i=0;i<10;i++){
		element e;
		e.key=i*5;
		if(push(p,e)){
			printf("push error\n");
			return -1;
		}
	}	

	stackempty(p);

	for(i=0;i<11;i++){
		element e;
		if(pop(p,&e)==-1){
			printf("pop error\n");
			return -1;
		}
		else{
			printf("top element of the element of stack is %d\n",e.key);
		}	
	}

	return 0;
}
Exemplo n.º 3
0
void push(char item,list *list)
{listnode *newnode;
 newnode=createstack(item);
 if(newnode==NULL)
  printf("Insertion failed\n");
 else if(stackfull(list))
  printf("cannot push as stack is full\n");
 else
{ newnode->next=list->head;
 list->head=newnode;
 list->count++;}
}
Exemplo n.º 4
0
void reversestr(char str[])
{
    int n = strlen(str);
    int i;
    struct stack* stack = createstack(n);
    for(i = 0; i<=n; i++)
        push(stack, str[i]);
    
    for(i = 0; i<=n; i++)
        str[i] = pop(stack);
    printf("\nThe reverse is %s", str);
    return ;
}
Exemplo n.º 5
0
int main()
{
	freopen("input.txt","r",stdin);
	int n;
	stack* st1=createstack();
	stack* st2=createstack();
	while(scanf("%d",&n)!=EOF)
	{
		getchar();
		for(int i=0;i<n;i++)
		{
			readthecommand(st1);
		}
		destroy(st1);
	}	
	/*while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			readthecomman();
		}
	}*/
	return 0;
}
Exemplo n.º 6
0
int main()	/* this main is used to impliment the operarions in the stack  */
{ 
int temp;
int x;
struct Arraystack *s= createstack(10);
push(s,10);
push(s,10);
push(s,10);
push(s,10);
push(s,10);
push(s,10);
push(s,10);
temp=pop(s);
printf("%d",temp);
free(s);
}
Exemplo n.º 7
0
int main()
{
	char a[11000];
	gets(a);
	int l;
	l=strlen(a);
	Pseqstack pastack=createstack(l);
	CStree t=createtree();
	CStree father=createtree();
	CStree current=createtree();
	int i;
	int j=0,len=0;
	int value=1;
	for(i=0;i<l;i++)
	{
		if(a[i]=='d')
		{
			j++;
            CStree temp=createtree();
            temp->info=++value;
            temp->lc=NULL;
            temp->rs=NULL;
			father=top(pastack);
			if(father->lc == NULL)
            {
                father->lc=temp;
            }
            else
            {
                current=father->lc;
                while (current->rs!= NULL)
				current=current->rs;
                current->rs=temp;
            }
			push(pastack,temp);
		}
		else if(a[i]=='u')
		{
			pop(pastack);
			j--;
		}
		if(len<j)
			len=j;
	}
	printf("%d=>%d",len,layer(t));
	return 0;
}
Exemplo n.º 8
0
int expression(char exp[20])			//will return '0' if equation is valid otherwise '1'
{
	struct node* s=createstack();
	int flag=0;
	int i=0;

	while(exp[i]!='\0')
	{
		if(exp[i]=='(')
			push(s,exp[i]);
		else if(exp[i]=='{')
			push(s,exp[i]);
		else if(exp[i]=='[')
			push(s,exp[i]);

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

			if(exp[i]==')'&&isempty(s)&&popAndTop(s)=='(')
			{
				i++;
				flag=1;
				continue;
			}
			else
			{
				
				flag=0;
				break;
			}
			
		}

	i++;
	}
	if(flag==1&&!isempty(s)){
		printf("Entered Equation is Valid\n");
		return 0;
	}

	else
	{
		printf("\nNot valid w.r.t. brackets\n");
		return 1;
	}
}
Exemplo n.º 9
0
int main()
{
    
    struct tree *root=NULL;
    
    insert(&root,10);
    insert(&root,20);
    insert(&root,5);
    insert(&root,4);
    insert(&root,6);
   
   
   struct stack *s;
   s=createstack();
   
   
   struct tree *c=root;
   c->vl=0;
   c->vr=0;
   push(s,c);
   
   while( !stackempty(s) )
   {
          
          while( ((tree*)stacktop(s))->left!=NULL && !((tree*)stacktop(s))->vl )
          {
              struct tree *n=(tree*)stacktop(s);
              n->vl=1;
              push(s,n->left);
          }
          while( ((tree*)stacktop(s))->right!=NULL && !((tree*)stacktop(s))->vr )
          {
              struct tree *n=(tree*)stacktop(s);
              n->vr=1;
              push(s,n->right);
          }
              struct tree *n=(tree*)pop(s);
              printf("%d ",n->d);
  
   }
   

   
   
   
    getch();
}
Exemplo n.º 10
0
 int main(void)
 {
 
     int r, m, n;
     struct timeval t1, t2, tv;

     createstack();
     printf("Enter m and n: ");
     scanf("%d%d", &m, &n);

     gettimeofday(&t1,NULL);
     r = ackermann(m,n);
     gettimeofday(&t2,NULL);

     printf("Result = %d\n", r);
     timersub(&t2,&t1,&tv);
    
     printf("Time taken for ackermann: %ld milliseconds\n", ((1000000*tv.tv_sec+tv.tv_usec)/1000));

     return 0;
}
Exemplo n.º 11
0
int main()
{
    int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    Stack * head = NULL;
    if (createstack(&head, 64) == FALSE) {
        printf("create stack failed.\n");
        exit(EXIT_FAILURE);
    }
    
    int i = 0;
    while (pushstack(head, data[i++]) == TRUE && i < 10);
    
    while (popstack(head, &i) == TRUE)
        printf("%d ", i);
    
    printf("\n");
    
    releasestack(head);

    return 0;
}
int main()
{
stack s;
char ip[10],op[10],ch;
int i=0,j=0,r;
printf("enter the infix expression to convert into suffix:\n");
scanf("%s",&ip);
createstack(&s);
push('\0',&s);
while(ip[i]!='\0')
{
while((precedence(ip[i]))<=(precedence(top(&s))))
{
pop(&ch,&s);
op[j++]=ch;
}
printf("the suffix expression is %s\n",op);
r=suffix(op);
printf("the value of expression is %d\n",r);
return 0;
}
Exemplo n.º 13
0
Tcb::Tcb(int priority, size_t stack_size, entry_t entry, uint8_t ttype): task_start(taskstart), task_entry(entry), sched_priority(priority), init_priority(priority), task_state(TSTATE_TASK_INVALID), flags(0), need_resched(false),  adj_stack_size(0), stack_alloc_ptr(nullptr), adj_stack_ptr(nullptr)
{
	int ret;
	
	ret = createstack(stack_size);
	if (ret < OK) {
		// add code herr
	}
	DEBUG_PRINT("Tcb:createstack(stack_size) OK\n");
	DEBUG_PRINT("\tstack_size:%ld\n",(uint32_t)stack_size);
	DEBUG_PRINT("\tadj_stack_ptr:%ld\n",(uint32_t)adj_stack_ptr);
	DEBUG_PRINT("\tadj_stack_size:%ld\n",(uint32_t)adj_stack_size);

	ttype &= TCB_FLAG_TTYPE_MASK;
	flags &= ~TCB_FLAG_TTYPE_MASK;
	flags |= ttype;

	ret = init_state();
	if (ret < OK) {
		// add code herr
	}
	DEBUG_PRINT("Tcb:init_state() OK\n");
}
Exemplo n.º 14
0
void main(){


	 createstack();
		printf("top:%d\n",top());

	 push(5);
	 push(7);
	 push(9);
	 push(88);
	 //push(9);

		//pop();
		//pop();

		 pop();
       top();
		 pop();
        pop();pop();pop();pop();pop();
	  printf("%d\ntop: ",top());


}
Exemplo n.º 15
0
int main(){
	char exp[100];
	char post[100];
	int k=-1;
	stack *st;
	int i=0,p=0;
	printf("enter string expression: ");
	//gets(exp); 
	//fgets(exp, sizeof(exp), stdin);
	scanf("%s",exp);
	printf("Infix expression : %s",exp);
	st=createstack(exp);
	
	for(i=0;i<strlen(exp);i++){
		if( (exp[i]>='a' && exp[i]<='z') || (exp[i]>='A' && exp[i]<='Z'))
			post[++k]=exp[i];
		else if(exp[i]=='(')
			push(st,exp[i]);
		else if(exp[i]==')'){
			while(!isempty(st) && peek(st)!='(')
				post[++k]=pop(st);
			pop(st);
		}
		else{
			while(precedence(exp[i]) < precedence(peek(st)))
				post[++k]=pop(st);
			push(st,exp[i]);
		}
	}
	while(!isempty(st))
		post[++k]=pop(st);
	
	post[++k]='\0';
	printf("Postfix expression :\n%s\n",post);
	return 0;
		
}
Exemplo n.º 16
0
void CreateInterpreter(void)
{
  if(seterror() == 0)
  {
    Write(BANNER);
    createstack();
    createhashtable();
    createIO();
    createmem(atol(GetOption("MemorySize")));
    initstack();
    inithashtable();
    lockmem();
    initlex();
    initlib();
    initsyslib();
    initmodify();
    parsefile(inipath);
    checkdefinitions();
    modify_definitions();
    lockmem();
  }
  else
    exit(1);
}
Exemplo n.º 17
0
void infixToPostfix(char exp[20])			
{
	struct node* s1=createHeaderOfList();			//s1: it is a link list, will be used to store output
	struct node* s2=createstack();					//s2: it is dynamic stack used for conversion
	int i=0;
	while(exp[i]!='\0')
	{
		if(exp[i]!='+'&&exp[i]!='-'&&exp[i]!='*'&&exp[i]!='/'&&exp[i]!='('&&exp[i]!=')')
		{
			insert_endOfList(s1,exp[i]);
		}
		else if(isempty(s2)==0)
		{
			push(s2,exp[i]);
		}
		else if((exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/')&&isempty(s2)!=0)   
		{
			if(CheckPrecedence(exp[i],top(s2))!=0)
			{
				insert_endOfList(s1,popAndTop(s2));
				if(exp[i]==top(s2)&&isempty(s2)!=0)
				{
					push(s2,exp[i]);
					insert_endOfList(s1,popAndTop(s2));
					
				}
				else
					push(s2,exp[i]);

			}
			else
			{
				push(s2,exp[i]);
			}

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

			push(s2,exp[i]);
		}
		else if(exp[i]==')')
		{
			while(top(s2)!='(')
			{
				insert_endOfList(s1,popAndTop(s2));
			}
			char temp=popAndTop(s2);
		}
			i++;
	}
	if(isempty(s2)!=0)
	{
		while(isempty(s2)!=0)
		{				
			insert_endOfList(s1,popAndTop(s2));
		}
	}
	print(s1);			//printing output link list
	
}
Exemplo n.º 18
0
/*
Deletes the line of number index, renumbering all 
lines after that line, and returns a pointer to the deleted line
*/
char * delete_line(text_t *txt, int index){
text_t * init_text=txt;
if(txt==NULL){
	//No Tree
	return NULL;
}else{

	int length=length_text(txt);
	//Check index with maxlength
	if(index>length){
		return NULL;
	}else{
		//Initialize stack
		createstack();

		text_t * uppernode;
		text_t * sel_node;
		text_t * other_node;
		while(txt->right!=NULL){
			push(txt);
			uppernode=txt;
			if(index<=txt->key){
				sel_node=txt->left;
				other_node=txt->right;
				txt->key=txt->key-1;
				txt=txt->left;

			}else{
				index=index-txt->key;
				sel_node=txt->right;
				other_node=txt->left;
				txt=txt->right;
			}
			

		}
		//Pop the last node
		pop();

		uppernode->left=other_node->left;
		uppernode->right=other_node->right;
		uppernode->key=other_node->key;
		uppernode->height=other_node->height;

		text_t * tmp_node=NULL;
		int finished=0;
		
		while( !stack_empty() && !finished ){
			int tmp_height, old_height;
			tmp_node = pop();
			old_height= tmp_node->height;
		
			if( tmp_node->left->height - tmp_node->right->height == 2 )
			{ 	
				if( tmp_node->left->left->height - tmp_node->right->height == 1 )
				{ 	right_rotation( tmp_node );
					tmp_node->right->height =  tmp_node->right->left->height + 1;
					tmp_node->height = tmp_node->right->height + 1;
				}
				else
				{ 
					left_rotation( tmp_node->left );
					right_rotation( tmp_node );
					tmp_height =
					tmp_node->left->left->height;
					tmp_node->left->height = tmp_height + 1;
					tmp_node->right->height = tmp_height + 1;
					tmp_node->height = tmp_height + 2;
				}
			}else if( tmp_node->left->height - tmp_node->right->height == -2 )
			{ 
				if( tmp_node->right->right->height -	tmp_node->left->height == 1 )
					{ 	
						left_rotation( tmp_node );
						tmp_node->left->height =tmp_node->left->right->height + 1;
						tmp_node->height = tmp_node->left->height + 1;
					}
					else
					{
						right_rotation( tmp_node->right );
						left_rotation( tmp_node );
						tmp_height = tmp_node->right->right->height;
						tmp_node->left->height = tmp_height + 1;
						tmp_node->right->height = tmp_height + 1;
						tmp_node->height = tmp_height + 2;
				}
			}
			else /* update height even if there
			was no rotation */
			{ 
				if( tmp_node->left->height > 	tmp_node->right->height )
					tmp_node->height = tmp_node->left->height + 1;
				else
					tmp_node->height =	tmp_node->right->height + 1;
			}
			if( tmp_node->height == old_height )
				finished = 1;
			}
			
			remove_stack();
			return (char *)sel_node->left;
/*
		if(index-txt->key==1){
			
		}else{
			//Node not found
			printf("shouldn't come here");
		}*/
		
	}
	
}
	
}
Exemplo n.º 19
0
/*
Inserts the line before the line of 
number index, if such a line exists, to new line, renumbering all lines after that line. If no such 
line exists, it appends new line as new last line
*/
void insert_line(text_t *txt, int index, char * new_line) {

	if(txt==NULL){
		//printf("Should not come here");
		return NULL;

	}else if( txt->left == NULL )
	{ 
		if(index>length_text(txt)+1){
			index=length_text(txt)+1;
		}
		txt->left = (struct text_t *) new_line;
		txt->key = 0;
		txt->height = 0;
		txt->right = NULL;
	}else{
		//check max line
		if(index>length_text(txt)+1){
			index=length_text(txt)+1;
		}
		createstack();
		while(txt->right!=NULL){
			push(txt);
			if(index<=txt->key){
				txt->key=txt->key+1;
				txt=txt->left;
			}else if(index>txt->key){
				index=index-txt->key;
				txt=txt->right;				
			}
		}

		//create a new Node
		text_t * old_node=malloc(sizeof(text_t));
		old_node->key=0;
		old_node->left=txt->left;
		old_node->right=txt->right;
		old_node->height=0;

		text_t * new_node=malloc(sizeof(text_t));
		new_node->key=0;
		new_node->left=(struct text_t *)new_line;
		new_node->right=NULL;
		new_node->height=0;

		if(index-txt->key==0){
		txt->left=new_node;
		txt->right=old_node;
		txt->height=height(txt);
		}else if(index-txt->key==1){
		txt->left=new_node;
		txt->right=old_node;
		txt->height=height(txt);
	
		}else{
		txt->left=old_node;
		txt->right=new_node;
		txt->height=height(txt);
		}

		txt->key=1;

		//balance the tree
		//Need stack
		text_t * tmp_node=NULL;
		int finished =0;
		while( !stack_empty() && !finished ){
			int tmp_height, old_height;
			tmp_node = pop();
			old_height= tmp_node->height;
			if( tmp_node->left->height - tmp_node->right->height == 2 )
			{ 	
				if( tmp_node->left->left->height - tmp_node->right->height == 1 )
				{ 	right_rotation( tmp_node );
					tmp_node->right->height =  tmp_node->right->left->height + 1;
					tmp_node->height = tmp_node->right->height + 1;
				}
				else
				{ 
					left_rotation( tmp_node->left );
					right_rotation( tmp_node );
					tmp_height =
					tmp_node->left->left->height;
					tmp_node->left->height = tmp_height + 1;
					tmp_node->right->height = tmp_height + 1;
					tmp_node->height = tmp_height + 2;
				}
			}else if( tmp_node->left->height - tmp_node->right->height == -2 )
			{ 
				if( tmp_node->right->right->height -	tmp_node->left->height == 1 )
					{ 	
						left_rotation( tmp_node );
						tmp_node->left->height =tmp_node->left->right->height + 1;
						tmp_node->height = tmp_node->left->height + 1;
					}
					else
					{
						right_rotation( tmp_node->right );
						left_rotation( tmp_node );
						tmp_height = tmp_node->right->right->height;
						tmp_node->left->height = tmp_height + 1;
						tmp_node->right->height = tmp_height + 1;
						tmp_node->height = tmp_height + 2;
				}
			}
			else /* update height even if there
			was no rotation */
			{ 
				if( tmp_node->left->height > 	tmp_node->right->height )
					tmp_node->height = tmp_node->left->height + 1;
				else
					tmp_node->height =	tmp_node->right->height + 1;
			}
			if( tmp_node->height == old_height )
				finished = 1;
			}
			
			remove_stack();
			
			}
			

}