链表原理见链接
http://blog.csdn.net/xiaoliu0518/article/details/31762897
函数名取得不是很好,大家见谅。
list头文件如下:
#ifndef __LIST_DEMO_H__
#define __LIST_DEMO_H__
#include <stdio.h>
#include <malloc.h>
typedef struct node{
int data;
struct node* next;
}node_t,*pnode_t;
//-----------------------基础-----------------------------------
//创建并初始化头节点
pnode_t create_list();
//创建一个数据为data的节点,并返回节点地址
pnode_t create_node(int data);
//判断链表是否为空链表(没有数据节点)
int list_empty(pnode_t head);
//打印链表的所有数据节点
void print_list(pnode_t head);
//-----------------------查找------------------------------------
//查找数据为data的节点,并返回节点的地址
pnode_t find_node(pnode_t head,int data); //最核心代码
//查找数据为data的节点的前一个节点
pnode_t find_prev(pnode_t head,int data); //最核心代码
//-----------------------插入------------------------------------
//把new指向的节点插入到p指向的节点后面
int insert_node_after(pnode_t p,pnode_t new); //最核心代码
//在p指向的节点后面插入一个数据为data的节点
int insert_data_after(pnode_t p,int data);
//把new_data插入到数据为data的节点后面
int insert_data(pnode_t head,int data,int new_data);
//把new_data插入到数据为data的节点的前面
int insert_prev(pnode_t head,int data,int new_data);
//-----------------------删除------------------------------------
//删除p指向的节点后面的节点
int del_after_node(pnode_t p); //最核心代码
//删除数据为data的节点后面的节点
int del_after_data(pnode_t head,int data);
//删除链表中数据为data的节点
int del_data(pnode_t head,int data);
//删除链表中的所有数据节点
int clear(pnode_t head);
//销毁链表(删除所有数据节点和头节点)
int destroy(pnode_t *phead);
#endif
#include <stdio.h>
#include <malloc.h>
#include "list_demo.h"
//-----------------------基础-----------------------------------
pnode_t create_list()
{
pnode_t head = malloc(sizeof(node_t));
if(head != NULL)
{
head->data = 0;
head->next = NULL;
}
return head;
}
pnode_t create_node(int data)
{
pnode_t p = (pnode_t)malloc(sizeof(node_t));
if(p != NULL)
{
p->data = data;
p->next = NULL;
}
return p;
}
int list_empty(pnode_t head)
{
if(head == NULL)
{
return -1; //链表不存在
}
if(head->next == NULL)
{
return 1; //链表为空
}
else
{
return 0;//链表非空
}
}
void print_list(pnode_t head)
{
pnode_t tmp = NULL;
if(head == NULL)
{
puts("list is not exist!\n");
return;
}
if(list_empty(head))
{
puts("list is empty!\n");
return;
}
tmp = head->next;
while(tmp!=NULL)
{
printf("%d\n",tmp->data);
tmp = tmp->next;
}
return;
}
//-----------------------查找------------------------------------
pnode_t find_node(pnode_t head,int data)
{
pnode_t tmp = NULL;
if(head == NULL)
{
puts("no list\n");
return NULL;
}
tmp = head->next;
while(tmp!=NULL)
{
if(tmp->data == data)
{
break;
}
tmp = tmp->next;
}
return tmp;
}
pnode_t find_prev(pnode_t head,int data)
{
pnode_t tmp = NULL;
if(head == NULL)
{
puts("no list\n");
return NULL;
}
tmp = head;
while(tmp->next!=NULL)
{
if(tmp->next->data == data)
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
//-----------------------插入------------------------------------
int insert_node_after(pnode_t p,pnode_t new)
{
if(p == NULL || new == NULL)
{
return -1;
}
new->next = p->next;
p->next = new;
return 0;
}
int insert_data_after(pnode_t p,int data)
{
int ret = 0;
pnode_t new = NULL;
if(p == NULL)
{
return -1;
}
new = create_node(data);
ret = insert_node_after(p,new);
return ret;
}
int insert_data(pnode_t head,int data,int new_data)
{
int ret = 0;
pnode_t p = NULL;
p = find_node(head,data);
if(p==NULL)
{
return -1;
}
ret = insert_data_after(p,new_data);
return ret;
}
int insert_prev(pnode_t head,int data,int new_data)
{
int ret = 0;
pnode_t p = NULL;
p = find_prev(head,data);
if(p==NULL)
{
return -1;
}
ret = insert_data_after(p,new_data);
return ret;
}
//-----------------------删除------------------------------------
int del_after_node(pnode_t p)
{
pnode_t tmp = NULL;
if(p == NULL)
{
return -1;
}
tmp = p->next;
if(tmp!=NULL)
{
p->next = tmp->next;
free(tmp);
}
return 0;
}
int del_data(pnode_t head,int data)
{
int ret = 0;
pnode_t p = find_prev(head,data);
if(p != NULL)
{
ret = del_after_node(p);
}
return ret;
}
int del_after_data(pnode_t head,int data)
{
int ret = 0;
pnode_t p = find_node(head,data);
if(p != NULL)
{
ret = del_after_node(p);
}
return ret;
}
int clear(pnode_t head)
{
if(head == NULL)
{
return -1;
}
while(head->next != NULL)
{
del_after_node(head);
}
return 0;
}
int destroy(pnode_t* phead)
{
if(phead == NULL)
{
return -1;
}
if(*(phead) != NULL)
{
clear(*phead);
free(*phead);
}
*phead = NULL;
return 0;
}
int main()
{
int i = 0;
int ret = 0;
pnode_t head = NULL;
head = create_list();
for(i = 0;i<5;i++)
{
insert_data_after(head,i*3);
}
print_list(head);
printf("-----------------\n");
insert_data(head,3,2);
insert_prev(head,6,5);
print_list(head);
printf("----------------\n");
del_data(head,5);
del_data(head,2);
print_list(head);
destroy(&head);
return 0;
}