有头链表的创建

有头链表的创建 插入 删除

struct Node
{
int date;
struct Nodenext;
};
struct Node cendnode(int date)
{//创建链表
struct Node
newnode = (struct Node
)malloc(sizeof(struct Node));
newnode->date = date;
newnode->next = NULL;
return newnode;
}
struct List
{
struct Node* firnode;//永远指向表头
struct Node* tailnode;//永远指向表尾
int listsize;
};
struct Listcentlist()
{
struct List
list = (struct List*)malloc(sizeof(struct List*));
list->listsize = 0;
list->firnode = NULL;
list->tailnode = NULL;
return list;
}
void inserthead(struct List*list ,int date )//尾插法插入
{
struct Node * newnode = cendnode(date);
if (list->listsize == 0)
{
list->firnode = newnode;
list->tailnode = newnode;
}
else
{
newnode->next = list->firnode;
list->firnode = newnode;
}
list->listsize++;

}
void detelhead(struct Listlist)//删除链表
{
if (list->listsize != 0)//判断链表里面是否存在数值
{
struct Node nextnode = list->firnode->next;
free(list->firnode);
list->firnode = nextnode;//把表头指向新的节点
}
}
void printlist(struct List
list)//打印链表
{
struct Node
pmove = list->firnode;
while (pmove!=NULL)
{
printf("%d\t", pmove->date);
pmove = pmove->next;
}
printf("\n");
}
int main()
{//无头链表:一般采用在封装的方式
struct List*list = centlist();
for (int i = 0; i < 3; i++)
{
inserthead(list, i);
}
detelhead(list);
detelhead(list);
inserthead(list, 3);
printlist(list);
}

理解思路:

源文本
理解思路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值