码迷,mamicode.com
首页 > 编程语言 > 详细

c语言反转链表

时间:2020-09-09 19:08:28      阅读:43      评论:0      收藏:0      [点我收藏+]

标签:int   head   printf   node   反转   oid   size   main   struct   

#include <stdio.h>
#include <malloc.h>
typedef struct Node
{
int data;
struct Node *next;
}Node;

void reverseNode(Node *head)
{

Node *cur = head->next;
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);
}

c语言反转链表

标签:int   head   printf   node   反转   oid   size   main   struct   

原文地址:https://www.cnblogs.com/niuniuc/p/13581740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!