数据结构PTA
20:C
22:B
27:D
填空
4-2:19
4-4:66
4-5:8
5-x:不加分号
⬇:top = p->next
链队列
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct QNode {
char data;
struct QNode *next;
} QNode;
typedef struct {
QNode *front;
QNode *rear;
} LinkQueue;
void Init(LinkQueue *l) {
l->front = l->rear = (QNode *)malloc(sizeof(QNode));
l->front->next = NULL;
}
void Enqueue(LinkQueue *l, char elem) {
QNode *p = (QNode *)malloc(sizeof(QNode));
p->data = elem;
p->next = NULL;
l->rear->next = p;
l->rear = p;
}
void PrintQueue(LinkQueue *l) {
if (l->front == l->rear) {
printf("Head:NULL\nPop:NULL\n");
return;
}
printf("Head:%c\n", l->front->next->data);
printf("Pop:");
while (l->front != l->rear) {
QNode *p = l->front->next;
printf("%c", p->data);
l->front->next = p->next;
if (l->rear == p) l->rear = l->front;
free(p);
}
printf("\n");
}
void main() {
LinkQueue l;
Init(&l);
char s[100];
scanf("%s", s);
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '#')
break;
Enqueue(&l, s[i]);
}
PrintQueue(&l);
}
十进制转换二进制
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int ElemType;
typedef struct
{ ElemType *base;
ElemType *top;
}SqStack;
void Init(SqStack *s){
s->base = s->top = (int*)malloc(MAXSIZE*sizeof(int));
}
void insert(SqStack *s,int e,int a[]){
while(e!=0){
a[s->top++] = e%2;
e /=2;
}
}
void out(SqStack *s,int a[]){
if(s->base == s->top)
return;
while(s->base != s->top){
printf("%d",a[s->base++]);
}
}
void main(){
SqStack s;
int a[MAXSIZE];
Init(&s);
insert(&s,15,a);
out(&s,a);
}
原文地址:https://blog.csdn.net/m0_60870041/article/details/143747540
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!