/*
双链表
*/
#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;
}
C语言实现双链表(无讲解)
最新推荐文章于 2024-09-09 21:22:46 发布