单链表&双链表的头插入&尾插入
2024-10-12 01:18:21
#include<stdio.h>
#include"stdlib.h" struct student
{
int data;
struct student *pnext;
};
struct students
{
struct students *prev;
int data;
struct students *pnext;
};
struct student *create(int dat); //单链表创造一个节点。
struct students *shuangcreate(int dat); //双链表创造一个节点。 void insert_head(struct student* pH,struct student* news);//单链表头插入
void insert_head_shuang(struct students* pH,struct students* news);//双链表头插入
void insert_tail_shuang(struct students* pH,struct students* news);//尾插入
int main()
{
struct student *ph=create();
struct students *phs=shuangcreate(); insert_head(ph,create());
insert_head(ph,create()); insert_head_shuang(phs,shuangcreate());
insert_head_shuang(phs,shuangcreate());
insert_head_shuang(phs,shuangcreate()); /*
insert_tail_shuang(phs,shuangcreate(1)); //尾插入
insert_tail_shuang(phs,shuangcreate(2)); printf("%d.\n",ph->data);
printf("%d.\n",ph->pnext->data);
printf("%d.\n",ph->pnext->pnext->data);
*/
printf("%d.\n",phs->data);
printf("%d.\n",phs->pnext->data);
printf("%d.\n",phs->pnext->pnext->data);
printf("%d.\n",phs->pnext->pnext->pnext->data); return ;
} struct student *create(int dat) //单链表创造一个节点。
{
struct student *p=(struct student*)malloc(sizeof(struct student));
if(NULL==p)
{
return NULL;
printf("节点分配失败");
}
p->data=dat;
p->pnext=NULL;
return p;
} struct students *shuangcreate(int dat) //双链表创造一个节点。
{
struct students *p=(struct students*)malloc(sizeof(struct students));
if(NULL==p)
{
return NULL;
printf("双链表节点分配失败");
}
p->data=dat;
p->pnext=NULL;
p->prev=NULL;
return p;
}
void insert_head(struct student* pH,struct student* news)
{
news->pnext=pH->pnext;
pH->pnext=news;
}
void insert_tail_shuang(struct students* pH,struct students* news) //双链表的尾插入
{
struct students* ph=pH;
while(NULL!=ph->pnext)
{
ph=ph->pnext;
printf("a\n");
}
ph->pnext=news;
news->prev=ph;
}
//画出双链表的节点链接情况
void insert_head_shuang(struct students* pH,struct students* news) //双链表的头插入
{
struct students* p=pH;
news->pnext=p->pnext;
if(p->pnext!=NULL) //考虑到只有一个头结点的情况。这里很重要 当第一次的时候p->pnext=NULL;
p->pnext->prev=news;
p->pnext=news;
news->prev=p;
}
接下来将会补齐单链表和双链表节点的 遍历 、逆序、和 删除 ;暂且放到这里,
最新文章
- 当PHP引擎试图实例化一个未知类的操作
- 通过命令行安装Android app
- SVN全量备份+增量备份脚本
- 自定义文件上传的按钮的样式css+js
- java Spring 基于注解的配置(一)
- SuperSocket入门(五)-常用协议实现模版及FixedSizeReceiveFilter示例
- tp框架基础知识
- APP的线程安全
- java语法部分一些小问题
- 运维yum搭建zabbix
- python中的__new__与__init__,新式类和经典类(2.x)
- [OpenCV]直线拟合
- MVC中常用的跳转方法
- H.264 SVC
- Java设计模式六大原则
- Django之URL控制器(路由层)
- ZeroTier One
- Slickflow.NET 开源工作流引擎高级开发(一) -- 流程外部事件的调用和变量存储实现
- 【js+jquery】通用、简单的JS 提示框
- 20145307陈俊达《网络对抗》Exp2 后门原理与实践