1 单链表部分代码(自用)
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
}Node;
Node* createList()
{
Node* head = (Node*)malloc(sizeof(Node));
head->next = NULL;
return head;
}
void addNode(Node* head, int data)
{
Node* temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->next = NULL;
newNode->data = data;
temp->next = newNode;
}
void printList(Node* head)
{
Node* temp = head->next;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
Node* head = createList();
for (int i = 7; i > 0; i--)
{
addNode(head, i);
}
printList(head);
return 0;
}
2 输出
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/749dab2ead854fdeb46086e10cc2c90b.png)