zoukankan      html  css  js  c++  java
  • 【数据结构复习】输入一个链表,然后输出它

    输入数字,-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;
    }
    

      

     
  • 相关阅读:
    正则表达式收藏
    c#mvc实现登录
    jQuery获取URL中的参数
    TortoiseSVN新人使用指南
    CheckBox获取一组及全选
    day45
    day44
    day43
    day43
    day42
  • 原文地址:https://www.cnblogs.com/AWCXV/p/11567570.html
Copyright © 2011-2022 走看看