单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。要实现对单链表中节点的插入、删除与查找的功能,就要先进行的单链表的初始化、创建和遍历,进而实现各功能,以下是对单链表节点的插入、删除、查找功能的具体实现:
				?
			
	
						
						
						
						
						
						
						
																		
    
        
    
        
                        
                
                    
                
                
                
                    
                
                
                
                    
                
                
                
                    
                
                        
    
 																		
						
																		
    
        
 												
						
																		
	
	
		
				
			
																		
						
						
					
				
				                | 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
 
								43
 
								44
 
								45
 
								46
 
								47
 
								48
 
								49
 
								50
 
								51
 
								52
 
								53
 
								54
 
								55
 
								56
 
								57
 
								58
 
								59
 
								60
 
								61
 
								62
 
								63
 
								64
 
								65
 
								66
 
								67
 
								68
 
								69
 
								70
 
								71
 
								72
 
								73
 
								74
 
								75
 
								76
 
								77
 
								78
 
								79
 
								80
 
								81
 
								82
 
								83
 
								84
 
								85
 
								86
 
								87
 
								88
 
								89
 
								90
 
								91
 
								92
 
								93
 
								94
 
								95
 
								96
 
								97
 
								98
 
								99
 
								100
 
								101
 
								102
 
								103
 
								104
 
								105
 
								106
 
								107
 
								108
 
								109
 
								110
 
								111
 
								112
 
								113
 
								114
 
								115
 
								116
 
								117
 
								118
 
								119
 
								120
 
								121
 
								122
 
								123
 
								124
 
								125
 
								126
 
								127
 
								128
						  | 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int ElemType;
/**
*链表通用类型
*ElemType 代表自定义的数据类型 
*struct Node *next 代表 结构体指针(指向下一个结构体,完成链表动作) 
*/
typedef struct Node{
ElemType data;
struct Node *next;
}Node; 
/*==========单链表的初始化================*/
/*
*头结点指针数据域设置为空 
*/
void initList(Node **pNode){
*pNode=NULL;
}
/*===========单链表的创建=================*/
/*
*功能实现:通过用户不断输入数据,创建链表
*利用游标俩个指针(p1,p2),将申请下的数据块(存入用户输入数据),链接起来 
*/
Node *create(Node *pHead){
Node *p1;
Node *p2;
p1=p2=(Node *)malloc(sizeof(Node)); //申请内存空间 
memset(p1,0,sizeof(Node)); //存入数据域清空 
scanf("%d",&p1->data);
p1->next=NULL; 
while(p1->data>0){ //输入负数结束 
if(pHead==NULL)
pHead=p1;
else
p2->next=p1;
p2=p1;
p1=(Node *)malloc(sizeof(Node));
memset(p1,0,sizeof(Node));
scanf("%d",&p1->data);
p1->next=NULL;
}
return pHead;
}
/*=================链表的遍历==================*/
/**
*从头结点开始,不断遍历出数据域的内容将表遍历 
*/
void printList(Node *pHead){
if(NULL==pHead)
printf("链表为空\\n");
else{
while(pHead!=NULL){
printf("%d ",pHead->data);
pHead=pHead->next;
}
} 
printf("\\n");
} 
/*===============插入节点==================*/
/**
*Node **pNode 传入头结点空间地址
*int i 传入要插入的结点位置 
*/
void insert_data(Node **pNode,int i){
Node *temp;
Node *target;
Node *p;
int item;
int j=1;
printf("输入要插入的节点值:");
scanf("%d",&item);
target=*pNode; 
for(;j<i-1;target=target->next,++j); //不断移动target位置,到要插入结点位置, 
temp=(Node *)malloc(sizeof(Node)); //申请内存空间 
temp->data=item; //存入要存入的数据位置 
p=target->next; 
target->next=temp;
temp->next=p; 
} 
/*===============删除节点====================*/
/**
*删除结点后,释放内存空间free(temp) 
*/
void delete_data(Node **pNode,int i){
Node *target;
Node *temp;
int j=1;
target=*pNode;
for(;j<i-1;target=target->next,++j);
temp=target->next;
target->next=temp->next;
free(temp);
}
/*===============查找结点====================*/
int search_data(Node *pNode,int elem){
Node *target;
int i=1;
for(target=pNode;target->data!=elem && target->next!=NULL;++i,target=target->next);
if(target->next==NULL)
return 0;
else
return i;
} 
int main(){
int i;
Node *pHead=NULL;
initList(&pHead);
pHead=create(pHead);
printList(pHead);
printf("输入插入节点位置\\n");
scanf("%d",&i);
insert_data(&pHead,i);
printList(pHead);
printf("输入删除节点位置\\n");
scanf("%d",&i);
delete_data(&pHead,i);
printList(pHead);
printf("输入查找节点\\n");
scanf("%d",&i);
printf("节点所在位置:%d",search_data(pHead,i));
return 0;
}
 | 
通过以上各功能的实现,希望对大家单链表的学习有所帮助。
相关文章
             猜你喜欢
        
        - 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
 - 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
 - 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
 - ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
 - ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
 
			TA的动态
			
		
				- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
 - 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
 - 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
 - 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
 - 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
 
快网idc优惠网
QQ交流群
				您的支持,是我们最大的动力!				
			
		
        热门文章
        
    
    - 
            2025-05-29 49
 - 
            2025-05-27 46
 - 
            
Java源码解析CopyOnWriteArrayList的讲解
2025-05-29 85 - 
            2025-05-25 75
 - 
            2025-05-25 20
 
		热门评论
	
	
        

    		
            	
        
        