1双向循环链表
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
typedef int datatype;
typedef struct node{
datatype data;
struct node *next;
struct node *priv;
}*doublelist;
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
doublelist creat_space();
doublelist toucha(doublelist head,datatype element);
doublelist weicha(doublelist head,datatype element);
int output(doublelist head);
doublelist toushan(doublelist head);
doublelist weishan(doublelist head);
#endif
test.c
#include "head.h"
doublelist creat_space()
{
doublelist p=(doublelist)malloc(sizeof(struct node));
if(p==NULL)
return NULL;
p->data=0;
p->next=p;
p->priv=p;
return p;
}
doublelist toucha(doublelist head,datatype element)
{
doublelist s=creat_space();
if(s==NULL)
return head;
s->data=element;
if(head==NULL)
head=s;
else
{
doublelist p=head->priv;
s->next=head;
head->priv=s;
head=s;
head->priv=p;
p->next=head;
}
return head;
}
doublelist weicha(doublelist head,datatype element)
{
doublelist s=creat_space();
if(s==NULL)
return NULL;
s->data=element;
if(head==NULL)
head=s;
else
{
doublelist p=head->priv;
p->next=s;
s->next=head;
s->priv=p;
head->priv=s;
}
return head;
}
int output(doublelist head)
{
doublelist p=head;
if(head==NULL)
return -1;
else
{
#if 1
while(p->next!=head)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d\n",p->data);
#endif
#if 0
while(p!=head)
{
printf("%d ",p->data);
p=p->priv;
}
printf("%d\n",p->data);
#endif
}
return 0;
}
doublelist toushan(doublelist head)
{
if(head==NULL)
return head;
if(head->next==head)
{
free(head);
head=NULL;
}
else
{
doublelist q=head->priv;
doublelist p=head;
q->next=head->next;
head->next->priv=q;
head=head->next;
free(p);
p=NULL;
}
return head;
}
doublelist weishan(doublelist head)
{
if(head==NULL)
return head;
if(head->next==head)
{
free(head);
head=NULL;
}
else
{
doublelist p=head->priv;
p->priv->next=head;
head->priv=p->priv;
free(p);
p=NULL;
}
return head;
}
main.c
#include "head.h"
int main()
{
doublelist head=NULL;
int i=0,n=0;
datatype element;
printf("input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
// head=toushan(head);
head=weishan(head);
int flag=output(head);
// printf("%d\n",flag);
return 0;
}
图
2双向链表(略去main.c head.h)
test.c
#include "head.h"
doublelink creat_space( )
{
doublelink p=(doublelink)malloc(sizeof(struct node));
if(p==NULL)
return NULL;
p->data=0;
p->next=NULL;
p->priv=NULL;
}
doublelink toucha(doublelink head,datatype element)
{
doublelink s=creat_space();
if(s==NULL)
return head;
s->data=element;
if(head==NULL)
head=s;
else
{
s->next=head;
head->priv=s;
head=s;
}
return head;
}
int output(doublelink head)
{
if(head==NULL)
return -1;
else
{
doublelink p=head;
while(p->next!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d\n",p->data);
}
/* while(p!=NULL)
{
printf("%d ",p->data);
p=p->priv;
}
printf("\n");
*/
return 0;
}
doublelink weicha(doublelink head,datatype element)
{
doublelink s=creat_space();
s->data=element;
if(head==NULL)
head=s;
else
{ doublelink p=head;
while(p->next!=NULL)
p=p->next;
p->next=s;
s->priv=p;
}
return head;
}
doublelink toushan(doublelink head)
{
if(head==NULL)
return head;
doublelink p=head;
head=head->next;
if(head!=NULL)
head->priv=NULL;
free(p);
p=NULL;
return head;
}
doublelink weishan(doublelink head)
{
if(head==NULL)
return head;
doublelink p=head;
if(head->next==NULL)
{
free(head);
head==NULL;
}
else
{
while(p->next!=NULL)
p=p->next;
// if(p->priv!=NULL)
p->priv->next=NULL;
free(p);
p=NULL;
}
return head;
}
图
3单向循环链表(略去main.c head.htest.c)
test.c
#include "head.h"
linklist creat_node()
{
linklist p=(linklist)malloc(sizeof(struct node));
if(p==NULL)
return NULL;
else {
p->data=0;
p->next=p;
}
return p;
}
linklist toucha(linklist head,datatype element)
{
linklist s=creat_node();
if(s==NULL)
return head;
s->data=element;
if(head==NULL)
head=s;
else
{
linklist p=head;
while(p->next!=head)
p=p->next;
s->next=head;
head=s;
p->next=head;
}
return head;
}
void output(linklist head)
{
if(head==NULL)
printf("empty\n");
else{
linklist p=head;
do
{
printf("%d ",p->data);
p=p->next;
}while(p!=head);
printf("\n");
}
}
linklist weicha(linklist head,datatype element)
{
linklist s=creat_node();
if(s==NULL)
return head;
s->data=element;
if(head==NULL)
head=s;
else
{
linklist p=head;
while(p->next!=head)
p=p->next;
p->next=s;
s->next=head;
}
return head;
}
linklist toushan(linklist head)
{
if(head==NULL)
return head;
if(head->next==head)
{
free(head);
head=NULL;
}
else
{
linklist s=head;
linklist p=head;
while(p->next!=head)
p=p->next;
head=head->next;
free(s);
s=NULL;
p->next=head;
}
return head;
}
linklist weishan(linklist head)
{
if(head==NULL)
return head;
else if(head->next==head)
{
free(head);
head=NULL;
}
else
{
linklist p=head;
while(p->next->next!=head)
p=p->next;
free(p->next);
p->next=head;
}
return head;
}
图