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
946

被折叠的 条评论
为什么被折叠?



