チェーンテーブルの操作C/C++

9118 ワード

一、チェーンテーブルを作成し、順次データを増加し、チェーンテーブル操作を遍歴する
/**
 *C    ,        ,      
 */
#include<iostream> 
using namespace std;
struct student {
    int data;
    struct student* next;
};
int main()
{
    int data;
    struct student *head,*tail,*temp;
    head = NULL;
    while(cin>>data && data!=-1) {
        temp = (struct student*)malloc(sizeof(struct student));
        temp->data = data;
        temp->next = NULL;
        if(head==NULL) {
        head = temp;
            tail = head;
        } else { 
            tail->next = temp;
            tail = temp;        
        }         
    }
       
    tail = head;
    while(tail!=NULL) {
        cout<<tail->data<<endl;
        tail = tail->next;
    }
    system("pause");
    return 0;
}
/**
 *C/C++  ,        ,      
 */
#include<iostream> 
using namespace std;
struct student {
    int data;
    struct student* next;
    student(int a) {
        this->data = a;
        this->next = NULL; 
    }
};
int main()
{
    int data;
    struct student *head,*tail,*temp;
    head = NULL;
    while(cin>>data && data!=-1) {       
        if(head==NULL) {
            head = new student(data);
            tail = head;
         } else {
             temp = new student(data);
             //temp       ,  tail next     
             tail->next = temp;
             tail = temp;              
         }         
    }    
       
    tail = head;
    while(tail!=NULL) {
        cout<<tail->data<<endl;
        tail = tail->next;
    }
    system("pause");
    return 0;
}
/**
 *C/C++  ,        ,      
 */
#include<iostream> 
using namespace std;
struct student {
    int data;
    struct student* next;
    student(int a) {
        this->data = a;
        this->next = NULL; 
    }
    student(int a, struct student* p) {
        p->next = this;
        this->data = a;
        this->next = NULL;
    }
};
int main()
{
    int data;
    struct student *head,*tail;
    head = NULL;
    while(cin>>data && data!=-1) {           
        if(head==NULL) {
            head = new student(data);
            tail = head;             
         } else {
              tail = new student(data, tail);
         }        
    }
       
    tail = head;
    while(tail != NULL) {
        cout<<tail->data<<endl;
        tail = tail->next;
    }
    system("pause");
    return 0;
}

二、(続き)