输入若干个整数(以0结束)逆序构建双向链表

编译:VS2017

语言:C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#pragma warning(disable:4996)
typedef struct Node {
    int data;
    struct Node *pre;
    struct Node *next;
}node;

int main()
{
    node *head,*p;
    int i;
    head = (node *)malloc(sizeof(node));
    head->data = 0;
    head->pre = NULL;
    printf("enter numbers 0 exit\n");
    scanf("%d", &i);
    p = (node *)malloc(sizeof(node));
    p->data = i;
    p->next = NULL;
    head->next = p;
    p->pre = head;
    scanf("%d", &i);
    while (i != 0)
    {
        p = (node *)malloc(sizeof(node));
        p->data = i;
        p->next = head->next;
        head->next->pre = p;
        head->next = p;
        p->pre = head;
        scanf("%d", &i);
    }
    p = head->next;
    while (p)
    {
        printf("%d\n", p->data);
        p = p->next;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_37372543/article/details/88359697