#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* function:
* 创建一个双向链表,将26个英文字母通过头插的方式插入,通过为尾删的方式读取并删除
* @param [ in]
* @param [out]
* @return
*/
typedef struct double_link_list{
char data;
struct double_link_list * pre;
struct double_link_list * next;
}node,*pnode;
pnode listInit(){
pnode H=(pnode)malloc(sizeof(node));
if(NULL==H){
printf("__%d__malloc failed",__LINE__);
}
H->data=0;
H->pre=NULL;
H->next=NULL;
return H;
}
int headInsert(pnode H,char data){
if(H==NULL){
puts("null pont pass");
return -1;
}
pnode newNode=(pnode)malloc(sizeof(node));
newNode->data=data;
newNode->next=H->next;
if(H->next!=NULL)
H->next->pre=newNode;
H->next=newNode;
newNode->pre=H;
printf("%c insert success\n",H->next->data);
return 0;
}
int tailDelete(pnode H){
if(H==NULL){
puts("null pont pass");
return -1;
}
pnode p=H;
//p指向最后一个节点
while(p->next!=NULL){
p=p->next;
}
while(p->pre!=NULL){
printf("%c->",p->data);
pnode del=p;
p=p->pre;
free(del);
}
return 0;
}
int print(pnode H){
if(H==NULL){
puts("null pont pass");
return -1;
}
pnode p=H;
//p指向最后一个节点
while(p->next!=NULL){
printf("%c->",p->next->data);
p=p->next;
}
printf("last=%c\n",p->data);
return 0;
}
int main(int argc, const char *argv[])
{
pnode H=listInit();
for (char i='a'; i<='z'; i++)
{
headInsert(H,i);
}
tailDelete(H);
//print(H);
return 0;
}
运行结果:



















