我还在学习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;
}
我真的不知道问题在哪里,错误的算法?
答案 0 :(得分:0)
问题出在这一行
List* B;
将此行更改为以下内容;
List* B = new List();
希望这有帮助。