作业:链栈,自己实现一遍,但是节点存储不是整数,存储学生信息(年龄,分数,姓名)三级引用。
1、建立学生信息结构体,将data改为学生信息结构体类型。
2、循环入栈和入队。
#include <myhead.h>
 #define N 4
 typedef struct//重定义数据结构 
 {
     int age;
     int score;
     char name[20];
 }stu;
 typedef struct node
 {
     stu data;
     struct node *next;
 }Node;
 typedef struct
 {
     int len;
     Node *rear;
     Node *front;
 }Queue,*pqueue;
 pqueue creat_queue()
 {
     pqueue p=malloc(sizeof(Queue));//创建新队列
     if(p==NULL)//判断是否创建成功
     {
         printf("队列创建失败\n");
     }
     p->rear=NULL;//队头指针指向空指针
     p->front=NULL;//同上
     p->len=0;//队列长度置零;
     return p;//返回队头地址
 }
 void in_queue(pqueue L)
 {
     if(L==NULL)//判断
     {
         printf("队列不存在\n");
     }
     Node *p=malloc(sizeof(Node));//创建新节点
     printf("请输入学生信息:");
     scanf("%d%s%d",&p->data.age,p->data.name,&p->data.score);
     if(L->rear==NULL)//判断是否是第一个节点
     {
         L->rear=p;
         L->front=p;
     }
     else
     {
         L->rear->next=p;
         L->rear=p;
     }
     L->len++;
     printf("输入成功\n");
 }
 void output_queue(pqueue L)
 {
     if(L==NULL||L->len==0)//判断
     {
         printf("队列不存在或为空\n");
     }
     Node *p=L->front;
     for(int i=0;i<L->len;i++)//循环输出
     {
         printf("%d\t%s\t%d\n",p->data.age,p->data.name,p->data.score);
         p=p->next;
     }
 }
 void out_queue(pqueue L)
 {
     if(L==NULL)//判断
     {
         printf("队列不存在\n");
     }
     Node *p=L->front;
     L->front=L->front->next;//指向下一个节点
     printf("弹出:\n");
     printf("%d\t%s\t%d\n",p->data.age,p->data.name,p->data.score);
     free(p);//销毁弹出节点
     p=NULL;
     L->len--;
 }
 int main(int argc, const char *argv[])
 {
     pqueue L=creat_queue();
     for(int i=0;i<N;i++)//循环输入
     {
         in_queue(L);
     }
     output_queue(L);
     out_queue(L);
     return 0;
 }






















