gpt4 book ai didi

c++ - 构建运行正常,但运行时崩溃?

转载 作者:行者123 更新时间:2023-12-03 17:49:02 25 4
gpt4 key购买 nike

我仍在学习有关ADT的信息,我从自己那里获得了许多链接列出的代码,但是我的代码崩溃了,有人可以告诉我我错在哪里吗?
我的程序是输入笔记本电脑品牌,然后输入品牌的类型。

#ifndef SISWA_H_INCLUDED
#define SISWA_H_INCLUDED

struct ALaptop{
char tipe[255];
};

struct ILaptop {
char merek[255];
ALaptop anak;
};

struct TNode {
ILaptop induk;
TNode *next;
TNode *prev;
TNode *up;
TNode *down;
};

struct List {
TNode *head;
TNode *tail;
};

void insert_induk(List *&list_,TNode *&Ilaptop);
void delete_induk(List *list_,char cari[]);
void insert_anak(TNode *&C,TNode *Ilaptop);
void insert_anak2(List *list_,char cari[]);
TNode* create_induk(char merek[]);
TNode* create_anak(char tipe[]);
void view(List *list_);
int Empty(List *list_);
void createList(List *list_);

#endif // SISWA_H_INCLUDED



#include "siswa.h"
#include <string.h>
#include <iostream>

using namespace std;

void insert_induk(List *&list_,TNode *&Ilaptop){
char p;
char tipe[255];
if (Empty(list_)==1){
Ilaptop=list_->head;
Ilaptop=list_->tail;
}
else{
list_->head->prev=Ilaptop;
Ilaptop->next=list_->head;
list_->head=Ilaptop;
}
TNode *bantu=Ilaptop;
cout<<"masukkan tipe laptop? (y/n) : ";
cin>>p;
while(p=='y'||p=='Y'){
cout<<"masukkan tipe laptop : ";
cin.ignore();
cin.getline(tipe,sizeof tipe);
insert_anak(bantu,create_anak(tipe));
cout<<"masukkan tipe laptop lagi? (y/n) : ";
cin>>p;
}
}

void insert_anak(TNode *&C,TNode *Ilaptop){
C->down=Ilaptop;
Ilaptop->up=C;
C=Ilaptop;
}

void insert_anak2(List *list_,char cari[]){
TNode *bantu;
char tipe[255];
char p;
bantu=list_->head;
if (Empty(list_)==0){
while (bantu!=NULL){
if(strcmp(bantu->induk.merek,cari)==0){
TNode *bantu10=bantu;
cout<<"Merek Laptop "<<bantu->induk.merek<<endl<<endl;
cout<<"masukkan tipe laptop : ";
cin.ignore();
cin.getline(tipe,sizeof tipe);
insert_anak(bantu10,create_anak(tipe));
cout<<"masukkan tipe laptop lagi? (y/n) : ";
cin>>p;
while(p=='y'||p=='Y'){
cout<<"masukkan tipe laptop : ";
cin.ignore();
cin.getline(tipe,sizeof tipe);
insert_anak(bantu10,create_anak(tipe));
cout<<"masukkan tipe laptop lagi? (y/n) : ";
cin>>p;
}
}
else {
bantu=bantu->next;
}
}
cout<<"Data tidak ada!";
}
else{
cout<<"Data masih kosong";
}
}

void delete_induk(List *list_,char cari[]){
TNode *bantu,*bantu2,*bantu3;
bantu=list_->head;
if (Empty(list_)==0){
while (bantu!=NULL){
if(strcmp(bantu->induk.merek,cari)==0){
bantu3=bantu;
bantu3->prev->next=bantu->next;
bantu->next->prev=bantu3;
while (bantu!=NULL) {
bantu2=bantu;
bantu=bantu->down;
delete bantu2;
}
return;
}
else {
bantu=bantu->next;
}
}
cout<<"Data tidak ada!";
}
else{
cout<<"Data masih kosong";
}
}


TNode* create_induk(char merek[]){
TNode *baru=new TNode;
baru->next=NULL;
baru->prev=NULL;
baru->up=NULL;
baru->down=NULL;
strcpy(baru->induk.merek,merek);
return baru;
}

TNode* create_anak(char tipe[]){
TNode *baru=new TNode;
baru->next=NULL;
baru->prev=NULL;
baru->up=NULL;
baru->down=NULL;
strcpy(baru->induk.anak.tipe,tipe);
return baru;
}

void view(List *list_){
TNode *bantu2;
TNode *bantu=list_->head;
if (Empty(list_)==0){
while(bantu!=NULL){
cout<<"| ";
bantu2=bantu->down;
while(bantu2!=NULL){
cout<<bantu2->induk.anak.tipe<<" | ";
bantu2=bantu->down;
}
bantu=bantu->next;
}
}
else{
cout<<"Data masih kosong";
}
}

int Empty(List *list_){
if (list_->head==NULL){
return 1;
}
else {
return 0;
}
}

void createList(List *list_){
list_->head=NULL;
list_->tail=NULL;
}



#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include "siswa.h"

using namespace std;



int main()
{
List *B;
int pil;
char merk[255];
TNode *induk;
createList(B);
menu:
cout<<"1. Tambah Merek Laptop\n2. Tambah Tipe Laptop\n3. Hapus Merek Laptop beserta tipe nya\n4. View seluruh Laptop\nMasukkan pilihan : ";
cin>>pil;

switch (pil){
case 1: cout<<"Masukkan Merek Laptop : ";
cin.ignore();
cin.getline(merk,sizeof merk);
induk=create_induk(merk);
insert_induk(B,induk);
getch();
goto menu;
case 2: cout<<"Masukkan Merek Laptop yang ingin ditambahkan Tipe nya : ";
cin.ignore();
cin.getline(merk,sizeof merk);
insert_anak2(B,merk);
getch();
goto menu;
case 3: cout<<"Masukkan Merek Laptop yang ingin dihapus : ";
cin.ignore();
cin.getline(merk,sizeof merk);
delete_induk(B,merk);
getch();
goto menu;
case 4: view(B);
getch();
goto menu;
default:getch();
goto menu;
}
return 0;
}

我真的不知道问题出在哪里,算法错误?

最佳答案

问题是在这条线

List* B;

将此行更改为以下行;
List* B = new List();

希望这可以帮助。

关于c++ - 构建运行正常,但运行时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35416666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com