我有这个程序不会抛出错误,但是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 process(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();
}
查看过程(确定):
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)
:
do
{
printf("-> %d\n",AUX->info);
AUX=AUX->prv;
}
while(AUX->nxt!=HEAD);
可能会发生这样的事情。
当您前往上一个节点并检查next是否为head时,如果您将列表构建为双链接和循环,则next将成为head。
尝试这样:
do
{
AUX=AUX->prv;
printf("-> %d\n",AUX->info);
}
while(AUX!=HEAD);
并且您应该避免将所有变量用作全局变量。
答案 1 :(得分: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();
}