當我運行此代碼並調用「List()」函數時,它僅打印用戶添加的最後一個。我希望從頭到尾打印。我希望你能幫助我瞭解我的清單功能。它只打印最後一個。嵌套鏈表打印問題
#include<stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct seat {
int k_no;
int k_name;
struct seat *next_k,*previous_k;
} *first_k,*temp_k,*last_k;
struct sefer {
char name[20];
int no;
struct sefer *next,*previous;
struct seat *bus;
} *first,*last,*temp;
void list();
void seat_link(int val);
void sefer_search();
int main()
{
int val;
printf ("how many names do you want to type ->");
scanf ("%d",&val);
int i;
int j;
for(i=0;i<val;i++) {
if (first==NULL) {
first =(sefer *)malloc(sizeof(struct sefer));
fflush(stdin);
printf (" %d. name->",i+1);
scanf ("%s",&first->name);
printf (" %d. capacity ->",i+1);
scanf ("%d",&first->no);
first->next=NULL; //2 inci düğüm daha oluşmadığı için null
first->previous=NULL;
last=first; //şimdilik sadece ilk düğüm olduğu için aynı zamanda son oluo
last->bus=NULL;
for(j=0;j<first->no;j++) {
//KOLTUKLAR OLUŞTURULCAK
if (last->bus==NULL){
first_k =(seat *)malloc(sizeof(struct seat));
fflush(stdin);
first_k->k_no=j;
first_k->k_name=1;
first_k->next_k=NULL;
first_k->previous_k=NULL;
last_k=first_k;
last->bus=first_k;
}
else {
temp_k =(seat *)malloc(sizeof(struct seat));
fflush(stdin);
temp_k->k_no=j;
temp_k->k_name=0;
last_k->next_k=temp_k;
temp_k->previous_k=last_k;
last_k=temp_k;
last_k->last_k=NULL;
}
}
}
else if (last==first) {
printf ("\n");
last = (sefer *)malloc(sizeof(struct sefer));
fflush(stdin);
printf (" %d. name ->",i+1);
scanf ("%s",&last->name);
printf (" %d. capacitiy ->",i+1);
scanf ("%d",&last->no);
first->next=last;
last>next=NULL;
last->previous=first;
last->bus=NULL;
for(j=0;j<last->no;j++) {
//KOLTUKLAR OLUŞTURULCAK
if (last->bus==NULL) {
first_k =(seat *)malloc(sizeof(struct seat));
fflush(stdin);
first_k->k_no=j;
first_k->k_name=2;
first_k->last_k=NULL;
first_k->previous_k=NULL;
last_k=first_k;
last->bus=first_k;
}
else {
temp_k =(seat *)malloc(sizeof(struct seat));
fflush(stdin);
temp_k->k_no=j;
temp_k->k_name=0;
last_k->next_k=temp_k;
temp_k->previous_k=last_k;
last_k=temp_k;
last_k->last_k=NULL;
}
}
}
else { // kayıt eklenmişse diğer düğümler oluşturulcak
printf ("\n");
temp=(sefer *) malloc(sizeof(struct sefer));
fflush(stdin);
printf (" %d. name",i+1);
scanf ("%s",&temp->name);
printf (" %d. capacity->",i+1);
scanf ("%d",&temp->no);
last->next=temp;
temp->previous=last;
last=temp;
last->next=NULL;
last->bus=NULL;
for(j=0;j<temp->no;j++){
//KOLTUKLAR OLUŞTURULCAK
if (last->bus==NULL){
first_k =(seat*)malloc(sizeof(struct seat));
fflush(stdin);
first_k->k_no=j;
first_k->k_name=3;
first_k->last_k=NULL;
first_k->previous_k=NULL;
last_k=first_k;
last->bus=first_k;
}
else{
temp_k =(seat *)malloc(sizeof(struct seat));
fflush(stdin);
temp_k->k_no=j;
temp_k->k_name=0;
last_k->next_k=temp_k;
temp_k->previous_k=last_k;
last_k=temp_k;
last_k->next_k=NULL;
}
}
}
}
list();
system("PAUSE");
return 0;
}
void sefer_search(){ //bağda arama yapar
int searching;
printf ("\n\t\t Aranacak Sefer Numarasını Giriniz:");
scanf ("%d",&searching);
temp=first;
while (1) {
if (temp->no==searching) {
break;
}
temp=temp->next;
}
}
void seat_link(int val) {
int j;
}
我的問題是在這裏實際上是:
void list(){
temp=first;
while (temp !=NULL) {
printf("\t%s --%d \n", temp->name,temp->no);
temp=temp->next;
}
printf ("\n");
last->bus=first_k;
while (last->bus !=NULL) {
printf("\t%d --%d \n",last->bus->k_name,last->otobus->k_no);
last->bus=last->bus->next_k;
}
}
請幫我
你的程序甚至不會編譯:'deg','sefer'和'koltuk'都是*未聲明的標識符*,還有其他錯誤'xxx'不是'yyy'的成員。 – 2014-12-02 18:54:46
他聲稱他的代碼編譯和工作,所以我在他的話他帶他。如果他們真的有必要解決這個問題,也許他應該進一步闡述,但從我所看到的,這是一個指針問題。 – MSalmo 2014-12-02 19:21:15
是的,我認爲你也是這樣。這可能是指針問題,但我還沒有弄清楚我該怎麼辦才能解決那個指針問題。 – 2014-12-02 19:48:42