C语言实现双链表(无讲解)

/*
双链表
*/
#include<stdio.h>
#include<stdlib.h>
struct student {
	struct student* after;
	int data;
	struct student* next;
};
//创建表头
struct student* createHeader() {
	struct student* newHeader = malloc(sizeof(struct student));
	newHeader->after = NULL;
	newHeader->data = 0;
	newHeader->next = NULL;
	return newHeader;
}
//创建新的节点
struct student* createList(struct student* list,int newData) {
	struct student* newList = malloc(sizeof(struct student));
	newList->data = newData;
	newList->after = list;
	newList->next = NULL;
	return newList;
}
//插入节点(头插法)
void insertByHeader(struct student* header,int newData, int state) {
	if (state == 0) {
		struct student* newInsertByHeader = createList(header, newData);
		newInsertByHeader->next = header->next;
		newInsertByHeader->after = header;
		header->next = newInsertByHeader;
	}
	if (state == 1) {
		struct student* newInsertByHeader = createList(header, newData);
		newInsertByHeader->next = header->next;
		newInsertByHeader->after = header;
		newInsertByHeader->next->after = newInsertByHeader;
		header->next = newInsertByHeader;
	}
}
//遍历打印
void printfByHeader(struct student* header) {
	struct student* pMove = header->next;
	while (pMove) {
		printf("节点编号:%d\n", pMove->data);
		printf("该节点之前节点地址:%d\t", pMove->after);
		printf("该节点之后节点地址:%d\n", pMove->next);
		pMove = pMove->next;
	}
}
int main() {
	struct student* header = createHeader();
	insertByHeader(header, 1,0);
	insertByHeader(header, 2,1); 
	insertByHeader(header, 3,1);
	insertByHeader(header, 4,1);
	insertByHeader(header, 5,1);
	insertByHeader(header, 6,1);
	printfByHeader(header);
	system("pause");
	return 0;
}

2020年6月20日17:47:01

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值