The second day on the National Day

#include 

typedef char datatype;
typedef struct dblist
{
    union
    {   
        datatype data;  
        int len;                                                                                                                                                                        
    };  
    struct dblist *pre;   
    struct dblist *next;  
}Node,*pdblist;

pdblist create_head()
{
    pdblist H = (pdblist)malloc(sizeof(Node));
    if(H==NULL)
    {   
        printf("申请空间失败\n");
        return NULL;
    }   
    H->len=0;
    H->pre=NULL;
    H->next=NULL;
    return H;
}

pdblist create_node(datatype data)
{
    pdblist new = (pdblist)malloc(sizeof(Node));
    if(new==NULL)                                                                                                                                                                       
    {   
        printf("申请空间失败\n");
        return NULL;
    }   
    new->data=data;
    new->pre=NULL;   
    new->next=NULL;  
    return new;
}

int insert_head(pdblist H,datatype data)
{
    if(H==NULL)
    {   
        printf("入参为空\n");
        return -1; 
    }
    pdblist new = create_node(data);
    new->next = H->next;
    if(H->next!=NULL)
    {
        H->next->pre=new;
    }
    new->pre=H;
    H->next=new;
    H->len++;
	printf("%c\n",H->next->data);
    return 0;
}


int dele_tail(pdblist H)
{
    if(H==NULL)
    {
        printf("入参为空\n");
        return -1;
    }
    if(H->next==NULL)
    {
        printf("双向链表为空\n");
        return -2;
    }
    pdblist p = H;
    while(p->next!=NULL)
    {
        p = p->next;
    }
    p->pre->next = p->next;   
    free(p);
    p=NULL;
    H->len--;
    return 0;
}

int main(int argc, const char *argv[])
{
	pdblist H = create_head();
	for(char i='a';i<='z';i++)
	{
		insert_head(H,i);
		dele_tail(H);
	}
	return 0;
}

你可能感兴趣的:(数据结构)