/*/
单链表
*/
#include<stdio.h>
#include<stdlib.h>
struct student {
int data;
struct student* next;
};
//创建链表表头
struct student* createHeader() {
struct student* newHeader = malloc(sizeof(struct student));
newHeader->data = 0;
newHeader->next = NULL;
return newHeader;
}
//创建新的链表节点
struct student* createList(int newData) {
struct student* newList = malloc(sizeof(struct student));
newList->data = newData;
newList->next = NULL;
return newList;
}
//插入节点(头插法)
void insertByHeader(struct student* header, int newData) {
struct student* newInsert = createList(newData);
newInsert->next = header->next;
header->next = newInsert;
}
//遍历打印
void printfByHeader(struct student* header) {
struct student* pMove = header->next;
while (pMove) {
printf("%d\n", pMove->data);
pMove = pMove->next;
}
}
int main() {
struct student* header = createHeader();
insertByHeader(header, 1);
insertByHeader(header, 2);
insertByHeader(header, 3);
insertByHeader(header, 4);
insertByHeader(header, 5);
printfByHeader(header);
system("pause");
return 0;
}
C语言实现单链表(无讲解)
最新推荐文章于 2024-11-10 21:43:58 发布