一、链表
head.h文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
int date;
struct node *next;
}node,*node_p;
node_p creat_lik_list();//创建头节点
node_p new_node(int date);//创建节点
void insert_head(node_p H,int date);//头插
int empty_lik(node_p H);//判空
void show_lik(node_p H);//输出
void delete_head_lik(node_p H);//头删
void insert_tail(node_p H,int date);//尾插
void delete_tail(node_p H);//尾删
void insert_pos(node_p H,int pos,int date);//按位置插入
void delete_pos(node_p H,int pos);//按位置删除
#endif
main.c文件
#include"head.h"
int main()
{
node_p H=creat_lik_list();
insert_head(H,11);
insert_head(H,22);
insert_tail(H,33);
insert_tail(H,44);
insert_tail(H,55);
insert_head(H,66);
delete_head_lik(H);
delete_tail(H);
insert_pos(H,2,77);
delete_pos(H,3);
show_lik(H);
}
test.c文件
#include"head.h"
node_p creat_lik_list()//创建头节点,创建链表,头节不存储值
{
node_p H =(node_p)malloc(sizeof(node));
if(H==NULL)
{
printf("apply to fail!\n");
return NULL;
}
H->next=NULL;//无节点指向空
H->date=0;
return H;
}
node_p new_node(int date)//创建节点,创建数据节点
{
node_p new =(node_p)malloc(sizeof(node));//申请节点地址
if(new==NULL)
{
printf("apply to fail!\n");
return NULL;
}
new->date=date;//节点赋值
return new;
}
void insert_head(node_p H,int date)
{
if(H==NULL)
{
printf("init error!\n");
return;
}
node_p new =new_node(date);//申请插入的值以及地址
new->next=H->next;//将头地址(NULL)赋值给申请节点内指针做保存,作为收尾
H->next=new;//将头节点内指针更新为申请的节点地址,把下一个节点的存储地址,放到上一个节点,更新头地址,前移
H->date++;//记录长度
}
int empty_lik(node_p H)
{
if(H==NULL)
{
printf("init error!\n");
return -1;
}
return H->next==NULL?1:0;
}
void show_lik(node_p H)
{
if(empty_lik(H))
{
printf("no input!\n");
return;
}
node_p p=H;//头节点的next表示第一个节点指针,需输出
while(p->next!=NULL)//链表节点最后一个的标志,参考头插逻辑
{
printf("%d->",p->next->date);
p=p->next;//当前节点的*next给p,该*next实际是下一个节点存储的实际位置(指针),换言之,当下次取p时,便到了下一个节点上
}
printf("NULL\n");
}
void delete_head_lik(node_p H)//头删是删除头节点下面的一个节点,该节点的指针是在头节点里,所以要删除该地址,并将该节点所包含的下一个节点的指针与头连接
{
if(empty_lik(H))
{
printf("input empty,don't delete!\n");
return;
}
node_p p=H->next;
H->next=H->next->next;//H->next是头节所存的地址,该地址也是下一个节点结构体申请的存储地址(下个节点的指针),所以H->next->next表示下一个节点内的*next
free(p);
H->date--;
}
void insert_tail(node_p H,int date)
{
if(H==NULL)
{
printf("init error!\n");
return;
}
node_p p=H;
while(p->next!=NULL)//找到最后的位置
{
p=p->next;
}
node_p new=new_node(date);
new->next=p->next;
p->next=new;
H->date++;
}
void delete_tail(node_p H)//删除最后一个元素,即倒数第二个节点存储的地址
{
if(empty_lik(H))
{
printf("input empty,don't delete!\n");
return;
}
node_p p=H;
while(p->next->next!=NULL)//前一个节点的next存放下一个节点申请的地址,再取next,便是下一个节点的next,换言之,判断下一个节点的地址
{
p=p->next;
}
node_p del =p->next;
p->next=p->next->next;
free(del);
H->date--;
}
void insert_pos(node_p H,int pos,int date)
{
if(empty_lik(H))
{
printf("input empty!\n");
return;
}
if(pos<=0||pos>H->date+1)
{
printf("input pos error!\n");
return;
}
node_p new=new_node(date);
node_p p=H;
for(int i=0;i<pos-1;i++)//确定循环次数用for,确定pos-1位置,pos为实际位置
{
p=p->next;
}
new->next=p->next;
p->next=new;
H->date++;
}
void delete_pos(node_p H,int pos)
{
if(empty_lik(H))
{
printf("input empty,don't delete!\n");
return;
}
if(pos<=0||pos>H->date)
{
printf("input pos error!\n");
return;
}
node_p p=H;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
node_p del=p->next;
p->next=p->next->next;
free(del);
H->date--;
}
运行结果:
二、求以下结构体大小
结构体大小:1+(1)+11+(1)+8=22