2019独角兽企业重金招聘Python工程师标准>>>
//
// main.c
// newline
//
// Created by Jason on 16/4/12.
// Copyright © 2016年 jason. All rights reserved.
//
#include <stdio.h>
#include "stdlib.h"
#define NULL 0
struct node{
int data;
struct node *next;
};
struct node*create(struct node *L){
int x, flg = 1;
struct node *p, *s;
L=(struct node *)malloc(sizeof(struct node)); //生成头结点
L->next = NULL;
p=L;
while(flg) //循环接受输入节点数据,0结束输入
{
printf("请输入节点数据\n");
scanf("%d", &x);
if(x != 0)
{
s=(struct node *)malloc(sizeof(struct node)); //生成新节点
s->data = x;
p->next = s; //把新节点插入链表尾部
p = s; //p指针再次指向尾节点
}
else
{
flg = 0; //输入0,改变循环变量,不接受新节点
}
}
p->next = NULL;
puts("链表输入结束\n");
return L;
}
void print_node(struct node *head){
struct node *tmp;
tmp = head->next;
while (tmp != NULL) {
printf("%d ", tmp->data);
tmp = tmp->next;
}
}
void reverse_node(struct node *head){
struct node *p1,*p2;
p1 = head->next;
head->next = NULL;
while (p1 != NULL) {
p2 = p1;
p1 = p1->next;
p2->next = head->next;
head->next = p2;
}
}
int main(int argc, const char * argv[]) {
struct node *head;
head = NULL;
head=create(head);/*创建单链表*/
puts("\n所有链表:\n");
print_node(head);
puts("\n");
puts("链表逆置:\n");
reverse_node(head);
print_node(head);
return 0;
}