程序人生——苏嵌第9天

苏嵌教育                                                                     暑期实习


                                          学习日志                                            姓名:胡昊          日期:2018.7.24



                                                

  • 今日学习任务:                                 学习栈和队列。

  • 今日任务完成情况:                         基本完成任务,掌握了栈和队列的基本操作。主要代码:
    /*************************************************************************
    	> File Name: work2.c
    	> Author: HuHao
    	> Mail: [email protected] 
    	> Created Time: 2018年07月24日 星期二 10时42分35秒
     ************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    #define MAX 100
    
    //定义ElemType结构类型
    typedef int ElemType;
    
    //定义队列结构体
    typedef struct Node
    {
    	ElemType data[MAX];
    	int front;
    	int rear;
    }Node,*Queue;
    
    Queue Q;
    ElemType e;
    
    //功能:初始化一个循环队列
    bool initQueue(Queue *Q)
    {
    	*Q=(Queue)malloc(sizeof(Node));
    	(*Q)->front=0;
    	(*Q)->rear=0;
    	return true;
    }
    
    //入队列操作
    bool enterQueue(Queue *Q,ElemType e)
    {
    	if(((*Q)->rear+1)%MAX==(*Q)->front)
    		return false;
    	(*Q)->data[(*Q)->rear]=e;
    	(*Q)->rear=((*Q)->rear+1)%MAX;
    	return true;
    }
    
    //若队列不空,则删除Q中队头元素,用e返回其值
    bool deleteQueue(Queue *Q,ElemType *e)
    {
    	if((*Q)->rear==(*Q)->front)
    		return false;
    	*e=(*Q)->data[(*Q)->front];
    	(*Q)->front=((*Q)->front+1)%MAX;
    	return true;
    }
    
    //功能:获得队列当前存储的长度  返回值:当前长度值
    int lengthQueue(Queue Q)
    {
    	return ((Q->rear)-Q->front+MAX)%MAX;
    }
    
    
    int main()
    {
    	int i=0,length=0;
    	ElemType data,dat;
    	initQueue(&Q);
    	data=10;
    	printf("Enter queue:\n");
    	while(data>0)
    	{
    		enterQueue(&Q,data);
    		printf("%d\t",data);
    		data--;
    	}
    	printf("\n");
    	length=lengthQueue(Q);
    	printf("the length of queue is %d\n",length);
    	printf("the datas go out of queue is\n");
    	for(i=0;i<length;i++)
    	{
    		deleteQueue(&Q,&dat);
    		printf("%d\t",dat);
    	}
    	printf("\n");
    	return 0;
    }
    
    /*************************************************************************
    	> File Name: stack.c
    	> Author: HuHao
    	> Mail: [email protected] 
    	> Created Time: 2018年07月24日 星期二 07时57分07秒
     ************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    #define TRUE 1
    #define FALSE 0
    //定义栈里的结构类型
    typedef int ElemType;
    typedef struct Node
    {
    	ElemType data;
    	struct Node *next;
    }Node;
    
    typedef struct Node *Stack;
    //定义输出函数
    bool visit(ElemType data)
    {
    	printf("data=%d\n",data);
    	return true;
    }
    
    //初始化栈
    bool initStack(Stack *s)
    {
    	*s=(Stack)malloc(sizeof(Node));
    	if(*s==NULL)
    	{
    		printf("Init stack error!\n");
    		return false;
    	}
    	(*s)->next=NULL;
    	printf("Init stack success!\n");
    	return true;
    }
    
    //判断栈是否为空
    bool emptyStack(Stack s)
    {
    	if(s->next==NULL)
    		return true;
    	else
    		return false;
    }
    
    //清理栈中顶部元素
    bool clearStack(Stack *s)
    {
    	if(emptyStack(*s)==true)
    	{
    		printf("Stack is NULL!\n");
    		return false;
    	}
    	Stack p;
    	p=(*s)->next;
    	free(p);
    	return true;
    }
    
    //求栈长度
    int lengthStack(Stack s)
    {
    	if(emptyStack(s)==true)
    	{
    		printf("Stack is NULL!\n");
    		return false;
    	}
    	int i=0;
    	while(s->next!=NULL)
    		i++;
    	return i;
    }
    
    //入栈操作
    bool insertStack(Stack *s)
    {
    	Stack p;
    	p=(Stack)malloc(sizeof(Node));
    	printf("Please input data:");
    	scanf("%d",&p->data);
    	p->next=(*s)->next;
    	(*s)->next=p;
    	return true;
    }
    
    //删除栈第一个元素操作
    bool deleteStack(Stack *s)
    {
    	Stack p;
    	p=*s;
    	p=p->next;
    	(*s)->next=p->next;
    	free(p);
    	return true;
    }
    
    int main()
    {
    	return 0;
    }
    

  • 今日开发中出现的问题汇总:         栈和队列的定义,操作,指针的传递。

  • 今日未解决问题:                             无

  • 自我评价:                                         自我感觉良好,基本掌握了新知识。

  • 其他:                                                  随着一天的学习落幕,收获满满,期待着明天的学习和明天的收获。
发布了9 篇原创文章 · 获赞 2 · 访问量 652

猜你喜欢

转载自blog.csdn.net/a45654645/article/details/81191696
今日推荐