[Review] input data structure a list, and then outputs it

Digital input, like the end of -1
to remember when a new node to the application, write
LNode * temp = (Lnode *) malloc (sizeof (LNode));
i.e., a pointer points to LNode.
Because malloc returns is a pointer.

#include <bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct LNode{
    ElemType data;
    LNode *next;
};
LNode *head,*tail;

void init(){
    head = (LNode*)malloc(sizeof(LNode));
    head->next = NULL;
    tail = head;
}

void input_data(){
    int x;
    cin >> x;
    while (x!=-1){
        LNode *temp = (LNode*)malloc(sizeof(LNode));
        temp->data = x;
        temp->next = NULL;
        tail->next = temp;
        tail = temp;
        cin >> x;
    }
}

void print(){
    LNode *temp = head->next;
    while (temp){
        cout<<temp->data<<" ";
        temp = temp->next;
    }
}

int main(){
    init();
    //freopen("D://rush.txt","r",stdin);
    input_data();
    print();
    fclose(stdin);
    return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/AWCXV/p/11567570.html