#include#include #define maxsize 10 typedef struct Data { int num; char name[20]; }Data; typedef struct sqqueue { struct Data data[maxsize]; int front, rear; }sqqueue; void init(sqqueue* queue)//初始化 { queue->front = 0; queue->rear = 0; int i; for (i =0; i data[i].num = 0; strcpy_s(queue->data[i].name, 20, "无"); } } void print(sqqueue* queue)//打印数据 { int i; printf("*存储的数据:n"); for (i = queue->front; i <=(queue->rear-queue->front+maxsize)%maxsize; i++) { printf("%2d)num:%d ",i+1, queue->data[i].num); printf("name:"); puts(queue->data[i].name); } printf("n"); } void insert(sqqueue* queue, Data new)//入列 { queue->data[queue->rear].num = new.num; strcpy_s(queue->data[queue->rear].name, 20, new.name); queue->rear = (queue->rear + 1) % maxsize; } Data delete(sqqueue* queue)//出列 { Data n; n.num = queue->data[queue->front].num; strcpy_s(n.name, 20, queue->data[queue->front].name); queue->data[queue->front].num = 0; strcpy_s(queue->data[queue->front].name, 20, "无"); queue->front = (queue->front + 1) % maxsize; return n; } Data gethead(sqqueue* queue)//返回队头元素 { return queue->data[queue->front]; } int main() { sqqueue queue; init(&queue); int n; printf("*请输入存储的数据个数:"); scanf_s("%d", &n); for (int i = 0; i < n; i++) { Data b; printf("%d)请输入第%d个num:",i+1,i+1); scanf_s("%d", &b.num); printf(" 请输入name:"); getchar(); gets(b.name); insert(&queue, b); } printf("n"); delete(&queue); print(&queue); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)