------------------------單鏈錶
#include <stdio.h>
#include <stdlib.h>
typedef struct node *link;
struct node{
unsigned int item;
link next;
};
static link head=NULL;
link make_node(unsigned int item)
{
link p=malloc(sizeof *p);
p->item=item;
p->next=NULL;
return p;
}
void insert(link r)
{
link p=head,q;
if(head==NULL||p->item>r->item)
{
r->next=head;
head=r;
return;
}
for(;p;p=p->next)
{
if(r->item<p->item)
{
r->next=q->next;
q->next=r;
return;
}
q=p;
}
if(p==NULL)
{
q->next=r;
}
}
void show(link p)
{
for(p=head;p;p=p->next)
printf("%3d",p->item);
putchar('\n');
}
void free_node(link p)
{
if(p)
{
free(p);
p=NULL;
}
}
void delete(unsigned int key)
{
link p=head,q;
if(head==NULL)
return;
if(p->item==key)
{
head=p->next;
free_node(p);
return;
}
for(;p;p=p->next)
{
if(p->item==key)
{
q->next=p->next;
free_node(p);
return;
}
q=p;
}
}
int main(int a,char *b[],char *c[])
{
link p;
p=make_node(1);
insert(p);
p=make_node(3);
insert(p);
p=make_node(7);
insert(p);
p=make_node(2);
insert(p);
p=make_node(4);
insert(p);
p=make_node(0);
insert(p);
p=make_node(6);
insert(p);
show(p);
delete(3);
delete(4);
delete(6);
delete(0);
delete(2);
delete(1);
show(p);
return 0;
}
------------------------雙向鏈錶
#include <stdio.h>
#include <stdlib.h>
typedef struct node *link;
struct node{
unsigned int item;
link pre,next;
};
struct node tailsentinel;
struct node headsentinel={0,NULL,&tailsentinel};
struct node tailsentinel={0,&headsentinel,NULL};
static link head=&headsentinel;
static link tail=&tailsentinel;
link make_node(unsigned int item)
{
link p=malloc(sizeof *p);
p->item=item;
p->pre=NULL;
p->next=NULL;
return p;
}
void insert_head(link p)
{
p->next=head->next;
head->next->pre=p;
head->next=p;
p->pre=head;
}
void insert_middle(link p)
{
link k,q;
for(q=head->next,k=head->next;k->next!=NULL;k=k->next)
{
if(p->item>k->item)
{
p->next=q->next;
q->next->pre=p;
p->pre=q;
q->next=p;
return;
}
q=k;
}
}
void insert_end(link p)
{
link k,q;
for(q=head->next,k=head->next;k->next!=NULL;k=k->next)
{
q=k;
}
if(k->next==NULL)
{
p->next=tail;
tail->pre=p;
p->pre=q;
q->next=p;
}
}
void insert(link r)
{
link p=head->next,q;
if(((int)(tail->pre)-(int)(head->next))==sizeof(struct node)||p->item>r->item)
{
r->next=head->next;
head->next->pre=r;
head->next=r;
r->pre=head;
return;
}
for(q=head->next;p->next;p=p->next)
{
if(r->item<p->item)
{
r->next=q->next;
q->next->pre=r;
r->pre=q;
q->next=r;
return;
}
q=p;
}
if(p->next==NULL)
{
r->next=tail;
tail->pre=r;
r->pre=q;
q->next=r;
}
}
void delete(unsigned int key)
{
link p;
for(p=head->next;p->next!=NULL;p=p->next)
{
if(p->item==key)
{
p->pre->next=p->next;
p->next->pre=p->pre;
free(p);
p=NULL;
return;
}
}
}
void show()
{
link p;
for(p=head->next;p->next!=NULL;p=p->next)
printf("%3d",p->item);
putchar('\n');
}
int main(int a,char *b[],char *c[])
{
link p;
p=make_node(1);
insert(p);
p=make_node(3);
insert(p);
p=make_node(7);
insert(p);
p=make_node(2);
insert(p);
p=make_node(4);
insert(p);
p=make_node(0);
insert(p);
p=make_node(6);
insert(p);
show();
delete(3);
delete(4);
delete(6);
delete(7);
delete(0);
delete(1);
delete(2);
show();
return 0;
}