c语言反转链表
2021-03-30 15:27
标签:int head printf node 反转 oid size main struct #include void reverseNode(Node *head) Node *cur = head->next; } c语言反转链表 标签:int head printf node 反转 oid size main struct 原文地址:https://www.cnblogs.com/niuniuc/p/13581740.html
#include
typedef struct Node
{
int data;
struct Node *next;
}Node;
{
Node *pre = NULL;
while(cur)
{
Node *nextp = cur->next;
cur->next = pre;
pre = cur;
cur = nextp;
}
head->next = pre;
void showNode(Node *head)
{
Node *p = head->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void init_node(Node *head)
{
Node *node1 = (Node *)malloc(sizeof(Node));
node1->data=1;
Node *node2 = (Node *)malloc(sizeof(Node));
node2->data=2;
Node *node3 = (Node *)malloc(sizeof(Node));
node3->data=3;
Node *node4 = (Node *)malloc(sizeof(Node));
node4->data=4;
head->next = node1;
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = NULL;
}
int main()
{
Node *head = (Node *)malloc(sizeof(Node));
init_node(head);
showNode(head);
reverseNode(head);
showNode(head);
}