C语言——数据结构(单链表)

 Link.c

#include"link.h"
plk create(){
	plk p=malloc(sizeof(lk));
	if(NULL==p){
		printf("申请头节点失败\n");
		return NULL;
	}
	p->len=0;
	p->next=NULL;
	return p;
}

int front_insert(plk L,int e){//L为所需插入结构体的地址,e为需要写入的信息
	if(L==NULL){
		printf("单链表不存在");
		return -1;
	}
	plk p=malloc(sizeof(lk));
	p->data=e;

	p->next=L->next;//获取储存在头节点中的下一个节点的地址数据

	L->next=p;

	L->len++;
	return 0;
	
}

int rear_insert(plk L,int e){
	if(NULL){
		printf("单链表不存在插入失败\n");
		return -1;
	}
	plk t=L;
	for(int i=0;i<len;i++){
		t=t->next;
	}
	plk p=malloc(sizeof(lk));
	p->data=e;
	
	p->next=NULL;
	t->next=p;
	
	L->len++;
	return 0;
}

int anypos_insert(plk L,int pos,int data){
	if(L==NULL||pos<1||pos>L->len+1){
		printf("插入失败");
		return -1;
	}
	plk t=L;
	for(int i=1;i<pos;i++){
		t=t->next;
	}
	plk p=malloc(sizeof(lk));
	p->data=data;//数据插入结构体
	p->next=t->next;//将存储在前驱中的下一节点地址赋给结构体p的地址域中
	t->next=p;//将p结构体的地址储存在前驱的地址域中,覆盖掉old地址
	L->len++;
	return 0;
}

int anypos_delete(plk L,int pos){
	if(L==NULL||pos<1||pos>L->len){
		printf("删除失败");
		retrun -1;
	}
	int i;
	plk t=L;
	for(i=0;i<pos;i++){
		t=t->next;
	}
	t->next=t->next->next;
	return 0;
}

int output_lk(plk L){
	int i=;
	plk t=L;

	for(i=0;i<len;i++){
		t=t->next;
		printf("%d\t",t->data);
	}
	printf("\n");
	return 0;
}

 Link.h

#ifndef _LINK1_H_
#define _LINK1_H_
#include<myhead.h>
typedef struct link1{
	union{
		int len;
		int data;
	};
	struct link1 *next;
}lk,*plk;
plk create();
int front_insert(plk,int);
int rear_insert(plk,int);
int anypos_insert(plk,int,int);
int anypos_delete(plk,int);
int anypos_find(plk,int);
int anypos_change(plk,int,int);
int output_lk(plk);
#endif

 main.c

#include <myhead.h>
#include"link.h"
int main(int argc, const char *argv[])
{
	int a[10]={1,2,5,4,6,9,8,7,3,12};

	plk L=create();//创建链表类结构体L
	//结构体———————>>>>NULL
	int i;

	/*for(i=0;i<10;i++){
		front_insert(L,a[i]);
	}*/

	for(i=0;i<10;i++){
		rear_insert(L,a[i]);
	}
	//anypos_insert(L,3,555);
	//anypos_delete(L,2);
	//anypos_find(L,5);
	anypos_change(L,1,10086);
	output_lk(L);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值