具有多个结构的链接列表

时间:2018-06-10 22:37:45

标签: c++ struct linked-list

我是链接列表的新手,我使用一个带链表的结构没有问题。但是,当我尝试使用struct" people"并使用其他结构将它们链接在一起" Queue",我遇到了分段错误。有人可以看看我的代码中有双结构的错误吗?

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.android.writer"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.google.firebase:firebase-database:11.0.4'
    implementation 'com.google.firebase:firebase-auth:11.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.rengwuxian.materialedittext:library:2.1.4'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.github.clans:fab:1.6.4'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.firebase:firebase-client-android:2.5.2+'
}


apply plugin: 'com.google.gms.google-services'

2 个答案:

答案 0 :(得分:0)

如果没有先为queueHead分配内存(在您的情况下为NULL),则无法执行此操作:

queueHead->ppl = node;
queueHead->next = NULL;

答案 1 :(得分:0)

queueHead->ppl的访问会导致seg fault。问题是queueHead是一个指针,需要指向一个有效的内存位置。

这样试试:

Queue *queueHead = new Queue;

另外,考虑使用智能指针类,例如std::unique_ptr用于管理指针/内存。你已经在你的程序中忘记了delete(希望这让你信服)。