02 -- 单链表

>>步骤1:编写listnode.h文件

//============================================================
#ifndef __listnode_H__
#define __listnode_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct NODE{
	int data;
	struct NODE*next;
}listnode;

listnode* list_create(void);
bool list_insert_head(listnode*H,int value);
bool list_delete(listnode*H,int value);
bool list_change(listnode*H,int pos,int value);
listnode*list_search(listnode*H,int value);
void list_show(listnode*H);

#endif
//============================================================

>>步骤2:编写listnode.c文件[增,删,改,查]

//======================================================
#include "listnode.h"

//[1]创建单链表头节点
//动画演示!
//[H_data][H_next] --> NULL
listnode* list_create()
{
	listnode* H = NULL;
	H = (listnode*)malloc(sizeof(listnode));
	H->data = 0;
	H->next	= NULL;
	return H;
}


//[2]头插法  [因为我们只知道头节点,所以所有节点的插入都插在头节点后面一位]
//动画演示!
//插入前:[H_data][H_next] --> [X0_data][X0_next] --> [X1_data][X1_next]
//插入后:[H_data][H_next] --> [p_data] [p_next]  --> [X0_data][X0_next] --> [X1_data][X1_next] 
bool list_insert_head(listnode*H,int value)
{
	listnode* P = NULL;
	P = (listnode*)malloc(sizeof(listnode));
	P->data = value;
	P->next = H->next; //注意
	H->next = P;
	return true;
}

//[3]删除值为value的节点
//动画演示!
//删除前:[H_data][H_next] --> [p_data][p_next] -->  [q_data] [q_next] --> [X0_data][X0_next]
//删除后:[H_data][H_next] --> [p_data][p_next] -->  [X0_data][X0_next]
bool list_delete(listnode*H,int value)
{
	
	listnode*P = H;
	listnode*Q = NULL;

	int i = 1;
	while(P->next->data!=value)
	{
		P = P->next;
	}
	Q = P->next;

	P->next = Q->next; 
	free(Q);
	return true;
}

//[4]修改节点值
bool list_change(listnode*H,int pos,int value)
{

	listnode*P = H;

	int i = 1;
	for(i;i<=pos;i++)
	{
		P=P->next;
	}

	P->data = value;
	return true;
}

//[5]查找某节点
listnode* list_search(listnode*H,int value)
{
	
	listnode*P = H;
	
	while(P->data != value)
	{
		P=P->next;
	}
	return P;
}
//[8]打印单链表
void list_show(listnode*H)
{
	listnode*P = H;
	
	while((P->next)>0)
	{
		printf("%d\t",P->data);
		P=P->next;
	}
	printf("%d\t",P->data);
}

//==============================================

>>步骤4:编写测试文件listnode_text.c

//==============================================
include "listnode.h"
int main(int argc, const char *argv[])
{
	listnode* H;

	listnode* P= NULL;
	H=list_create();
	printf("\n单链表:头插入\n");
	list_insert_head(H,10);
	list_insert_head(H,20);
	list_insert_head(H,30);
	list_insert_head(H,40);
	list_insert_head(H,50);
	list_show(H);

	printf("\n单链表:查找20\n");
	P = list_search(H,20);
	printf("\n找到了data = %d\n",P->data);

	printf("\n单链表:修改\n");
	list_change(H,1,100);
	list_change(H,2,200);
	list_change(H,3,300);
	list_change(H,4,400);
	list_change(H,5,500);
	list_show(H);
	
	
	printf("\n单链表:删除\n");
	list_delete(H,100);
	list_delete(H,200);
	list_delete(H,300);
	list_delete(H,400);
	list_delete(H,500);
	list_show(H);

	return 0;
}
//===============================================

>>步骤5:Makefile文件

VPATH=/home/yqj/单链表/

listnode_text:listnode.o listnode_text.c
        gcc -g listnode.o listnode_text.c -o listnode_text
listnode.o:listnode.c listnode.h
        gcc listnode.c -c -o listnode.o

.PHONY:clean
clean:
        rm -rf listnode.o listnode_text

>>步骤6:验证结果

topeet@ubuntu:/home/yqj/desktop/单链表$ ./listnode_text 

单链表:头插入
0	50	40	30	20	10	
单链表:查找20

找到了data = 20

单链表:修改
0	100	200	300	400	500	
单链表:删除
0	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值