2017-10-06 95 views
-1

我有這個程序不會拋出錯誤,但viewl過程只顯示列表中的第一項。我如何爲viewl程序打印,但與程序查看器相反的順序?按降序顯示列表

#include <stdio.h> 
#include <conio.h> 
#include <windows.h> 
#include <stdlib.h> 

struct list 
{ 
    int info; 
    struct list *nxt,*prv; 
}*HEAD=NULL,*AUX=NULL,*P=NULL,*F=NULL,*QD=NULL,*FD=NULL; 

主要功能(OK)

int main() 
{ 
    void insertr(void); 
    void extractr(void); 
    void viewr(void); 
    void viewl(void); 
    void deleten(); 
    void deletep(); 
    char opc; 
    do 
    { 
     system("cls"); 
     printf("___________________________________________________\n"); 
     printf("_______¡¡¡DOUBLE-LINKED CIRCULAR LIST MENU!!!______\n"); 
     printf("___________________________________________________\n"); 
     printf("_________________SELECT AN OPTION__________________\n"); 
     printf("___________________________________________________\n"); 
     printf("___________________________________________________\n"); 
     printf("__________1) INSERT________________________________\n"); 
     printf("__________2) VIEW ASCENDING________________________\n"); 
     printf("__________3) VIEW DESCENDING_______________________\n"); 
     printf("__________4) ENTERING AND ELIMINATE NEXT___________\n"); 
     printf("__________5) ENTERING AND ELIMINATE PREVIOUS_______\n"); 
     printf("__________6) EXIT__________________________________\n"); 
     printf("___________________________________________________\n"); 
     opc=getch(); 
     switch(opc) 
     { 
     case '1': 
      insertr(); 
      break; 
     case '2': 
      viewr(); 
      break; 
     case '3': 
      viewl(); 
      break; 
     case '4': 
      deleten(); 
      break; 
     case '5': 
      deletep(); 
      break; 
     } 
    } 
    while(opc!='6'); 
    getch(); 
    return 0; 
} 

插入右過程

void insertr(void) 
{ 
    P=HEAD;/* very first execution of this method P=NULL */ 
    AUX=(struct list *)malloc(sizeof(struct list)); 
    system("cls"); 
    printf("ENTER AN ENTIRE NUMBER: "); 
    scanf("%d",&AUX->info); 
    AUX->nxt=HEAD; 
    AUX->prv=HEAD; 
    F=AUX; 
    if(HEAD==NULL) 
    { 
     HEAD=AUX; 
     P = AUX;/*first execution of this method P is no longer NULL but P is pointing to AUX */ 
    } 
    else 
    { 
     while (P->nxt!=HEAD) 
     { 
      P=P->nxt; 
     } 
    } 
    P->nxt=AUX; 
    AUX->prv=P; 
    HEAD->prv=AUX; 
} 


void deleten() 
{ 
    int x; 
    system("cls"); 
    printf("ENTER A NUMBER TO ELIMINATE THE FOLLOWING: "); 
    scanf("%d",&x); 

    FD=HEAD; 
    QD=HEAD; 
    while(FD->info!=x&&FD->nxt!=HEAD) 
    { 
     FD=FD->nxt; 
    } 
    QD=FD->nxt; 

    if(FD->nxt==HEAD&&FD->info!=x) 
    { 
     printf("\nENTERED NUMBER IS NOT LISTED"); 
    } 
    else 
    { 
     if(FD->info==x) 
     { 
      FD->nxt=QD->nxt; 
      (QD->nxt)->prv=FD; 
      printf("\nDELETED %d",QD->info); 
      free(QD); 
     } 
    } 
    getch(); 
} 

deletea處理(OK)

