一、链表的设计原理(一般建议使用堆空间存放链表)
-
设计链表的节点(数据域+指针域) -
创建链表头结点(head)头结点相当于定义这一条链表的起点(钥匙) 初始化链表头结点(数据域 + 指针域) -
创建链表子节点(node)子节点插入到头结点后面(可以存储更多信息) 初始化链表子结点(数据域 + 指针域) -
插入节点(insert)将创建的节点放进链表里面,创立完整链表。 尾插法,将新节点放置链表末尾 头插法,将新节点放置头结点后一个 排序插法,可以按冲从小到大,也可以从大到小 -
剔除节点(delete) 将不需要节点删除,将链表不需要的节点从链表中拿出来,从此和链表无关。 原理是:使用链表头结点,遍历整个链表,将各节点中的数据与待删除数据作用是比对,可找到剔除节点的位置。将该节点从链表中删除。 -
修改节点内的数据 更改节点数据域中的内容 -
销毁链表 如果链表是使用堆空间存储,销毁链表时需使用free函数将每个节点的堆空间释放
二、链表代码设计 (双向循环链表)
- 设计链表的节点(数据域+指针域)
typedef struct double_clink_list
{
int data;
struct double_clink_list * next;
struct double_clink_list * prev;
}double_cll,*double_cll_p;
- 创建链表头结点(head)
double_cll_p init_double_cll(void)
{
double_cll_p head = calloc(1,sizeof(double_cll));
if(head == NULL)
{
printf("init_double_cll error\n");
return NULL;
}
head->data = 0;
head->next = head;
head->prev = head;
return head;
}
- 创建链表子节点(node)
double_cll_p create_node_double_cll(int data)
{
double_cll_p node = calloc(1,sizeof(double_cll));
if(node == NULL)
{
printf("create_node_double_cll error\n");
return NULL;
}
node->data = data;
node->next = node;
node->prev = node;
return node;
}
- 插入节点(insert)
int tail_insert_double_cll(double_cll_p node,double_cll_p head)
{
if(head == NULL || node == NULL)
{
printf("tail_insert_double_cll error\n");
return -1;
}
node->prev = head->prev;
node->next = head;
head->prev = node;
node->prev->next = node;
return 0;
}
尾插图解
int head_insert_double_cll(double_cll_p node,double_cll_p head)
{
if(head == NULL|| node == NULL)
{
printf("head_insert_double_cll error\n");
return -1;
}
node->next = head->next;
head->next = node;
node->prev = head;
node->next->prev = node;
return 0;
}
int sort_insert_double_cll(double_cll_p node,double_cll_p head)
{
if(head == NULL || node == NULL)
{
printf("sort_insert_double_cll error\n");
return -1;
}
double_cll_p p = head;
for(;p->next != head;p = p->next)
{
if(p->next->data > node->data)
{
node->next = p->next;
p->next = node;
node->next->prev = node;
node->prev = p;
return 0;
}
}
node->next = p->next;
p->next = node;
node->next->prev = node;
node->prev = p;
return 0;
}
- 剔除节点(delete)
int del_double_cll(double_cll_p p,double_cll_p head)
{
if(p == NULL || head == NULL)
{
printf("del_double_cll error\n");
return -1;
}
p->next->prev = p->prev;
p->prev->next = p->next;
p->next = NULL;
p->prev = NULL;
free(p);
return 0;
}
- 查找对应数据节点并返回该节点地址
double_cll_p find_double_cll(int data,double_cll_p head)
{
if(head == NULL)
{
printf("find_double_cll error\n");
return NULL;
}
double_cll_p p = head->next;
for(;p != head; p = p->next)
{
if(p->data == data)
return p;
}
return NULL;
}
- 销毁链表
int free_double_cll(double_cll_p head)
{
if(head == NULL)
{
printf("double_cll not exist\n");
return -1;
}
double_cll_p p = head;
for(;p->next != head;)
{
p = p->next;
free(p->prev);
}
free(p);
return 0;
}
完整 C程序 和 头文件
#include "double_cll.h"
int main(int argc,char *argv[])
{
double_cll_p head = init_double_cll();
int data;
double_cll_p q = NULL;
while(1)
{
scanf("%d",&data);
if(data>0)
{
if(NULL==find_double_cll(data,head))
{
double_cll_p node = create_node_double_cll(data);
sort_insert_double_cll(node,head);
}
else
{
printf("data repetition \n");
}
show_double_cll(head);
}
else if(data ==0)
break;
else
{
if((q=find_double_cll(-data,head))!=NULL)
{
del_double_cll(q,head);
}
else
{
printf("data absence \n");
}
show_double_cll(head);
}
}
free_double_cll(head);
return 0;
}
double_cll_p init_double_cll(void)
{
double_cll_p head = calloc(1,sizeof(double_cll));
if(head == NULL)
{
printf("init_double_cll error\n");
return NULL;
}
head->data = 0;
head->next = head;
head->prev = head;
return head;
}
double_cll_p create_node_double_cll(int data)
{
double_cll_p node = calloc(1,sizeof(double_cll));
if(node == NULL)
{
printf("create_node_double_cll error\n");
return NULL;
}
node->data = data;
node->next = node;
node->prev = node;
return node;
}
int tail_insert_double_cll(double_cll_p node,double_cll_p head)
{
if(head == NULL || node == NULL)
{
printf("tail_insert_double_cll error\n");
return -1;
}
node->prev = head->prev;
node->next = head;
head->prev = node;
node->prev->next = node;
return 0;
}
int head_insert_double_cll(double_cll_p node,double_cll_p head)
{
if(head == NULL|| node == NULL)
{
printf("head_insert_double_cll error\n");
return -1;
}
node->next = head->next;
head->next = node;
node->prev = head;
node->next->prev = node;
return 0;
}
int sort_insert_double_cll(double_cll_p node,double_cll_p head)
{
if(head == NULL || node == NULL)
{
printf("sort_insert_double_cll error\n");
return -1;
}
double_cll_p p = head;
for(;p->next != head;p = p->next)
{
if(p->next->data > node->data)
{
node->next = p->next;
p->next = node;
node->next->prev = node;
node->prev = p;
return 0;
}
}
node->next = p->next;
p->next = node;
node->next->prev = node;
node->prev = p;
return 0;
}
int show_double_cll(double_cll_p head)
{
if(head == NULL)
{
printf("show_double_cll error\n");
return -1;
}
double_cll_p p = head;
for(;p->next!=head;p = p->next)
{
printf("%d\t",p->next->data);
}
printf("\n");
return 0;
}
double_cll_p find_double_cll(int data,double_cll_p head)
{
if(head == NULL)
{
printf("find_double_cll error\n");
return NULL;
}
double_cll_p p = head->next;
for(;p != head; p = p->next)
{
if(p->data == data)
return p;
}
return NULL;
}
int free_double_cll(double_cll_p head)
{
if(head == NULL)
{
printf("double_cll not exist\n");
return -1;
}
double_cll_p p = head;
for(;p->next != head;)
{
p = p->next;
free(p->prev);
}
free(p);
return 0;
}
int del_double_cll(double_cll_p p,double_cll_p head)
{
if(p == NULL || head == NULL)
{
printf("del_double_cll error\n");
return -1;
}
p->next->prev = p->prev;
p->prev->next = p->next;
p->next = NULL;
p->prev = NULL;
free(p);
return 0;
}
#ifndef __double_cll_h__
#define __double_cll_h__
#include<stdio.h>
#include <stdlib.h>
typedef struct double_clink_list
{
int data;
struct double_clink_list * next;
struct double_clink_list * prev;
}double_cll,*double_cll_p;
double_cll_p init_double_cll(void);
double_cll_p create_node_double_cll(int data);
int tail_insert_double_cll(double_cll_p node,double_cll_p head);
int head_insert_double_cll(double_cll_p node,double_cll_p head);
int sort_insert_double_cll(double_cll_p node,double_cll_p head);
int show_double_cll(double_cll_p head);
double_cll_p find_double_cll(int data,double_cll_p head);
int free_double_cll(double_cll_p head);
int del_double_cll(double_cll_p p,double_cll_p head);
#endif
结束语:需要完整C程序的私信我发邮箱
|