单链表的基本操作(插入、删除等)

废话不多说,直接上代码!

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h> 
typedef struct LNode{
	int data;
	struct LNode*next;
}LNode;
LNode *CL(int len){
	LNode*L=(LNode*)malloc(sizeof(LNode));
	LNode*temp=L;
	temp->next=NULL;
	for(int i=1;i<len;i++){
		LNode*p=(LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		p->next=temp->next;
		temp->next=p;
	} 
	return(LNode*)L;
} 

LNode*TTCL(int len){
	LNode*L=(LNode*)malloc(sizeof(LNode));
	LNode*temp=L;
	temp->next=NULL;
	for(int i=1;i<=len;i++){
		LNode*p=(LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		temp->next=p;
		temp=p; 
	}
	temp->next=NULL;
	return (LNode*)L; 
}
 
int Search(LNode*L,int key){
	LNode*temp=L;
	int pos=0;
	int i=1;
	while(temp->next){
		temp=temp->next;
		if(key==temp->data){
			pos=i;
			printf("%s%d","The position is \n",i);
			return pos;
		}
		i++;
	}
	printf("No such data!\n");
	return -1;
}
LNode*replace(LNode*L,int pos,int key){
	LNode*temp=L;
	temp=temp->next;
	for(int i=1;i<pos;i++){
		temp=temp->next;
	}
	if(temp!=NULL)
	temp->data=key;
	return(LNode*)L;	
}
LNode*Insert(LNode*L,int pos,int key){
	LNode*temp=L;
	int i=0;	
	while(temp!=NULL&&(i<pos-1)){
		temp=temp->next;
		++i;
	} 
	if((temp==NULL)||(i>pos-1)){
		printf("insert fail!\n");
	}
	LNode*New=(LNode*)malloc(sizeof(LNode));
	New->data=key;
	New->next=temp->next;
	temp->next=New;
	return (LNode*)L;
}
LNode*Del(LNode*L,int pos){
	
	LNode*temp=L;
	int i=0;
	while((i<pos-1)&&temp!=NULL){
		temp=temp->next;
		++i;
	}
	if(temp==NULL||i>pos-1){
		printf("Wrong with the del!\n");
		return (LNode*)L;
	}
	LNode*del=temp->next;
	temp->next=temp->next->next;
	free(del);
}
void printlist(LNode*L){
	printf("das");
	if(L->next==NULL){
	printf("empty list!\n");
	exit(0);
}
	LNode*temp=L;
	int count=0;
	printf("List:\n");
	while(temp->next){
		temp=temp->next;
		printf("%d\t",temp->data);
		count++;
			if(count%5==0)
			printf("\n");
	}
	printf("\n");
}
int main(){
	int len;
	LNode*L;
	scanf("%d",len);
	printlist(L);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值