void deletep() 
{ 
    int x; 
    system("cls"); 
    printf("ENTER A NUMBER TO REMOVE THE PREVIOUS "); 
    scanf("%d",&x); 

    FD=HEAD; 
    QD=HEAD; 
    while (FD->info!=x&&FD->nxt!=HEAD) 
    { 
     FD=FD->nxt; 
    } 
    QD=FD->prv; 
    if(FD->nxt==HEAD&&FD->info!=x) 
    { 
     printf("\nENTERED NUMBER IS NOT LISTED"); 
    } 
    else 
    { 
     if(FD->info==x) 
     { 
      FD->prv=QD->prv; 
      (QD->prv)->nxt=FD; 
      printf("\nDELETED %d",QD->info); 
      free(QD); 
     } 
    } 
    getch(); 
} 

viewr處理(OK):

void viewr(void) 
{ 
    system("cls"); 
    if(HEAD==NULL) 
    { 
     printf("EMPTY LIST"); 
     getchar(); 
     return; 
    } 
    AUX=HEAD; 
    printf("LIST:\n\n"); 
    while(AUX->nxt!=HEAD) 
    { 
     printf("-> %d\n",AUX->info); 
     AUX=AUX->nxt; 
    } 
    if(AUX->nxt==HEAD) 
    { 
     printf("-> %d\n",AUX->info); 
    } 
    getch(); 
} 

此函數失敗:

void viewl(void) 
{ 
    system("cls"); 
    if(HEAD==NULL) 
    { 
     printf("EMPTY LIST"); 
     getchar(); 
     return; 
    } 
    AUX=F; 
    printf("LIST:\n\n"); 
    do 
    { 
     printf("-> %d\n",AUX->info); 
     AUX=AUX->prv; 
    } 
    while(AUX->nxt!=HEAD); 
    getch(); 
} 
+0

爲什麼所有變量的全局變量?和大寫? –

+0

另外,爲什麼你在循環插入到雙向鏈表中?這徹底擊敗了目的。您的插入函數應該在一段時間內運行,而不是線性的。那天你沒有這些相同的問題嗎?爲什麼不嘗試在再次發佈之前修復它們? –

+0

http://idownvotedbecau.se/nodebugging/ – bolov

回答

0

的代碼有幾個邏輯錯誤,其他變量,等這工作沒有任何問題

#include<stdio.h> 
#include<stdlib.h> 
#include<conio.h> 
#include<windows.h> 

//THIS IS TO CREATE THE LIST 

struct list{ 
    int info; 
    struct list *nxt,*prv; 
}*HEAD=NULL,*AUX=NULL,*P=NULL,*FD=NULL,*QD=NULL,*Fd=NULL,*Qd=NULL; 

int main(){ 
    void insertr(void); 
    void viewr(void); 
    void viewl(void); 
    void deleten(); 
    void deletep(); 
    char opt; 
    do{ 
     system("cls"); 

     printf("___________________________________________________\n"); 
     printf("_______¡¡¡DOUBLE-LINKED CIRCULAR LIST MENU!!!______\n"); 
     printf("___________________________________________________\n"); 
     printf("_________________SELECT AN OPTION__________________\n"); 
     printf("___________________________________________________\n"); 
     printf("___________________________________________________\n"); 
     printf("__________1) INSERT________________________________\n"); 
     printf("__________2) VIEW ASCENDING________________________\n"); 
     printf("__________3) VIEW DESCENDING_______________________\n"); 
     printf("__________4) ENTERING AND ELIMINATE NEXT___________\n"); 
     printf("__________5) ENTERING AND ELIMINATE PREVIOUS_______\n"); 
     printf("__________6) EXIT__________________________________\n"); 
     printf("___________________________________________________\n"); 
     opt=getch(); 
     switch(opt) {  
     case '1': 
      insertr(); 
      break;  
     case '2': 
      viewr(); 
      break; 
     case '3': 
      viewl(); 
      break; 
     case '4': 
      deleten(); 
      break; 
      case'5': 
       deletep(); 
       break; 
     } 
    }while(opt!='6'); 
    return 0; 
} 

