C语言数据结构:DAY3

1.思维导图

2.单链表的指定位置删除
void Position_delete(node_p H,datatype index){
	if(H==NULL){
		printf("入参为空\n");
		return;
	}
	node_p p=H;
	if(index>H->data || index<=0){
		printf("不可以删除那里\n");
		return;
	}
	for(int i=0;i<index-1;i++){
		p=p->next;
	}
	node_p del=p->next;
	p->next=del->next;
	free(del);
	H->data--;
}

3.结构体大小

结果为22字节(前提是在所有成员的对齐量都为2字节)

4.链表源码

main.c

#include "link_list.h"
int main(){
	node_p H=create_link_list();
	insert_head(H,15);	
	insert_head(H,14);
	insert_head(H,13);
	
	printf("初始化并头插3个值:\n");
	show_list(H);	
	
	Delete_head(H);
	
	Insert_tail(H,99);

	printf("头删一次外加一次尾插99:\n");

	show_list(H);


	printf("在第四个插入98:\n");
	Position_insert(H,4,98);
	show_list(H);
	printf("%d\n",H->data);
	return 0;
}

link_list.c

#include "link_list.h"
node_p create_link_list(){
	node_p H=(node_p)malloc(sizeof(node));
	if (H==NULL){
		printf("空间申请失败\n");
		return NULL;
	}
	H->data=0;
	H->next=NULL;
	return H;
}

node_p create_node(datatype data){
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL){
		printf("空间申请失败\n");
		return NULL;
	}
	new->data=data;
	return new;
}

void insert_head(node_p H,datatype data){
	if(H==NULL){
		printf("入参为空\n");
		return;
	}
	node_p new=create_node(data);
	new->next=H->next;
	H->next=new;
	H->data++;

}

int empty_link(node_p H){
	if(H==NULL){
		printf("入参为空\n");
		return;
	}
	return H->next==NULL?1:0;
}
void show_list(node_p H){
	if(H==NULL){
		printf("入参为空\n");
		return;
	}
	if(empty_link(H)){
		printf("链表为空,无法输出\n");
		return;
	}
	node_p p=H->next;
	while(p!=NULL){
		printf("%-4d",p->data);
		p=p->next;
	}
	putchar(10);


}
void Delete_head(node_p H){
	if(H==NULL){
		printf("入参为空\n");
		return;
	}
	if(empty_link(H)){
		printf("链表为空,无法输出\n");
		return;
	}
	node_p p=H->next;
	H->next=H->next->next;
	free(p);
	H->data--;
	


}
void Insert_tail(node_p H,datatype data){
	if(H==NULL){
		printf("入参为空\n");
		return;
	}
	node_p p=H->next;
	while(p->next){// p->next!=NULL
		p=p->next;
	}
	node_p new=create_node(data);
	new->next=p->next;
	p->next=new;
	H->data++;
}
void Delete_tail(node_p H){
	if(H==NULL){
		printf("入参为空\n");
		return;
	}
	if(empty_link(H)){
		printf("链表为空,无法删除\n");
		return;
	}
	node_p p=H;
	while(p->next->next){
		p=p->next;
	}
	node_p del=p->next;
	p->next=NULL;
	free(del);
	H->data--;
}
void Position_insert(node_p H,datatype index,datatype data){
	if(H==NULL){
		printf("入参为空\n");
		return;
	}
	node_p p=H;
	if(index>H->data+1 || index<=0){
		printf("不可以插入那里\n");
		return;
	}
	for(int i=0;i<index-1;i++){
		p=p->next;
	}
	node_p new=create_node(data);
	new->next=p->next;
	p->next=new;
	H->data++;


}
void Position_delete(node_p H,datatype index){
	if(H==NULL){
		printf("入参为空\n");
		return;
	}
	node_p p=H;
	if(index>H->data || index<=0){
		printf("不可以删除那里\n");
		return;
	}
	for(int i=0;i<index-1;i++){
		p=p->next;
	}
	node_p del=p->next;
	p->next=del->next;
	free(del);
	H->data--;
	


}

link_list.h

#ifndef __LINK_LIST_H__
#define __LINK_LIST_H__
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node{
	int data;
	struct node* next;
}node,*node_p;

node_p create_link_list(); //初始化单链表头结点
node_p create_node(datatype data);//创建一个新节点
void insert_head(node_p H,datatype data);
int empty_link(node_p H);
void show_list(node_p H);
void Delete_head(node_p H);
void Insert_tail(node_p H,datatype data);
void Delete_tail(node_p H);
void Position_insert(node_p H,datatype index,datatype data);
void Position_delete(node_p H,datatype index);
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值