-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();
}
爲什麼所有變量的全局變量?和大寫? –
另外,爲什麼你在循環插入到雙向鏈表中?這徹底擊敗了目的。您的插入函數應該在一段時間內運行,而不是線性的。那天你沒有這些相同的問題嗎?爲什麼不嘗試在再次發佈之前修復它們? –
http://idownvotedbecau.se/nodebugging/ – bolov