void insertr(void){ 
    P=HEAD; /* very first execution of this method P=NULL */ 
    AUX=(struct list *)malloc(sizeof(struct list)); 
    system("cls"); 
    printf("ENTER AN ENTIRE NUMBER: "); 
    scanf("%d",&AUX->info); 
    AUX->nxt=HEAD; 
    AUX->prv=HEAD; // The pointer to the previous node is null (HEAD is null) 
    if(HEAD==NULL){ //I ask if HEAD is equal to Null 
     HEAD=AUX; 
     P=AUX; /* The first execution of this method P is no longer NULL but P is pointing to AUX */ 
    }else{ //THIS YES 
     while(P->nxt!=HEAD){ 
      P=P->nxt; 
     } 
    } 
    P->nxt=AUX; // The next pointer of P takes the value of AUX 
    AUX->prv=P; 
    HEAD->prv=AUX; 
} 

void deleten(){ 
    int x; 
    system("cls"); 
    printf("ENTER A NUMBER TO ELIMINATE THE FOLLOWING: "); 
    scanf("%d",&x); 
    FD=HEAD; 
    QD=HEAD; 
    while(FD->info!=x&&FD->nxt!=HEAD){ // if what I am going to eliminate is different from the number that was entered and if there is another 
     FD=FD->nxt; //step to the next node 
    } 
    QD=FD->nxt; 
    if(FD->nxt==HEAD&&FD->info==x){ 
     printf("\nIT IS THE HEADBOARD! CAN NOT BE ELIMINATED"); 
    }else 
     if(FD->nxt==HEAD&&FD->info!=x){ 
     printf("\nTHE NUMBER ENTERED IS NOT IN THE LIST"); 
    }else{ 
      FD->nxt=QD->nxt; 
      (QD->nxt)->prv=FD; 
      printf("\nDELETED %d",QD->info); 
      free(QD); //free space in memory occupied by QD 
     } 
     getch(); 
} 

void deletep() 
{ 
    int xd; 
    system("cls"); 
    printf("IENTER A NUMBER TO REMOVE THE PREVIOUS: "); 
    scanf("%d",&xd); 

    Fd=HEAD; 
    Qd=HEAD; 
    while(Fd->info!=xd){ 
     Fd=Fd->prv; 
    } Qd=Fd->prv; 

    if(Fd==HEAD){ 
     printf("\nIT'S THE HEAD! CAN NOT BE ELIMINATED"); 
    }else{ 
     if(Qd==HEAD){ 
      HEAD=HEAD->nxt; 
      Fd->prv=Qd->prv; 
      (Fd->prv)->nxt=Fd; 
      printf("\nDELETED %d",Qd->info); 
     }else{ 
      Fd->prv=Qd->prv; 
      (Qd->prv)->nxt=Fd; 
      printf("\nDELETED %d",Qd->info); 
     } 
     free(Qd); 
    } 
    getch(); 
} 

void viewr(void) 
{ 
    system("cls"); 
    if(HEAD==NULL){ 
     printf("LISTA VACIA"); 
     getchar(); 
     return; 
    } 
    AUX=HEAD; 
    printf("LISTA:\n\n"); 
    while(AUX->nxt!=HEAD){ 
     printf("->%d\n",AUX->info); 
     AUX=AUX->nxt; 
    } 
    if(AUX->nxt==HEAD){ 
     printf("->%d\n",AUX->info); 
    } 
    getch(); 
} 

void viewl(void){ 
    system("cls"); 
    if(HEAD==NULL){ 
     printf("EMPTY LIST"); 
     getchar(); 
     return; 
    } AUX=HEAD->prv; 
    printf("LIST:\n\n"); 
    do{ 
     printf("->%d\n",AUX->info); 
     AUX=AUX->prv; 
    }while(AUX->nxt!=HEAD); 
    getch(); 
} 
0
在你的代碼

do 
{ 
    printf("-> %d\n",AUX->info); 
    AUX=AUX->prv; 
} 
while(AUX->nxt!=HEAD); 

可以發生這樣的事情。

enter image description here

當你要上一個節點,並檢查是否其次是頭,接下來會頭,如果你建立你的清單是雙聯和循環。

嘗試這樣的:

do 
{ 
    AUX=AUX->prv; 
    printf("-> %d\n",AUX->info); 
} 
while(AUX!=HEAD); 

,你應該避免使用所有的變量爲全局。