写一个简单的单向链表

构造一个节点
struct list
{
    
    
	int data;  //数据域
	struct list *next; //	指针域
};

或者

struct
{
    
    
	int data;
	struct list *next;
}list,*p_list;


初始化头结点
struct list *init_head()
{
    
    
	/*给头结点申请空间,有可能申请失败,需要判断*/
	/*malloc头文件:stdlib.h*/
	struct list *head = malloc(sizeof(struct list));
	/*注意"=="不要写成"=",会引起段错误*/
	if(head == NULL)
	{
    
    
		printf("malloc head fail!\n");
		return NULL;	                                                      
	}
	/*给头结点赋值*/
	head->data = 0;
	head->next = NULL;

	return head;
}

初始化其他节点
struct list *node(int data)
{
    
    
	/*给节点申请堆空间,有可能申请失败需要判断*/
	struct list *node = malloc(sizeof(struct list));
	if(node == NULL)
	{
    
    
		printf("malloc node fail!\n");
		return NULL;
	}

	node->data = data;
	node->next = NULL;
	
	return node;
}

插入节点(头插法)
void insert_head(struct list *head,struct list *node)
{
    
    
	/*
	if(head == NULL)
		head->next = node;
	esle
	{
		node->next = head->next;
		head->next = node;
	}
	*/
	/*头结点为空也成立*/
	node->next = head->next;
	head->next = node;
}

初始化并用尾插法插入节点
/*初始化与尾插法结合*/
void insert_tail(struct list *head,int data)
{
    
    
    /*给新节点申请空间*/
    struct list *node = malloc(sizeof(struct list));

    /*赋值*/
    node->data = data;
    node->next = NULL;

    /*尾插法插入链表*/
    struct list *p;
    for(p=head->next;p->next!=NULL;p=p->next); //找到最后一个节点
    p->next = node;  //插入节点

}

删除节点
/*删除节点*/
void delect(struct list *head,int data)
{
    
    
    struct list *p,*tmp;
    for(p=head;p->next!=NULL;p=p->next)
    {
    
    
        if(p->next->data == data)
        {
    
    
            tmp = p->next;
            p->next = p->next->next;
            free(tmp);
            
            /*删最后一个节点后,p->next = NULL,for循环中p = p->next = NULL;
            这时还要进行一次判断,p->next对空指针进行访问,引起段错误(p为NULL,没有p->next)*/
            if(p->next == NULL)  //防止删除最后一个节点的时候出现段错误
				break;
        }
    }
}
遍历打印链表
void display(struct list *head)
{
    
    
	struct list  *p;
	for(p=head->next;p!=NULL;p=p->next)
	{
    
    
		printf("%d-->",p-->data);
	}
	printf("\n");
}

释放链表
void release(struct list *head)
{
    
    
	struct list *p, *tmp;
	for(p=head->next;p!=NULL;)
	{
    
    
		tmp = p;
		p = p->next;
		free(p);
	}
	free(head);  //或者先释放头结点也可以
}

主函数
#inlcude <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
    
    
	/*初始化链表*/
	struct list *head = init_head();
	if(head == NULL)
	{
    
    
		printf("init head fail!\n");
	}

	/*初始化其他节点,给节点赋值*/
	struct list *node1,*node2,*node3;
	node1 = init_node(10);
	node2 = init_node(20);
	node3 = init_node(30);

	/*头插法插入节点*/
	insert_head(head,node1);
	insert_head(head,node2);
	insert_head(head,node3);

	/*初始化并用尾插法插入节点*/
    insert_tail(head,40);
    insert_tail(head,50);
    insert_tail(head,60);

	/*遍历输出链表*/
	display(head);

	/*删除值为40的节点*/
    delect(head,40);
    printf("删除后:\n")
    display(head);

	/*释放链表*/
	release(head);

	return 0;
}
运行结果
head-->10-->20-->30-->40-->50-->60-->
删除后:
head-->10-->20-->30-->50-->60-->

猜你喜欢

转载自blog.csdn.net/weixin_54241007/article/details/128189124