双向循环链表
1>双向循环链表的节点创建
2>双向循环链表头插
3>双向循环链表遍历
4>双向循环链表尾插
5>双向循环链表头删
6>双向循环链表尾删
doubleLoopLink_main.c
#include "doubleLoopLink_head.h"
int main(int argc, const char *argv[])
{
dlln head=NULL;
int n;
printf("please enter n=");
scanf("%d",&n);
for(int i=0;i<n;i++){
datatype a;
printf("the %d element:",i+1);
scanf(" %c",&a);
//head=insert_from_head(head,a);
head=insert_rear(head,a);
}
puts("");
printf("-------------delete from head\n");
head=delete_from_head(head);
printf("-------------output\n");
output_dll(head);
printf("-------------delete rear\n");
head=delete_rear(head);
printf("-------------output\n");
output_dll(head);
return 0;
}
doubleLoopLink_func.c
#include "doubleLoopLink_head.h"
dlln create_dlln(){
dlln node=(dlln)malloc(sizeof(struct doubleLoopLinkNode));
if(NULL==node){return NULL;}
node->data='\0';
node->next=node;
node->prev=node;
return node;
}
dlln insert_from_head(dlln head,datatype data){
dlln node=create_dlln();
if(NULL==node){return head;}
node->data=data;
if(NULL==head){
head=node;
}
else{
node->next=head;
node->prev=head->prev;
head->prev->next=node;
head->prev=node;
head=node;
}
return head;
}
dlln insert_rear(dlln head,datatype element){
dlln node=create_dlln();
if(NULL==node){return head;}
node->data=element;
if(NULL==head) head=node;
else{
node->next=head;
node->prev=head->prev;
head->prev->next=node;
head->prev=node;
}
return head;
}
dlln delete_from_head(dlln head){
if(NULL==head){return head;}
if(head->next==head){
free(head);
head=NULL;
return head;
}
dlln del=head;
head=del->next;
head->prev=del->prev;
del->prev->next=head;
free(del);
del=NULL;
return head;
}
dlln delete_rear(dlln head){
if(NULL==head) return head;
if(head->next==head){
free(head);
head=NULL;
return head;
}
dlln del=head->prev;
del->prev->next=head;
head->prev=del->prev;
free(del);
del=NULL;
return head;
}
int output_dll(dlln head){
if(NULL==head) return FAILED;
dlln p=head;
do{
printf("%c\t",p->data);
p=p->next;
}while(p!=head);
puts("");
return SUCCESS;
}
doubleLoopLink_head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define datatype char
enum status{FAILED=-1,SUCCESS};
typedef struct doubleLoopLinkNode{
datatype data;
struct doubleLoopLinkNode *prev;
struct doubleLoopLinkNode *next;
} *dlln;
dlln create_dlln();
dlln insert_from_head(dlln head,datatype element);
dlln insert_rear(dlln head,datatype element);
dlln delete_from_head(dlln head);
dlln delete_rear(dlln head);
int output_dl(dlln head);
#endif