zoukankan      html  css  js  c++  java
  • 数据结构与算法之循环单链表

    循环单链表

    #include <stdio.h>
    #include <malloc.h>
    typedef int Elemtype;
    typedef struct LNode		
    {
    	Elemtype date;
        struct LNode *next;
    } LinkNode;
    /*******************************************
    ********************************************/
    void CreateListF(LinkNode *&L,Elemtype a[],int n)   //头插法建立循环单链表
    { 
    	LinkNode *s;int i;
    	L=(LinkNode *)malloc(sizeof(LinkNode));  	
    	L->next=NULL;
    	for (i=0;i<n;i++)
    	{	
    		s=(LinkNode *)malloc(sizeof(LinkNode));
    		s->date=a[i];
    		s->next=L->next;			
    		L->next=s;
    	}
    	s=L->next;	
    	while (s->next!=NULL)		
    		s=s->next;
    	s->next=L;					
    
    }
    /*******************************************
    ********************************************/
    void CreateListR(LinkNode *&L,Elemtype a[],int n)  //尾插法建立循环单链表
    {
    	LinkNode *s,*r;int i;
    	L=(LinkNode *)malloc(sizeof(LinkNode));  	
    	L->next=NULL;
    	r=L;				
    	for (i=0;i<n;i++)
    	{	
    		s=(LinkNode *)malloc(sizeof(LinkNode));
    		s->date=a[i];
    		r->next=s;			
    		r=s;
    	}
    	r->next=L;				
    }
    /*******************************************
    ********************************************/
    void InitList(LinkNode *&L)
    {
    	L=(LinkNode *)malloc(sizeof(LinkNode));	//创建头结点,并指向自己 
    	L->next=L;
    }
    /*******************************************
    ********************************************/
    void DestroyList(LinkNode *&L)
    {
    	LinkNode *p=L,*q=p->next;
    	while (q!=L)
    	{
    		free(p);
    		p=q;
    		q=p->next;
    	}
    	free(p);
    }
    /*******************************************
    ********************************************/
    bool ListEmpty(LinkNode *L)//判空 
    {
    	return(L->next==L);
    }
    /*******************************************
    ********************************************/
    int ListLength(LinkNode *L)//计算长度 
    {
    	LinkNode *p=L;int i=0;
    	while (p->next!=L)
    	{
    		i++;
    		p=p->next;
    	}
    	return(i);
    }
    /*******************************************
    ********************************************/
    void DispList(LinkNode *L)//输出链表 
    {
    	LinkNode *p=L->next;
    	while (p!=L)
    	{
    		printf("%c ",p->date);
    		p=p->next;
    	}
    	printf("
    ");
    }
    /*******************************************
    ********************************************/
    bool GetElem(LinkNode *L,int i)//根据元素查下标 
    {
    	int j=0;
    	LinkNode *p;
    	if (L->next!=L)	
    	{
    		if (i==1)
    		{
    			printf("%c",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 false;
    }
    /*******************************************
    ********************************************/
    int LocateElem(LinkNode *L,Elemtype e)//根据下标查元素 
    {
    	LinkNode *p=L->next;
    	int n=1;
    	while (p!=L && p->date!=e)
    	{
    		p=p->next;
    		n++;
    	}
    	if (p==L)
    		return(0);
    	else
    		return(n);
    }
    /*******************************************
    ********************************************/
    bool ListInsert(LinkNode *&L,int i,Elemtype e)//插入元素 
    {
    	int j=0;
    	LinkNode *p=L,*s;
    	if (p->next==L || i==1)		
    	{
    		s=(LinkNode *)malloc(sizeof(LinkNode));	
    		s->date=e;								
    		s->next=p->next;		
    		p->next=s;
    		return true;
    	}
    	else
    	{
    		p=L->next;
    		while (j<i-2 && p!=L)
    		{
    			j++;
    			p=p->next;
    		}
    		if (p==L)			
    			return false;
    		else				
    		{
    			s=(LinkNode *)malloc(sizeof(LinkNode));	
    			s->date=e;								
    			s->next=p->next;						
    			p->next=s;
    			return true;
    		}
    	}
    }
    /*******************************************
    ********************************************/
    bool ListDelete(LinkNode *&L,int i,Elemtype &e)//删除元素 
    {
    	int j=0;
    	LinkNode *p=L,*q;
    	if (p->next!=L)					
    	{
    		if (i==1)				
    		{
    			q=L->next;			
    			e=q->date;
    			L->next=q->next;
    			free(q);
    			return true;
    		}
    		else						
    		{
    			p=L->next;
    			while (j<i-2 && p!=L)
    			{
    				j++;
    				p=p->next;
    			}
    			if (p==L)			
    				return false;
    			else					
    			{
    				q=p->next;			
    				e=q->date;
    				p->next=q->next;	
    				free(q);			
    				return true;
    			}
    		}
    	}
    	else return false;
    }
    /*******************************************
    ********************************************/
    int main()
    {
        LinkNode *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;
    }
    
  • 相关阅读:
    库函数的使用:POJ1488-TEX Quotes(getline()的使用)
    字符串:HDU3064-最长回文
    字符串:HDU5371-Hotaru's problem(manacher 的应用)
    字符串-POJ3974-Palindrome
    Oracle数据库编程:在JDBC中应用Oracle
    C++、GDAL创建shapefile文件
    CStdioFile
    Js中获取frames中的元素
    约瑟夫环问题(循环链表)
    【Oracle 函数索引】一次数据库的优化过程
  • 原文地址:https://www.cnblogs.com/AmosAlbert/p/12832382.html
Copyright © 2011-2022 走看看