zoukankan      html  css  js  c++  java
  • 数据结构--链队列基本操作

    #include<stdio.h>              //链队列的基本操作,首先我们要知道队列是在队头进行删除,队尾进行插入 
    #include<stdlib.h>
    #include<malloc.h> 
    typedef struct Sq
    {
    	int data;            //数据域 
    	struct Sq *next;           //指针域 
    }Sq; 
    typedef struct
    {
    	Sq *front;            //指向队列的队头 
    	Sq *rear;			 //指向对了的队尾 
    }Squeue;
    /*到此,队列的数据结构类型定义完毕*/ 
    void init_Squeue(Squeue *p)      //队列的初始化 
    {
    	p->front=(Sq *)malloc(sizeof(Sq));
    	p->rear=p->front;
    	p->front->next=NULL;
    	
    }
    
    void delete_Squeue(Squeue *p,int t)    //出栈 
    {
    	Sq *e;
    	e=p->front->next;
    	while(t--)
    	{
    		p->front=e; 
    		e=e->next;
    			
    	}
    	
    } 
    void insert_Squeue(Squeue *p,int m)    //入栈 
    {
    	while(m--)
    	{
    		Sq *t;
    		t=p->rear;
    		p->rear=(Sq *)malloc(sizeof(Sq));
    		t->next=p->rear;
    		scanf("%d",&p->rear->data);
    		//printf("%d ",p->rear->data);
    	}
    	p->rear->next=NULL;
    } 
    int getlength(Squeue *p)        //返回栈的长度 
    {
    	Sq *q;
    	int i;
    	if(p->front==NULL) return 0;
    	q=p->front->next;
    	for(i=0;q!=p->rear;q=q->next)
    	i++;
    	return i+1;
    		
    }
    int is_empty(Squeue *p)        //判断是否为空 
    {
    	if(p->front==p->rear) printf("队列为空!");
    	else printf("队列不为空!");
    } 
    void printf_Squeue(Squeue *p)
    {
    	printf("队列里面还有如下元素"); 
    	Sq *q;
    	q=p->front->next;
    	for(;q!=p->rear; q=q->next) 
    	printf("%d ",q->data);
    	printf("%d
    ",q->data);
    } 
    int main(void)
    {
    	void init_Squeue(Squeue *p);
    	void delete_Squeue(Squeue *p,int t);
    	void insert_Squeue(Squeue *p,int m);
    	int getlength(Squeue *p) ;
    	int is_empty(Squeue *p);
    	int n;
    	scanf("%d",&n);
    	while(n--)
    	{
    		int m,t,l; 
    		Squeue y,*p;
    		p=&y;
    		printf("请输入要入队列元素个数"); printf("
    ");
    		scanf("%d",&m);
    		init_Squeue(p);
    		insert_Squeue(p,m);
    		printf("请输入要出队列元素个数"); printf("
    ");
    		scanf("%d",&t);
    		delete_Squeue(p,t);
    		l=getlength(p);
    		printf("如今队列的长度为:"); printf("
    ");
    		printf("%d
    ",l); 
    		is_empty(p); printf("
    ");
    		printf_Squeue(p);
    		printf("
    ");printf("
    ");
    	}
    	return 0;
    } 
    <img src="https://img-blog.csdn.net/20160415192028976?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
    
    
    

    非学无以广才,非志无以成学! 【Magic_chao

  • 相关阅读:
    Android sdk 下载路径
    centos修改用户用户组
    centos7 通过shell切换root用户
    java 服务上传图片到linux没有读写权限
    Mybatis第二天
    Mybatis第一天
    反射
    注解
    多线程第二天
    java---过滤器、监听器
  • 原文地址:https://www.cnblogs.com/logo-88/p/9649215.html
Copyright © 2011-2022 走看看