有人可以帮我扭转链表吗?当我选择显示时,反转列表后不见了... 必须有一些方法来打印列表的反向,这将保留在头部....
#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("\n\n\t\t\tMEGA 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("\n\n: ");
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("\n\nAFTER 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("\n\n1>Yes!\t\t2>NO!");
printf("\n\n: ");
scanf("%d",&e);
if(e==1)
{
exit();
}
else
{
if(e==2)
{
break;
}
else
{
printf("\n\t\tINVALID SELECTION!!");
}
}
break;
default:
printf("\n\t\tINVALID SELECTION!!");
break;
}
}
getch();
}
答案 0 :(得分:0)
程序无法反转列表的原因是因为当用户选择菜单选项9时,他们得到的只是一个声明代码无法正常工作的打印语句。
在较高级别,可以像这样完成反转链表:
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