思维导图
1.双向链表的基本操作
头文件:
#ifndef __DOUBLE__
#define __DOUBLE__
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
struct node *pri;
}node,*node_p;
node_p create_double();
node_p create_node(int data);
int empty_double(node_p H);
void insert_head(node_p H,int data);
void show(node_p H);
void delete_head(node_p H);
void insert_tail(node_p H,int data);
void delete_tail(node_p H);
void insert_location(node_p H,int data,int location);
void delete_location(node_p H,int location);
#endif
主函数:
#include "doublelink.h"
int main(int argc, const char *argv[])
{
node_p H=create_double();
insert_head(H,1);
insert_head(H,3);
insert_head(H,5);
insert_head(H,7);
insert_head(H,18);
insert_head(H,66);
insert_head(H,777);
show(H);
printf("add 9 at the head\n");
insert_head(H,9);
show(H);
printf("delete the head\n");
delete_head(H);
show(H);
printf("add 15 in the end\n");
insert_tail(H,15);
show(H);
printf("delete the end\n");
delete_tail(H);
show(H);
printf("add 88 at the location 3\n");
insert_location(H,88,3);
show(H);
printf("delete the location 4\n");
delete_location(H,4);
show(H);
return 0;
}
自定义函数:
#include "doublelink.h"
node_p create_double()
{
node_p H=(node_p)malloc(sizeof(node));
if(H==NULL)
{
printf("apply fail\n");
return NULL;
}
H->data=0;
H->next=NULL;
H->next=NULL;
return H;
}
node_p create_node(int data)
{
node_p new=(node_p)malloc(sizeof(node));
if(new==NULL)
{
printf("apply fail\n");
return NULL;
}
new->data=data;
return new;
}
int empty_double(node_p H)
{
if(H==NULL)
{
printf("apply fail\n");
return -1;
}
return H->next==NULL?1:0;
}
void insert_head(node_p H,int data)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
node_p new=create_node(data);
if(H->next!=NULL)
{
new->next=H->next;
H->next->pri=new;
}
H->next=new;
new->pri=H;
H->data++;
}
void show(node_p H)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(empty_double(H))
{
printf("the double_link list is void\n");
return;
}
node_p p=H->next;
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
putchar(10);
putchar(10);
}
void delete_head(node_p H)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(empty_double(H))
{
printf("the double_link list is void\n");
return;
}
if(H->next->next==NULL)
{
node_p q=H->next;
H->next=NULL;
free(q);
H->data--;
return;
}
node_p q=H->next;
H->next->next->pri=H;
H->next=H->next->next;
free(q);
H->data--;
}
void insert_tail(node_p H,int data)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
node_p p=H->next;
node_p new=create_node(data);
while(p->next!=NULL)
{
p=p->next;
}
new->next=NULL;
p->next=new;
new->pri=p;
H->data++;
}
void delete_tail(node_p H)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(empty_double(H))
{
printf("the double_link list is void\n");
return;
}
node_p p=H->next;
while(p->next->next!=NULL)
{
p=p->next;
}
node_p q=p->next;
p->next=NULL;
free(q);
H->data--;
}
void insert_location(node_p H,int data,int location)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(empty_double(H))
{
printf("the double_link list is void\n");
return;
}
if(location<=0||location>data+1)
{
printf("the location is wrong\n");
return;
}
node_p new=create_node(data);
node_p p=H;
for(int i=0;i<location-1;i++)
{
p=p->next;
}
new->next=p->next;
p->next->pri=new;
p->next=new;
new->pri=p;
H->data++;
}
void delete_location(node_p H,int location)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(empty_double(H))
{
printf("the double_link list is void\n");
return;
}
node_p p=H;
for(int i=0;i<location-1;i++)
{
p=p->next;
}
if(location==H->data)
{
node_p q=p->next;
p->next=NULL;
free(q);
H->data--;
}
node_p q=p->next;
p->next=q->next;
q->next->pri=p;
free(q);
H->data--;
return;
}
功能实现
2.顺序表和链表的区别
见思维导图