输入数字,-1结束就好
要记住在新申请节点的时候,这样写
LNode *temp = (Lnode*)malloc(sizeof(LNode));
即指向LNode的一个指针。
因为malloc返回的就是一个指针。
#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;
}