• 数据结构与算法之循环双链表


    循环双链表

    #include <stdio.h>
    #include <malloc.h>
    typedef char Elemtype;
    typedef struct DNode		
    {
    	Elemtype date;
    	struct DNode *prior;	
    	struct DNode *next;		
    } DLinkNode;
    void CreateListF(DLinkNode *&L,Elemtype a[],int n)            //头插法建立循环双链表
    {
    	DLinkNode *s;int i=0;
    	L=(DLinkNode *)malloc(sizeof(DLinkNode));  	
    	L->next=NULL;
    	for (i;i<n;i++)
    	{	
    		s=(DLinkNode *)malloc(sizeof(DLinkNode));
    		s->date=a[i];
    		s->next=L->next;			
    		if (L->next!=NULL) L->next->prior=s;
    		L->next=s;s->prior=L;
    	}
    	s=L->next; 
    	while (s->next!=NULL)
    		s=s->next;
    	s->next=L;						
    	L->prior=s;				
    
    }
    /*******************************************
    ********************************************/ 
    void CreateListR(DLinkNode *&L,Elemtype a[],int n)    //尾插法创建链表    
    {
    	DLinkNode *s,*r;int i;
    	L=(DLinkNode *)malloc(sizeof(DLinkNode)); 
    	L->next=NULL;
    	r=L;					
    	for (i=0;i<n;i++)
    	{	
    		s=(DLinkNode *)malloc(sizeof(DLinkNode));
    		s->date=a[i];
    		r->next=s;s->prior=r;	
    		r=s;
    	}
    	r->next=L;				
    	L->prior=r;				
    }
    /*******************************************
    ********************************************/ 
    void InitList(DLinkNode *&L)      //初始化链表 
    {
    	L=(DLinkNode *)malloc(sizeof(DLinkNode));  	
    	L->prior=L->next=L;
    }
    /*******************************************
    ********************************************/ 
    void DestroyList(DLinkNode *&L)              // 释放链表 
    {
    	DLinkNode *p=L,*q=p->next;
    	while (q!=L)
    	{ 
    		free(p);
    		p=q;
    		q=p->next;
    	}
    	free(p);
    }
    /*******************************************
    ********************************************/ 
    bool ListEmpty(DLinkNode *L)             //判空 
    {
    	return(L->next==L);
    }
    /*******************************************
    ********************************************/ 
    int ListLength(DLinkNode *L)          //计算链表长度 
    {
    	DLinkNode *p=L;
    	int i=0;
    	while (p->next!=L)
    	{
    		i++;
    		p=p->next;
    	}
    	return(i);
    }
    /*******************************************
    ********************************************/ 
    void DispList(DLinkNode *L)            //输出链表 
    {
    	DLinkNode *p=L->next;
    	while (p!=L)
    	{
    		printf("%c ",p->date);
    		p=p->next;
    	}
    	printf("
    ");
    }
    /*******************************************
    ********************************************/ 
    bool GetElem(DLinkNode *L,int i)    //根据元素查下标 
    {
    	int j=0;
    	DLinkNode *p;
    	if (L->next!=L)		
    	{
    		if (i==1)
    		{
    			printf("L->next->date");
    		}
    		else			
    		{
    			p=L->next;
    			while (j<i-1 && p!=L)
    			{
    				j++;
    				p=p->next;
    			}
    			if (p==L)
    				return false;
    			else
    			{
    				printf("%d",p->date);
    			}
    		}
    	}
    	else				
    		return 0;
    }
    /*******************************************
    ********************************************/ 
    int LocateElem(DLinkNode *L,Elemtype e)      //根据下标查元素 
    {
    	int n=1;
    	DLinkNode *p=L->next;
    	while (p!=NULL && p->date!=e)
    	{
    		n++;
    		p=p->next;
    	}
    	if (p==NULL)
    		return(0);
    	else
    		return(n);
    }
    /*******************************************
    ********************************************/ 
    bool ListInsert(DLinkNode *&L,int i,Elemtype e)      //插入元素 
    {
    	int j=0;
    	DLinkNode *p=L,*s;
    	if (p->next==L)				
    	{	
    		s=(DLinkNode *)malloc(sizeof(DLinkNode));
    		s->date=e;
    		p->next=s;s->next=p;
    		p->prior=s;s->prior=p;
    		return true;
    	}
    	else if (i==1)					
    	{
    		s=(DLinkNode *)malloc(sizeof(DLinkNode));	
    		s->date=e;
    		s->next=p->next;p->next=s;	
    		s->next->prior=s;s->prior=p;
    		return true;
    	}
    	else
    	{	
    		p=L->next;
    		while (j<i-2 && p!=L)
    		{	j++;
    			p=p->next;
    		}
    		if (p==L)				
    			return false;
    		else					
    		{
    			s=(DLinkNode *)malloc(sizeof(DLinkNode));	
    			s->date=e;	
    			s->next=p->next;
    			if (p->next!=NULL) p->next->prior=s;
    			s->prior=p;
    			p->next=s;
    			return true;
    		}
    	}
    }
    /*******************************************
    ********************************************/ 
    bool ListDelete(DLinkNode *&L,int i,Elemtype &e)    //删除结点 
    {
    	int j=0;
    	DLinkNode *p=L,*q;
    	if (p->next!=L)					
    	{	
    		if (i==1)					
    		{	
    			q=L->next;				
    			e=q->date;
    			L->next=q->next;
    			q->next->prior=L;
    			free(q);
    			return true;
    		}
    		else						
    		{	
    			p=L->next;
    			while (j<i-2 && p!=NULL)		
    			{
    				j++;
    				p=p->next;
    			}
    			if (p==NULL)				
    				return false;
    			else					
    			{
    				q=p->next;				
    				if (q==NULL) return 0;	
    				e=q->date;
    				p->next=q->next;		
    				if (p->next!=NULL) p->next->prior=p;
    				free(q);			
    				return true;
    			}
    		}
    	}
    	else return false;					
    }
    /*******************************************
    ********************************************/ 
    int main()
    {
        DLinkNode *L;
        Elemtype a[5]={'a','b','c','d','e'};       
        Elemtype e;
        InitList(L);                        //初始化链表L
        printf("初始化完成
    ");
     /*******************************************
    ********************************************/   
        CreateListF(L,a,5);                  //依次插入a,b,c,d,e
        printf("头插入元素后输出的链表为:
    ");
    /*******************************************
    ********************************************/
        DispList(L);                        //输出链表L
    /*******************************************
    ********************************************/
        printf("此链表长度为:");
        printf("%d
    ",ListLength(L));            //输出链表L长度
    /*******************************************
    ********************************************/ 
        if(ListEmpty(L)==false)                  //判断链表L是否为空
            printf("不为空
    ");
        else
            printf("为空
    ");
    /*******************************************
    ********************************************/
        printf("链表的第3个元素为:");   //输出链表的第3个元素 
        GetElem(L,3);
    /*******************************************
    ********************************************/ 
        printf("元素a的位置为:");         //查找a的位置 
        LocateElem(L,'a');
    /*******************************************
    ********************************************/ 
        ListInsert(L,4,'f');            //在第4个元素位置上插入元素f 
        printf("插入元素后的链表为:
    ");
    /*******************************************
    ********************************************/
        DispList(L);                        //输出链表L
    /*******************************************
    ********************************************/
        ListDelete(L,3,e);              //删除L的第3个元素
    /*******************************************
    ********************************************/   
        printf("删除元素后的链表为:
    ");
        DispList(L);                       //输出链表L
    /*******************************************
    ********************************************/ 
        DestroyList(L);                   //释放链表L
        printf("释放链表");
        return 0;
    }
    
  • 相关阅读:
    快乐
    端午节杂记
    消失一段时间
    做好自己,做最好的自己
    童童的儿童节
    *完成第一个Java的程序(Jogl的一个窗口框架)
    *JS实现HashTable的例子
    *JavaScript检查ActiveX控件是否已经安装过
    *DOS命令REG操作注册表
    *汇总c#.net常用函数和方法集
  • 原文地址:https://www.cnblogs.com/AmosAlbert/p/12832381.html
Copyright © 2020-2023  润新知