char* Top(stack s) { if(!Isempty(s)) return s->next->symbol; printf("Empty stack!\n"); return 0; }
void Clearqueue(Queue *pqueue) { while(Isempty(pqueue)!=1) { Dequeue(pqueue); } }
void MakeEmpty(stack s) { if(s == NULL) printf("Must use CreatStack first!\n"); else while(!Isempty(s)) Pop(s); }
int main() { Yourstack testing; testing.array=(int*) malloc(sizeof(int)*42); Isempty(testing.increment); Pushintostack(testing.array,&testing.increment,a); Popfromstack(testing.array,&testing.increment); return 0; }
int Popfromstack(int *array, int *increment) { int temp; if(Isempty(*increment)==1) { temp=array[*increment]; *increment=*increment+1; return temp; } else{return -1;} }
void Pop(stack s) { Position first; if(Isempty(s)) printf("Empty stack!\n"); else { first = s->next; s->next = s->next->next; free(first); } }
int Dequeue(Queue *pqueue) { int data; Node* pnode=pqueue->front; if(pnode!=NULL&&Isempty(pqueue)!=1) { data=pnode->ID; pqueue->size--; pqueue->front=pnode->next; free(pnode); if(pqueue->size==0)pqueue->rear=NULL; } return data; }
int main(){ Stack S; ElemType e; Initstack(&S); printf("enter number!\n"); scanf("%d",e); while(e){ Push(&S,e%8); e=e/8; } while(!Isempty(S)){ Pop(&S,e); printf("%d\n",e); } return 0; }
//BFS algorithm int shortestpath(Node *array, int Vnum, int root, int des) { int i,out,ss; //create a queue Queue *head=Initqueue(); Queue *path=Initqueue(); Node *travel,*p; int *color = (int *)malloc(Vnum*sizeof(int)); int *father = (int *)malloc(Vnum*sizeof(int)); for(i=0;i<Vnum;i++)father[i]=i; for(i=0;i<Vnum;i++)color[i]=white; //initialize colar Enqueue(head,root); color[root]=grey; father[root]=root; while(Isempty(head)!=1) { out = Dequeue(head); travel=array+out; for(p=travel->next;p!=NULL;p=p->next) { if(color[p->ID]==white) { Enqueue(head,p->ID); color[p->ID]=grey; father[p->ID]=out; } } color[out]=black; } if(color[des]==white) { printf("Error: there is no path between %d and %d.",root,des); ss=false; } else { //if there is a path, then print it out //for(i=0;i<Vnum;i++)printf("%d is father of %d\n",father[i],i); PrintPath(father,root,des); ss=true; } Clearqueue(head); Destroyqueue(head); free(color); free(father); return ss; }
//en queue Node* Enqueue(Queue *pqueue,int data) { Node *pnode = (Node *)malloc(sizeof(Node)); if(pnode != NULL) { pnode->ID = data; pnode->next = NULL; if(Isempty(pqueue)) { pqueue->front = pnode; } else { pqueue->rear->next = pnode; } pqueue->rear = pnode; pqueue->size++; } return pnode; }
int main(){ Stack S; S.base= (ElemType *)malloc (STACK_INIT *sizeof(ElemType)); if(!S.base) return OVERFLOW; S.top=S.base; S.stacksize=STACK_INIT; return OK; ElemType e; // Initstack(&S); printf("enter element!\n"); scanf("%d",e); while(e){ Push(&S,e%8); e=e/8; } while(!Isempty(S)){ Pop(&S,e); printf("%d\n",e); } return 0; }
void Destroyqueue(Queue *pqueue) { if(Isempty(pqueue)!=1)Clearqueue(pqueue); free(pqueue); }
int main() { char *sign1 = (char*)malloc(sizeof(char)* 10); char *sign2 = (char*)malloc(sizeof(char)* 10); stack s = CreatStack(); while(scanf("%s", sign1) == 1) { printf("7%s7\n", Top(s)); printf("1%s1\n", sign1); if(Isempty(s)) { if(strcmp(sign1, "(") == 0 || strcmp(sign1, "[") == 0 || strcmp(sign1, "{") == 0 || strcmp(sign1, "begin") == 0) { printf("AAAAAAAAAAA\n"); Push(sign1, s); printf("3%s3\n", Top(s)); } else { printf("AWrong!!!\n"); break; } } else { printf("6%s6\n", Top(s)); if(strcmp(sign1, "(") == 0 || strcmp(sign1, "[") == 0 || strcmp(sign1, "{") == 0 || strcmp(sign1, "begin") == 0) { printf("BBBBBBBBBB\n"); Push(sign1, s); printf("4%s4\n", Top(s)); } else if(strcmp(sign1, ")") == 0 || strcmp(sign1, "]") == 0 || strcmp(sign1, "}") == 0 || strcmp(sign1, "end") == 0) { printf("2%s2\n", Top(s)); if((strcmp(Top(s), "(") == 0 && strcmp(sign1, ")") == 0) || (strcmp(Top(s), "[") == 0 && strcmp(sign1, "]") == 0)|| (strcmp(Top(s), "{") == 0 && strcmp(sign1, "}") == 0) || (strcmp(Top(s), "begin") == 0 && strcmp(sign1, "end") == 0)) { printf("CCCCCCCCCCCCCCCC\n"); Pop(s); } else { printf("BWrong!!!\n"); printf("5%s5\n", Top(s)); break; } } } } if(Isempty(s)) printf("You are right!\n"); else printf("CWrong!!!\n"); while(!Isempty(s)) { printf("%s\n", Top(s)); Pop(s); } return 0; }