第一题目
#include <stdio.h> #define SIZE 5 static int F=0; static int counts=0; typedef struct Queue { int rear; int num_str[SIZE]; }Queue,*queue; void inite(queue h); void Enterqueue(queue h,int value); void dequeue(queue h); int isfully(queue h); int main() { Queue queue; inite(&queue); Enterqueue(&queue, 10); Enterqueue(&queue, 20); Enterqueue(&queue, 30); Enterqueue(&queue, 40); Enterqueue(&queue, 50); printf("入队%d个元素\n",counts); printf("出队\n"); dequeue(&queue); dequeue(&queue); dequeue(&queue); dequeue(&queue); } int isfully(queue h) { if(((h->rear+1)%SIZE)==0) return 1; return 0; } void inite(queue h) { h->rear=0; } void Enterqueue(queue h,int value) { if(!isfully(h)) { h->num_str[h->rear]=value; h->rear=(h->rear+1)%SIZE; counts++; } else { printf("队列满了\n"); } } void dequeue(queue h) { printf("%d ",h->num_str[F]); F=(F+1)%SIZE; }
第二题
#include <stdio.h> #include <stdlib.h> typedef struct Queue_data { int data; struct Queue_data * next; }queue_data; typedef struct { struct Queue_data * rear; }queue; queue * creates(queue_data * head); queue_data * create(); queue * enqu(queue * tmp,queue_data * heads); void depue(queue * tmp,queue_data * heads); int isempty(queue * tmp,queue_data * heads); queue * set_empty(queue * tmp,queue_data * heads); int main() { queue_data * head; queue * tmp; head=create(); tmp=creates(head); enqu(tmp,head); depue(tmp,head); tmp=set_empty(tmp,head); if(isempty(tmp,head)) { printf("队列已经置空\n"); } depue(tmp,head); printf("\n"); return 0; } queue * set_empty(queue * tmp,queue_data * heads) { queue_data * temp; tmp->rear=tmp->rear->next; while(tmp->rear!=tmp->rear->next) { temp=tmp->rear->next; tmp->rear->next=temp->next; free(temp); } return tmp; } int isempty(queue * tmp,queue_data * heads) { return tmp->rear==heads; } queue_data * create() { queue_data * head=(queue_data *)malloc(sizeof(queue_data)); head->data=0; head->next=NULL; return head; } queue * creates(queue_data * head) { queue * tmp=(queue *)malloc(sizeof(queue)); tmp->rear=head; return tmp; } queue * enqu(queue * tmp,queue_data * heads) { queue_data * h=heads; queue * t=tmp; int num; printf("请输入你的入队数据\n"); while(scanf("%d",&num)&&num!=-1) { queue_data * add=(queue_data *)malloc(sizeof(queue_data)); add->data=num; add->next=h->next; h->next=add; h=add; } t->rear=h; h->next=heads; return tmp; } void depue(queue * tmp,queue_data * heads) { if(!isempty(tmp,heads)) { queue * t=tmp; queue_data * h=heads->next; while(h!=t->rear) { queue_data * temp=h; printf("%d ",h->data); h=h->next; //free(temp); } printf("%d ",h->data); printf("\n"); } else { printf("空表\n"); exit(0); } }
第三题目
#include <stdio.h> #include <stdlib.h> typedef struct Stack//栈定义 { int num[10]; int rear; }stack; typedef struct Link//队列定义 { int nums[10]; int rear_s; int front_s; }link; stack *push(stack * temp_x,int num); link *enque(link * temp_xs,int nums); int deque(link * temp); int main() { stack *temp=(stack *)malloc(sizeof(stack)); temp->rear=-1; link *temps=(link *)malloc(sizeof(link));; temps->rear_s=0; temps->front_s=0; int num_s[10]; int num; printf("请输入你的数字\n"); for(int i=0;i<10;i++) { scanf("%d",&num_s[i]); } printf("奇数入栈偶数字入队列\n"); for(int i=0;i<10;i++) { if(num_s[i]%2==0) { temps=enque(temps,num_s[i]); } else { temp=push(temp,num_s[i]); } } for(int i=0;i<temps->rear_s;i++) { temp=push(temp,deque(temps)); temp->rear+1; } printf("重新入栈输出\n"); for(int i=temp->rear;i>=0;i--) { printf("%d ",temp->num[i]); } } int deque(link * temps) { if(temps->front_s!=temps->rear_s) { int num; num=temps->nums[temps->front_s]; temps->front_s=(temps->front_s+1)%10; return num; } } stack * push(stack * temp_x,int num) { temp_x->rear+=1; temp_x->num[(temp_x->rear)]=num; return temp_x; } link * enque(link * temp_xs,int num) { temp_xs->nums[temp_xs->rear_s]=num; temp_xs->rear_s=(temp_xs->rear_s+1)%10; return temp_xs; }
字符串
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct String { char str[10]; }string; void replace(string * s,string * t,string * p); int main() { string *s=(string *)malloc(sizeof(string)); string *t=(string *)malloc(sizeof(string)); string *q=(string *)malloc(sizeof(string)); char str_re[10]; strcpy(s->str,"i love you"); printf("i love you\n"); strcpy(t->str,"love"); printf("匹配词为love\n"); printf("输入你要替换的字符串\n"); scanf("%s",str_re); strcpy(q->str,str_re); replace(s,t,q); } void replace(string *s,string *t,string * q) { int str_len=strlen(s->str); int str1_len=strlen(t->str); int str2_len=strlen(q->str); int index; int remove_len=str1_len-str2_len; int i=0,j=0; while(i<str_len && j<str1_len) { if(s->str[i]==t->str[j]) { i++; j++; } else { i=i-j+1; j=0; } } if(j==str1_len) { index=i-str1_len; } if(remove_len==0||remove_len>0) { int j=0; for(int i=index;j<str2_len;j++,i++) { s->str[i]=q->str[j]; } } else { remove_len=-1*remove_len; int i; int j=0; int p; for(i=index;j<str1_len;i++,j++) { s->str[i]=q->str[j]; index++; } printf("\n"); for(int k=0;k<remove_len;k++) { for(p=strlen(s->str);p>strlen(s->str+i);p--) { s->str[p+1]=s->str[p]; } s->str[index++]=q->str[j++]; i++; } } puts(s->str); }
标签:tmp,题目,temp,int,queue,data,rear From: https://www.cnblogs.com/whpazz/p/16796118.html