#pragma once
#include<stdio.h>
#include<stdlib.h>
typedef int SLTDateType;
typedef struct SListNode {
SLTDateType data;
struct SListNode* next;
}SLTNode;
void SListPrint(SLTNode* phead);//create一个头节点
void SListPushBack(SLTNode** phead, SLTDateType x);
#include"SList.h"
void SListPrint(SLTNode* phead) {
SLTNode* cur = phead;
while (cur != NULL) {
printf("%d->", cur->data);
cur = cur -> next;
}
}
void SListPushBack(SLTNode** phead, SLTDateType x) {
/*SLTNode* tail = phead;
while (tail->next != NULL) {
tail = tail->next;
}*/
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
newnode->data = x;
newnode->next = NULL;
if (*phead == NULL) {
*phead = newnode;
}
else {
SLTNode* tail = *phead;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = newnode;
}
}
#include"SList.h"
void Test() {
SLTNode* plist = NULL;//plist,头指针
SListPushBack(&plist,1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
}
int main() {
Test();
return 0;
}
这篇博客展示了如何在C语言中实现单链表的插入(PushBack)操作和打印(Print)功能。通过定义链表节点结构体,创建头节点,并在链表尾部插入元素,最后遍历链表并打印所有数据。示例代码中包含了一个测试函数,用于演示插入多个元素后的链表打印效果。
883

被折叠的 条评论
为什么被折叠?



