有人可以帮助我反转链表吗? 当我选择显示时,反转列表后消失了。必须有某种方法可以打印列表的背面,该列表将保留在头部中。
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct nodes
{
int data;
struct node *next;
}
node;
void display(node*);
void display(node *head)
{
while(head!=NULL)
{
printf("%d)->",head->data);
head=head->next;
}
}
void main()
{
node *head,*p,*q,*r;
int i,n,x,e,temp,a,b,c;
clrscr();
printf("nENTER NUMBER OF DATA TO BE ENTERED: ");
scanf("%d",&n);
head=(node*)malloc(sizeof(node));
printf("nENTER DATA NUMBER 1: ");
scanf("%d",&(head->data));
head->next=NULL;
p=head;
for(i=1;i<n;i++)
{
p->next=(node*)malloc(sizeof(node));
p=p->next;
p->next=NULL;
printf("ENTER DATA NUMBER %d: ",i+1);
scanf("%d",&(p->data));
}
while(1)
{
printf("nntttMEGA LINKED LIST PROGRAM");
printf("n1]Display Linked List.");
printf("n2]Delete Linked List.");
printf("n3]Re-edit linked List.");
printf("n4]Add Node @1st Position.");
printf("n5]Add Node @Last Position.");
printf("n6]Re-Create Linked List!!");
printf("n7]Sort Linked List");
printf("n8]Count Total Nodes.");
printf("n9]Reverse Linked List.");
printf("n10]EXIT!!");
printf("nn: ");
scanf("%d",&x);
switch(x)
{
case 1:
if(head==(0||NULL))
{
printf("nLIST IS EITHER EMPTY OR DELETED!!");
}
else
{ printf("nYOUR LINKED LIST: ");
p=head;
while(p!=NULL)
{
printf("%d)->",p->data);
p=p->next;
}
}
break;
case 2:
if(head!=(NULL||0))
{
for(i=1;i<n;i++)
{
do
{
p=head;
head=head->next;
free(p);
}
while(head->next!=NULL);
}
printf("nYOUR LINKED LIST HAS BEEN DELETED");
}
else
{
printf("nNO LINKED LIST AVAILABLE TO DELETE!!");
}
break;
case 3:
if(head==(NULL||0))
{
printf("nNOT APPLICABLE!!");
}
else
{
printf("nRE-ENTER DATA NUMBER 1: ");
scanf("%d",&head->data);
head->next=NULL;
p=head;
for(i=1;i<n;i++)
{
p->next=(node*)malloc(sizeof(node));
p=p->next;
p->next=NULL;
printf("RE-ENTER DATA NUMBER %d: ",i+1);
scanf("%d",&(p->data));
}
}
break;
case 4:
if(head!=(0||NULL))
{
p=(node*)malloc(sizeof(node));
printf("nENTER DATA FOR POSITION 1: ");
scanf("%d",&(p->data));
p->next=head;
head=p;
display(head);
}
else
{
printf("nNOT APPLICABLE!!");
}
break;
case 5:
if(head!=(0||NULL))
{
p=(node*)malloc(sizeof(node));
printf("nENTER DATA FOR LAST POSITION: ");
scanf("%d",&(p->data));
p->next=NULL;
if(head==NULL)
display(head);
q=head;
while(q->next!=NULL)
q=q->next;
q->next=p;
display(head);
}
else
{
printf("nNOT APPLICABLE!!");
}
break;
case 6:
if(head!=(NULL|0))
{
printf("nDELETE THE CURRENT LINKED LIST BEFORE CREATING A NEW ONE!!");
}
else
{
printf("nENTER NUMBER OF DATA TO BE ENTERED: ");
scanf("%d",&n);
head=(node*)malloc(sizeof(node));
printf("nENTER DATA NUMBER 1: ");
scanf("%d",&(head->data));
head->next=NULL;
p=head;
for(i=1;i<n;i++)
{
p->next=(node*)malloc(sizeof(node));
p=p->next;
p->next=NULL;
printf("ENTER DATA NUMBER %d: ",i+1);
scanf("%d",&(p->data));
}
}
break;
case 7:
if(head!=NULL)
{
printf("nBEFORE SORTING: ");
display(head);
q=head;
while(q!=NULL)
{
p=q->next;
while(p!=NULL)
{
if((q->data)>(p->data))
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
p=p->next;
}
q=q->next;
}
p=head;
printf("nnAFTER SORTING: ");
display(head);
}
else
{
printf("nNOT APPLICABLE!!");
}
break;
case 8:
if(head!=NULL)
{
p=head;
for(temp=0;p!=NULL;temp++)
{
p=p->next;
}
printf("nTOTAL NODES: %d",temp);
}
else
{
printf("nNOT APPLICABLE!!");
}
break;
case 9:
printf("nSTILL WORKING ON THIS CODE!!");
break;
case 10:
printf("nARE YOU SURE YOU WANT TO EXIT?");
printf("nn1>Yes!tt2>NO!");
printf("nn: ");
scanf("%d",&e);
if(e==1)
{
exit();
}
else
{
if(e==2)
{
break;
}
else
{
printf("nttINVALID SELECTION!!");
}
}
break;
default:
printf("nttINVALID SELECTION!!");
break;
}
}
getch();
}
程序无法反转列表的原因是,当用户选择菜单选项 9 时,他们得到的只是一个声明代码尚未工作的 print 语句。
在高级别上,反转链表可以像这样完成:
for each element in the input list, from beginning to end:
remove element from input list
place element in the beginning of the output list
return output list